/
ons-page.js
529 lines (467 loc) · 16 KB
/
ons-page.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
import _Object$defineProperty from 'babel-runtime/core-js/object/define-property';
import _setImmediate from 'babel-runtime/core-js/set-immediate';
import _Object$getPrototypeOf from 'babel-runtime/core-js/object/get-prototype-of';
import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
import _createClass from 'babel-runtime/helpers/createClass';
import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
import _inherits from 'babel-runtime/helpers/inherits';
/*
Copyright 2013-2015 ASIAL CORPORATION
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import onsElements from '../ons/elements';
import util from '../ons/util';
import internal from '../ons/internal';
import autoStyle from '../ons/autostyle';
import ModifierUtil from '../ons/internal/modifier-util';
import BaseElement from './base/base-element';
import deviceBackButtonDispatcher from '../ons/internal/device-back-button-dispatcher';
import contentReady from '../ons/content-ready';
import './ons-toolbar'; // ensures that 'ons-toolbar' element is registered
var defaultClassName = 'page';
var scheme = {
'': 'page--*',
'.page__content': 'page--*__content',
'.page__background': 'page--*__background'
};
/**
* @element ons-page
* @category page
* @modifier material
* [en]Material Design style[/en]
* [ja][/ja]
* @description
* [en]
* This component defines the root of each page. If the content is large it will become scrollable.
*
* A navigation bar can be added to the top of the page using the `<ons-toolbar>` element.
* [/en]
* [ja]ページ定義のためのコンポーネントです。このコンポーネントの内容はスクロールが許可されます。[/ja]
* @tutorial vanilla/Reference/page
* @guide lifecycle.html#events
* [en]Overview of page events[/en]
* [ja]Overview of page events[/ja]
* @guide fundamentals.html#managing-pages
* [en]Managing multiple pages[/en]
* [ja]複数のページを管理する[/ja]
* @guide theming.html#modifiers [en]More details about the `modifier` attribute[/en][ja]modifier属性の使い方[/ja]
* @seealso ons-toolbar
* [en]Use the `<ons-toolbar>` element to add a navigation bar to the top of the page.[/en]
* [ja][/ja]
* @example
* <ons-page>
* <ons-toolbar>
* <div class="left">
* <ons-back-button>Back</ons-back-button>
* </div>
* <div class="center">Title</div>
* <div class="right">
* <ons-toolbar-button>
* <ons-icon icon="md-menu"></ons-icon>
* </ons-toolbar-button>
* </div>
* </ons-toolbar>
*
* <p>Page content</p>
* </ons-page>
*
* @example
* <script>
* myApp.handler = function(done) {
* loadMore().then(done);
* }
* </script>
*
* <ons-page on-infinite-scroll="myApp.handler">
* <ons-toolbar>
* <div class="center">List</div>
* </ons-toolbar>
*
* <ons-list>
* <ons-list-item>#1</ons-list-item>
* <ons-list-item>#2</ons-list-item>
* <ons-list-item>#3</ons-list-item>
* ...
* </ons-list>
* </ons-page>
*/
var PageElement = function (_BaseElement) {
_inherits(PageElement, _BaseElement);
/**
* @event init
* @description
* [en]Fired right after the page is attached.[/en]
* [ja]ページがアタッチされた後に発火します。[/ja]
* @param {Object} event [en]Event object.[/en]
*/
/**
* @event show
* @description
* [en]Fired right after the page is shown.[/en]
* [ja]ページが表示された後に発火します。[/ja]
* @param {Object} event [en]Event object.[/en]
*/
/**
* @event hide
* @description
* [en]Fired right after the page is hidden.[/en]
* [ja]ページが隠れた後に発火します。[/ja]
* @param {Object} event [en]Event object.[/en]
*/
/**
* @event destroy
* @description
* [en]Fired right before the page is destroyed.[/en]
* [ja]ページが破棄される前に発火します。[/ja]
* @param {Object} event [en]Event object.[/en]
*/
/**
* @attribute modifier
* @type {String}
* @description
* [en]Specify modifier name to specify custom styles.[/en]
* [ja]スタイル定義をカスタマイズするための名前を指定します。[/ja]
*/
/**
* @attribute on-infinite-scroll
* @type {String}
* @description
* [en]Path of the function to be executed on infinite scrolling. Example: `app.loadData`. The function receives a done callback that must be called when it's finished.[/en]
* [ja][/ja]
*/
function PageElement() {
_classCallCheck(this, PageElement);
var _this = _possibleConstructorReturn(this, (PageElement.__proto__ || _Object$getPrototypeOf(PageElement)).call(this));
_this._deriveHooks();
_this._defaultClassName = defaultClassName;
_this.classList.add(defaultClassName);
_this._initialized = false;
contentReady(_this, function () {
_this._compile();
_this._isShown = false;
_this._contentElement = _this._getContentElement();
_this._backgroundElement = _this._getBackgroundElement();
});
return _this;
}
_createClass(PageElement, [{
key: '_compile',
value: function _compile() {
var _this2 = this;
autoStyle.prepare(this);
var toolbar = util.findChild(this, 'ons-toolbar');
var background = util.findChild(this, '.page__background') || util.findChild(this, '.background') || document.createElement('div');
background.classList.add('page__background');
this.insertBefore(background, !toolbar && this.firstChild || toolbar && toolbar.nextSibling);
var content = util.findChild(this, '.page__content') || util.findChild(this, '.content') || document.createElement('div');
content.classList.add('page__content');
if (!content.parentElement) {
util.arrayFrom(this.childNodes).forEach(function (node) {
if (node.nodeType !== 1 || _this2._elementShouldBeMoved(node)) {
content.appendChild(node); // Can trigger detached connectedCallbacks
}
});
}
this._tryToFillStatusBar(content); // Must run before child pages try to fill status bar.
this.insertBefore(content, background.nextSibling); // Can trigger attached connectedCallbacks
if ((!toolbar || !util.hasModifier(toolbar, 'transparent')) && content.children.length === 1 && util.isPageControl(content.children[0])) {
this._defaultClassName += ' page--wrapper';
this.attributeChangedCallback('class');
}
var bottomToolbar = util.findChild(this, 'ons-bottom-toolbar');
if (bottomToolbar) {
this._defaultClassName += ' page-with-bottom-toolbar';
this.attributeChangedCallback('class');
}
ModifierUtil.initModifier(this, scheme);
}
}, {
key: '_elementShouldBeMoved',
value: function _elementShouldBeMoved(el) {
if (el.classList.contains('page__background')) {
return false;
}
var tagName = el.tagName.toLowerCase();
if (tagName === 'ons-fab') {
return !el.hasAttribute('position');
}
var fixedElements = ['script', 'ons-toolbar', 'ons-bottom-toolbar', 'ons-modal', 'ons-speed-dial', 'ons-dialog', 'ons-alert-dialog', 'ons-popover', 'ons-action-sheet'];
return el.hasAttribute('inline') || fixedElements.indexOf(tagName) === -1;
}
}, {
key: '_tryToFillStatusBar',
value: function _tryToFillStatusBar() {
var _this3 = this;
var content = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this._contentElement;
internal.autoStatusBarFill(function () {
util.toggleAttribute(_this3, 'status-bar-fill', !util.findParent(_this3, function (e) {
return e.hasAttribute('status-bar-fill');
}) // Not already filled
&& (_this3._canAnimateToolbar(content) || !util.findChild(content, util.isPageControl)) // Has toolbar or cannot delegate
);
});
}
}, {
key: '_canAnimateToolbar',
value: function _canAnimateToolbar() {
var content = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this._contentElement;
if (util.findChild(this, 'ons-toolbar')) {
return true;
}
return !!util.findChild(content, function (el) {
return util.match(el, 'ons-toolbar') && !el.hasAttribute('inline');
});
}
}, {
key: 'connectedCallback',
value: function connectedCallback() {
var _this4 = this;
if (!util.isAttached(this)) {
// Avoid detached calls
return;
}
contentReady(this, function () {
_this4._tryToFillStatusBar(); // Ensure status bar when the element was compiled before connected
if (_this4.hasAttribute('on-infinite-scroll')) {
_this4.attributeChangedCallback('on-infinite-scroll', null, _this4.getAttribute('on-infinite-scroll'));
}
if (!_this4._initialized) {
_this4._initialized = true;
_setImmediate(function () {
_this4.onInit && _this4.onInit();
util.triggerElementEvent(_this4, 'init');
});
if (!util.hasAnyComponentAsParent(_this4)) {
_setImmediate(function () {
return _this4._show();
});
}
}
});
}
}, {
key: 'updateBackButton',
value: function updateBackButton(show) {
if (this.backButton) {
show ? this.backButton.show() : this.backButton.hide();
}
}
}, {
key: '_onScroll',
value: function _onScroll() {
var _this5 = this;
var c = this._contentElement,
overLimit = (c.scrollTop + c.clientHeight) / c.scrollHeight >= this._infiniteScrollLimit;
if (this._onInfiniteScroll && !this._loadingContent && overLimit) {
this._loadingContent = true;
this._onInfiniteScroll(function () {
return _this5._loadingContent = false;
});
}
}
/**
* @property onDeviceBackButton
* @type {Object}
* @description
* [en]Back-button handler.[/en]
* [ja]バックボタンハンドラ。[/ja]
*/
}, {
key: '_getContentElement',
value: function _getContentElement() {
var result = util.findChild(this, '.page__content');
if (result) {
return result;
}
util.throw('Fail to get ".page__content" element');
}
}, {
key: '_getBackgroundElement',
value: function _getBackgroundElement() {
var result = util.findChild(this, '.page__background');
if (result) {
return result;
}
util.throw('Fail to get ".page__background" element');
}
}, {
key: '_getBottomToolbarElement',
value: function _getBottomToolbarElement() {
return util.findChild(this, 'ons-bottom-toolbar') || internal.nullElement;
}
}, {
key: '_getToolbarElement',
value: function _getToolbarElement() {
return util.findChild(this, 'ons-toolbar') || document.createElement('ons-toolbar');
}
}, {
key: 'attributeChangedCallback',
value: function attributeChangedCallback(name, last, current) {
var _this6 = this;
switch (name) {
case 'class':
util.restoreClass(this, this._defaultClassName, scheme);
break;
case 'modifier':
ModifierUtil.onModifierChanged(last, current, this, scheme);
break;
case 'on-infinite-scroll':
if (current === null) {
this.onInfiniteScroll = null;
} else {
this.onInfiniteScroll = function (done) {
var f = util.findFromPath(current);
_this6.onInfiniteScroll = f;
f(done);
};
}
break;
}
}
}, {
key: '_show',
value: function _show() {
if (!this._isShown && util.isAttached(this)) {
this._isShown = true;
this.setAttribute('shown', '');
this.onShow && this.onShow();
util.triggerElementEvent(this, 'show');
util.propagateAction(this, '_show');
}
}
}, {
key: '_hide',
value: function _hide() {
if (this._isShown) {
this._isShown = false;
this.removeAttribute('shown');
this.onHide && this.onHide();
util.triggerElementEvent(this, 'hide');
util.propagateAction(this, '_hide');
}
}
}, {
key: '_destroy',
value: function _destroy() {
this._hide();
this.onDestroy && this.onDestroy();
util.triggerElementEvent(this, 'destroy');
if (this.onDeviceBackButton) {
this.onDeviceBackButton.destroy();
}
util.propagateAction(this, '_destroy');
this.remove();
}
}, {
key: '_deriveHooks',
value: function _deriveHooks() {
var _this7 = this;
this.constructor.events.forEach(function (event) {
var key = 'on' + event.charAt(0).toUpperCase() + event.slice(1);
_Object$defineProperty(_this7, key, {
configurable: true,
enumerable: true,
get: function get() {
return _this7['_' + key];
},
set: function set(value) {
if (!(value instanceof Function)) {
util.throw('"' + key + '" hook must be a function');
}
_this7['_' + key] = value.bind(_this7);
}
});
});
}
}, {
key: 'name',
set: function set(str) {
this.setAttribute('name', str);
},
get: function get() {
return this.getAttribute('name');
}
}, {
key: 'backButton',
get: function get() {
return this.querySelector('ons-back-button');
}
/**
* @property onInfiniteScroll
* @description
* [en]Function to be executed when scrolling to the bottom of the page. The function receives a done callback as an argument that must be called when it's finished.[/en]
* [ja][/ja]
*/
}, {
key: 'onInfiniteScroll',
set: function set(value) {
var _this8 = this;
if (value && !(value instanceof Function)) {
util.throw('"onInfiniteScroll" must be function or null');
}
contentReady(this, function () {
if (!value) {
_this8._contentElement.removeEventListener('scroll', _this8._boundOnScroll);
} else if (!_this8._onInfiniteScroll) {
_this8._infiniteScrollLimit = 0.9;
_this8._boundOnScroll = _this8._onScroll.bind(_this8);
_setImmediate(function () {
return _this8._contentElement.addEventListener('scroll', _this8._boundOnScroll);
});
}
_this8._onInfiniteScroll = value;
});
},
get: function get() {
return this._onInfiniteScroll;
}
}, {
key: 'onDeviceBackButton',
get: function get() {
return this._backButtonHandler;
},
set: function set(callback) {
if (this._backButtonHandler) {
this._backButtonHandler.destroy();
}
this._backButtonHandler = deviceBackButtonDispatcher.createHandler(this, callback);
}
}, {
key: 'scrollTop',
get: function get() {
return this._contentElement.scrollTop;
},
set: function set(newValue) {
this._contentElement.scrollTop = newValue;
}
}], [{
key: 'observedAttributes',
get: function get() {
return ['modifier', 'on-infinite-scroll', 'class'];
}
}, {
key: 'events',
get: function get() {
return ['init', 'show', 'hide', 'destroy'];
}
/**
* @property data
* @type {*}
* @description
* [en]User's custom data passed to `pushPage()`-like methods.[/en]
* [ja][/ja]
*/
}]);
return PageElement;
}(BaseElement);
export default PageElement;
onsElements.Page = PageElement;
customElements.define('ons-page', PageElement);