CheckBoxTree.js 17 KB

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