-
Notifications
You must be signed in to change notification settings - Fork 113
/
notifications.js
144 lines (135 loc) · 5.02 KB
/
notifications.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
(function() {
function SimpleNotification(opts) {
// TODO - check permissions
this.id = opts.id
this.onClick = opts.onClick || this.defaultOnClick
this.onButtonClick = opts.onButtonClick || this.defaultOnButtonClick
this.data = opts.data
this.app = opts.parent
this.closeOnClick = true
var message = opts.message || jstorrent.constants.manifest.name
if (typeof message != 'string') {
message = JSON.stringify(message)
}
if (typeof opts.details != 'string') {
opts.details = JSON.stringify(opts.details)
}
this.type = opts.type || 'basic'
if (jstorrent.device.platform == 'Android') {
if (this.type == 'progress') {
this.type = 'basic' // not supported yet for cordova
}
}
this.notificationOpts = {
type: this.type,
title: message,
priority: opts.priority || 0,
message: opts.details,
iconUrl: "/images/js-128.png"
}
if (opts.contextMessage) {
this.notificationOpts.contextMessage = opts.contextMessage
}
if (opts.buttons) {
this.notificationOpts.buttons = opts.buttons
}
if (opts.progress) {
this.notificationOpts.progress = opts.progress || 0
}
//console.log('notification - opts', this.notificationOpts)
this.show()
}
jstorrent.Notification = SimpleNotification
SimpleNotification.prototype = {
updateTimestamp: function() {
// hopefully causes it to come to the foreground
// again. Nope. :-( doesn't. there doesn't seem to be any way
// to actually bring an existing notification back to the
// foreground
chrome.notifications.update(this.id, {eventTime: new Date().getTime()}, _.bind(function(wasUpdated) {
//console.log('notification.wasupdated',this.id)
},this))
},
defaultOnClick: function() {
//this._collection each blah.remove(this) // onClosed event gets triggered, which does this
chrome.notifications.clear(this.id, function(id) {
//console.log('cleared notification with id',id)
// hopefully onClosed gets triggered... ?
})
},
defaultOnButtonClick: function(idx) {
//this._collection each blah.remove(this) // onClosed event gets triggered, which does this
chrome.notifications.clear(this.id, function(id) {
console.log('(button click) cleared notification with id,idx',id,idx)
// hopefully onClosed gets triggered... ?
})
},
close: function() {
chrome.notifications.clear(this.id, function(){})
},
get_key: function() {
return this.id
},
show: function() {
if (jstorrent.options.disable_notifications) {
console.log("notifications were disabled in jstorrent.options",this.notificationOpts)
return
}
chrome.notifications.getPermissionLevel( function(p) {
if (p == "granted") {
var notification = chrome.notifications.create(this.id, this.notificationOpts, function(id) {
//console.log('created notification with id',id)
})
} else {
console.log('notification suppressed by user',p,this.notificationOpts)
}
}.bind(this))
},
handleClick: function() {
this.onClick()
if (this.closeOnClick) {
this.defaultOnClick()
}
},
handleButtonClick: function(idx) {
if (this.onButtonClick) {
this.onButtonClick(idx)
} else {
this.defaultOnButtonClick(idx)
}
// suspect
if (this.closeOnClick) {
this.defaultOnClick()
}
}
}
function Notifications() {
chrome.notifications.onClicked.addListener(this.onClicked.bind(this))
chrome.notifications.onButtonClicked.addListener(this.onButtonClicked.bind(this))
chrome.notifications.onClosed.addListener(this.onClosed.bind(this))
chrome.notifications.getAll( function(all) { this.all = all }.bind(this) )
}
Notifications.prototype = {
get: function(id) {
return this.all[id]
},
add: function(n) {
this.all[n.id] = n
},
remove: function(n) {
delete this[n.id]
chrome.notifications.clear(n.id)
},
containsKey: function(id) {
return !!this.all[id]
},
onClicked: function(id) {
},
onButtonClicked: function(id) {
},
onClosed: function(id) {
delete this[id]
}
}
jstorrent.Notifications = Notifications
})();