summaryrefslogtreecommitdiff
path: root/includes/js/dijit/_tree/dndSelector.js
blob: b19210f8470a65796e78c63a7c65909b3c176734 (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
if(!dojo._hasResource["dijit._tree.dndSelector"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
dojo._hasResource["dijit._tree.dndSelector"] = true;
dojo.provide("dijit._tree.dndSelector");
dojo.require("dojo.dnd.common");
dojo.require("dijit._tree.dndContainer");

dojo.declare("dijit._tree.dndSelector",
	dijit._tree.dndContainer,
	{
		constructor: function(tree, params){
			this.selection={};
			this.anchor = null;
			this.simpleSelection=false;
		
			this.events.push(
				dojo.connect(this.tree.domNode, "onmousedown", this,"onMouseDown"),
				dojo.connect(this.tree.domNode, "onmouseup", this,"onMouseUp")
			);
		},
	
		// object attributes (for markup)
		singular: false,	// is singular property
	
		// methods
		getSelectedItems: function(){
			var selectedItems = []
			for (var i in this.selection){
				selectedItems.push(dijit.getEnclosingWidget(this.selection[i]).item);
			}
			return selectedItems;
		},

		getSelectedNodes: function(){
			return this.selection;
		},

		selectNone: function(){
			// summary: unselects all items
			return this._removeSelection()._removeAnchor();	// self
		},

		insertItems: function(item, parent){
			// summary: inserts new data items (see Container's insertNodes method for details)
			
			//we actually need to add things to the store here instead of adding noes to the tree directly		
		},

		destroy: function(){
			// summary: prepares the object to be garbage-collected
			dojo.dnd.Selector.superclass.destroy.call(this);
			this.selection = this.anchor = null;
		},

		// mouse events
		onMouseDown: function(e){
			// summary: event processor for onmousedown
			// e: Event: mouse event
			if(!this.current){ return; }

			var item = dijit.getEnclosingWidget(this.current).item
			var id = this.tree.model.getIdentity(item);

			if (!this.current.id) {
				this.current.id=id;
			}

			if (!this.current.type) {
				this.current.type="data";
			}

			if(!this.singular && !dojo.dnd.getCopyKeyState(e) && !e.shiftKey && (this.current.id in this.selection)){
				this.simpleSelection = true;
				dojo.stopEvent(e);
				return;
			}

			if(this.singular){
				if(this.anchor == this.current){
					if(dojo.dnd.getCopyKeyState(e)){
						this.selectNone();
					}
				}else{
					this.selectNone();
					this.anchor = this.current;
					this._addItemClass(this.anchor, "Anchor");

					this.selection[this.current.id] = this.current;
				}
			}else{
				if(!this.singular && e.shiftKey){	
					if (dojo.dnd.getCopyKeyState(e)){
						//TODO add range to selection
					}else{
						//TODO select new range from anchor 
					}
				}else{
					if(dojo.dnd.getCopyKeyState(e)){
						if(this.anchor == this.current){
							delete this.selection[this.anchor.id];
							this._removeAnchor();
						}else{
							if(this.current.id in this.selection){
								this._removeItemClass(this.current, "Selected");
								delete this.selection[this.current.id];
							}else{
								if(this.anchor){
									this._removeItemClass(this.anchor, "Anchor");
									this._addItemClass(this.anchor, "Selected");
								}
								this.anchor = this.current;
								this._addItemClass(this.current, "Anchor");
								this.selection[this.current.id] = this.current;
							}
						}
					}else{
						var item = dijit.getEnclosingWidget(this.current).item
						var id = this.tree.model.getIdentity(item);
						if(!(id in this.selection)){
							this.selectNone();
							this.anchor = this.current;
							this._addItemClass(this.current, "Anchor");
							this.selection[id] = this.current;
						}
					}
				}
			}

			dojo.stopEvent(e);
		},

		onMouseMove: function() {

		},

		onOverEvent: function() {
			this.onmousemoveEvent = dojo.connect(this.node, "onmousemove", this, "onMouseMove");
		},

		onMouseUp: function(e){
			// summary: event processor for onmouseup
			// e: Event: mouse event
			if(!this.simpleSelection){ return; }
			this.simpleSelection = false;
			this.selectNone();
			if(this.current){
				this.anchor = this.current;
				this._addItemClass(this.anchor, "Anchor");
				this.selection[this.current.id] = this.current;
			}
		},
		_removeSelection: function(){
			// summary: unselects all items
			var e = dojo.dnd._empty;
			for(var i in this.selection){
				if(i in e){ continue; }
				var node = dojo.byId(i);
				if(node){ this._removeItemClass(node, "Selected"); }
			}
			this.selection = {};
			return this;	// self
		},
		_removeAnchor: function(){
			if(this.anchor){
				this._removeItemClass(this.anchor, "Anchor");
				this.anchor = null;
			}
			return this;	// self
		}
});

}