Brak opisu

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