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

View File

@ -98,7 +98,7 @@ function setContents(notificationObj) {
let closeButton = notiDoc.getElementById('close'); let closeButton = notiDoc.getElementById('close');
// note: use onclick because we only want one handler, for case // 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) { closeButton.onclick = function(clickEvent) {
clickEvent.stopPropagation() clickEvent.stopPropagation()
ipc.send('electron-notify-close', winId, notificationObj) ipc.send('electron-notify-close', winId, notificationObj)

View File

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

View File

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

View File

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