-
Notifications
You must be signed in to change notification settings - Fork 29
/
nw-desktop-notifications.js
156 lines (142 loc) · 3.59 KB
/
nw-desktop-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
145
146
147
148
149
150
151
152
153
154
155
156
(function(){
var requireNode = window.require;
var WINDOW_WIDTH = 290;
var gui = null;
var counter = 0;
if(requireNode){
gui = requireNode('nw.gui');
}
if(!window.LOCAL_NW){
window.LOCAL_NW = {};
}
function makeNewNotifyWindow(){
var win = gui.Window.open(
'nw-desktop-notifications.html', {
frame: false,
toolbar: false,
width: WINDOW_WIDTH,
height: 0,
'always-on-top': true,
show: false,
resizable: false
});
window.LOCAL_NW.DesktopNotificationsWindow = win;
window.LOCAL_NW.DesktopNotificationsWindowIsLoaded = false;
win.on('loaded', function(){
window.LOCAL_NW.DesktopNotificationsWindowIsLoaded = true;
$(win.window.document.body).find('#closer').click(function(){
slideOutNotificationWindow();
});
});
}
function closeAnyOpenNotificationWindows(){
if(!gui){
return false;
}
if(window.LOCAL_NW.DesktopNotificationsWindow){
window.LOCAL_NW.DesktopNotificationsWindow.close(true);
window.LOCAL_NW.DesktopNotificationsWindow = null;
}
}
function notify(icon, title, content, onClick){
if(!gui){
return false;
}
if(!window.LOCAL_NW.DesktopNotificationsWindow){
makeNewNotifyWindow();
}
var continuation = function(){
appendNotificationToWindow(icon, title, content, onClick);
slideInNotificationWindow();
$(window.LOCAL_NW.DesktopNotificationsWindow.window.document.body).find('#shouldstart').text('true');
};
if(window.LOCAL_NW.DesktopNotificationsWindowIsLoaded){
continuation();
}
else{
window.LOCAL_NW.DesktopNotificationsWindow.on('loaded',continuation);
}
return true;
}
function makeNotificationMarkup(iconUrl, title, content, id){
return "<li id='"+id+"'>"+
"<div class='icon'>" +
"<img src='"+iconUrl+"' />" +
"</div>" +
"<div class='title'>"+truncate(title, 35)+"</a></div>" +
"<div class='description'>"+truncate(content, 37)+"</div>" +
"</li>";
}
function appendNotificationToWindow(iconUrl, title, content, onClick){
var elemId = getUniqueId();
var markup = makeNotificationMarkup(iconUrl, title, content, elemId);
var jqBody = $(window.LOCAL_NW.DesktopNotificationsWindow.window.document.body);
jqBody.find('#notifications').append(markup);
jqBody.find('#'+elemId).click(onClick);
}
function slideInNotificationWindow(){
var win = window.LOCAL_NW.DesktopNotificationsWindow;
if(win.NOTIFICATION_IS_SHOWING){
return;
}
var y = screen.availTop;
var x = WINDOW_WIDTH;
win.moveTo(getXPositionOfNotificationWindow(win),y);
win.show();
win.NOTIFICATION_IS_SHOWING = true;
if(document.hasFocus()){
//win.blur();
}
function animate(){
setTimeout(function(){
if(y<60){
win.resizeTo(x,y);
y+=10;
animate();
}
},5);
}
animate();
}
function slideOutNotificationWindow(callback){
var win = window.LOCAL_NW.DesktopNotificationsWindow;
var y = win.height;
var x = WINDOW_WIDTH;
function animate(){
setTimeout(function(){
if(y>-10){
win.resizeTo(x,y);
y-=10;
animate();
}
else{
win.hide();
if(callback){
callback();
}
}
},5);
}
animate();
win.NOTIFICATION_IS_SHOWING = false;
}
function getXPositionOfNotificationWindow(win){
return screen.availLeft + screen.availWidth - (WINDOW_WIDTH+10);
}
function getUniqueId(){
return (+(new Date())) + '-' + (counter ++);
}
function truncate(str, size){
str = $.trim(str);
if(str.length > size){
return $.trim(str.substr(0,size))+'...';
}
else{
return str;
}
}
window.LOCAL_NW.desktopNotifications = {
notify: notify,
closeAnyOpenNotificationWindows: closeAnyOpenNotificationWindows
};
})();