Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Favicon unread count #267

Merged
merged 3 commits into from Jan 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
47 changes: 46 additions & 1 deletion app/assets/javascripts/application.js
Expand Up @@ -31,6 +31,46 @@ function getMarkedOrCurrentRows() {
return hasMarkedRows() ? getMarkedRows() : getCurrentRow()
}

function updateFavicon() {
$.get( "/notifications/unread_count", function(data) {
var unread_count = data["count"];
if ( unread_count > 0 ) {
var old_link = document.getElementById('favicon-count');
if ( old_link ) {
$(old_link).remove();
}

var canvas = document.createElement('canvas'),
ctx,
img = document.createElement('img'),
link = document.getElementById('favicon').cloneNode(true),
txt = unread_count + '';

link.id = "favicon-count";

if (canvas.getContext) {
canvas.height = canvas.width = 16;
ctx = canvas.getContext('2d');
img.onload = function () {
ctx.drawImage(this, 0, 0);

ctx.fillStyle = '#f93e00';
var width = ctx.measureText(txt).width;
ctx.fillRect(0, 0, width+2, 12);

ctx.font = 'bold 10px "helvetica", sans-serif';
ctx.fillStyle = '#fff';
ctx.fillText(txt, 1, 10);

link.href = canvas.toDataURL('image/png');
document.body.appendChild(link);
};
img.src = "/favicon-16x16.png";
}
}
});
}

document.addEventListener("turbolinks:load", function() {
$('button.archive_selected, button.unarchive_selected').click(toggleArchive);
$('button.mute_selected').click(mute);
Expand Down Expand Up @@ -66,6 +106,8 @@ document.addEventListener("turbolinks:load", function() {
})

$('[data-toggle="tooltip"]').tooltip()

updateFavicon()
});

document.addEventListener("turbolinks:before-cache", function() {
Expand Down Expand Up @@ -148,11 +190,14 @@ function markReadSelected() {
var rows = getMarkedOrCurrentRows();
$.post("/notifications/mark_read_selected", {'id[]': getIdsFromRows(rows)}).done(function () {
rows.removeClass('active');
updateFavicon();
})
}

function markRead(id) {
$.get( "/notifications/"+id+"/mark_read");
$.get( "/notifications/"+id+"/mark_read", function() {
updateFavicon();
});
$('#notification-'+id).removeClass('active');
}

Expand Down
6 changes: 6 additions & 0 deletions app/controllers/notifications_controller.rb
Expand Up @@ -31,6 +31,12 @@ def index
@notifications = scope.newest.page(page).per(per_page)
end

def unread_count
scope = current_user.notifications
count = scope.inbox.distinct.group(:unread).count.fetch(true){ 0 }
render json: { 'count': count }
end

def mute_selected
notifications = current_user.notifications.where(id: params[:id])
notifications.each do |notification|
Expand Down
2 changes: 1 addition & 1 deletion app/views/layouts/application.html.erb
Expand Up @@ -16,7 +16,7 @@
<link rel="icon" type="image/png" sizes="192x192" href="/android-icon-192x192.png">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="96x96" href="/favicon-96x96.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
<link id="favicon" rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
<link rel="manifest" href="/manifest.json">
<meta name="msapplication-TileColor" content="#ffffff">
<meta name="msapplication-TileImage" content="/ms-icon-144x144.png">
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Expand Up @@ -16,6 +16,7 @@
post :sync
post :mute_selected
post :mark_read_selected
get :unread_count
end

member do
Expand Down