manipulation.js 3.3 KB

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