~dpm/+junk/oltest

« back to all changes in this revision

Viewing changes to www/ambiance/js/page.js

  • Committer: David Planella
  • Date: 2015-06-20 10:23:35 UTC
  • Revision ID: david.planella@ubuntu.com-20150620102335-8qhojl4z1r6v2dfv
Initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2013, 2014 Adnane Belmadiaf <daker@ubuntu.com>
 
3
 * License granted by Canonical Limited
 
4
 *
 
5
 * This file is part of ubuntu-html5-ui-toolkit.
 
6
 *
 
7
 * This package is free software; you can redistribute it and/or modify
 
8
 * it under the terms of the GNU Lesser General Public License as
 
9
 * published by the Free Software Foundation; either version 3 of the
 
10
 * License, or
 
11
 * (at your option) any later version.
 
12
 
 
13
 * This package is distributed in the hope that it will be useful,
 
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
 * GNU General Public License for more details.
 
17
 
 
18
 * You should have received a copy of the GNU Lesser General Public
 
19
 * License along with this program. If not, see
 
20
 * <http://www.gnu.org/licenses/>.
 
21
 */
 
22
 
 
23
/**
 
24
 * One of the navigation pattern that can be used within an Ubuntu App is the deep navigation. This
 
25
 pattern is implemented by the Pagestack. A Pagestack contains one or more Pages. Each page displays full-screen. See the Pagestack class.
 
26
 
 
27
Each Page must have <em>id</em> and <em>data-title</em> attributes. The <em>id</em> attribute is used a unique reference to push the Page to the top of the Pagestack (see the Pagestack class). The <em>data-title</em> attribute is used to update the Header title as pages are pushed and poped.
 
28
 
 
29
 * @class Page
 
30
 * @constructor
 
31
 * @namespace UbuntuUI
 
32
 * @example
 
33
      </body>
 
34
        <div data-role="mainview">
 
35
 
 
36
          <header data-role="header">
 
37
          </header>
 
38
 
 
39
          <div data-role="content">
 
40
            <div data-role="pagestack">
 
41
              <div data-role="page" data-title="Main" id="main">
 
42
                [...]
 
43
              </div>
 
44
              <div data-role="page" data-title="My Data" id="data">
 
45
                [...]
 
46
              </div>
 
47
            </div>
 
48
          </div>
 
49
 
 
50
        </div>
 
51
      </body>
 
52
 
 
53
      JavaScript access:
 
54
      var page = UI.page("pageID");
 
55
 */
 
56
var Page = function (id) {
 
57
    this.id = id;
 
58
    this.onActivatedCallbacks = [];
 
59
 
 
60
    this._header = document.querySelector('div[data-role="mainview"] header');
 
61
    this._tabTitle = this._header.querySelector('[data-role="tabtitle"]');
 
62
    this.__setup();
 
63
};
 
64
Page.PAGE_ACTIVATED_EVENT = 'ubuntu-html5-on-page-activated';
 
65
 
 
66
Page.prototype = {
 
67
    /**
 
68
     * Returns the DOM element associated with the selector this widget is bind to.
 
69
     * @method element
 
70
     * @example
 
71
       var mypage = UI.page("pageid").element();
 
72
    */
 
73
    element: function () {
 
74
        return document.getElementById(this.id);
 
75
    },
 
76
 
 
77
    /**
 
78
     * actions property.
 
79
     * @property {List} actions
 
80
     */
 
81
    get actions() {
 
82
        // TODO: Not implemented yet
 
83
        return [];
 
84
    },
 
85
    set actions(value) {
 
86
        // TODO: Not implemented yet
 
87
    },
 
88
 
 
89
    /**
 
90
     * title property.
 
91
     * @property {String} title
 
92
     */
 
93
    get title() {
 
94
        // TODO: Not implemented yet
 
95
        return "";
 
96
    },
 
97
    set title(value) {
 
98
        // TODO: Not implemented yet
 
99
    },
 
100
 
 
101
    /**
 
102
     * Deactivates the current page.
 
103
     * @method {} deactivate
 
104
     */
 
105
    deactivate: function () {
 
106
        this.__updateVisibleState('none');
 
107
    },
 
108
 
 
109
    /**
 
110
     * Activates the current page.
 
111
     * @method {} activate
 
112
     * @param {Object} properties - Data to be passed down to any activation callback listening for the page activation (see Page.onactivated)
 
113
     */
 
114
    activate: function (properties) {
 
115
        this.__hideVisibleSibling();
 
116
        this.__updateVisibleState('block');
 
117
        this.__updateHeaderTitle();
 
118
        this.__dispatchActivatedEvent(properties);
 
119
    },
 
120
 
 
121
    /**
 
122
     * Activates the current page.
 
123
     * @method {} onactivated
 
124
     * @param {Function} callback - Callback function called with activation properties (from Pagestack.push) when the page is activated
 
125
     */
 
126
    onactivated: function(callback) {
 
127
        if (callback && typeof callback === 'function')
 
128
            this.onActivatedCallbacks.push(callback);
 
129
    },
 
130
 
 
131
    /**
 
132
     * @private
 
133
     */
 
134
    __setup: function() {
 
135
        var self = this;
 
136
        if (this.id && this.element() != null) {
 
137
            this.element().addEventListener(
 
138
                Page.PAGE_ACTIVATED_EVENT,
 
139
                function(event) {
 
140
                    if (! event.target || self.onActivatedCallbacks.length === 0)
 
141
                        return;
 
142
                    self.onActivatedCallbacks.forEach(
 
143
                        function(callback) {
 
144
                            callback(event.data);
 
145
                        });
 
146
                });
 
147
        }
 
148
    },
 
149
 
 
150
    /**
 
151
     * @private
 
152
     */
 
153
    __dispatchActivatedEvent: function (data) {
 
154
        if ( ! this.element())
 
155
            return;
 
156
        var event = document.createEvent('Event');
 
157
        event.initEvent(Page.PAGE_ACTIVATED_EVENT,true,true);
 
158
        event.data = data;
 
159
        this.element().dispatchEvent(event);
 
160
    },
 
161
 
 
162
    /**
 
163
     * @private
 
164
     */
 
165
    __updateHeaderTitle: function () {
 
166
        if (!this.element().getAttribute('data-title'))
 
167
            return;
 
168
 
 
169
        if (!this._header)
 
170
            return;
 
171
 
 
172
        var DEFAULT_TITLE = 'Unknown';
 
173
        var title = DEFAULT_TITLE;
 
174
        try {
 
175
            title = this.element().getAttribute('data-title');
 
176
        } catch (e) {}
 
177
 
 
178
        title = title || DEFAULT_TITLE;
 
179
        this._tabTitle.textContent= title;
 
180
    },
 
181
 
 
182
    /**
 
183
     * Validates that a given DOM node element is a Ubuntu UI Page.
 
184
     * @method {DOM Element} isPage
 
185
     * @return {Boolean} if the DOM element is a page
 
186
     */
 
187
    isPage: function (element) {
 
188
        return element.tagName === 'DIV' &&
 
189
            element.hasAttribute('data-role') &&
 
190
            element.getAttribute('data-role') === 'page';
 
191
    },
 
192
 
 
193
    /**
 
194
     * @private
 
195
     */
 
196
    __updateVisibleState: function (displayStyle) {
 
197
        if (!this.__isValidId(this.id))
 
198
            return;
 
199
        var page = document.getElementById(this.id);
 
200
        if (!this.isPage(page)) {
 
201
            return;
 
202
        }
 
203
        page.style.display = displayStyle;
 
204
    },
 
205
 
 
206
    /**
 
207
     * @private
 
208
     */
 
209
    __hideVisibleSibling: function () {
 
210
        if (!this.__isValidId(this.id))
 
211
            return;
 
212
        var page = document.getElementById(this.id);
 
213
        if (!this.isPage(page)) {
 
214
            return;
 
215
        }
 
216
        var children = page.parentNode.children;
 
217
        for (var idx = 0; idx < children.length; ++idx) {
 
218
            if (this.isPage(children[idx])) {
 
219
                children[idx].style.display = 'none';
 
220
            }
 
221
        }
 
222
    },
 
223
 
 
224
    /**
 
225
     * @private
 
226
     */
 
227
    __isValidId: function (id) {
 
228
        return id && typeof (id) === 'string';
 
229
    },
 
230
 
 
231
    /**
 
232
     * @private
 
233
     */
 
234
    get __thisSelector() {
 
235
        return "#" + this.id;
 
236
    }
 
237
};