manipulation.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. exports.removeElement = function(elem){
  2. if(elem.prev) elem.prev.next = elem.next;
  3. if(elem.next) elem.next.prev = elem.prev;
  4. if(elem.parent){
  5. var childs = elem.parent.children;
  6. childs.splice(childs.lastIndexOf(elem), 1);
  7. }
  8. };
  9. exports.replaceElement = function(elem, replacement){
  10. var prev = replacement.prev = elem.prev;
  11. if(prev){
  12. prev.next = replacement;
  13. }
  14. var next = replacement.next = elem.next;
  15. if(next){
  16. next.prev = replacement;
  17. }
  18. var parent = replacement.parent = elem.parent;
  19. if(parent){
  20. var childs = parent.children;
  21. childs[childs.lastIndexOf(elem)] = replacement;
  22. }
  23. };
  24. exports.appendChild = function(elem, child){
  25. child.parent = elem;
  26. if(elem.children.push(child) !== 1){
  27. var sibling = elem.children[elem.children.length - 2];
  28. sibling.next = child;
  29. child.prev = sibling;
  30. child.next = null;
  31. }
  32. };
  33. exports.append = function(elem, next){
  34. var parent = elem.parent,
  35. currNext = elem.next;
  36. next.next = currNext;
  37. next.prev = elem;
  38. elem.next = next;
  39. next.parent = parent;
  40. if(currNext){
  41. currNext.prev = next;
  42. if(parent){
  43. var childs = parent.children;
  44. childs.splice(childs.lastIndexOf(currNext), 0, next);
  45. }
  46. } else if(parent){
  47. parent.children.push(next);
  48. }
  49. };
  50. exports.prepend = function(elem, prev){
  51. var parent = elem.parent;
  52. if(parent){
  53. var childs = parent.children;
  54. childs.splice(childs.lastIndexOf(elem), 0, prev);
  55. }
  56. if(elem.prev){
  57. elem.prev.next = prev;
  58. }
  59. prev.parent = parent;
  60. prev.prev = elem.prev;
  61. prev.next = elem;
  62. elem.prev = prev;
  63. };