Debian Codegraph @

dojox/widget/Selection.js

raw symbols SCIP
1 define(["dojo/_base/declare", "dojo/_base/array", "dojo/sniff", "dojo/_base/lang", "dojo/Stateful"],
2 function(declare, arr, has, 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 whether 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
107 if(this.selectionMode == "single"){
108 if(value){
109 this.set("selectedItem", item);
110 }else if(this.isItemSelected(item)){
111 this.set("selectedItems", null);
112 }
113 }else{ // multiple
114 if(value){
115 if(this.isItemSelected(item)){
116 return; // already selected
117 }
118 if(sel == null){
119 sel = [item];
120 }else{
121 sel.unshift(item);
122 }
123 this.set("selectedItems", sel);
124 }else{
125 var res = arr.filter(sel, function(sitem){
126 return sitem.id != item.id;
127 });
128 if(res == null || res.length == sel.length){
129 return; // already not selected
130 }
131 this.set("selectedItems", res);
132 }
133 }
134 },
135
136 selectFromEvent: function(e, item, renderer, dispatch){
137 // summary:
138 // Applies selection triggered by an user interaction
139 // e: Event
140 // The source event of the user interaction.
141 // item: Object
142 // The render item that has been selected/deselected.
143 // renderer: Object
144 // The visual renderer of the selected/deselected item.
145 // dispatch: Boolean
146 // Whether an event must be dispatched or not.
147 // returns: Boolean
148 // Returns true if the selection has changed and false otherwise.
149 // tags:
150 // protected
151
152 if(this.selectionMode == "none"){
153 return false;
154 }
155
156 var changed;
157 var oldSelectedItem = this.get("selectedItem");
158 var selected = item ? this.isItemSelected(item): false;
159 var evtMultiSelectKey = has("mac") ? e.metaKey : e.ctrlKey;
160 if(item == null){
161 if(!evtMultiSelectKey && this.selectedItem != null){
162 this.set("selectedItem", null);
163 changed = true;
164 }
165 }else if(this.selectionMode == "multiple"){
166 if(evtMultiSelectKey){
167 this.setItemSelected(item, !selected);
168 changed = true;
169 }else{
170 this.set("selectedItem", item);
171 changed = true;
172 }
173 }else{ // single
174 if(evtMultiSelectKey){
175 //if the object is selected deselects it.
176 this.set("selectedItem", selected ? null : item);
177 changed = true;
178 }else{
179 if(!selected){
180 this.set("selectedItem", item);
181 changed = true;
182 }
183 }
184 }
185
186 if(dispatch && changed){
187 this.dispatchChange(oldSelectedItem, this.get("selectedItem"), renderer, e);
188 }
189
190 return changed;
191 },
192
193 dispatchChange: function(oldSelectedItem, newSelectedItem, renderer, triggerEvent){
194 // summary:
195 // Dispatch a selection change event.
196 // oldSelectedItem: Object
197 // The previously selectedItem.
198 // newSelectedItem: Object
199 // The new selectedItem.
200 // renderer: Object
201 // The visual renderer of the selected/deselected item.
202 // triggerEvent: Event
203 // The event that lead to the selection of the item.
204 this.onChange({
205 oldValue: oldSelectedItem,
206 newValue: newSelectedItem,
207 renderer: renderer,
208 triggerEvent: triggerEvent
209 });
210 },
211
212 onChange: function(){
213 // summary:
214 // Called when the selection changed.
215 // tags:
216 // callback
217 }
218 });
219 });