manipulation.js 3.5 KB

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