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

content-ready.js 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import _setImmediate from "babel-runtime/core-js/set-immediate";
  2. import _WeakMap from "babel-runtime/core-js/weak-map";
  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. var readyMap = void 0,
  16. queueMap = void 0;
  17. function isContentReady(element) {
  18. if (element.childNodes.length > 0) {
  19. setContentReady(element);
  20. }
  21. return readyMap.has(element);
  22. }
  23. function setContentReady(element) {
  24. readyMap.set(element, true);
  25. }
  26. function addCallback(element, fn) {
  27. if (!queueMap.has(element)) {
  28. queueMap.set(element, []);
  29. }
  30. queueMap.get(element).push(fn);
  31. }
  32. function consumeQueue(element) {
  33. var callbacks = queueMap.get(element, []) || [];
  34. queueMap.delete(element);
  35. callbacks.forEach(function (callback) {
  36. return callback();
  37. });
  38. }
  39. export default function contentReady(element) {
  40. var fn = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : function () {};
  41. if (readyMap === undefined) {
  42. readyMap = new _WeakMap();
  43. queueMap = new _WeakMap();
  44. }
  45. addCallback(element, fn);
  46. if (isContentReady(element)) {
  47. consumeQueue(element);
  48. return;
  49. }
  50. var observer = new MutationObserver(function (changes) {
  51. setContentReady(element);
  52. consumeQueue(element);
  53. });
  54. observer.observe(element, { childList: true, characterData: true });
  55. // failback for elements has empty content.
  56. _setImmediate(function () {
  57. setContentReady(element);
  58. consumeQueue(element);
  59. });
  60. }