add sticky notf (#49)

This commit is contained in:
Lynn 2017-03-29 20:11:08 -07:00 committed by GitHub
parent be655b3076
commit 7989bcc044
5 changed files with 38 additions and 22 deletions

View File

@ -22,9 +22,18 @@
<label for='flash'>flash:</label>
<input type='checkbox' id='flash'/>
</p>
<p>
<label for='sticky'>sticky:</label>
<input type='checkbox' id='sticky'/>
</p>
<p>
<label for='color'>color:</label>
<input type='text' id='color' value='white'/>
</p>
<p>
<label for='tag'>tag:</label>
<input type='text' id='tag' value=''/>
</p>
<br>
<hr>
<p>Badge Count:<p>
@ -42,21 +51,22 @@
var body = document.getElementById('body').value;
var imageUrl = document.getElementById('image').value;
var shouldFlash = document.getElementById('flash').checked;
var shouldStick = document.getElementById('sticky').checked;
var color = document.getElementById('color').value;
var tag = document.getElementById('tag').value;
num++;
// notfs with same groupId will replace existing notf.
var groupId = (num % 2).toString();
var notf = new SYM_API.Notification(title, {
body: (body + ' num=' + num + ' groupId=' + groupId),
body: (body + ' num=' + num + ' tag=' + tag),
image: imageUrl,
flash: shouldFlash,
color: color || 'white',
sticky: shouldStick,
data: {
hello: 'hello word'
},
groupId: groupId
tag: tag
});
notf.addEventListener('click', onclick);

View File

@ -98,7 +98,7 @@ function setContents(notificationObj) {
let closeButton = notiDoc.getElementById('close');
// note: use onclick because we only want one handler, for case
// when content gets overwritten by notf with same groupId
// when content gets overwritten by notf with same tag
closeButton.onclick = function(clickEvent) {
clickEvent.stopPropagation()
ipc.send('electron-notify-close', winId, notificationObj)

View File

@ -265,13 +265,13 @@ function showNotification(notificationObj) {
return;
}
// check if group id provided. should replace existing notification
// check if tag id provided. should replace existing notification
// if has same grouping id.
let groupId = notificationObj.groupId;
if (groupId) {
let tag = notificationObj.tag;
if (tag) {
// first check waiting items
for(let i = 0; i < notificationQueue.length; i++) {
if (groupId === notificationQueue[ i ].groupId) {
if (tag === notificationQueue[ i ].tag) {
notificationQueue[ i ] = notificationObj;
resolve();
return;
@ -280,7 +280,7 @@ function showNotification(notificationObj) {
// next check items being shown
for(let i = 0; i < activeNotifications.length; i++) {
if (groupId === activeNotifications[ i ].groupId) {
if (tag === activeNotifications[ i ].tag) {
let notificationWindow = activeNotifications[ i ];
// be sure to call close event for existing, so it gets
@ -322,6 +322,7 @@ function showNotification(notificationObj) {
}
function setNotificationContents(notfWindow, notfObj) {
// Display time per notification basis.
let displayTime = notfObj.displayTime ? notfObj.displayTime : config.displayTime;
@ -329,21 +330,23 @@ function setNotificationContents(notfWindow, notfObj) {
clearTimeout(notfWindow.displayTimer);
}
// Set timeout to hide notification
var updatedNotificationWindow = notfWindow;
updatedNotificationWindow.tag = notfObj.tag;
let timeoutId;
let closeFunc = buildCloseNotification(notfWindow, notfObj, function() {
return timeoutId
});
let closeNotificationSafely = buildCloseNotificationSafely(closeFunc);
timeoutId = setTimeout(function() {
closeNotificationSafely('timeout');
}, displayTime);
var updatedNotificationWindow = notfWindow;
updatedNotificationWindow.displayTimer = timeoutId;
updatedNotificationWindow.groupId = notfObj.groupId;
// don't start timer to close if we aren't sticky
if (!notfObj.sticky) {
timeoutId = setTimeout(function() {
closeNotificationSafely('timeout');
}, displayTime);
updatedNotificationWindow.displayTimer = timeoutId;
}
// Trigger onShowFunc if existent
if (notfObj.onShowFunc) {

View File

@ -17,7 +17,8 @@ class Notify {
image: options.image || options.icon,
flash: options.flash,
color: options.color,
groupId: options.groupId,
tag: options.tag,
sticky: options.sticky || false,
onShowFunc: onShow.bind(this),
onClickFunc: onClick.bind(this),
onCloseFunc: onClose.bind(this),

View File

@ -24,9 +24,11 @@ class Notify {
* icon {string} url of image to show in notification
* flash {bool} true if notification should flash (default false)
* color {string} background color for notification
* groupId {string} non-empty string to unique identify notf, if another
* notification arrives with same groupId then it's content will
* tag {string} non-empty string to unique identify notf, if another
* notification arrives with same tag then it's content will
* replace existing notification.
* sticky {bool} if true notification will stay until user closes. default
* is false.
* data {object} arbitrary object to be stored with notification
* }
*/