OLD | NEW |
(Empty) | |
| 1 library node; |
| 2 |
| 3 class Node { |
| 4 |
| 5 // nodes always have an order greater than their ancestors'. |
| 6 // there's no guarantee regarding order between siblings |
| 7 |
| 8 int _order = 0; |
| 9 int get order => _order; |
| 10 void reorderChild(Node child) { // internal, do not call |
| 11 assert(child._attached == _attached); |
| 12 if (child._order <= _order) { |
| 13 child._order = _order + 1; |
| 14 child.reorderChildren(); |
| 15 } |
| 16 } |
| 17 void reorderChildren() { // internal, do not call |
| 18 // override this in subclasses with child nodes |
| 19 // simply call reorderChild(child) for each child |
| 20 } |
| 21 |
| 22 bool _attached = false; |
| 23 bool get attached => _attached; |
| 24 void attach() { |
| 25 // override this in subclasses with child nodes |
| 26 // simply call attach() for each child then call your superclass |
| 27 _attached = true; |
| 28 attachChildren(); |
| 29 } |
| 30 attachChildren() { } // workaround for lack of inter-class mixins in Dart |
| 31 void detach() { |
| 32 // override this in subclasses with child nodes |
| 33 // simply call detach() for each child then call your superclass |
| 34 _attached = false; |
| 35 detachChildren(); |
| 36 } |
| 37 detachChildren() { } // workaround for lack of inter-class mixins in Dart |
| 38 |
| 39 void setAsChild(Node child) { // only for use by subclasses |
| 40 assert(child != null); |
| 41 if (attached) |
| 42 child.attach(); |
| 43 reorderChild(child); |
| 44 } |
| 45 void dropChild(Node child) { // only for use by subclasses |
| 46 assert(child != null); |
| 47 assert(child.attached == attached); |
| 48 if (attached) |
| 49 child.detach(); |
| 50 } |
| 51 |
| 52 } |
OLD | NEW |