From f21afd2b5d4588da926a45163020d792961ecdb7 Mon Sep 17 00:00:00 2001 From: Lance Stout Date: Fri, 27 Sep 2013 09:47:54 -0700 Subject: [PATCH] Add unread count in page title and app badge. --- clientapp/models/me.js | 9 +++++++++ clientapp/models/state.js | 24 +++++++++++++++++++++++- clientapp/pages/base.js | 5 +---- clientapp/views/main.js | 9 +++++++++ 4 files changed, 42 insertions(+), 5 deletions(-) diff --git a/clientapp/models/me.js b/clientapp/models/me.js index 623d570..075f801 100644 --- a/clientapp/models/me.js +++ b/clientapp/models/me.js @@ -17,6 +17,7 @@ module.exports = HumanModel.define({ this.setActiveContact(this._activeContact); }, this); + this.contacts.bind('change:unreadCount', this.updateUnreadCount, this); app.state.bind('change:active', this.updateIdlePresence, this); }, session: { @@ -113,5 +114,13 @@ module.exports = HumanModel.define({ } app.api.sendPresence(update); + }, + updateUnreadCount: function () { + var unreadCounts = this.contacts.pluck('unreadCount'); + var count = unreadCounts.reduce(function (a, b) { return a + b; }); + if (count === 0) { + count = ''; + } + app.state.badge = '' + count; } }); diff --git a/clientapp/models/state.js b/clientapp/models/state.js index c3806d4..055648e 100644 --- a/clientapp/models/state.js +++ b/clientapp/models/state.js @@ -13,6 +13,13 @@ module.exports = HumanModel.define({ self.focused = true; self.markActive(); }); + if (window.macgap) { + document.addEventListener('sleep', function () { + clearTimeout(this.idleTimer); + self.markInactive(); + }, true); + } + this.markActive(); }, session: { @@ -21,7 +28,22 @@ module.exports = HumanModel.define({ connected: ['bool', true, false], hasConnected: ['bool', true, false], idleTimeout: ['number', true, 600000], - idleSince: 'date' + idleSince: 'date', + allowAlerts: ['bool', true, false], + badge: 'string', + pageTitle: 'string' + }, + derived: { + title: { + deps: ['pageTitle', 'badge'], + fn: function () { + var base = this.pageTitle ? 'Otalk - ' + this.pageTitle : 'Otalk'; + if (this.badge) { + return this.badge + ' • ' + base; + } + return base; + } + } }, markActive: function () { clearTimeout(this.idleTimer); diff --git a/clientapp/pages/base.js b/clientapp/pages/base.js index aa7c929..4187f7a 100644 --- a/clientapp/pages/base.js +++ b/clientapp/pages/base.js @@ -22,10 +22,7 @@ module.exports = HumanView.extend({ app.currentPage = this; - document.title = function () { - var title = _.result(self, 'title'); - return title ? title + '- Otalk' : 'Otalk'; - }(); + app.state.pageTitle = _.result(self, 'title'); this.trigger('pageloaded'); diff --git a/clientapp/views/main.js b/clientapp/views/main.js index e306e65..e019019 100644 --- a/clientapp/views/main.js +++ b/clientapp/views/main.js @@ -9,6 +9,9 @@ var MUCListItem = require('../views/mucListItem'); module.exports = HumanView.extend({ template: templates.body, + initialize: function () { + this.listenTo(app.state, 'change:title', this.handleTitle); + }, events: { 'click a[href]': 'handleLinkClick', 'click .reconnect': 'handleReconnect' @@ -38,5 +41,11 @@ module.exports = HumanView.extend({ app.navigate(path); return false; } + }, + handleTitle: function (e) { + document.title = app.state.title; + if (window.macgap) { + window.macgap.dock.badge = app.state.badge; + } } });