]>
Commit | Line | Data |
---|---|---|
1 | Module('NodeSupport')({ | |
2 | prototype : { | |
3 | parent : null, | |
4 | ||
5 | children : [], | |
6 | ||
7 | appendChild : function(child) { | |
8 | if(child.parent) { | |
9 | child.parent.removeChild(child); | |
10 | } | |
11 | ||
12 | if(!this.hasOwnProperty('children')) { | |
13 | this.children = []; | |
14 | } | |
15 | ||
16 | this.children.push(child); | |
17 | this[child.name] = child; | |
18 | child.setParent(this); | |
19 | return child; | |
20 | }, | |
21 | ||
22 | insertBefore : function (child, beforeChild) { | |
23 | var position; | |
24 | ||
25 | if (child.parent) { | |
26 | child.parent.removeChild(child); | |
27 | } | |
28 | ||
29 | if (!this.hasOwnProperty('children')) { | |
30 | this.children = []; | |
31 | } | |
32 | ||
33 | if (typeof beforeChild === 'undefined') { | |
34 | this.appendChild(child); | |
35 | } else { | |
36 | position = this.children.indexOf(beforeChild); | |
37 | this.children.splice(position, 0, child); | |
38 | ||
39 | this[child.name] = child; | |
40 | child.setParent(this); | |
41 | } | |
42 | ||
43 | return child; | |
44 | ||
45 | }, | |
46 | ||
47 | insertChild : function(child, position) { | |
48 | console.warn('NodeSupport insertChild method is deprecated, try insertBefore'); | |
49 | ||
50 | if (child.parent) { | |
51 | child.parent.removeChild(child); | |
52 | } | |
53 | ||
54 | if (!this.hasOwnProperty('children')) { | |
55 | this.children = []; | |
56 | } | |
57 | ||
58 | if (typeof position == 'undefined') { | |
59 | this.children.push(child); | |
60 | this[child.name] = child; | |
61 | child.setParent(this); | |
62 | return child; | |
63 | } | |
64 | ||
65 | this.children.splice(position, 0, child); | |
66 | this[child.name] = child; | |
67 | child.setParent(this); | |
68 | return child; | |
69 | }, | |
70 | ||
71 | removeChild : function (child) { | |
72 | var position = this.children.indexOf(child); | |
73 | ||
74 | if (position !== -1) { | |
75 | this.children.splice(position, 1); | |
76 | delete this[child.name]; | |
77 | child.parent = null; | |
78 | } | |
79 | ||
80 | return child; | |
81 | }, | |
82 | ||
83 | setParent : function (parent) { | |
84 | this.parent = parent; | |
85 | return this; | |
86 | }, | |
87 | ||
88 | getDescendants : function () { | |
89 | var nodes = []; | |
90 | this.children.forEach(function (node) { | |
91 | nodes.push(node); | |
92 | }); | |
93 | this.children.forEach(function (node) { | |
94 | nodes = nodes.concat(node.getDescendants()); | |
95 | }); | |
96 | return nodes; | |
97 | }, | |
98 | ||
99 | getPreviousSibling : function () { | |
100 | if (typeof this.parent === 'undefined') { | |
101 | return; | |
102 | } | |
103 | ||
104 | if (this.parent.children[0] === this) { | |
105 | return; | |
106 | } | |
107 | ||
108 | return this.parent.children[ this.parent.children.indexOf(this) - 1 ]; | |
109 | }, | |
110 | ||
111 | getNextSibling : function () { | |
112 | if (typeof this.parent === 'undefined') { | |
113 | return; | |
114 | } | |
115 | ||
116 | if (this.parent.children[ this.parent.children.length - 1 ] === this) { | |
117 | return; | |
118 | } | |
119 | ||
120 | return this.parent.children[ this.parent.children.indexOf(this) + 1 ]; | |
121 | } | |
122 | } | |
123 | }); |