Skip to content

Commit

Permalink
initial commit of desktop notifications via notify.js by alexgibson
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Grim committed Feb 6, 2015
1 parent 7e0d35e commit 09f4a64
Show file tree
Hide file tree
Showing 15 changed files with 383 additions and 15 deletions.
1 change: 1 addition & 0 deletions following.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<script src="js/jquery.jsonrpcclient.js"></script>
<script src="js/jquery.storageapi.js"></script>
<script src="js/options.js"></script>
<script src="js/notify.js"></script>
<script src="js/mobile_abstract.js"></script>
<script src="js/twister_io.js"></script>
<script src="js/polyglot.min.js"></script>
Expand Down
2 changes: 2 additions & 0 deletions home.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<script src="js/jquery.jsonrpcclient.js"></script>
<script src="js/jquery.storageapi.js"></script>
<script src="js/options.js"></script>
<script src="js/notify.js"></script>
<script src="js/mobile_abstract.js"></script>
<script src="js/twister_io.js"></script>
<script src="js/polyglot.min.js"></script>
Expand All @@ -26,6 +27,7 @@
<script src="js/interface_common.js"></script>
<script src="js/interface_home.js"></script>
<script src="js/jquery.textcomplete.js"></script>

<script>
$(function(){setTimeout(mensAutocomplete, 800);})
changeStyle();
Expand Down
17 changes: 17 additions & 0 deletions js/interface_common.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,23 @@ function updateHashtagModal(postboard,hashtag,timeoutArgs) {
var resource = $hashtagModalClass.attr("data-resource");

requestHashtag(postboard,hashtag,resource,timeoutArgs);

if( _hashtagPendingPostsUpdated ) {
var desktopNotification = new Notify(polyglot.t('notify_desktop_title'), {
body: 'You got '+polyglot.t("new_posts", _hashtagPendingPostsUpdated)+' in search result.',
icon: '../img/twister_mini.png',
tag: 'twister_notification_new_posts_modal',
timeout: _desktopNotificationTimeout,
notifyClick: function() {
$(".postboard-news").hide();
displayHashtagPending($(".hashtag-modal .postboard-posts"));
}
});
desktopNotification.show();

_hashtagPendingPostsUpdated = 0;
}

// use extended timeout parameters on modal refresh (requires twister_core >= 0.9.14).
// our first query above should be faster (with default timeoutArgs of twisterd),
// then we may possibly collect more posts on our second try by waiting more.
Expand Down
81 changes: 81 additions & 0 deletions js/interface_localization.js

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions js/mobile_abstract.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,17 @@ var MAL = function()
newTweetsBar.fadeIn("slow");
newTweetsBarMenu.text(String(newPosts));
newTweetsBarMenu.addClass("show");

var desktopNotification = new Notify(polyglot.t('notify_desktop_title'), {
body: 'You got '+polyglot.t("new_posts", newPosts)+' in postboard.',
icon: '../img/twister_mini.png',
tag: 'twister_notification_new_postboard',
timeout: _desktopNotificationTimeout,
notifyClick: function() {
requestTimelineUpdate("latest",postsPerRefresh,followingUsers,promotedPostsOnly);
}
});
desktopNotification.show();
} else {
newTweetsBar.hide();
newTweetsBar.text("");
Expand Down
196 changes: 196 additions & 0 deletions js/notify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
/*
* Author: Alex Gibson
* https://github.com/alexgibson/notify.js
* License: MIT license
*/

(function(global, factory) {
if (typeof define === 'function' && define.amd) {
// AMD environment
define(function() {
return factory(global, global.document);
});
} else if (typeof module !== 'undefined' && module.exports) {
// CommonJS environment
module.exports = factory(global, global.document);
} else {
// Browser environment
global.Notify = factory(global, global.document);
}
} (typeof window !== 'undefined' ? window : this, function (w, d) {

'use strict';

function isFunction (item) {
return typeof item === 'function';
}

function Notify(title, options) {

if (typeof title !== 'string') {
throw new Error('Notify(): first arg (title) must be a string.');
}

this.title = title;

this.options = {
icon: '',
body: '',
tag: '',
notifyShow: null,
notifyClose: null,
notifyClick: null,
notifyError: null,
permissionGranted: null,
permissionDenied: null,
timeout: null
};

this.permission = null;

if (!Notify.isSupported) {
return;
}

//User defined options for notification content
if (typeof options === 'object') {

for (var i in options) {
if (options.hasOwnProperty(i)) {
this.options[i] = options[i];
}
}

//callback when notification is displayed
if (isFunction(this.options.notifyShow)) {
this.onShowCallback = this.options.notifyShow;
}

//callback when notification is closed
if (isFunction(this.options.notifyClose)) {
this.onCloseCallback = this.options.notifyClose;
}

//callback when notification is clicked
if (isFunction(this.options.notifyClick)) {
this.onClickCallback = this.options.notifyClick;
}

//callback when notification throws error
if (isFunction(this.options.notifyError)) {
this.onErrorCallback = this.options.notifyError;
}
}
}

// true if the browser supports HTML5 Notification
Notify.isSupported = 'Notification' in w;

// true if the permission is not granted
Notify.needsPermission = !(Notify.isSupported && Notification.permission === 'granted');

// returns current permission level ('granted', 'default', 'denied' or null)
Notify.permissionLevel = (Notify.isSupported ? Notification.permission : null);

// asks the user for permission to display notifications. Then calls the callback functions is supplied.
Notify.requestPermission = function (onPermissionGrantedCallback, onPermissionDeniedCallback) {
if (!Notify.isSupported) {
return;
}
w.Notification.requestPermission(function (perm) {
switch (perm) {
case 'granted':
Notify.needsPermission = false;
if (isFunction(onPermissionGrantedCallback)) {
onPermissionGrantedCallback();
}
break;
case 'denied':
if (isFunction(onPermissionDeniedCallback)) {
onPermissionDeniedCallback();
}
break;
}
});
};


Notify.prototype.show = function () {

if (!Notify.isSupported) {
return;
}

this.myNotify = new Notification(this.title, {
'body': this.options.body,
'tag' : this.options.tag,
'icon' : this.options.icon
});

if (this.options.timeout && !isNaN(this.options.timeout)) {
setTimeout(this.close.bind(this), this.options.timeout * 1000);
}

this.myNotify.addEventListener('show', this, false);
this.myNotify.addEventListener('error', this, false);
this.myNotify.addEventListener('close', this, false);
this.myNotify.addEventListener('click', this, false);
};

Notify.prototype.onShowNotification = function (e) {
if (this.onShowCallback) {
this.onShowCallback(e);
}
};

Notify.prototype.onCloseNotification = function (e) {
if (this.onCloseCallback) {
this.onCloseCallback(e);
}
this.destroy();
};

Notify.prototype.onClickNotification = function (e) {
if (this.onClickCallback) {
this.onClickCallback(e);
}
};

Notify.prototype.onErrorNotification = function (e) {
if (this.onErrorCallback) {
this.onErrorCallback(e);
}
this.destroy();
};

Notify.prototype.destroy = function () {
this.myNotify.removeEventListener('show', this, false);
this.myNotify.removeEventListener('error', this, false);
this.myNotify.removeEventListener('close', this, false);
this.myNotify.removeEventListener('click', this, false);
};

Notify.prototype.close = function () {
this.myNotify.close();
};

Notify.prototype.handleEvent = function (e) {
switch (e.type) {
case 'show':
this.onShowNotification(e);
break;
case 'close':
this.onCloseNotification(e);
break;
case 'click':
this.onClickNotification(e);
break;
case 'error':
this.onErrorNotification(e);
break;
}
};

return Notify;

}));
19 changes: 18 additions & 1 deletion js/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ $(function() {

});

var _desktopNotificationTimeout = 4; // it should be an option

var TwisterOptions = function()
{
this.getOption = function(optionName, defaultValue) {
Expand Down Expand Up @@ -88,6 +90,20 @@ var TwisterOptions = function()
player[0].play();
}

this.notificationDesktopTest = function() {
$('#notifications-desktop-test').on('click', function(){
var desktopNotification = new Notify(polyglot.t('notify_desktop_title'), {
body: polyglot.t('notify_desktop_test'),
icon: '../img/twister_mini.png',
tag: 'twister_notification_test',
timeout: _desktopNotificationTimeout,
notifyError: function() { alert(polyglot.t('notify_desktop_error')); },
permissionDenied: function() { alert(polyglot.t('notify_desktop_perm_denied')); }
});
desktopNotification.show();
})
}

this.keysSendDefault = "ctrlenter";
this.keysSend = function() {
$('#keysOpt select')[0].value = $.Options.getOption('keysSend',this.keysSendDefault);
Expand Down Expand Up @@ -338,6 +354,7 @@ var TwisterOptions = function()
this.InitOptions = function() {
this.soundNotifOptions();
this.volumeControl();
this.notificationDesktopTest();
this.keysSend();
this.setLang();
this.setTheme();
Expand Down Expand Up @@ -365,7 +382,7 @@ function localizeLabels()
{
$("label[for=tab_language]").text(polyglot.t("Language"));
$("label[for=t-2]").text(polyglot.t("Theme"));
$("label[for=t-3]").text(polyglot.t("Sound"));
$("label[for=t-3]").text(polyglot.t("Notifications"));
$("label[for=t-4]").text(polyglot.t("Keys"));
$("label[for=t-5]").text(polyglot.t("Postboard"));
$("label[for=t-6]").text(polyglot.t("Users"));
Expand Down
2 changes: 2 additions & 0 deletions js/twister_actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ var maxExpandPost = 8;
var maxExpandPostTop = 4;
var _hashtagProcessedMap = {};
var _hashtagPendingPosts = [];
var _hashtagPendingPostsUpdated = 0;
var autoUpdateHashtag = false;

// ----------------
Expand Down Expand Up @@ -335,6 +336,7 @@ function processHashtag(postboard, hashtag, data) {
if( !(key in _hashtagProcessedMap) ) {
_hashtagProcessedMap[key] = true;
_hashtagPendingPosts.push(data[i]);
_hashtagPendingPostsUpdated++;
}
}

Expand Down

0 comments on commit 09f4a64

Please sign in to comment.