|
1 | 1 | part of angular.core.dom;
|
2 | 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 | 3 | /**
|
15 | 4 | * 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. |
| 5 | + * can not be structurally changed. A Block can have [BlockHole] placeholders |
| 6 | + * embedded in its DOM. A [BlockHole] can contain other [Block]s and it is the |
| 7 | + * only way in which DOM structure can be modified. |
20 | 8 | *
|
21 | 9 | * A [Block] is a collection of DOM nodes
|
22 |
| - * |
| 10 | +
|
23 | 11 | * A [Block] can be created from [BlockFactory].
|
24 | 12 | *
|
25 | 13 | */
|
26 |
| -class Block implements ElementWrapper { |
27 |
| - List<dom.Node> elements; |
28 |
| - ElementWrapper next; |
29 |
| - ElementWrapper previous; |
| 14 | +class Block { |
| 15 | + final List<dom.Node> nodes; |
| 16 | + Block(this.nodes); |
| 17 | +} |
30 | 18 |
|
| 19 | +/** |
| 20 | + * A BlockHole maintains an ordered list of [Block]'s. It contains a |
| 21 | + * [placeholder] node that is used as the insertion point for block nodes. |
| 22 | + */ |
| 23 | +class BlockHole { |
| 24 | + final dom.Node placeholder; |
31 | 25 | final NgAnimate _animate;
|
| 26 | + final List<Block> _blocks = <Block>[]; |
32 | 27 |
|
33 |
| - Block(this.elements, this._animate); |
34 |
| - |
35 |
| - Block insertAfter(ElementWrapper previousBlock) { |
36 |
| - // Update Link List. |
37 |
| - next = previousBlock.next; |
38 |
| - if (next != null) { |
39 |
| - next.previous = this; |
40 |
| - } |
41 |
| - previous = previousBlock; |
42 |
| - previousBlock.next = this; |
| 28 | + BlockHole(this.placeholder, this._animate); |
43 | 29 |
|
44 |
| - // Update DOM |
45 |
| - List<dom.Node> previousElements = previousBlock.elements; |
46 |
| - dom.Node previousElement = previousElements[previousElements.length - 1]; |
47 |
| - dom.Node insertBeforeElement = previousElement.nextNode; |
48 |
| - dom.Node parentElement = previousElement.parentNode; |
| 30 | + void insert(Block block, { Block insertAfter }) { |
| 31 | + dom.Node previousNode = _lastNode(insertAfter); |
| 32 | + _blocksInsertAfter(block, insertAfter); |
49 | 33 |
|
50 |
| - _animate.insert(elements, parentElement, insertBefore: insertBeforeElement); |
51 |
| - |
52 |
| - return this; |
| 34 | + _animate.insert(block.nodes, placeholder.parentNode, |
| 35 | + insertBefore: previousNode.nextNode); |
53 | 36 | }
|
54 | 37 |
|
55 |
| - Block remove() { |
56 |
| - bool preventDefault = false; |
57 |
| - |
58 |
| - _animate.remove(elements); |
59 |
| - |
60 |
| - // Remove block from list |
61 |
| - if (previous != null && (previous.next = next) != null) { |
62 |
| - next.previous = previous; |
63 |
| - } |
64 |
| - next = previous = null; |
65 |
| - return this; |
| 38 | + void remove(Block block) { |
| 39 | + _blocks.remove(block); |
| 40 | + _animate.remove(block.nodes); |
66 | 41 | }
|
67 | 42 |
|
68 |
| - Block moveAfter(ElementWrapper previousBlock) { |
69 |
| - var previousElements = previousBlock.elements, |
70 |
| - previousElement = previousElements[previousElements.length - 1], |
71 |
| - insertBeforeElement = previousElement.nextNode, |
72 |
| - parentElement = previousElement.parentNode; |
73 |
| - |
74 |
| - elements.forEach((el) => parentElement.insertBefore(el, insertBeforeElement)); |
| 43 | + void move(Block block, { Block moveAfter }) { |
| 44 | + dom.Node previousNode = _lastNode(moveAfter); |
| 45 | + _blocks.remove(block); |
| 46 | + _blocksInsertAfter(block, moveAfter); |
75 | 47 |
|
76 |
| - // Remove block from list |
77 |
| - previous.next = next; |
78 |
| - if (next != null) { |
79 |
| - next.previous = previous; |
80 |
| - } |
81 |
| - // Add block to list |
82 |
| - next = previousBlock.next; |
83 |
| - if (next != null) { |
84 |
| - next.previous = this; |
85 |
| - } |
86 |
| - previous = previousBlock; |
87 |
| - previousBlock.next = this; |
88 |
| - return this; |
| 48 | + _animate.move(block.nodes, placeholder.parentNode, |
| 49 | + insertBefore: previousNode.nextNode); |
89 | 50 | }
|
90 |
| -} |
91 | 51 |
|
92 |
| -/** |
93 |
| - * A BlockHole is an instance of a hole. BlockHoles designate where child |
94 |
| - * [Block]s can be added in parent [Block]. BlockHoles wrap a DOM element, |
95 |
| - * and act as references which allows more blocks to be added. |
96 |
| - */ |
97 |
| -class BlockHole extends ElementWrapper { |
98 |
| - List<dom.Node> elements; |
99 |
| - ElementWrapper previous; |
100 |
| - ElementWrapper next; |
| 52 | + void _blocksInsertAfter(Block block, Block insertAfter) { |
| 53 | + int index = (insertAfter != null) ? _blocks.indexOf(insertAfter) : -1; |
| 54 | + _blocks.insert(index + 1, block); |
| 55 | + } |
101 | 56 |
|
102 |
| - BlockHole(this.elements); |
| 57 | + dom.Node _lastNode(Block insertAfter) => |
| 58 | + insertAfter == null |
| 59 | + ? placeholder |
| 60 | + : insertAfter.nodes[insertAfter.nodes.length - 1]; |
103 | 61 | }
|
104 |
| - |
0 commit comments