]> git.wh0rd.org - tt-rss.git/blob - lib/CheckBoxTree.js
rework pref-feeds dialog; update other pref panes
[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 if ( storeItem == this.root ) {
110 if( typeof(storeItem.checkbox) == "undefined" ) {
111 this.root.checkbox = undefined; // create a new checbox reference as undefined.
112 if( this.checkboxRoot ) {
113 currState = this.root.checkbox = this.checkboxState;
114 }
115 } else {
116 currState = this.root.checkbox;
117 }
118 } else { // a valid dojo.store.item
119 currState = this.store.getValue(storeItem, this.checkboxIdent);
120 if( currState == undefined && this.checkboxAll) {
121 this._setCheckboxState( storeItem, this.checkboxState );
122 currState = this.checkboxState;
123 }
124 }
125 return currState // the current state of the checkbox (true/false or undefined)
126 },
127
128 _setCheckboxState: function(/*dojo.data.Item*/ storeItem, /*Boolean*/ newState ) {
129 // summary:
130 // Set/update the checkbox state on the dojo.data store.
131 // description:
132 // Set/update the checkbox state on the dojo.data.store. Retreive the current
133 // state of the checkbox and validate if an update is required, this will keep
134 // update events to a minimum. On completion a 'onCheckboxChange' event is
135 // triggered.
136 // If the current state is undefined (ie: no checkbox attribute specified for
137 // this dojo.data.item) the 'checkboxAll' attribute is checked to see if one
138 // needs to be created. In case of the root the 'checkboxRoot' attribute is checked.
139 // NOTE: the store.setValue function will create the 'checkbox' attribute for the
140 // item if none exists.
141 // storeItem:
142 // The item in the dojo.data.store whos checkbox state is updated.
143 // newState:
144 // The new state of the checkbox: true or false
145 // example:
146 // | model.setCheckboxState(item, true);
147 //
148 var stateChanged = true;
149
150 if( storeItem != this.root ) {
151 var currState = this.store.getValue(storeItem, this.checkboxIdent);
152 if( currState != newState && ( currState !== undefined || this.checkboxAll ) ) {
153 this.store.setValue(storeItem, this.checkboxIdent, newState);
154 } else {
155 stateChanged = false; // No changes to the checkbox
156 }
157 } else { // Tree root instance
158 if( this.root.checkbox != newState && ( this.root.checkbox !== undefined || this.checkboxRoot ) ) {
159 this.root.checkbox = newState;
160 } else {
161 stateChanged = false;
162 }
163 }
164 if( stateChanged ) { // In case of any changes trigger the update event.
165 this.onCheckboxChange(storeItem);
166 }
167 return stateChanged;
168 },
169
170 _updateChildCheckbox: function(/*dojo.data.Item*/ parentItem, /*Boolean*/ newState ) {
171 // summary:
172 // Set all child checkboxes to true/false depending on the parent checkbox state.
173 // description:
174 // If a parent checkbox changes state, all child and grandchild checkboxes will be
175 // updated to reflect the change. For example, if the parent state is set to true,
176 // all child and grandchild checkboxes will receive that same 'true' state.
177 // If a child checkbox changes state and has multiple parent, all of its parents
178 // need to be re-evaluated.
179 // parentItem:
180 // The parent dojo.data.item whos child/grandchild checkboxes require updating.
181 // newState:
182 // The new state of the checkbox: true or false
183 //
184 if( this.mayHaveChildren( parentItem )) {
185 this.getChildren( parentItem, dojo.hitch( this,
186 function( children ) {
187 dojo.forEach( children, function(child) {
188 if( this._setCheckboxState(child, newState) ) {
189 var parents = this._getParentsItem(child);
190 if( parents.length > 1 ) {
191 this._updateParentCheckbox( child, newState );
192 }
193 }
194 if( this.mayHaveChildren( child )) {
195 this._updateChildCheckbox( child, newState );
196 }
197 }, this );
198 }),
199 function(err) {
200 console.error(this, ": updating child checkboxes: ", err);
201 }
202 );
203 }
204 },
205
206 _updateParentCheckbox: function(/*dojo.data.Item*/ storeItem, /*Boolean*/ newState ) {
207 // summary:
208 // Update the parent checkbox state depending on the state of all child checkboxes.
209 // description:
210 // Update the parent checkbox state depending on the state of all child checkboxes.
211 // The parent checkbox automatically changes state if ALL child checkboxes are true
212 // or false. If, as a result, the parent checkbox changes state, we will check if
213 // its parent needs to be updated as well all the way upto the root.
214 // storeItem:
215 // The dojo.data.item whos parent checkboxes require updating.
216 // newState:
217 // The new state of the checkbox: true or false
218 //
219 var parents = this._getParentsItem(storeItem);
220 dojo.forEach( parents, function( parentItem ) {
221 if( newState ) { // new state = true (checked)
222 this.getChildren( parentItem, dojo.hitch( this,
223 function(siblings) {
224 var allChecked = true;
225 dojo.some( siblings, function(sibling) {
226 siblState = this.getCheckboxState(sibling);
227 if( siblState !== undefined && allChecked )
228 allChecked = siblState;
229 return !(allChecked);
230 }, this );
231 if( allChecked ) {
232 this._setCheckboxState( parentItem, true );
233 this._updateParentCheckbox( parentItem, true );
234 }
235 }),
236 function(err) {
237 console.error(this, ": updating parent checkboxes: ", err);
238 }
239 );
240 } else { // new state = false (unchecked)
241 if( this._setCheckboxState( parentItem, false ) ) {
242 this._updateParentCheckbox( parentItem, false );
243 }
244 }
245 }, this );
246 },
247
248 _getParentsItem: function(/*dojo.data.Item*/ storeItem ) {
249 // summary:
250 // Get the parent(s) of a dojo.data item.
251 // description:
252 // Get the parent(s) of a dojo.data item. The '_reverseRefMap' entry of the item is
253 // used to identify the parent(s). A child will have a parent reference if the parent
254 // specified the '_reference' attribute.
255 // For example: children:[{_reference:'Mexico'}, {_reference:'Canada'}, ...
256 // storeItem:
257 // The dojo.data.item whos parent(s) will be returned.
258 //
259 var parents = [];
260
261 if( storeItem != this.root ) {
262 var references = storeItem[this.store._reverseRefMap];
263 for(itemId in references ) {
264 parents.push(this.store._itemsByIdentity[itemId]);
265 }
266 if (!parents.length) {
267 parents.push(this.root);
268 }
269 }
270 return parents // parent(s) of a dojo.data.item (Array of dojo.data.items)
271 },
272
273 validateData: function(/*dojo.data.Item*/ storeItem, /*thisObject*/ scope ) {
274 // summary:
275 // Validate/normalize the parent(s) checkbox data in the dojo.data store.
276 // description:
277 // Validate/normalize the parent-child checkbox relationship if the attribute
278 // 'checkboxStrict' is set to true. This function is called as part of the post
279 // creation of the Tree instance. All parent checkboxes are set to the appropriate
280 // state according to the actual state(s) of their children.
281 // This will potentionally overwrite whatever was specified for the parent in the
282 // dojo.data store. This will garantee the tree is in a consistent state after startup.
283 // storeItem:
284 // The element to start traversing the dojo.data.store, typically model.root
285 // scope:
286 // The scope to use when this method executes.
287 // example:
288 // | this.model.validateData(this.model.root, this.model);
289 //
290 if( !scope.checkboxStrict ) {
291 return;
292 }
293 scope.getChildren( storeItem, dojo.hitch( scope,
294 function(children) {
295 var allChecked = true;
296 var childState;
297 dojo.forEach( children, function( child ) {
298 if( this.mayHaveChildren( child )) {
299 this.validateData( child, this );
300 }
301 childState = this.getCheckboxState( child );
302 if( childState !== undefined && allChecked )
303 allChecked = childState;
304 }, this);
305
306 if ( this._setCheckboxState( storeItem, allChecked) ) {
307 this._updateParentCheckbox( storeItem, allChecked);
308 }
309 }),
310 function(err) {
311 console.error(this, ": validating checkbox data: ", err);
312 }
313 );
314 },
315
316 onCheckboxChange: function(/*dojo.data.Item*/ storeItem ) {
317 // summary:
318 // Callback whenever a checkbox state has changed state, so that
319 // the Tree can update the checkbox. This callback is generally
320 // triggered by the '_setCheckboxState' function.
321 // tags:
322 // callback
323 }
324
325 });
326
327 dojo.declare( "lib._CheckBoxTreeNode", dijit._TreeNode,
328 {
329 // _checkbox: [protected] dojo.doc.element
330 // Local reference to the dojo.doc.element of type 'checkbox'
331 _checkbox: null,
332
333 _createCheckbox: function() {
334 // summary:
335 // Create a checkbox on the CheckBoxTreeNode
336 // description:
337 // Create a checkbox on the CheckBoxTreeNode. The checkbox is ONLY created if a
338 // valid reference was found in the dojo.data store or the attribute 'checkboxAll'
339 // is set to true. If the current state is 'undefined' no reference was found and
340 // 'checkboxAll' is set to false.
341 // Note: the attribute 'checkboxAll' is validated by the getCheckboxState function
342 // therefore no need to do that here. (see getCheckboxState for details).
343 //
344 var currState = this.tree.model.getCheckboxState( this.item );
345 if( currState !== undefined ) {
346 this._checkbox = new dijit.form.CheckBox();
347 //this._checkbox = dojo.doc.createElement('input');
348 this._checkbox.type = 'checkbox';
349 this._checkbox.attr('checked', currState);
350 dojo.place(this._checkbox.domNode, this.expandoNode, 'after');
351 }
352 },
353
354 postCreate: function() {
355 // summary:
356 // Handle the creation of the checkbox after the CheckBoxTreeNode has been instanciated.
357 // description:
358 // Handle the creation of the checkbox after the CheckBoxTreeNode has been instanciated.
359 this._createCheckbox();
360 this.inherited( arguments );
361 }
362
363 });
364
365 dojo.declare( "lib.CheckBoxTree", dijit.Tree,
366 {
367
368 onNodeChecked: function(/*dojo.data.Item*/ storeItem, /*treeNode*/ treeNode) {
369 // summary:
370 // Callback when a checkbox tree node is checked
371 // tags:
372 // callback
373 },
374
375 onNodeUnchecked: function(/*dojo.data.Item*/ storeItem, /* treeNode */ treeNode) {
376 // summary:
377 // Callback when a checkbox tree node is unchecked
378 // tags:
379 // callback
380 },
381
382 _onClick: function(/*TreeNode*/ nodeWidget, /*Event*/ e) {
383 // summary:
384 // Translates click events into commands for the controller to process
385 // description:
386 // the _onClick function is called whenever a 'click' is detected. This
387 // instance of _onClick only handles the click events associated with
388 // the checkbox whos DOM name is INPUT.
389 //
390 var domElement = e.target;
391
392 // Only handle checkbox clicks here
393 if(domElement.type != 'checkbox') {
394 return this.inherited( arguments );
395 }
396
397 this._publish("execute", { item: nodeWidget.item, node: nodeWidget} );
398 // Go tell the model to update the checkbox state
399
400 this.model.updateCheckbox( nodeWidget.item, nodeWidget._checkbox.checked );
401 // Generate some additional events
402 //this.onClick( nodeWidget.item, nodeWidget, e );
403 if(nodeWidget._checkbox.checked) {
404 this.onNodeChecked( nodeWidget.item, nodeWidget);
405 } else {
406 this.onNodeUnchecked( nodeWidget.item, nodeWidget);
407 }
408 this.focusNode(nodeWidget);
409 },
410
411 _onCheckboxChange: function(/*dojo.data.Item*/ storeItem ) {
412 // summary:
413 // Processes notification of a change to a checkbox state (triggered by the model).
414 // description:
415 // Whenever the model changes the state of a checkbox in the dojo.data.store it will
416 // trigger the 'onCheckboxChange' event allowing the Tree to make the same changes
417 // on the tree Node. There are several conditions why a tree node or checkbox does not
418 // exists:
419 // a) The node has not been created yet (the user has not expanded the tree node yet).
420 // b) The checkbox may be null if condition (a) exists or no 'checkbox' attribute was
421 // specified for the associated dojo.data.item and the attribute 'checkboxAll' is
422 // set to false.
423 // tags:
424 // callback
425 var model = this.model,
426 identity = model.getIdentity(storeItem),
427 nodes = this._itemNodesMap[identity];
428
429 // As of dijit.Tree 1.4 multiple references (parents) are supported, therefore we may have
430 // to update multiple nodes which are all associated with the same dojo.data.item.
431 if( nodes ) {
432 dojo.forEach( nodes, function(node) {
433 if( node._checkbox != null ) {
434 node._checkbox.attr('checked', this.model.getCheckboxState( storeItem ));
435 }
436 }, this );
437 }
438 },
439
440 postCreate: function() {
441 // summary:
442 // Handle any specifics related to the tree and model after the instanciation of the Tree.
443 // description:
444 // Validate if we have a 'write' store first. Subscribe to the 'onCheckboxChange' event
445 // (triggered by the model) and kickoff the initial checkbox data validation.
446 //
447 var store = this.model.store;
448 if(!store.getFeatures()['dojo.data.api.Write']){
449 throw new Error("lib.CheckboxTree: store must support dojo.data.Write");
450 }
451 this.connect(this.model, "onCheckboxChange", "_onCheckboxChange");
452 this.model.validateData( this.model.root, this.model );
453 this.inherited(arguments);
454 },
455
456 _createTreeNode: function( args ) {
457 // summary:
458 // Create a new CheckboxTreeNode instance.
459 // description:
460 // Create a new CheckboxTreeNode instance.
461 return new lib._CheckBoxTreeNode(args);
462 }
463
464 });