2
* Copyright (C) 2013, 2014 Adnane Belmadiaf <daker@ubuntu.com>
3
* License granted by Canonical Limited
5
* This file is part of ubuntu-html5-ui-toolkit.
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
11
* (at your option) any later version.
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.
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/>.
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.
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.
34
<div data-role="mainview">
36
<header data-role="header">
39
<div data-role="content">
40
<div data-role="pagestack">
41
<div data-role="page" data-title="Main" id="main">
44
<div data-role="page" data-title="My Data" id="data">
54
var page = UI.page("pageID");
56
var Page = function (id) {
58
this.onActivatedCallbacks = [];
60
this._header = document.querySelector('div[data-role="mainview"] header');
61
this._tabTitle = this._header.querySelector('[data-role="tabtitle"]');
64
Page.PAGE_ACTIVATED_EVENT = 'ubuntu-html5-on-page-activated';
68
* Returns the DOM element associated with the selector this widget is bind to.
71
var mypage = UI.page("pageid").element();
73
element: function () {
74
return document.getElementById(this.id);
79
* @property {List} actions
82
// TODO: Not implemented yet
86
// TODO: Not implemented yet
91
* @property {String} title
94
// TODO: Not implemented yet
98
// TODO: Not implemented yet
102
* Deactivates the current page.
103
* @method {} deactivate
105
deactivate: function () {
106
this.__updateVisibleState('none');
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)
114
activate: function (properties) {
115
this.__hideVisibleSibling();
116
this.__updateVisibleState('block');
117
this.__updateHeaderTitle();
118
this.__dispatchActivatedEvent(properties);
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
126
onactivated: function(callback) {
127
if (callback && typeof callback === 'function')
128
this.onActivatedCallbacks.push(callback);
134
__setup: function() {
136
if (this.id && this.element() != null) {
137
this.element().addEventListener(
138
Page.PAGE_ACTIVATED_EVENT,
140
if (! event.target || self.onActivatedCallbacks.length === 0)
142
self.onActivatedCallbacks.forEach(
144
callback(event.data);
153
__dispatchActivatedEvent: function (data) {
154
if ( ! this.element())
156
var event = document.createEvent('Event');
157
event.initEvent(Page.PAGE_ACTIVATED_EVENT,true,true);
159
this.element().dispatchEvent(event);
165
__updateHeaderTitle: function () {
166
if (!this.element().getAttribute('data-title'))
172
var DEFAULT_TITLE = 'Unknown';
173
var title = DEFAULT_TITLE;
175
title = this.element().getAttribute('data-title');
178
title = title || DEFAULT_TITLE;
179
this._tabTitle.textContent= title;
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
187
isPage: function (element) {
188
return element.tagName === 'DIV' &&
189
element.hasAttribute('data-role') &&
190
element.getAttribute('data-role') === 'page';
196
__updateVisibleState: function (displayStyle) {
197
if (!this.__isValidId(this.id))
199
var page = document.getElementById(this.id);
200
if (!this.isPage(page)) {
203
page.style.display = displayStyle;
209
__hideVisibleSibling: function () {
210
if (!this.__isValidId(this.id))
212
var page = document.getElementById(this.id);
213
if (!this.isPage(page)) {
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';
227
__isValidId: function (id) {
228
return id && typeof (id) === 'string';
234
get __thisSelector() {
235
return "#" + this.id;