1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // Window initialization code. Set up event handlers.
6 window
.addEventListener("load", function() {
7 // set up the event listeners.
8 chrome
.notificationProvider
.onCreated
.addListener(notificationCreated
);
9 chrome
.notificationProvider
.onUpdated
.addListener(notificationUpdated
);
10 chrome
.notificationProvider
.onCleared
.addListener(notificationCleared
);
13 function displayImage(text
, bitmap
, div
) {
14 var image
= document
.createElement("p");
15 image
.appendChild(document
.createTextNode(text
));
16 var imageCanvas
= document
.createElement("canvas");
17 image
.appendChild(imageCanvas
);
18 div
.appendChild(image
);
20 var imageContext
= imageCanvas
.getContext('2d');
21 imageCanvas
.width
= bitmap
.width
;
22 imageCanvas
.height
= bitmap
.height
;
23 var imagedata
= imageContext
.createImageData(bitmap
.width
,
25 var dataView
= new Uint8Array(bitmap
.data
);
26 for (var i
= 0; i
< bitmap
.width
* bitmap
.height
* 4; i
+= 1) {
27 imagedata
.data
[i
] = dataView
[i
];
29 imageContext
.putImageData(imagedata
, 0, 0);
32 function addNotification(senderId
, notificationId
, details
) {
33 var list
= document
.getElementById("notification_list");
35 var div
= document
.createElement("div");
36 div
.class = "notifications";
37 div
.id
= senderId
+ notificationId
;
39 line
= document
.createElement("br");
40 div
.appendChild(line
);
42 // Create a close button.
43 var closeButton
= document
.createElement("button");
44 closeButton
.class = "closeButtons";
45 var buttonText
= document
.createTextNode("close notificaiton");
46 closeButton
.appendChild(buttonText
);
47 div
.appendChild(closeButton
);
48 closeButton
.addEventListener("click", function(){
49 clearNotification(senderId
, notificationId
)
53 var bold
= document
.createElement("b");
54 var title
= document
.createElement("p");
55 bold
.appendChild(document
.createTextNode(details
.title
));
56 title
.appendChild(bold
);
57 div
.appendChild(title
);
59 // Display notification ID.
60 var id
= document
.createElement("p");
61 id
.appendChild(document
.createTextNode("Notification ID: " + notificationId
));
64 // Disply the type of notication.
65 var notType
= document
.createElement("p");
66 notType
.appendChild(document
.createTextNode("Type: " + details
.type
));
67 div
.appendChild(notType
);
69 // Display the priority field of the notification.
70 var priority
= document
.createElement("p");
71 priority
.appendChild(document
.createTextNode("Priority: " +
73 div
.appendChild(priority
);
75 // Display the message of the notification.
76 var message
= document
.createElement("p");
77 message
.appendChild(document
.createTextNode("Message: " + details
.message
));
78 div
.appendChild(message
);
80 // Display the icon image of the notification.
81 displayImage("Icon: ", details
.iconBitmap
, div
);
83 // Display the context message of the notification if it has one.
84 if ("contextMessage" in details
) {
85 var message
= document
.createElement("p");
86 message
.appendChild(document
.createTextNode("Message: " + details
.message
));
87 div
.appendChild(message
);
90 // Display the progress of the notification if it has one.
91 if (details
.type
== "progress" && "progress" in details
) {
92 var progress
= document
.createElement("p");
93 progress
.appendChild(document
.createTextNode("Progress: " +
95 div
.appendChild(progress
);
98 // Display if the notification is clickable.
99 if ("isClickable" in details
) {
100 var clickable
= document
.createElement("p");
101 clickable
.appendChild(document
.createTextNode("IsClickable: " +
102 details
.isClickable
));
103 div
.appendChild(clickable
);
106 // Display the time the notification was created.
107 if ("eventTime" in details
) {
108 var time
= document
.createElement("p");
109 time
.appendChild(document
.createTextNode("Event Time: " +
111 div
.appendChild(time
);
114 // Display the list data of the notification if it's of list type.
115 if (details
.type
= "list" && "items" in details
) {
116 for (var i
= 0, size
= details
.items
.length
; i
< size
; i
++) {
117 var item
= document
.createElement("p");
118 item
.appendChild(document
.createTextNode(
119 "Item " + (i
+1) + ": " +
120 details
.items
[i
].title
+ " - " +
121 details
.items
[i
].message
));
122 div
.appendChild(item
);
126 // Display image of the notification if it's of image type.
127 if (details
.type
= "image" && "imageBitmap" in details
) {
128 displayImage("Image: ", details
.imageBitmap
, div
);
131 // Display the buttons of the notification if it has some.
132 if ("buttons" in details
) {
133 for (var i
= 0, size
= details
.buttons
.length
; i
< size
; i
++) {
134 var button
= document
.createElement("p");
135 button
.appendChild(document
.createTextNode(
136 "Button " + (i
+1) + ": " + details
.buttons
[i
].title
));
137 div
.appendChild(button
);
138 if ("iconBitmap" in details
.buttons
[i
]) {
139 displayImage("Button " + (i
+1) + ": ",
140 details
.buttons
[i
].iconBitmap
,
146 div
.appendChild(document
.createElement("br"));
147 list
.appendChild(div
);
150 function clearedCallback(ifCleared
){}
152 function clearNotification(senderId
, notificationId
) {
153 var list
= document
.getElementById("notification_list");
154 list
.removeChild(document
.getElementById(senderId
+ notificationId
));
155 chrome
.notificationProvider
.notifyOnCleared(senderId
,
160 function notificationCreated(senderId
, notificationId
, details
){
161 addNotification(senderId
, notificationId
, details
);
164 function notificationUpdated(senderId
, notificationId
, details
){
165 var list
= document
.getElementById("notification_list");
166 list
.removeChild(document
.getElementById(senderId
+ notificationId
));
167 addNotification(senderId
, notificationId
, details
);
170 function notificationCleared(senderId
, notificationId
){
171 clearNotification(senderId
, notificationId
);