Skip to content

Commit

Permalink
Merge tag '0.6.84' into develop
Browse files Browse the repository at this point in the history
Update rich notification handling
  • Loading branch information
potomak committed Jan 2, 2015
2 parents 3c6c446 + 098fafd commit 77102c9
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 25 deletions.
44 changes: 24 additions & 20 deletions app/assets/javascripts/notifier.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,46 @@
// See:
// * https://developer.mozilla.org/en-US/docs/Web/API/notification
// * https://developer.chrome.com/apps/notifications
function Notifier() {}

// Returns "true" if this browser supports notifications.
Notifier.prototype.hasSupport = function() {
return !!window.webkitNotifications;
}

// Request permission for this page to send notifications. If allowed,
// calls function "cb" with true.
Notifier.prototype.requestPermission = function(cb) {
window.webkitNotifications.requestPermission(function() {
cb && cb((new Notifier()).hasPermission());
return "Notification" in window;
};

// Request permission for this page to send notifications.
Notifier.prototype.requestPermission = function(callback) {
Notification.requestPermission(function(permission) {
callback(permission === "granted");
});
}
};

Notifier.prototype.hasPermission = function() {
return this.hasSupport() && 0 == window.webkitNotifications.checkPermission();
}
return Notification.permission === "granted";
};

// Popup a notification with icon, title, and body. Returns false if
// permission was not granted.
Notifier.prototype.notify = function(icon, title, body) {
if (this.hasPermission()) {
var popup = window.webkitNotifications.createNotification(icon, title, body);
popup.show();

popup.onclick = function(x) {
console.log(icon, title, body);

var notification = new Notification(body, { icon: icon, title: title });
console.log(notification);

notification.onclick = function() {
window.focus();
this.cancel();
notification.close();
};

setTimeout(function(){
popup.cancel();
setTimeout(function() {
notification.close();
}, 10000);

return true;
}

return false;
}
};

var NOTIFIER = new Notifier();
var NOTIFIER = new Notifier();
6 changes: 3 additions & 3 deletions app/assets/javascripts/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ function permissionCallback(hasPermission) {
$(document).ready(function() {
if(NOTIFIER.hasSupport()) {
permissionCallback(NOTIFIER.hasPermission());

$("#request_notification_permission a").click(function(event) {
NOTIFIER.requestPermission(permissionCallback);
event.preventDefault();
});
}
else {
$("#request_notification_permission").hide();
$("#notification_not_supported").show();
}

if($('#colorpicker').length > 0) {
$('#colorpicker').farbtastic('#user_color');
}
});
});
3 changes: 2 additions & 1 deletion app/views/users/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@

<div id="request_notification_permission">
<%= link_to "", "#" %>
<div id="notification_not_supported" style="display: none">Note: notifications are not supported by your browser.</div>
</div>

<%= render 'form' %>
<%= link_to 'Back', current_user %>
<%= link_to 'Back', current_user %>
2 changes: 1 addition & 1 deletion config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
end

module TomatoesApp
VERSION = '0.6.83'
VERSION = '0.6.84'
REPO = 'https://github.com/potomak/tomatoes'

class Application < Rails::Application
Expand Down

0 comments on commit 77102c9

Please sign in to comment.