Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

internal.js 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. import _Promise from 'babel-runtime/core-js/promise';
  2. import _typeof from 'babel-runtime/helpers/typeof';
  3. import _setImmediate from 'babel-runtime/core-js/set-immediate';
  4. /*
  5. Copyright 2013-2015 ASIAL CORPORATION
  6. Licensed under the Apache License, Version 2.0 (the "License");
  7. you may not use this file except in compliance with the License.
  8. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing, software
  11. distributed under the License is distributed on an "AS IS" BASIS,
  12. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. See the License for the specific language governing permissions and
  14. limitations under the License.
  15. */
  16. import util from '../util';
  17. import platform from '../platform';
  18. import pageAttributeExpression from '../page-attribute-expression';
  19. var internal = {};
  20. internal.config = {
  21. autoStatusBarFill: true,
  22. animationsDisabled: false,
  23. warningsDisabled: false
  24. };
  25. internal.nullElement = window.document.createElement('div');
  26. /**
  27. * @return {Boolean}
  28. */
  29. internal.isEnabledAutoStatusBarFill = function () {
  30. return !!internal.config.autoStatusBarFill;
  31. };
  32. /**
  33. * @param {String} html
  34. * @return {String}
  35. */
  36. internal.normalizePageHTML = function (html) {
  37. return ('' + html).trim();
  38. };
  39. internal.waitDOMContentLoaded = function (callback) {
  40. if (window.document.readyState === 'loading' || window.document.readyState == 'uninitialized') {
  41. var wrappedCallback = function wrappedCallback() {
  42. callback();
  43. window.document.removeEventListener('DOMContentLoaded', wrappedCallback);
  44. };
  45. window.document.addEventListener('DOMContentLoaded', wrappedCallback);
  46. } else {
  47. _setImmediate(callback);
  48. }
  49. };
  50. internal.autoStatusBarFill = function (action) {
  51. var onReady = function onReady() {
  52. if (internal.shouldFillStatusBar()) {
  53. action();
  54. }
  55. document.removeEventListener('deviceready', onReady);
  56. };
  57. if ((typeof device === 'undefined' ? 'undefined' : _typeof(device)) === 'object') {
  58. document.addEventListener('deviceready', onReady);
  59. } else if (['complete', 'interactive'].indexOf(document.readyState) === -1) {
  60. internal.waitDOMContentLoaded(onReady);
  61. } else {
  62. onReady();
  63. }
  64. };
  65. internal.shouldFillStatusBar = function () {
  66. return internal.isEnabledAutoStatusBarFill() && (platform.isWebView() && platform.isIOS7above() && !platform.isIPhoneX() || document.body.querySelector('.ons-status-bar-mock.ios'));
  67. };
  68. internal.templateStore = {
  69. _storage: {},
  70. /**
  71. * @param {String} key
  72. * @return {String/null} template
  73. */
  74. get: function get(key) {
  75. return internal.templateStore._storage[key] || null;
  76. },
  77. /**
  78. * @param {String} key
  79. * @param {String} template
  80. */
  81. set: function set(key, template) {
  82. internal.templateStore._storage[key] = template;
  83. }
  84. };
  85. window.document.addEventListener('_templateloaded', function (e) {
  86. if (e.target.nodeName.toLowerCase() === 'ons-template') {
  87. internal.templateStore.set(e.templateId, e.template);
  88. }
  89. }, false);
  90. internal.waitDOMContentLoaded(function () {
  91. register('script[type="text/ons-template"]');
  92. register('script[type="text/template"]');
  93. register('script[type="text/ng-template"]');
  94. register('template');
  95. function register(query) {
  96. var templates = window.document.querySelectorAll(query);
  97. for (var i = 0; i < templates.length; i++) {
  98. internal.templateStore.set(templates[i].getAttribute('id'), templates[i].textContent || templates[i].content);
  99. }
  100. }
  101. });
  102. /**
  103. * @param {String} page
  104. * @return {Promise}
  105. */
  106. internal.getTemplateHTMLAsync = function (page) {
  107. return new _Promise(function (resolve, reject) {
  108. internal.waitDOMContentLoaded(function () {
  109. var cache = internal.templateStore.get(page);
  110. if (cache) {
  111. if (cache instanceof DocumentFragment) {
  112. return resolve(cache);
  113. }
  114. var html = typeof cache === 'string' ? cache : cache[1];
  115. return resolve(internal.normalizePageHTML(html));
  116. }
  117. var local = window.document.getElementById(page);
  118. if (local) {
  119. var _html = local.textContent || local.content;
  120. return resolve(_html);
  121. }
  122. var xhr = new XMLHttpRequest();
  123. xhr.open('GET', page, true);
  124. xhr.onload = function () {
  125. var html = xhr.responseText;
  126. if (xhr.status >= 400 && xhr.status < 600) {
  127. reject(html);
  128. } else {
  129. // Refresh script tags
  130. var fragment = util.createFragment(html);
  131. util.arrayFrom(fragment.querySelectorAll('script')).forEach(function (el) {
  132. var script = document.createElement('script');
  133. script.type = el.type || 'text/javascript';
  134. script.appendChild(document.createTextNode(el.text || el.textContent || el.innerHTML));
  135. el.parentNode.replaceChild(script, el);
  136. });
  137. internal.templateStore.set(page, fragment);
  138. resolve(fragment);
  139. }
  140. };
  141. xhr.onerror = function () {
  142. util.throw('Page template not found: ' + page);
  143. };
  144. xhr.send(null);
  145. });
  146. });
  147. };
  148. /**
  149. * @param {String} page
  150. * @return {Promise}
  151. */
  152. internal.getPageHTMLAsync = function (page) {
  153. var pages = pageAttributeExpression.evaluate(page);
  154. var getPage = function getPage(page) {
  155. if (typeof page !== 'string') {
  156. return _Promise.reject('Must specify a page.');
  157. }
  158. return internal.getTemplateHTMLAsync(page).catch(function (error) {
  159. if (pages.length === 0) {
  160. return _Promise.reject(error);
  161. }
  162. return getPage(pages.shift());
  163. });
  164. };
  165. return getPage(pages.shift());
  166. };
  167. export default internal;