Merge lp:~daker/ubuntu-html5-theme/fix.1293899 into lp:ubuntu-html5-theme

Proposed by Adnane Belmadiaf
Status: Merged
Approved by: Alexandre Abreu
Approved revision: 166
Merged at revision: 189
Proposed branch: lp:~daker/ubuntu-html5-theme/fix.1293899
Merge into: lp:ubuntu-html5-theme
Diff against target: 90 lines (+65/-1)
1 file modified
0.1/ambiance/js/toolbars.js (+65/-1)
To merge this branch: bzr merge lp:~daker/ubuntu-html5-theme/fix.1293899
Reviewer Review Type Date Requested Status
Alexandre Abreu Approve
Review via email: mp+213394@code.launchpad.net

Commit message

Added timer to hide the toolbar

To post a comment you must log in.
162. By Adnane Belmadiaf

Fixed toolbar docs

Revision history for this message
Alexandre Abreu (abreu-alexandre) wrote :

I think it could be a bit better/cleaner. Maybe something more consistent, and prone to errors (forgot to add a window.clearTimeout, etc.) w/ e.g. the MutationObserver class:

    function ToolbarListener(id) {
 this._id = id;
 this._onChangedCallbacks = [];
 this._listen();
    };
    ToolbarListener.prototype = {
 onchanged: function(callback) {
     if (callback && typeof callback === 'function')
  this._onChangedCallbacks.push(callback);
 },
 _listen: function() {
     var mutationObserverClass =
  this._getNativeMutationObserverClass();
     if ( ! mutationObserverClass) {
  console.error(
      'Could not listen to toolbar changes: no mutation observer found');
  return;
     }
     var toolbar = document.getElementById(this._id);
     if (toolbar) {
  var observer = new mutationObserverClass(
      this._onMutated.bind(this));
  observer.observe(toolbar, {attributes: true});
     }
 },
 _onMutated: function(mutations, observer) {
     for (var i = 0; i < this._onChangedCallbacks.length; ++i) {
  this._onChangedCallbacks[i](mutations);
     }
 },
 _getNativeMutationObserverClass: function() {
     return window.MutationObserver || window.WebKitMutationObserver;
 },
    };

and in function Toolbar(id, touchInfoDelegate):

 this._timer = null;

 var listener = new ToolbarListener(id);
        var self = this;
 listener.onchanged(function() {
     var toolbar = self.toolbar;
     function __isToolbarVisible() {
  return Array.prototype.slice.call(toolbar.classList).indexOf('revealed') >= 0;
     }
     if (__isToolbarVisible()) {
  self._timer = window.setTimeout(
      function () { self.hide(); },
      5000);
     }
     else {
  if (self._timer) {
      window.clearTimeout(self._timer);
      self._timer = null;
  }
     }
 });

no need to sparkle logic bits in the class, the states are kept consistent in one location along w/ the management of the timer

?

review: Needs Fixing
163. By Adnane Belmadiaf

Fixed the timer class

164. By Adnane Belmadiaf

Fixed timer name

Revision history for this message
Alexandre Abreu (abreu-alexandre) wrote :

Could you make sure that the ToolbarListener is private to the Toolbar def? like inside at the top of the (function() { ?

review: Needs Fixing
Revision history for this message
Alexandre Abreu (abreu-alexandre) wrote :

And you can remove the cleartimeouts ... they are being handled automatically

165. By Adnane Belmadiaf

Made ToolbarListener private

166. By Adnane Belmadiaf

Removed last clearTimeout

Revision history for this message
Alexandre Abreu (abreu-alexandre) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file '0.1/ambiance/js/toolbars.js'
2--- 0.1/ambiance/js/toolbars.js 2014-02-13 17:20:57 +0000
3+++ 0.1/ambiance/js/toolbars.js 2014-04-04 21:23:13 +0000
4@@ -43,7 +43,7 @@
5 </footer>
6
7 JavaScript access:
8- var toolbar = UI.footer("footerID");
9+ var toolbar = UI.toolbar("toolbarID");
10 UI.button('home').click(function () {
11 UI.pagestack.push("main");
12 });
13@@ -52,6 +52,45 @@
14
15 var Toolbar = (function () {
16
17+ function ToolbarListener(id) {
18+ this._id = id;
19+ this._onChangedCallbacks = [];
20+ this._listen();
21+ };
22+
23+ ToolbarListener.prototype = {
24+ onchanged: function (callback) {
25+ if (callback && typeof callback === 'function')
26+ this._onChangedCallbacks.push(callback);
27+ },
28+ _listen: function () {
29+ var mutationObserverClass =
30+ this._getNativeMutationObserverClass();
31+ if (!mutationObserverClass) {
32+ console.error(
33+ 'Could not listen to toolbar changes: no mutation observer found');
34+ return;
35+ }
36+ var toolbar = document.getElementById(this._id);
37+ if (toolbar) {
38+ var observer = new mutationObserverClass(
39+ this._onMutated.bind(this));
40+ observer.observe(toolbar, {
41+ attributes: true
42+ });
43+ }
44+ },
45+ _onMutated: function (mutations, observer) {
46+ for (var i = 0; i < this._onChangedCallbacks.length; ++i) {
47+ this._onChangedCallbacks[i](mutations);
48+ }
49+ },
50+ _getNativeMutationObserverClass: function () {
51+ return window.MutationObserver || window.WebKitMutationObserver;
52+ },
53+ };
54+
55+
56 function Toolbar(id, touchInfoDelegate) {
57
58 this.PHASE_START = "start";
59@@ -90,6 +129,31 @@
60 touchEvents.touchMove, this.toolbar, this.__onTouchMove.bind(this));
61 touchInfoDelegate.registerTouchEvent(
62 touchEvents.touchLeave, this.toolbar, this.__onTouchLeave.bind(this));
63+
64+ this._timer = null;
65+
66+ var listener = new ToolbarListener(id);
67+ var self = this;
68+ listener.onchanged(function () {
69+ var toolbar = self.toolbar;
70+
71+ function __isToolbarVisible() {
72+ return Array.prototype.slice.call(toolbar.classList)
73+ .indexOf('revealed') >= 0;
74+ }
75+ if (__isToolbarVisible()) {
76+ self._timer = window.setTimeout(
77+ function () {
78+ self.hide();
79+ },
80+ 5000);
81+ } else {
82+ if (self._timer) {
83+ window.clearTimeout(self._timer);
84+ self._timer = null;
85+ }
86+ }
87+ });
88 };
89
90 Toolbar.prototype = {

Subscribers

People subscribed via source and target branches