]> git.wh0rd.org - tt-rss.git/blob - lib/CheckBoxTree.js
assorted DnD and pref layout fixes
[tt-rss.git] / lib / CheckBoxTree.js
1 dojo.provide("lib.CheckBoxTree");
2 dojo.provide("lib.CheckBoxStoreModel");
3
4 // THIS WIDGET IS BASED ON DOJO/DIJIT 1.4.0 AND WILL NOT WORK WITH PREVIOUS VERSIONS
5 //
6 // Release date: 02/05/2010
7 //
8
9 dojo.require("dijit.Tree");
10 dojo.require("dijit.form.CheckBox");
11
12 dojo.declare( "lib.CheckBoxStoreModel", dijit.tree.TreeStoreModel,
13 {
14 // checkboxAll: Boolean
15 // If true, every node in the tree will receive a checkbox regardless if the 'checkbox' attribute
16 // is specified in the dojo.data.
17 checkboxAll: true,
18
19 // checkboxState: Boolean
20 // The default state applied to every checkbox unless otherwise specified in the dojo.data.
21 // (see also: checkboxIdent)
22 checkboxState: false,
23
24 // checkboxRoot: Boolean
25 // If true, the root node will receive a checkbox eventhough it's not a true entry in the store.
26 // This attribute is independent of the showRoot attribute of the tree itself. If the tree
27 // attribute 'showRoot' is set to false to checkbox for the root will not show either.
28 checkboxRoot: false,
29
30 // checkboxStrict: Boolean
31 // If true, a strict parent-child checkbox relation is maintained. For example, if all children
32 // are checked the parent will automatically be checked or if any of the children are unchecked
33 // the parent will be unchecked.
34 checkboxStrict: true,
35
36 // checkboxIdent: String
37 // The attribute name (attribute of the dojo.data.item) that specifies that items checkbox initial
38 // state. Example: { name:'Egypt', type:'country', checkbox: true }
39 // If a dojo.data.item has no 'checkbox' attribute specified it will depend on the attribute
40 // 'checkboxAll' if one will be created automatically and if so what the initial state will be as
41 // specified by 'checkboxState'.
42 checkboxIdent: "checkbox",
43
44 updateCheckbox: function(/*dojo.data.Item*/ storeItem, /*Boolean*/ newState ) {
45 // summary:
46 // Update the checkbox state (true/false) for the item and the associated parent and
47 // child checkboxes if any.
48 // description:
49 // Update a single checkbox state (true/false) for the item and the associated parent
50 // and child checkboxes if any. This function is called from the tree if a user checked
51 // or unchecked a checkbox on the tree. The parent and child tree nodes are updated to
52 // maintain consistency if 'checkboxStrict' is set to true.
53 // storeItem:
54 // The item in the dojo.data.store whos checkbox state needs updating.
55 // newState:
56 // The new state of the checkbox: true or false
57 // example:
58 // | model.updateCheckboxState(item, true);
59 //
60 this._setCheckboxState( storeItem, newState );
61 if( this.checkboxStrict ) {
62 this._updateChildCheckbox( storeItem, newState );
63 this._updateParentCheckbox( storeItem, newState );
64 }
65 },
66 setAllChecked: function(checked) {
67 var items = this.store._arrayOfAllItems;
68 this.setCheckboxState(items, checked);
69 },
70 setCheckboxState: function(items, checked) {
71 for (var i = 0; i < items.length; i++) {
72 this._setCheckboxState(items[i], checked);
73 }
74 },
75 getCheckedItems: function() {
76 var items = this.store._arrayOfAllItems;
77 var result = [];
78
79 for (var i = 0; i < items.length; i++) {
80 if (this.store.getValue(items[i], 'checkbox'))
81 result.push(items[i]);
82 }
83
84 return result;
85 },
86
87 getCheckboxState: function(/*dojo.data.Item*/ storeItem) {
88 // summary:
89 // Get the current checkbox state from the dojo.data.store.
90 // description:
91 // Get the current checkbox state from the dojo.data store. A checkbox can have three
92 // different states: true, false or undefined. Undefined in this context means no
93 // checkbox identifier (checkboxIdent) was found in the dojo.data store. Depending on
94 // the checkbox attributes as specified above the following will take place:
95 // a) If the current checkbox state is undefined and the checkbox attribute 'checkboxAll' or
96 // 'checkboxRoot' is true one will be created and the default state 'checkboxState' will
97 // be applied.
98 // b) If the current state is undefined and 'checkboxAll' is false the state undefined remains
99 // unchanged and is returned. This will prevent any tree node from creating a checkbox.
100 //
101 // storeItem:
102 // The item in the dojo.data.store whos checkbox state is returned.
103 // example:
104 // | var currState = model.getCheckboxState(item);
105 //
106 var currState = undefined;
107
108 // Special handling required for the 'fake' root entry (the root is NOT a dojo.data.item).
109 // this stuff is only relevant for Forest store -fox
110 /* if ( storeItem == this.root ) {
111 if( typeof(storeItem.checkbox) == "undefined" ) {
112 this.root.checkbox = undefined; // create a new checbox reference as undefined.
113 if( this.checkboxRoot ) {
114 currState = this.root.checkbox = this.checkboxState;
115 }
116 } else {
117 currState = this.root.checkbox;
118 }
119 } else { // a valid dojo.store.item
120 currState = this.store.getValue(storeItem, this.checkboxIdent);
121 if( currState == undefined && this.checkboxAll) {
122 this._setCheckboxState( storeItem, this.checkboxState );
123 currState = this.checkboxState;
124 }
125 } */
126
127 currState = this.store.getValue(storeItem, this.checkboxIdent);
128 if( currState == undefined && this.checkboxAll) {
129 this._setCheckboxState( storeItem, this.checkboxState );
130 currState = this.checkboxState;
131 }
132
133 return currState // the current state of the checkbox (true/false or undefined)
134 },
135
136 _setCheckboxState: function(/*dojo.data.Item*/ storeItem, /*Boolean*/ newState ) {
137 // summary:
138 // Set/update the checkbox state on the dojo.data store.
139 // description:
140 // Set/update the checkbox state on the dojo.data.store. Retreive the current
141 // state of the checkbox and validate if an update is required, this will keep
142 // update events to a minimum. On completion a 'onCheckboxChange' event is
143 // triggered.
144 // If the current state is undefined (ie: no checkbox attribute specified for
145 // this dojo.data.item) the 'checkboxAll' attribute is checked to see if one
146 // needs to be created. In case of the root the 'checkboxRoot' attribute is checked.
147 // NOTE: the store.setValue function will create the 'checkbox' attribute for the
148 // item if none exists.
149 // storeItem:
150 // The item in the dojo.data.store whos checkbox state is updated.
151 // newState:
152 // The new state of the checkbox: true or false
153 // example:
154 // | model.setCheckboxState(item, true);
155 //
156 var stateChanged = true;
157
158 if( storeItem != this.root ) {
159 var currState = this.store.getValue(storeItem, this.checkboxIdent);
160 if( currState != newState && ( currState !== undefined || this.checkboxAll ) ) {
161 this.store.setValue(storeItem, this.checkboxIdent, newState);
162 } else {
163 stateChanged = false; // No changes to the checkbox
164 }
165 } else { // Tree root instance
166 if( this.root.checkbox != newState && ( this.root.checkbox !== undefined || this.checkboxRoot ) ) {
167 this.root.checkbox = newState;
168 } else {
169 stateChanged = false;
170 }
171 }
172 if( stateChanged ) { // In case of any changes trigger the update event.
173 this.onCheckboxChange(storeItem);
174 }
175 return stateChanged;
176 },
177
178 _updateChildCheckbox: function(/*dojo.data.Item*/ parentItem, /*Boolean*/ newState ) {
179 // summary:
180 // Set all child checkboxes to true/false depending on the parent checkbox state.
181 // description:
182 // If a parent checkbox changes state, all child and grandchild checkboxes will be
183 // updated to reflect the change. For example, if the parent state is set to true,
184 // all child and grandchild checkboxes will receive that same 'true' state.
185 // If a child checkbox changes state and has multiple parent, all of its parents
186 // need to be re-evaluated.
187 // parentItem:
188 // The parent dojo.data.item whos child/grandchild checkboxes require updating.
189 // newState:
190 // The new state of the checkbox: true or false
191 //
192 if( this.mayHaveChildren( parentItem )) {
193 this.getChildren( parentItem, dojo.hitch( this,
194 function( children ) {
195 dojo.forEach( children, function(child) {
196 if( this._setCheckboxState(child, newState) ) {
197 var parents = this._getParentsItem(child);
198 if( parents.length > 1 ) {
199 this._updateParentCheckbox( child, newState );
200 }
201 }
202 if( this.mayHaveChildren( child )) {
203 this._updateChildCheckbox( child, newState );
204 }
205 }, this );
206 }),
207 function(err) {
208 console.error(this, ": updating child checkboxes: ", err);
209 }
210 );
211 }
212 },
213
214 _updateParentCheckbox: function(/*dojo.data.Item*/ storeItem, /*Boolean*/ newState ) {
215 // summary:
216 // Update the parent checkbox state depending on the state of all child checkboxes.
217 // description:
218 // Update the parent checkbox state depending on the state of all child checkboxes.
219 // The parent checkbox automatically changes state if ALL child checkboxes are true
220 // or false. If, as a result, the parent checkbox changes state, we will check if
221 // its parent needs to be updated as well all the way upto the root.
222 // storeItem:
223 // The dojo.data.item whos parent checkboxes require updating.
224 // newState:
225 // The new state of the checkbox: true or false
226 //
227 var parents = this._getParentsItem(storeItem);
228 dojo.forEach( parents, function( parentItem ) {
229 if( newState ) { // new state = true (checked)
230 this.getChildren( parentItem, dojo.hitch( this,
231 function(siblings) {
232 var allChecked = true;
233 dojo.some( siblings, function(sibling) {
234 siblState = this.getCheckboxState(sibling);
235 if( siblState !== undefined && allChecked )
236 allChecked = siblState;
237 return !(allChecked);
238 }, this );
239 if( allChecked ) {
240 this._setCheckboxState( parentItem, true );
241 this._updateParentCheckbox( parentItem, true );
242 }
243 }),
244 function(err) {
245 console.error(this, ": updating parent checkboxes: ", err);
246 }
247 );
248 } else { // new state = false (unchecked)
249 if( this._setCheckboxState( parentItem, false ) ) {
250 this._updateParentCheckbox( parentItem, false );
251 }
252 }
253 }, this );
254 },
255
256 _getParentsItem: function(/*dojo.data.Item*/ storeItem ) {
257 // summary:
258 // Get the parent(s) of a dojo.data item.
259 // description:
260 // Get the parent(s) of a dojo.data item. The '_reverseRefMap' entry of the item is
261 // used to identify the parent(s). A child will have a parent reference if the parent
262 // specified the '_reference' attribute.
263 // For example: children:[{_reference:'Mexico'}, {_reference:'Canada'}, ...
264 // storeItem:
265 // The dojo.data.item whos parent(s) will be returned.
266 //
267 var parents = [];
268
269 if( storeItem != this.root ) {
270 var references = storeItem[this.store._reverseRefMap];
271 for(itemId in references ) {
272 parents.push(this.store._itemsByIdentity[itemId]);
273 }
274 if (!parents.length) {
275 parents.push(this.root);
276 }
277 }
278 return parents // parent(s) of a dojo.data.item (Array of dojo.data.items)
279 },
280
281 validateData: function(/*dojo.data.Item*/ storeItem, /*thisObject*/ scope ) {
282 // summary:
283 // Validate/normalize the parent(s) checkbox data in the dojo.data store.
284 // description:
285 // Validate/normalize the parent-child checkbox relationship if the attribute
286 // 'checkboxStrict' is set to true. This function is called as part of the post
287 // creation of the Tree instance. All parent checkboxes are set to the appropriate
288 // state according to the actual state(s) of their children.
289 // This will potentionally overwrite whatever was specified for the parent in the
290 // dojo.data store. This will garantee the tree is in a consistent state after startup.
291 // storeItem:
292 // The element to start traversing the dojo.data.store, typically model.root
293 // scope:
294 // The scope to use when this method executes.
295 // example:
296 // | this.model.validateData(this.model.root, this.model);
297 //
298 if( !scope.checkboxStrict ) {
299 return;
300 }
301 scope.getChildren( storeItem, dojo.hitch( scope,
302 function(children) {
303 var allChecked = true;
304 var childState;
305 dojo.forEach( children, function( child ) {
306 if( this.mayHaveChildren( child )) {
307 this.validateData( child, this );
308 }
309 childState = this.getCheckboxState( child );
310 if( childState !== undefined && allChecked )
311 allChecked = childState;
312 }, this);
313
314 if ( this._setCheckboxState( storeItem, allChecked) ) {
315 this._updateParentCheckbox( storeItem, allChecked);
316 }
317 }),
318 function(err) {
319 console.error(this, ": validating checkbox data: ", err);
320 }
321 );
322 },
323
324 onCheckboxChange: function(/*dojo.data.Item*/ storeItem ) {
325 // summary:
326 // Callback whenever a checkbox state has changed state, so that
327 // the Tree can update the checkbox. This callback is generally
328 // triggered by the '_setCheckboxState' function.
329 // tags:
330 // callback
331 }
332
333 });
334
335 dojo.declare( "lib._CheckBoxTreeNode", dijit._TreeNode,
336 {
337 // _checkbox: [protected] dojo.doc.element
338 // Local reference to the dojo.doc.element of type 'checkbox'
339 _checkbox: null,
340
341 _createCheckbox: function() {
342 // summary:
343 // Create a checkbox on the CheckBoxTreeNode
344 // description:
345 // Create a checkbox on the CheckBoxTreeNode. The checkbox is ONLY created if a
346 // valid reference was found in the dojo.data store or the attribute 'checkboxAll'
347 // is set to true. If the current state is 'undefined' no reference was found and
348 // 'checkboxAll' is set to false.
349 // Note: the attribute 'checkboxAll' is validated by the getCheckboxState function
350 // therefore no need to do that here. (see getCheckboxState for details).
351 //
352 var currState = this.tree.model.getCheckboxState( this.item );
353 if( currState !== undefined ) {
354 this._checkbox = new dijit.form.CheckBox();
355 //this._checkbox = dojo.doc.createElement('input');
356 this._checkbox.type = 'checkbox';
357 this._checkbox.attr('checked', currState);
358 dojo.place(this._checkbox.domNode, this.expandoNode, 'after');
359 }
360 },
361
362 postCreate: function() {
363 // summary:
364 // Handle the creation of the checkbox after the CheckBoxTreeNode has been instanciated.
365 // description:
366 // Handle the creation of the checkbox after the CheckBoxTreeNode has been instanciated.
367 this._createCheckbox();
368 this.inherited( arguments );
369 }
370
371 });
372
373 dojo.declare( "lib.CheckBoxTree", dijit.Tree,
374 {
375
376 onNodeChecked: function(/*dojo.data.Item*/ storeItem, /*treeNode*/ treeNode) {
377 // summary:
378 // Callback when a checkbox tree node is checked
379 // tags:
380 // callback
381 },
382
383 onNodeUnchecked: function(/*dojo.data.Item*/ storeItem, /* treeNode */ treeNode) {
384 // summary:
385 // Callback when a checkbox tree node is unchecked
386 // tags:
387 // callback
388 },
389
390 _onClick: function(/*TreeNode*/ nodeWidget, /*Event*/ e) {
391 // summary:
392 // Translates click events into commands for the controller to process
393 // description:
394 // the _onClick function is called whenever a 'click' is detected. This
395 // instance of _onClick only handles the click events associated with
396 // the checkbox whos DOM name is INPUT.
397 //
398 var domElement = e.target;
399
400 // Only handle checkbox clicks here
401 if(domElement.type != 'checkbox') {
402 return this.inherited( arguments );
403 }
404
405 this._publish("execute", { item: nodeWidget.item, node: nodeWidget} );
406 // Go tell the model to update the checkbox state
407
408 this.model.updateCheckbox( nodeWidget.item, nodeWidget._checkbox.checked );
409 // Generate some additional events
410 //this.onClick( nodeWidget.item, nodeWidget, e );
411 if(nodeWidget._checkbox.checked) {
412 this.onNodeChecked( nodeWidget.item, nodeWidget);
413 } else {
414 this.onNodeUnchecked( nodeWidget.item, nodeWidget);
415 }
416 this.focusNode(nodeWidget);
417 },
418
419 _onCheckboxChange: function(/*dojo.data.Item*/ storeItem ) {
420 // summary:
421 // Processes notification of a change to a checkbox state (triggered by the model).
422 // description:
423 // Whenever the model changes the state of a checkbox in the dojo.data.store it will
424 // trigger the 'onCheckboxChange' event allowing the Tree to make the same changes
425 // on the tree Node. There are several conditions why a tree node or checkbox does not
426 // exists:
427 // a) The node has not been created yet (the user has not expanded the tree node yet).
428 // b) The checkbox may be null if condition (a) exists or no 'checkbox' attribute was
429 // specified for the associated dojo.data.item and the attribute 'checkboxAll' is
430 // set to false.
431 // tags:
432 // callback
433 var model = this.model,
434 identity = model.getIdentity(storeItem),
435 nodes = this._itemNodesMap[identity];
436
437 // As of dijit.Tree 1.4 multiple references (parents) are supported, therefore we may have
438 // to update multiple nodes which are all associated with the same dojo.data.item.
439 if( nodes ) {
440 dojo.forEach( nodes, function(node) {
441 if( node._checkbox != null ) {
442 node._checkbox.attr('checked', this.model.getCheckboxState( storeItem ));
443 }
444 }, this );
445 }
446 },
447
448 postCreate: function() {
449 // summary:
450 // Handle any specifics related to the tree and model after the instanciation of the Tree.
451 // description:
452 // Validate if we have a 'write' store first. Subscribe to the 'onCheckboxChange' event
453 // (triggered by the model) and kickoff the initial checkbox data validation.
454 //
455 var store = this.model.store;
456 if(!store.getFeatures()['dojo.data.api.Write']){
457 throw new Error("lib.CheckboxTree: store must support dojo.data.Write");
458 }
459 this.connect(this.model, "onCheckboxChange", "_onCheckboxChange");
460 this.model.validateData( this.model.root, this.model );
461 this.inherited(arguments);
462 },
463
464 _createTreeNode: function( args ) {
465 // summary:
466 // Create a new CheckboxTreeNode instance.
467 // description:
468 // Create a new CheckboxTreeNode instance.
469 return new lib._CheckBoxTreeNode(args);
470 }
471
472 });