OLD | NEW |
| (Empty) |
1 // Copyright (c) 2013 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. | |
4 | |
5 var Galore = Galore || {}; | |
6 | |
7 Galore.controller = { | |
8 | |
9 BUTTON_IMAGE_SIZE: 64, | |
10 NOTIFICATION_ICON_SIZE: 80, | |
11 | |
12 create: function() { | |
13 var controller = Object.create(this); | |
14 controller.counter = 0; | |
15 controller.prefix = chrome.runtime.getURL('').slice(0, -1); | |
16 controller.view = Galore.view.create(this.prepare_.bind(controller)); | |
17 controller.listen_('onDisplayed'); | |
18 controller.listen_('onError'); | |
19 controller.listen_('onClosed'); | |
20 controller.listen_('onClicked'); | |
21 controller.listen_('onButtonClicked'); | |
22 return controller; | |
23 }, | |
24 | |
25 /** @private */ | |
26 listen_: function(event) { | |
27 var listener = this.event_.bind(this, event); | |
28 chrome.experimental.notification[event].addListener(listener); | |
29 }, | |
30 | |
31 /** @private */ | |
32 prepare_: function() { | |
33 Galore.NOTIFICATIONS.forEach(function (type) { | |
34 type.notifications.forEach(function (options) { | |
35 this.view.addNotificationButton( | |
36 type.templateType, | |
37 type.name, | |
38 this.replace_(options.iconUrl, this.BUTTON_IMAGE_SIZE), | |
39 this.notify_.bind(this, type.templateType, options)); | |
40 }, this); | |
41 }, this); | |
42 }, | |
43 | |
44 /** @private */ | |
45 id_: function() { | |
46 this.counter += 1; | |
47 return String(this.counter); | |
48 }, | |
49 | |
50 /** @private */ | |
51 notify_: function(type, options) { | |
52 var id = this.id_(); | |
53 var priority = this.view.getPriority(); | |
54 var expanded = this.expand_(options, type, priority); | |
55 if (chrome.experimental.notification.create) { | |
56 chrome.experimental.notification.create(id, expanded, function() {}); | |
57 } else { | |
58 expanded.replaceId = id; | |
59 delete expanded.buttonOneIconUrl; | |
60 delete expanded.buttonOneTitle; | |
61 delete expanded.buttonTwoIconUrl; | |
62 delete expanded.buttonTwoTitle; | |
63 chrome.experimental.notification.show(expanded, function() {}); | |
64 } | |
65 this.event_('create', id, 'priority: ' + priority); | |
66 }, | |
67 | |
68 /** @private */ | |
69 expand_: function(options, type, priority) { | |
70 var expanded = {templateType: type, priority: priority}; | |
71 Object.keys(options).forEach(function (key) { | |
72 expanded[key] = this.replace_(options[key], this.NOTIFICATION_ICON_SIZE); | |
73 }, this); | |
74 return expanded; | |
75 }, | |
76 | |
77 /** @private */ | |
78 replace_: function(option, size) { | |
79 var replaced; | |
80 if (typeof option === 'string') { | |
81 replaced = option.replace(/\$#/g, this.counter); | |
82 replaced = replaced.replace(/\$@/g, this.prefix); | |
83 replaced = replaced.replace(/\$%/g, size); | |
84 } else if (Array.isArray(option)) { | |
85 replaced = []; | |
86 option.forEach(function(element) { | |
87 replaced.push(this.replace_(element, size)); | |
88 }, this); | |
89 } else { | |
90 replaced = {}; | |
91 Object.keys(option).forEach(function (key) { | |
92 replaced[key] = this.replace_(option[key], size); | |
93 }, this); | |
94 } | |
95 return replaced; | |
96 }, | |
97 | |
98 /** @private */ | |
99 event_: function(event, id, var_args) { | |
100 this.view.logEvent('Notification #' + id + ': ' + event + '(' + | |
101 Array.prototype.slice.call(arguments, 2).join(', ') + | |
102 ')'); | |
103 } | |
104 | |
105 }; | |
OLD | NEW |