No Description

page-loader.js 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
  2. import _createClass from 'babel-runtime/helpers/createClass';
  3. /*
  4. Copyright 2013-2015 ASIAL CORPORATION
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. */
  15. import util from './util';
  16. import internal from './internal';
  17. // Default implementation for global PageLoader.
  18. function loadPage(_ref, done) {
  19. var page = _ref.page,
  20. parent = _ref.parent,
  21. _ref$params = _ref.params,
  22. params = _ref$params === undefined ? {} : _ref$params;
  23. internal.getPageHTMLAsync(page).then(function (html) {
  24. var pageElement = util.createElement(html);
  25. parent.appendChild(pageElement);
  26. done(pageElement);
  27. });
  28. }
  29. function unloadPage(element) {
  30. if (element._destroy instanceof Function) {
  31. element._destroy();
  32. } else {
  33. element.remove();
  34. }
  35. }
  36. export var PageLoader = function () {
  37. /**
  38. * @param {Function} [fn] Returns an object that has "element" property and "unload" function.
  39. */
  40. function PageLoader(loader, unloader) {
  41. _classCallCheck(this, PageLoader);
  42. this._loader = loader instanceof Function ? loader : loadPage;
  43. this._unloader = unloader instanceof Function ? unloader : unloadPage;
  44. }
  45. /**
  46. * Set internal loader implementation.
  47. */
  48. _createClass(PageLoader, [{
  49. key: 'load',
  50. /**
  51. * @param {any} options.page
  52. * @param {Element} options.parent A location to load page.
  53. * @param {Object} [options.params] Extra parameters for ons-page.
  54. * @param {Function} done Take an object that has "element" property and "unload" function.
  55. */
  56. value: function load(_ref2, done) {
  57. var page = _ref2.page,
  58. parent = _ref2.parent,
  59. _ref2$params = _ref2.params,
  60. params = _ref2$params === undefined ? {} : _ref2$params;
  61. this._loader({ page: page, parent: parent, params: params }, function (pageElement) {
  62. if (!(pageElement instanceof Element)) {
  63. throw Error('pageElement must be an instance of Element.');
  64. }
  65. done(pageElement);
  66. });
  67. }
  68. }, {
  69. key: 'unload',
  70. value: function unload(pageElement) {
  71. if (!(pageElement instanceof Element)) {
  72. throw Error('pageElement must be an instance of Element.');
  73. }
  74. this._unloader(pageElement);
  75. }
  76. }, {
  77. key: 'internalLoader',
  78. set: function set(fn) {
  79. if (!(fn instanceof Function)) {
  80. throw Error('First parameter must be an instance of Function');
  81. }
  82. this._loader = fn;
  83. },
  84. get: function get() {
  85. return this._loader;
  86. }
  87. }]);
  88. return PageLoader;
  89. }();
  90. export var defaultPageLoader = new PageLoader();
  91. export var instantPageLoader = new PageLoader(function (_ref3, done) {
  92. var page = _ref3.page,
  93. parent = _ref3.parent,
  94. _ref3$params = _ref3.params,
  95. params = _ref3$params === undefined ? {} : _ref3$params;
  96. var element = util.createElement(page.trim());
  97. parent.appendChild(element);
  98. done(element);
  99. }, unloadPage);