| OLD | NEW |
| (Empty) | |
| 1 part of angular.core.dom; |
| 2 |
| 3 /** |
| 4 * ElementWrapper is an interface for [Block]s and [BlockHole]s. Its purpose is |
| 5 * to allow treating [Block] and [BlockHole] under same interface so that |
| 6 * [Block]s can be added after [BlockHole]. |
| 7 */ |
| 8 abstract class ElementWrapper { |
| 9 List<dom.Node> elements; |
| 10 ElementWrapper next; |
| 11 ElementWrapper previous; |
| 12 } |
| 13 |
| 14 /** |
| 15 * A Block is a fundamental building block of DOM. It is a chunk of DOM which |
| 16 * Can not be structural changed. It can only have its attributes changed. |
| 17 * A Block can have [BlockHole]s embedded in its DOM. A [BlockHole] can |
| 18 * contain other [Block]s and it is the only way in which DOM can be changed |
| 19 * structurally. |
| 20 * |
| 21 * A [Block] is a collection of DOM nodes and [Directive]s for those nodes. |
| 22 * |
| 23 * A [Block] is responsible for instantiating the [Directive]s and for |
| 24 * inserting / removing itself to/from DOM. |
| 25 * |
| 26 * A [Block] can be created from [BlockFactory]. |
| 27 * |
| 28 */ |
| 29 class Block implements ElementWrapper { |
| 30 List<dom.Node> elements; |
| 31 ElementWrapper previous = null; |
| 32 ElementWrapper next = null; |
| 33 |
| 34 Function onInsert; |
| 35 Function onRemove; |
| 36 Function onMove; |
| 37 |
| 38 List<dynamic> _directives = []; |
| 39 |
| 40 Block(this.elements); |
| 41 |
| 42 Block insertAfter(ElementWrapper previousBlock) { |
| 43 // Update Link List. |
| 44 next = previousBlock.next; |
| 45 if (next != null) { |
| 46 next.previous = this; |
| 47 } |
| 48 previous = previousBlock; |
| 49 previousBlock.next = this; |
| 50 |
| 51 // Update DOM |
| 52 List<dom.Node> previousElements = previousBlock.elements; |
| 53 dom.Node previousElement = previousElements[previousElements.length - 1]; |
| 54 dom.Node insertBeforeElement = previousElement.nextNode; |
| 55 dom.Node parentElement = previousElement.parentNode; |
| 56 bool preventDefault = false; |
| 57 |
| 58 Function insertDomElements = () { |
| 59 for(var i = 0, ii = elements.length; i < ii; i++) { |
| 60 parentElement.insertBefore(elements[i], insertBeforeElement); |
| 61 } |
| 62 }; |
| 63 |
| 64 if (onInsert != null) { |
| 65 onInsert({ |
| 66 "preventDefault": () { |
| 67 preventDefault = true; |
| 68 return insertDomElements; |
| 69 }, |
| 70 "element": elements[0] |
| 71 }); |
| 72 } |
| 73 |
| 74 if (!preventDefault) { |
| 75 insertDomElements(); |
| 76 } |
| 77 return this; |
| 78 } |
| 79 |
| 80 Block remove() { |
| 81 bool preventDefault = false; |
| 82 |
| 83 Function removeDomElements = () { |
| 84 for(var j = 0, jj = elements.length; j < jj; j++) { |
| 85 dom.Node current = elements[j]; |
| 86 dom.Node next = j+1 < jj ? elements[j+1] : null; |
| 87 |
| 88 while(next != null && current.nextNode != next) { |
| 89 current.nextNode.remove(); |
| 90 } |
| 91 elements[j].remove(); |
| 92 } |
| 93 }; |
| 94 |
| 95 if (onRemove != null) { |
| 96 onRemove({ |
| 97 "preventDefault": () { |
| 98 preventDefault = true; |
| 99 return removeDomElements(); |
| 100 }, |
| 101 "element": elements[0] |
| 102 }); |
| 103 } |
| 104 |
| 105 if (!preventDefault) { |
| 106 removeDomElements(); |
| 107 } |
| 108 |
| 109 // Remove block from list |
| 110 if (previous != null && (previous.next = next) != null) { |
| 111 next.previous = previous; |
| 112 } |
| 113 next = previous = null; |
| 114 return this; |
| 115 } |
| 116 |
| 117 Block moveAfter(ElementWrapper previousBlock) { |
| 118 var previousElements = previousBlock.elements, |
| 119 previousElement = previousElements[previousElements.length - 1], |
| 120 insertBeforeElement = previousElement.nextNode, |
| 121 parentElement = previousElement.parentNode, |
| 122 blockElements = elements; |
| 123 |
| 124 for(var i = 0, ii = blockElements.length; i < ii; i++) { |
| 125 parentElement.insertBefore(blockElements[i], insertBeforeElement); |
| 126 } |
| 127 |
| 128 // Remove block from list |
| 129 previous.next = next; |
| 130 if (next != null) { |
| 131 next.previous = previous; |
| 132 } |
| 133 // Add block to list |
| 134 next = previousBlock.next; |
| 135 if (next != null) { |
| 136 next.previous = this; |
| 137 } |
| 138 previous = previousBlock; |
| 139 previousBlock.next = this; |
| 140 return this; |
| 141 } |
| 142 } |
| 143 |
| 144 /** |
| 145 * A BlockHole is an instance of a hole. BlockHoles designate where child |
| 146 * [Block]s can be added in parent [Block]. BlockHoles wrap a DOM element, |
| 147 * and act as references which allows more blocks to be added. |
| 148 */ |
| 149 |
| 150 class BlockHole extends ElementWrapper { |
| 151 List<dom.Node> elements; |
| 152 |
| 153 ElementWrapper previous; |
| 154 |
| 155 ElementWrapper next; |
| 156 |
| 157 BlockHole(this.elements); |
| 158 } |
| 159 |
| OLD | NEW |