Source: level3/mutation-observer.js

  1. // level 0
  2. import animate from '../level0/animate';
  3. import present from '../level0/present';
  4. import update from '../level0/update';
  5. // level 1
  6. import AnimatedManager from '../level1/animated-manager';
  7. import AnimatedStateFactory from '../level1/animated-state-factory';
  8. import Bus from '../level1/bus';
  9. import BusAnimatedManager from '../level1/bus-animated-manager';
  10. import Matcher from '../level1/matcher';
  11. import RunLoop from '../level1/runloop';
  12. // level 2
  13. import InnerMutationObserver from '../level2/mutation-observer';
  14. /**
  15. * @example
  16. * import BoxartMutationObserver from 'boxart-mutation-observer';
  17. *
  18. * import animations from './animations';
  19. *
  20. * new BoxartMutationObserver({animations})
  21. * .observe(rootElment);
  22. */
  23. class BoxartMutationObserver {
  24. constructor({loop = RunLoop.main, animations}) {
  25. this.animations = animations;
  26. const bus = this.bus = new Bus();
  27. const animationTypes = {};
  28. const matcher = this.matcher = new Matcher();
  29. Object.keys(animations).forEach(key => {
  30. matcher.add(key, Object.keys(animations[key]));
  31. const type = animationTypes[key.split(' ')[0]] = animations[key];
  32. Object.keys(type).forEach(name => {
  33. const anim = type[name];
  34. if (anim.update && !anim.update.copy) {
  35. try {
  36. const update = update.context(anim.update);
  37. if (typeof update === 'function') {
  38. anim.update = update;
  39. }
  40. } catch (e) {
  41. console.error(e);
  42. }
  43. }
  44. if (anim.update && anim.update.compile) {
  45. try {
  46. anim.update = anim.update.compile();
  47. } catch (e) {
  48. console.error(e);
  49. }
  50. }
  51. if (anim.animate && !anim.animate.done) {
  52. try {
  53. const animate = animate.context(anim.animate);
  54. if (typeof animate === 'function') {
  55. anim.animate = animate;
  56. }
  57. } catch (e) {
  58. console.error(e);
  59. }
  60. }
  61. if (anim.animate && anim.animate.compile) {
  62. try {
  63. anim.animate = anim.animate.compile();
  64. } catch (e) {
  65. console.error(e);
  66. }
  67. }
  68. if (anim.present && !anim.present.store) {
  69. try {
  70. const present = present.context(anim.present);
  71. if (typeof present === 'function') {
  72. anim.present = present;
  73. }
  74. } catch (e) {
  75. console.error(e);
  76. }
  77. }
  78. if (anim.present && anim.present.compile) {
  79. try {
  80. anim.present = anim.present.compile();
  81. } catch (e) {
  82. console.error(e);
  83. }
  84. }
  85. });
  86. });
  87. const {create: factory} = new AnimatedStateFactory(animations);
  88. const manager = this.manager = new AnimatedManager(factory, loop);
  89. new BusAnimatedManager(manager, bus);
  90. this.innerObserver = new InnerMutationObserver({bus, loop, matcher});
  91. }
  92. observe(target) {
  93. this.innerObserver.observe(target);
  94. }
  95. disconnect() {
  96. this.innerObserver.disconnect();
  97. }
  98. getManager() {
  99. return this.manager;
  100. }
  101. getAnimated(element) {
  102. return this.manager.getAnimated(element);
  103. }
  104. }
  105. export default BoxartMutationObserver;
  106. export {
  107. animate,
  108. present,
  109. update,
  110. };