From 9f56d7d19d5bb7eb0dd1bcde9f7d21b3ccd0cf54 Mon Sep 17 00:00:00 2001 From: Sam Saffron Date: Tue, 10 Jun 2014 10:03:29 +1000 Subject: [PATCH] BUGFIX: unclearable blue unread circles There were 2 issues: 1. We were resetting our tracking on large amounts of idle time 2. We used focus trakcing which is fragile and broken on iPad vs page visibility API --- .../discourse/initializers/focus-event.js.es6 | 62 +++++++++++++++++-- .../javascripts/discourse/lib/screen_track.js | 1 - 2 files changed, 57 insertions(+), 6 deletions(-) diff --git a/app/assets/javascripts/discourse/initializers/focus-event.js.es6 b/app/assets/javascripts/discourse/initializers/focus-event.js.es6 index bf137e3004c..225745b2095 100644 --- a/app/assets/javascripts/discourse/initializers/focus-event.js.es6 +++ b/app/assets/javascripts/discourse/initializers/focus-event.js.es6 @@ -5,14 +5,66 @@ export default { name: 'focus-event', initialize: function() { + var hidden = "hidden"; // Default to true Discourse.set('hasFocus', true); - $(window).focus(function() { - Discourse.setProperties({hasFocus: true, notify: false}); - }).blur(function() { - Discourse.set('hasFocus', false); - }); + var gotFocus = function() { + if(!Discourse.get('hasFocus')){ + Discourse.setProperties({hasFocus: true, notify: false}); + } + }; + + var lostFocus = function() { + if(Discourse.get('hasFocus')) { + Discourse.set('hasFocus', false); + } + }; + + var onchange = function(evt) { + var v = 'visible', h = 'hidden', + evtMap = { + focus:v, focusin:v, pageshow:v, blur:h, focusout:h, pagehide:h + }; + + evt = evt || window.event; + if (evt.type in evtMap){ + if(evtMap[evt.type] === 'hidden') { + lostFocus(); + } else { + gotFocus(); + } + } + else { + if(this[hidden]) { + lostFocus(); + } else { + gotFocus(); + } + } + }; + + // from StackOverflow http://stackoverflow.com/a/1060034/17174 + if (hidden in document) { + document.addEventListener('visibilitychange', onchange); + } + else if ((hidden = 'mozHidden') in document) { + document.addEventListener('mozvisibilitychange', onchange); + } + else if ((hidden = 'webkitHidden') in document) { + document.addEventListener('webkitvisibilitychange', onchange); + } + else if ((hidden = 'msHidden') in document) { + document.addEventListener('msvisibilitychange', onchange); + } + // IE 9 and lower: + else if ('onfocusin' in document) { + document.onfocusin = document.onfocusout = onchange; + } + // All others (including iPad which is a bit weird and gives onpageshow / hide + else { + window.onpageshow = window.onpagehide = window.onfocus = window.onblur = onchange; + } } }; diff --git a/app/assets/javascripts/discourse/lib/screen_track.js b/app/assets/javascripts/discourse/lib/screen_track.js index 5cadfd76423..70b47de1e1a 100644 --- a/app/assets/javascripts/discourse/lib/screen_track.js +++ b/app/assets/javascripts/discourse/lib/screen_track.js @@ -149,7 +149,6 @@ Discourse.ScreenTrack = Ember.Object.extend({ // If the user hasn't scrolled the browser in a long time, stop tracking time read var sinceScrolled = new Date().getTime() - this.get('lastScrolled'); if (sinceScrolled > PAUSE_UNLESS_SCROLLED) { - this.reset(); return; }