manipulation.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.prepend = exports.prependChild = exports.append = exports.appendChild = exports.replaceElement = exports.removeElement = void 0;
  4. /**
  5. * Remove an element from the dom
  6. *
  7. * @category Manipulation
  8. * @param elem The element to be removed
  9. */
  10. function removeElement(elem) {
  11. if (elem.prev)
  12. elem.prev.next = elem.next;
  13. if (elem.next)
  14. elem.next.prev = elem.prev;
  15. if (elem.parent) {
  16. var childs = elem.parent.children;
  17. var childsIndex = childs.lastIndexOf(elem);
  18. if (childsIndex >= 0) {
  19. childs.splice(childsIndex, 1);
  20. }
  21. }
  22. elem.next = null;
  23. elem.prev = null;
  24. elem.parent = null;
  25. }
  26. exports.removeElement = removeElement;
  27. /**
  28. * Replace an element in the dom
  29. *
  30. * @category Manipulation
  31. * @param elem The element to be replaced
  32. * @param replacement The element to be added
  33. */
  34. function replaceElement(elem, replacement) {
  35. var prev = (replacement.prev = elem.prev);
  36. if (prev) {
  37. prev.next = replacement;
  38. }
  39. var next = (replacement.next = elem.next);
  40. if (next) {
  41. next.prev = replacement;
  42. }
  43. var parent = (replacement.parent = elem.parent);
  44. if (parent) {
  45. var childs = parent.children;
  46. childs[childs.lastIndexOf(elem)] = replacement;
  47. elem.parent = null;
  48. }
  49. }
  50. exports.replaceElement = replaceElement;
  51. /**
  52. * Append a child to an element.
  53. *
  54. * @category Manipulation
  55. * @param parent The element to append to.
  56. * @param child The element to be added as a child.
  57. */
  58. function appendChild(parent, child) {
  59. removeElement(child);
  60. child.next = null;
  61. child.parent = parent;
  62. if (parent.children.push(child) > 1) {
  63. var sibling = parent.children[parent.children.length - 2];
  64. sibling.next = child;
  65. child.prev = sibling;
  66. }
  67. else {
  68. child.prev = null;
  69. }
  70. }
  71. exports.appendChild = appendChild;
  72. /**
  73. * Append an element after another.
  74. *
  75. * @category Manipulation
  76. * @param elem The element to append after.
  77. * @param next The element be added.
  78. */
  79. function append(elem, next) {
  80. removeElement(next);
  81. var parent = elem.parent;
  82. var currNext = elem.next;
  83. next.next = currNext;
  84. next.prev = elem;
  85. elem.next = next;
  86. next.parent = parent;
  87. if (currNext) {
  88. currNext.prev = next;
  89. if (parent) {
  90. var childs = parent.children;
  91. childs.splice(childs.lastIndexOf(currNext), 0, next);
  92. }
  93. }
  94. else if (parent) {
  95. parent.children.push(next);
  96. }
  97. }
  98. exports.append = append;
  99. /**
  100. * Prepend a child to an element.
  101. *
  102. * @category Manipulation
  103. * @param parent The element to prepend before.
  104. * @param child The element to be added as a child.
  105. */
  106. function prependChild(parent, child) {
  107. removeElement(child);
  108. child.parent = parent;
  109. child.prev = null;
  110. if (parent.children.unshift(child) !== 1) {
  111. var sibling = parent.children[1];
  112. sibling.prev = child;
  113. child.next = sibling;
  114. }
  115. else {
  116. child.next = null;
  117. }
  118. }
  119. exports.prependChild = prependChild;
  120. /**
  121. * Prepend an element before another.
  122. *
  123. * @category Manipulation
  124. * @param elem The element to prepend before.
  125. * @param prev The element be added.
  126. */
  127. function prepend(elem, prev) {
  128. removeElement(prev);
  129. var parent = elem.parent;
  130. if (parent) {
  131. var childs = parent.children;
  132. childs.splice(childs.indexOf(elem), 0, prev);
  133. }
  134. if (elem.prev) {
  135. elem.prev.next = prev;
  136. }
  137. prev.parent = parent;
  138. prev.prev = elem.prev;
  139. prev.next = elem;
  140. elem.prev = prev;
  141. }
  142. exports.prepend = prepend;
  143. //# sourceMappingURL=manipulation.js.map