This repository has been archived by the owner on Feb 22, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 248
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(blockhole): Change blockhole to have the insert / remove / move …
…methods. Closes #689
- Loading branch information
Showing
11 changed files
with
116 additions
and
197 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,104 +1,61 @@ | ||
part of angular.core.dom; | ||
|
||
/** | ||
* ElementWrapper is an interface for [Block]s and [BlockHole]s. Its purpose is | ||
* to allow treating [Block] and [BlockHole] under same interface so that | ||
* [Block]s can be added after [BlockHole]. | ||
*/ | ||
abstract class ElementWrapper { | ||
List<dom.Node> elements; | ||
ElementWrapper next; | ||
ElementWrapper previous; | ||
} | ||
|
||
/** | ||
* A Block is a fundamental building block of DOM. It is a chunk of DOM which | ||
* can not be structural changed. It can only have its attributes changed. | ||
* A Block can have [BlockHole]s embedded in its DOM. A [BlockHole] can | ||
* contain other [Block]s and it is the only way in which DOM can be changed | ||
* structurally. | ||
* can not be structurally changed. A Block can have [BlockHole] placeholders | ||
* embedded in its DOM. A [BlockHole] can contain other [Block]s and it is the | ||
* only way in which DOM structure can be modified. | ||
* | ||
* A [Block] is a collection of DOM nodes | ||
* | ||
* A [Block] can be created from [BlockFactory]. | ||
* | ||
*/ | ||
class Block implements ElementWrapper { | ||
List<dom.Node> elements; | ||
ElementWrapper next; | ||
ElementWrapper previous; | ||
class Block { | ||
final List<dom.Node> nodes; | ||
Block(this.nodes); | ||
} | ||
|
||
/** | ||
* A BlockHole maintains an ordered list of [Block]'s. It contains a | ||
* [placeholder] node that is used as the insertion point for block nodes. | ||
*/ | ||
class BlockHole { | ||
final dom.Node placeholder; | ||
final NgAnimate _animate; | ||
final List<Block> _blocks = <Block>[]; | ||
|
||
Block(this.elements, this._animate); | ||
|
||
Block insertAfter(ElementWrapper previousBlock) { | ||
// Update Link List. | ||
next = previousBlock.next; | ||
if (next != null) { | ||
next.previous = this; | ||
} | ||
previous = previousBlock; | ||
previousBlock.next = this; | ||
BlockHole(this.placeholder, this._animate); | ||
|
||
// Update DOM | ||
List<dom.Node> previousElements = previousBlock.elements; | ||
dom.Node previousElement = previousElements[previousElements.length - 1]; | ||
dom.Node insertBeforeElement = previousElement.nextNode; | ||
dom.Node parentElement = previousElement.parentNode; | ||
void insert(Block block, { Block insertAfter }) { | ||
dom.Node previousNode = _lastNode(insertAfter); | ||
_blocksInsertAfter(block, insertAfter); | ||
|
||
_animate.insert(elements, parentElement, insertBefore: insertBeforeElement); | ||
|
||
return this; | ||
_animate.insert(block.nodes, placeholder.parentNode, | ||
insertBefore: previousNode.nextNode); | ||
} | ||
|
||
Block remove() { | ||
bool preventDefault = false; | ||
|
||
_animate.remove(elements); | ||
|
||
// Remove block from list | ||
if (previous != null && (previous.next = next) != null) { | ||
next.previous = previous; | ||
} | ||
next = previous = null; | ||
return this; | ||
void remove(Block block) { | ||
_blocks.remove(block); | ||
_animate.remove(block.nodes); | ||
} | ||
|
||
Block moveAfter(ElementWrapper previousBlock) { | ||
var previousElements = previousBlock.elements, | ||
previousElement = previousElements[previousElements.length - 1], | ||
insertBeforeElement = previousElement.nextNode, | ||
parentElement = previousElement.parentNode; | ||
|
||
elements.forEach((el) => parentElement.insertBefore(el, insertBeforeElement)); | ||
void move(Block block, { Block moveAfter }) { | ||
dom.Node previousNode = _lastNode(moveAfter); | ||
_blocks.remove(block); | ||
_blocksInsertAfter(block, moveAfter); | ||
|
||
// Remove block from list | ||
previous.next = next; | ||
if (next != null) { | ||
next.previous = previous; | ||
} | ||
// Add block to list | ||
next = previousBlock.next; | ||
if (next != null) { | ||
next.previous = this; | ||
} | ||
previous = previousBlock; | ||
previousBlock.next = this; | ||
return this; | ||
_animate.move(block.nodes, placeholder.parentNode, | ||
insertBefore: previousNode.nextNode); | ||
} | ||
} | ||
|
||
/** | ||
* A BlockHole is an instance of a hole. BlockHoles designate where child | ||
* [Block]s can be added in parent [Block]. BlockHoles wrap a DOM element, | ||
* and act as references which allows more blocks to be added. | ||
*/ | ||
class BlockHole extends ElementWrapper { | ||
List<dom.Node> elements; | ||
ElementWrapper previous; | ||
ElementWrapper next; | ||
void _blocksInsertAfter(Block block, Block insertAfter) { | ||
int index = (insertAfter != null) ? _blocks.indexOf(insertAfter) : -1; | ||
_blocks.insert(index + 1, block); | ||
} | ||
|
||
BlockHole(this.elements); | ||
dom.Node _lastNode(Block insertAfter) => | ||
insertAfter == null | ||
? placeholder | ||
: insertAfter.nodes[insertAfter.nodes.length - 1]; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.