1 | define(["dojo/_base/declare", "dojo/_base/array", "dojo/_base/lang", "dojo/Stateful"], |
---|
2 | function(declare, arr, lang, Stateful){ |
---|
3 | |
---|
4 | return declare("dojox.widget.Selection", Stateful, { |
---|
5 | // summary: |
---|
6 | // Base class for widgets that manage a list of selected data items. |
---|
7 | |
---|
8 | constructor: function(){ |
---|
9 | this.selectedItems = []; |
---|
10 | }, |
---|
11 | |
---|
12 | // selectionMode: String |
---|
13 | // Valid values are: |
---|
14 | // |
---|
15 | // 1. "none": No selection can be done. |
---|
16 | // 2. "single": Only one item can be selected at a time. |
---|
17 | // 3. "multiple": Several item can be selected using the control key modifier. |
---|
18 | // Default value is "single". |
---|
19 | selectionMode: "single", |
---|
20 | |
---|
21 | _setSelectionModeAttr: function(value){ |
---|
22 | if(value != "none" && value != "single" && value != "multiple"){ |
---|
23 | value = "single"; //default value |
---|
24 | } |
---|
25 | if(value != this.selectionMode){ |
---|
26 | this.selectionMode = value; |
---|
27 | if(value == "none"){ |
---|
28 | this.set("selectedItems", null); |
---|
29 | }else if(value == "single"){ |
---|
30 | this.set("selectedItem", this.selectedItem); // null or last selected item |
---|
31 | } |
---|
32 | } |
---|
33 | }, |
---|
34 | |
---|
35 | // selectedItem: Object |
---|
36 | // In single selection mode, the selected item or in multiple selection mode the last selected item. |
---|
37 | // Warning: Do not use this property directly, make sure to call set() or get() methods. |
---|
38 | selectedItem: null, |
---|
39 | |
---|
40 | _setSelectedItemAttr: function(value){ |
---|
41 | if(this.selectedItem != value){ |
---|
42 | this._set("selectedItem", value); |
---|
43 | this.set("selectedItems", value ? [value] : null); |
---|
44 | } |
---|
45 | }, |
---|
46 | |
---|
47 | // selectedItems: Object[] |
---|
48 | // The list of selected items. |
---|
49 | // Warning: Do not use this property directly, make sure to call set() or get() methods. |
---|
50 | selectedItems: null, |
---|
51 | |
---|
52 | _setSelectedItemsAttr: function(value){ |
---|
53 | var oldSelectedItems = this.selectedItems; |
---|
54 | |
---|
55 | this.selectedItems = value; |
---|
56 | this.selectedItem = null; |
---|
57 | |
---|
58 | if(oldSelectedItems != null && oldSelectedItems.length>0){ |
---|
59 | this.updateRenderers(oldSelectedItems, true); |
---|
60 | } |
---|
61 | if(this.selectedItems && this.selectedItems.length>0){ |
---|
62 | this.selectedItem = this.selectedItems[0]; |
---|
63 | this.updateRenderers(this.selectedItems, true); |
---|
64 | } |
---|
65 | }, |
---|
66 | |
---|
67 | _getSelectedItemsAttr: function(){ |
---|
68 | return this.selectedItems == null ? [] : this.selectedItems.concat(); |
---|
69 | }, |
---|
70 | |
---|
71 | isItemSelected: function(item){ |
---|
72 | // summary: |
---|
73 | // Returns wether an item is selected or not. |
---|
74 | // item: Object |
---|
75 | // The item to test the selection for. |
---|
76 | if(this.selectedItems == null || this.selectedItems.length== 0){ |
---|
77 | return false; |
---|
78 | } |
---|
79 | |
---|
80 | return arr.some(this.selectedItems, lang.hitch(this, function(sitem){ |
---|
81 | return this.getIdentity(sitem) == this.getIdentity(item); |
---|
82 | })); |
---|
83 | }, |
---|
84 | |
---|
85 | getIdentity: function(item){ |
---|
86 | // summary: |
---|
87 | // This function must be implemented to return the id of a item. |
---|
88 | // item: Object |
---|
89 | // The item to query the identity for. |
---|
90 | }, |
---|
91 | |
---|
92 | setItemSelected: function(item, value){ |
---|
93 | // summary: |
---|
94 | // Change the selection state of an item. |
---|
95 | // item: Object |
---|
96 | // The item to change the selection state for. |
---|
97 | // value: Boolean |
---|
98 | // True to select the item, false to deselect it. |
---|
99 | |
---|
100 | if(this.selectionMode == "none" || item == null){ |
---|
101 | return; |
---|
102 | } |
---|
103 | |
---|
104 | // copy is returned |
---|
105 | var sel = this.get("selectedItems"); |
---|
106 | var old = this.get("selectedItems"); |
---|
107 | |
---|
108 | if(this.selectionMode == "single"){ |
---|
109 | if(value){ |
---|
110 | this.set("selectedItem", item); |
---|
111 | }else if(this.isItemSelected(item)){ |
---|
112 | this.set("selectedItems", null); |
---|
113 | } |
---|
114 | }else{ // multiple |
---|
115 | if(value){ |
---|
116 | if(this.isItemSelected(item)){ |
---|
117 | return; // already selected |
---|
118 | } |
---|
119 | if(sel == null){ |
---|
120 | sel = [item]; |
---|
121 | }else{ |
---|
122 | sel.unshift(item); |
---|
123 | } |
---|
124 | this.set("selectedItems", sel); |
---|
125 | }else{ |
---|
126 | var res = arr.filter(sel, function(sitem){ |
---|
127 | return sitem.id != item.id; |
---|
128 | }); |
---|
129 | if(res == null || res.length == sel.length){ |
---|
130 | return; // already not selected |
---|
131 | } |
---|
132 | this.set("selectedItems", res); |
---|
133 | } |
---|
134 | } |
---|
135 | }, |
---|
136 | |
---|
137 | selectFromEvent: function(e, item, renderer, dispatch){ |
---|
138 | // summary: |
---|
139 | // Applies selection triggered by an user interaction |
---|
140 | // e: Event |
---|
141 | // The source event of the user interaction. |
---|
142 | // item: Object |
---|
143 | // The render item that has been selected/deselected. |
---|
144 | // renderer: Object |
---|
145 | // The visual renderer of the selected/deselected item. |
---|
146 | // dispatch: Boolean |
---|
147 | // Whether an event must be dispatched or not. |
---|
148 | // returns: Boolean |
---|
149 | // Returns true if the selection has changed and false otherwise. |
---|
150 | // tags: |
---|
151 | // protected |
---|
152 | |
---|
153 | if(this.selectionMode == "none"){ |
---|
154 | return false; |
---|
155 | } |
---|
156 | |
---|
157 | var changed; |
---|
158 | var oldSelectedItem = this.get("selectedItem"); |
---|
159 | var selected = item ? this.isItemSelected(item): false; |
---|
160 | |
---|
161 | if(item == null){ |
---|
162 | if(!e.ctrlKey && this.selectedItem != null){ |
---|
163 | this.set("selectedItem", null); |
---|
164 | changed = true; |
---|
165 | } |
---|
166 | }else if(this.selectionMode == "multiple"){ |
---|
167 | if(e.ctrlKey){ |
---|
168 | this.setItemSelected(item, !selected); |
---|
169 | changed = true; |
---|
170 | }else{ |
---|
171 | this.set("selectedItem", item); |
---|
172 | changed = true; |
---|
173 | } |
---|
174 | }else{ // single |
---|
175 | if(e.ctrlKey){ |
---|
176 | //if the object is selected deselects it. |
---|
177 | this.set("selectedItem", selected ? null : item); |
---|
178 | changed = true; |
---|
179 | }else{ |
---|
180 | if(!selected){ |
---|
181 | this.set("selectedItem", item); |
---|
182 | changed = true; |
---|
183 | } |
---|
184 | } |
---|
185 | } |
---|
186 | |
---|
187 | if(dispatch && changed){ |
---|
188 | this.dispatchChange(oldSelectedItem, this.get("selectedItem"), renderer, e); |
---|
189 | } |
---|
190 | |
---|
191 | return changed; |
---|
192 | }, |
---|
193 | |
---|
194 | dispatchChange: function(oldSelectedItem, newSelectedItem, renderer, triggerEvent){ |
---|
195 | // summary: |
---|
196 | // Dispatch a selection change event. |
---|
197 | // oldSelectedItem: Object |
---|
198 | // The previously selectedItem. |
---|
199 | // newSelectedItem: Object |
---|
200 | // The new selectedItem. |
---|
201 | // renderer: Object |
---|
202 | // The visual renderer of the selected/deselected item. |
---|
203 | // triggerEvent: Event |
---|
204 | // The event that lead to the selection of the item. |
---|
205 | this.onChange({ |
---|
206 | oldValue: oldSelectedItem, |
---|
207 | newValue: newSelectedItem, |
---|
208 | renderer: renderer, |
---|
209 | triggerEvent: triggerEvent |
---|
210 | }); |
---|
211 | }, |
---|
212 | |
---|
213 | onChange: function(){ |
---|
214 | // summary: |
---|
215 | // Called when the selection changed. |
---|
216 | // tags: |
---|
217 | // callback |
---|
218 | } |
---|
219 | }); |
---|
220 | }); |
---|