| 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 next; | |
| 32 ElementWrapper previous; | |
| 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 elements.forEach((el) => parentElement.insertBefore(el, insertBeforeElem
ent)); | |
| 60 | |
| 61 if (onInsert != null) { | |
| 62 onInsert({ | |
| 63 "preventDefault": () { | |
| 64 preventDefault = true; | |
| 65 return insertDomElements; | |
| 66 }, | |
| 67 "element": elements[0] | |
| 68 }); | |
| 69 } | |
| 70 | |
| 71 if (!preventDefault) { | |
| 72 insertDomElements(); | |
| 73 } | |
| 74 return this; | |
| 75 } | |
| 76 | |
| 77 Block remove() { | |
| 78 bool preventDefault = false; | |
| 79 | |
| 80 Function removeDomElements = () { | |
| 81 for (var j = 0; j < elements.length; j++) { | |
| 82 dom.Node current = elements[j]; | |
| 83 dom.Node next = j+1 < elements.length ? elements[j+1] : null; | |
| 84 | |
| 85 while(next != null && current.nextNode != next) { | |
| 86 current.nextNode.remove(); | |
| 87 } | |
| 88 elements[j].remove(); | |
| 89 } | |
| 90 }; | |
| 91 | |
| 92 if (onRemove != null) { | |
| 93 onRemove({ | |
| 94 "preventDefault": () { | |
| 95 preventDefault = true; | |
| 96 return removeDomElements(); | |
| 97 }, | |
| 98 "element": elements[0] | |
| 99 }); | |
| 100 } | |
| 101 | |
| 102 if (!preventDefault) { | |
| 103 removeDomElements(); | |
| 104 } | |
| 105 | |
| 106 // Remove block from list | |
| 107 if (previous != null && (previous.next = next) != null) { | |
| 108 next.previous = previous; | |
| 109 } | |
| 110 next = previous = null; | |
| 111 return this; | |
| 112 } | |
| 113 | |
| 114 Block moveAfter(ElementWrapper previousBlock) { | |
| 115 var previousElements = previousBlock.elements, | |
| 116 previousElement = previousElements[previousElements.length - 1], | |
| 117 insertBeforeElement = previousElement.nextNode, | |
| 118 parentElement = previousElement.parentNode; | |
| 119 | |
| 120 elements.forEach((el) => parentElement.insertBefore(el, insertBeforeElement)
); | |
| 121 | |
| 122 // Remove block from list | |
| 123 previous.next = next; | |
| 124 if (next != null) { | |
| 125 next.previous = previous; | |
| 126 } | |
| 127 // Add block to list | |
| 128 next = previousBlock.next; | |
| 129 if (next != null) { | |
| 130 next.previous = this; | |
| 131 } | |
| 132 previous = previousBlock; | |
| 133 previousBlock.next = this; | |
| 134 return this; | |
| 135 } | |
| 136 } | |
| 137 | |
| 138 /** | |
| 139 * A BlockHole is an instance of a hole. BlockHoles designate where child | |
| 140 * [Block]s can be added in parent [Block]. BlockHoles wrap a DOM element, | |
| 141 * and act as references which allows more blocks to be added. | |
| 142 */ | |
| 143 class BlockHole extends ElementWrapper { | |
| 144 List<dom.Node> elements; | |
| 145 ElementWrapper previous; | |
| 146 ElementWrapper next; | |
| 147 | |
| 148 BlockHole(this.elements); | |
| 149 } | |
| 150 | |
| OLD | NEW |