summaryrefslogtreecommitdiff
path: root/lib/CheckBoxTree.js
blob: 2b0c71642b8010474cd51fe422bf629ad981f798 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
define(["dojo/_base/declare", "dijit/Tree", "lib/_CheckBoxTreeNode" ], function (declare) {

	return declare( "lib.CheckBoxTree", dijit.Tree,
		{

			onNodeChecked: function(/*dojo.data.Item*/ storeItem, /*treeNode*/ treeNode) {
				// summary:
				//		Callback when a checkbox tree node is checked
				// tags:
				//		callback
			},

			onNodeUnchecked: function(/*dojo.data.Item*/ storeItem, /* treeNode */ treeNode) {
				// summary:
				//		Callback when a checkbox tree node is unchecked
				// tags:
				//		callback
			},

			_onClick: function(/*TreeNode*/ nodeWidget, /*Event*/ e) {
				// summary:
				//		Translates click events into commands for the controller to process
				// description:
				//		the _onClick function is called whenever a 'click' is detected. This
				//		instance of _onClick only handles the click events associated with
				//		the checkbox whos DOM name is INPUT.
				//
				var domElement = e.target;

				// Only handle checkbox clicks here
				if(domElement.type != 'checkbox') {
					return this.inherited( arguments );
				}

				this._publish("execute", { item: nodeWidget.item, node: nodeWidget} );
				// Go tell the model to update the checkbox state

				this.model.updateCheckbox( nodeWidget.item, nodeWidget._checkbox.checked );
				// Generate some additional events
				//this.onClick( nodeWidget.item, nodeWidget, e );
				if(nodeWidget._checkbox.checked) {
					this.onNodeChecked( nodeWidget.item, nodeWidget);
				} else {
					this.onNodeUnchecked( nodeWidget.item, nodeWidget);
				}
				this.focusNode(nodeWidget);
			},

			_onCheckboxChange: function(/*dojo.data.Item*/ storeItem ) {
				// summary:
				//		Processes notification of a change to a checkbox state (triggered by the model).
				// description:
				//		Whenever the model changes the state of a checkbox in the dojo.data.store it will
				//		trigger the 'onCheckboxChange' event allowing the Tree to make the same changes
				//		on the tree Node. There are several conditions why a tree node or checkbox does not
				//		exists:
				//		a) 	The node has not been created yet (the user has not expanded the tree node yet).
				//		b) 	The checkbox may be null if condition (a) exists or no 'checkbox' attribute was
				//			specified for the associated dojo.data.item and the attribute 'checkboxAll' is
				//			set to false.
				// tags:
				//		callback
				var model 	 = this.model,
					identity = model.getIdentity(storeItem),
					nodes 	 = this._itemNodesMap[identity];

				// As of dijit.Tree 1.4 multiple references (parents) are supported, therefore we may have
				// to update multiple nodes which are all associated with the same dojo.data.item.
				if( nodes ) {
					dojo.forEach( nodes, function(node) {
						if( node._checkbox != null ) {
							node._checkbox.attr('checked', this.model.getCheckboxState( storeItem ));
						}
					}, this );
				}
			},

			postCreate: function() {
				// summary:
				//		Handle any specifics related to the tree and model after the instanciation of the Tree.
				// description:
				//		Validate if we have a 'write' store first. Subscribe to the 'onCheckboxChange' event
				//		(triggered by the model) and kickoff the initial checkbox data validation.
				//
				var store = this.model.store;
				if(!store.getFeatures()['dojo.data.api.Write']){
					throw new Error("lib.CheckboxTree: store must support dojo.data.Write");
				}
				this.connect(this.model, "onCheckboxChange", "_onCheckboxChange");
				this.model.validateData( this.model.root, this.model );
				this.inherited(arguments);
			},

			_createTreeNode: function( args ) {
				// summary:
				//		Create a new CheckboxTreeNode instance.
				// description:
				//		Create a new CheckboxTreeNode instance.
				return new lib._CheckBoxTreeNode(args);
			}

		});

});