1 | /** |
---|
2 | * The check box marks a task complete. It is a simulated form field |
---|
3 | * with three states ... |
---|
4 | * 0=unchecked, 1=some children checked, 2=all children checked |
---|
5 | * When a task is clicked, the state of the nodes and parent and children |
---|
6 | * are updated, and this behavior cascades. |
---|
7 | * |
---|
8 | * @extends YAHOO.widget.TextNode |
---|
9 | * @constructor |
---|
10 | * @param oData {object} A string or object containing the data that will |
---|
11 | * be used to render this node. |
---|
12 | * @param oParent {Node} This node's parent node |
---|
13 | * @param expanded {boolean} The initial expanded/collapsed state |
---|
14 | * @param checked {boolean} The initial checked/unchecked state |
---|
15 | */ |
---|
16 | YAHOO.widget.TaskNode = function(oData, oParent, expanded, checked) { |
---|
17 | |
---|
18 | if (oData) { |
---|
19 | this.init(oData, oParent, expanded); |
---|
20 | this.setUpLabel(oData); |
---|
21 | if (checked && checked === true) { |
---|
22 | this.check(); |
---|
23 | } |
---|
24 | |
---|
25 | /* |
---|
26 | if (!this.tree.checkClickEvent) { |
---|
27 | this.tree.checkClickEvent = |
---|
28 | new YAHOO.util.CustomEvent("checkclick", this.tree); |
---|
29 | } |
---|
30 | */ |
---|
31 | } |
---|
32 | |
---|
33 | this.logger = new YAHOO.widget.LogWriter(this.toString()); |
---|
34 | }; |
---|
35 | |
---|
36 | YAHOO.widget.TaskNode.prototype = new YAHOO.widget.TextNode(); |
---|
37 | |
---|
38 | /** |
---|
39 | * True if checkstate is 1 (some children checked) or 2 (all children checked), |
---|
40 | * false if 0. |
---|
41 | * @type boolean |
---|
42 | */ |
---|
43 | YAHOO.widget.TaskNode.prototype.checked = false; |
---|
44 | |
---|
45 | /** |
---|
46 | * checkState |
---|
47 | * 0=unchecked, 1=some children checked, 2=all children checked |
---|
48 | * @type int |
---|
49 | */ |
---|
50 | YAHOO.widget.TaskNode.prototype.checkState = 0; |
---|
51 | |
---|
52 | /** |
---|
53 | * The id of the check element |
---|
54 | * @type string |
---|
55 | */ |
---|
56 | YAHOO.widget.TaskNode.prototype.getCheckElId = function() { |
---|
57 | return "ygtvcheck" + this.index; |
---|
58 | }; |
---|
59 | |
---|
60 | /** |
---|
61 | * Returns the check box element |
---|
62 | * @return the check html element (img) |
---|
63 | */ |
---|
64 | YAHOO.widget.TaskNode.prototype.getCheckEl = function() { |
---|
65 | return document.getElementById(this.getCheckElId()); |
---|
66 | }; |
---|
67 | |
---|
68 | /** |
---|
69 | * The style of the check element, derived from its current state |
---|
70 | * @return {string} the css style for the current check state |
---|
71 | */ |
---|
72 | YAHOO.widget.TaskNode.prototype.getCheckStyle = function() { |
---|
73 | return "ygtvcheck" + this.checkState; |
---|
74 | }; |
---|
75 | |
---|
76 | /** |
---|
77 | * Returns the link that will invoke this node's check toggle |
---|
78 | * @return {string} returns the link required to adjust the checkbox state |
---|
79 | */ |
---|
80 | YAHOO.widget.TaskNode.prototype.getCheckLink = function() { |
---|
81 | return "YAHOO.widget.TreeView.getNode(\'" + this.tree.id + "\'," + |
---|
82 | this.index + ").checkClick()"; |
---|
83 | }; |
---|
84 | |
---|
85 | /** |
---|
86 | * Invoked when the user clicks the check box |
---|
87 | */ |
---|
88 | YAHOO.widget.TaskNode.prototype.checkClick = function() { |
---|
89 | this.logger.log("previous checkstate: " + this.checkState); |
---|
90 | if (this.checkState === 0) { |
---|
91 | this.check(); |
---|
92 | } else { |
---|
93 | this.uncheck(); |
---|
94 | } |
---|
95 | |
---|
96 | // this.tree.checkClickEvent.fire(this); |
---|
97 | |
---|
98 | this.onCheckClick(); |
---|
99 | }; |
---|
100 | |
---|
101 | /** |
---|
102 | * Override to get the check click event |
---|
103 | */ |
---|
104 | YAHOO.widget.TaskNode.prototype.onCheckClick = function() { |
---|
105 | this.logger.log("check was clicked"); |
---|
106 | } |
---|
107 | |
---|
108 | /** |
---|
109 | * Refresh the state of this node's parent, and cascade up. |
---|
110 | */ |
---|
111 | YAHOO.widget.TaskNode.prototype.updateParent = function() { |
---|
112 | var p = this.parent; |
---|
113 | |
---|
114 | if (!p || !p.updateParent) { |
---|
115 | this.logger.log("Abort udpate parent: " + this.index); |
---|
116 | return; |
---|
117 | } |
---|
118 | |
---|
119 | var somethingChecked = false; |
---|
120 | var somethingNotChecked = false; |
---|
121 | |
---|
122 | for (var i=0;i< p.children.length;++i) { |
---|
123 | if (p.children[i].checked) { |
---|
124 | somethingChecked = true; |
---|
125 | // checkState will be 1 if the child node has unchecked children |
---|
126 | if (p.children[i].checkState == 1) { |
---|
127 | somethingNotChecked = true; |
---|
128 | } |
---|
129 | } else { |
---|
130 | somethingNotChecked = true; |
---|
131 | } |
---|
132 | } |
---|
133 | |
---|
134 | if (somethingChecked) { |
---|
135 | p.setCheckState( (somethingNotChecked) ? 1 : 2 ); |
---|
136 | } else { |
---|
137 | p.setCheckState(0); |
---|
138 | } |
---|
139 | |
---|
140 | p.updateCheckHtml(); |
---|
141 | p.updateParent(); |
---|
142 | }; |
---|
143 | |
---|
144 | /** |
---|
145 | * If the node has been rendered, update the html to reflect the current |
---|
146 | * state of the node. |
---|
147 | */ |
---|
148 | YAHOO.widget.TaskNode.prototype.updateCheckHtml = function() { |
---|
149 | if (this.parent && this.parent.childrenRendered) { |
---|
150 | this.getCheckEl().className = this.getCheckStyle(); |
---|
151 | } |
---|
152 | }; |
---|
153 | |
---|
154 | /** |
---|
155 | * Updates the state. The checked property is true if the state is 1 or 2 |
---|
156 | * |
---|
157 | * @param the new check state |
---|
158 | */ |
---|
159 | YAHOO.widget.TaskNode.prototype.setCheckState = function(state) { |
---|
160 | this.checkState = state; |
---|
161 | this.checked = (state > 0); |
---|
162 | }; |
---|
163 | |
---|
164 | /** |
---|
165 | * Check this node |
---|
166 | */ |
---|
167 | YAHOO.widget.TaskNode.prototype.check = function() { |
---|
168 | this.logger.log("check"); |
---|
169 | this.setCheckState(2); |
---|
170 | for (var i=0; i<this.children.length; ++i) { |
---|
171 | this.children[i].check(); |
---|
172 | } |
---|
173 | this.updateCheckHtml(); |
---|
174 | this.updateParent(); |
---|
175 | }; |
---|
176 | |
---|
177 | /** |
---|
178 | * Uncheck this node |
---|
179 | */ |
---|
180 | YAHOO.widget.TaskNode.prototype.uncheck = function() { |
---|
181 | this.setCheckState(0); |
---|
182 | for (var i=0; i<this.children.length; ++i) { |
---|
183 | this.children[i].uncheck(); |
---|
184 | } |
---|
185 | this.updateCheckHtml(); |
---|
186 | this.updateParent(); |
---|
187 | }; |
---|
188 | |
---|
189 | // Overrides YAHOO.widget.TextNode |
---|
190 | YAHOO.widget.TaskNode.prototype.getNodeHtml = function() { |
---|
191 | this.logger.log("Generating html"); |
---|
192 | var sb = new Array(); |
---|
193 | |
---|
194 | sb[sb.length] = '<table border="0" cellpadding="0" cellspacing="0">'; |
---|
195 | sb[sb.length] = '<tr>'; |
---|
196 | |
---|
197 | for (i=0;i<this.depth;++i) { |
---|
198 | sb[sb.length] = '<td class="' + this.getDepthStyle(i) + '"> </td>'; |
---|
199 | } |
---|
200 | |
---|
201 | sb[sb.length] = '<td'; |
---|
202 | sb[sb.length] = ' id="' + this.getToggleElId() + '"'; |
---|
203 | sb[sb.length] = ' class="' + this.getStyle() + '"'; |
---|
204 | if (this.hasChildren(true)) { |
---|
205 | sb[sb.length] = ' onmouseover="this.className='; |
---|
206 | sb[sb.length] = 'YAHOO.widget.TreeView.getNode(\''; |
---|
207 | sb[sb.length] = this.tree.id + '\',' + this.index + ').getHoverStyle()"'; |
---|
208 | sb[sb.length] = ' onmouseout="this.className='; |
---|
209 | sb[sb.length] = 'YAHOO.widget.TreeView.getNode(\''; |
---|
210 | sb[sb.length] = this.tree.id + '\',' + this.index + ').getStyle()"'; |
---|
211 | } |
---|
212 | sb[sb.length] = ' onclick="javascript:' + this.getToggleLink() + '"> '; |
---|
213 | sb[sb.length] = '</td>'; |
---|
214 | |
---|
215 | // check box |
---|
216 | sb[sb.length] = '<td'; |
---|
217 | sb[sb.length] = ' id="' + this.getCheckElId() + '"'; |
---|
218 | sb[sb.length] = ' class="' + this.getCheckStyle() + '"'; |
---|
219 | sb[sb.length] = ' onclick="javascript:' + this.getCheckLink() + '">'; |
---|
220 | sb[sb.length] = ' </td>'; |
---|
221 | |
---|
222 | |
---|
223 | sb[sb.length] = '<td>'; |
---|
224 | sb[sb.length] = '<a'; |
---|
225 | sb[sb.length] = ' id="' + this.labelElId + '"'; |
---|
226 | sb[sb.length] = ' class="' + this.labelStyle + '"'; |
---|
227 | sb[sb.length] = ' href="' + this.href + '"'; |
---|
228 | sb[sb.length] = ' target="' + this.target + '"'; |
---|
229 | if (this.hasChildren(true)) { |
---|
230 | sb[sb.length] = ' onmouseover="document.getElementById(\''; |
---|
231 | sb[sb.length] = this.getToggleElId() + '\').className='; |
---|
232 | sb[sb.length] = 'YAHOO.widget.TreeView.getNode(\''; |
---|
233 | sb[sb.length] = this.tree.id + '\',' + this.index + ').getHoverStyle()"'; |
---|
234 | sb[sb.length] = ' onmouseout="document.getElementById(\''; |
---|
235 | sb[sb.length] = this.getToggleElId() + '\').className='; |
---|
236 | sb[sb.length] = 'YAHOO.widget.TreeView.getNode(\''; |
---|
237 | sb[sb.length] = this.tree.id + '\',' + this.index + ').getStyle()"'; |
---|
238 | } |
---|
239 | sb[sb.length] = ' >'; |
---|
240 | sb[sb.length] = this.label; |
---|
241 | sb[sb.length] = '</a>'; |
---|
242 | sb[sb.length] = '</td>'; |
---|
243 | sb[sb.length] = '</tr>'; |
---|
244 | sb[sb.length] = '</table>'; |
---|
245 | |
---|
246 | return sb.join(""); |
---|
247 | |
---|
248 | }; |
---|
249 | |
---|
250 | YAHOO.widget.TaskNode.prototype.toString = function() { |
---|
251 | return "TaskNode (" + this.index + ") " + this.label; |
---|
252 | }; |
---|
253 | |
---|