manipulation.js 3.3 KB

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