manipulation.js 3.1 KB

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