1 | define([ |
---|
2 | "./LineWithActionsWidget", |
---|
3 | "dijit/_Container", |
---|
4 | "dijit/_TemplatedMixin", |
---|
5 | "dijit/_WidgetBase", |
---|
6 | "dijit/_WidgetsInTemplateMixin", |
---|
7 | "dijit/registry", |
---|
8 | "dojo/_base/array", |
---|
9 | "dojo/_base/declare", |
---|
10 | "dojo/_base/event", |
---|
11 | "dojo/_base/lang", |
---|
12 | "dojo/dnd/Source", |
---|
13 | "dojo/dom-class", |
---|
14 | "dojo/dom-construct", |
---|
15 | "dojo/fx", |
---|
16 | "dojo/query", |
---|
17 | "dojo/text!./templates/Selector.html" |
---|
18 | ], function(LineWithActionsWidget, _Container, _TemplatedMixin, _WidgetBase, _WidgetsInTemplateMixin, registry, baseArray, declare, event, lang, Source, domClass, domConstruct, fx, query, templateString) { |
---|
19 | |
---|
20 | function get(selector, item) { |
---|
21 | if ( lang.isFunction(selector) ) { |
---|
22 | return selector(item); |
---|
23 | } else { |
---|
24 | return item[selector || 'title']; |
---|
25 | } |
---|
26 | } |
---|
27 | |
---|
28 | return declare([_WidgetBase,_TemplatedMixin,_WidgetsInTemplateMixin,_Container],{ |
---|
29 | templateString: templateString, |
---|
30 | baseClass: 'rftSelector', |
---|
31 | |
---|
32 | title: "", |
---|
33 | selectedActions: null, |
---|
34 | itemActions: null, |
---|
35 | itemTitle: 'title', |
---|
36 | // main selector action: Object |
---|
37 | // title: |
---|
38 | // description: |
---|
39 | // icon: |
---|
40 | dndType: null, |
---|
41 | |
---|
42 | _source: null, |
---|
43 | _folded: true, |
---|
44 | _titleLine: null, |
---|
45 | _selectorLine: null, |
---|
46 | _selectedItem: null, |
---|
47 | |
---|
48 | startup: function() { |
---|
49 | if ( this._started ){ return; } |
---|
50 | this.inherited(arguments); |
---|
51 | domClass.add(this.selectedColorNode, "pending"); |
---|
52 | |
---|
53 | this._createTitleLine(); |
---|
54 | this._createSelectorLine(); |
---|
55 | if (this.dndType) { |
---|
56 | this._createSource(); |
---|
57 | } |
---|
58 | |
---|
59 | fx.wipeOut({ |
---|
60 | node: this.optionsNode |
---|
61 | }).play(); |
---|
62 | }, |
---|
63 | _createTitleLine: function() { |
---|
64 | var actions = {}; |
---|
65 | var action = null; |
---|
66 | var userActions = |
---|
67 | lang.isFunction(this.selectedActions) ? |
---|
68 | this.selectedActions() : |
---|
69 | this.selectedActions; |
---|
70 | if ( userActions ) { |
---|
71 | for (var actionName in userActions) { |
---|
72 | if ( userActions.hasOwnProperty(actionName) ) { |
---|
73 | action = userActions[actionName]; |
---|
74 | actions[actionName] = { |
---|
75 | callback: action.callback && |
---|
76 | lang.hitch(this,this._onSelectedAction, |
---|
77 | action.callback), |
---|
78 | properties: { |
---|
79 | blockButton: true, |
---|
80 | label: action.title || actionName, |
---|
81 | icon: action.icon, |
---|
82 | tooltip: action.description |
---|
83 | } |
---|
84 | |
---|
85 | }; |
---|
86 | } |
---|
87 | } |
---|
88 | } |
---|
89 | |
---|
90 | this._titleLine = new LineWithActionsWidget({ |
---|
91 | title: this.title, |
---|
92 | actions: actions |
---|
93 | },this.titleNode); |
---|
94 | this._titleLine.startup(); |
---|
95 | }, |
---|
96 | _createSelectorLine: function() { |
---|
97 | this._selectorLine = new LineWithActionsWidget({ |
---|
98 | title: 'None', |
---|
99 | actions: { |
---|
100 | "Toggle dropdown" : { |
---|
101 | callback: lang.hitch(this, this.onToggle), |
---|
102 | properties: { |
---|
103 | blockButton: true, |
---|
104 | showLabel: false, |
---|
105 | icon: "HalfArrowDown" |
---|
106 | } |
---|
107 | } |
---|
108 | } |
---|
109 | },this.selectedItemNode); |
---|
110 | this._selectorLine.startup(); |
---|
111 | this.own(this._selectorLine.on('click',lang.hitch(this, 'onToggle'))); |
---|
112 | }, |
---|
113 | _createSource: function() { |
---|
114 | this._source = new Source(this.optionsNode, { |
---|
115 | singular: true, |
---|
116 | delay: 5, |
---|
117 | selfAccept: false, |
---|
118 | copyOnly: true, |
---|
119 | withHandles: this.withHandles, |
---|
120 | creator: lang.hitch(this, "_createNode") |
---|
121 | }); |
---|
122 | }, |
---|
123 | _onSelect: function(item, widget) { |
---|
124 | this._selectedItem = item; |
---|
125 | this.onToggle(); |
---|
126 | this._selectorLine.set("title", get(this.itemTitle,item)); |
---|
127 | baseArray.forEach(this.optionsNode.childNodes, function(node){ |
---|
128 | var line = registry.byNode(node); |
---|
129 | if (line) { |
---|
130 | if (line === widget) { |
---|
131 | domClass.add(line.domNode, "inheritBgColor light"); |
---|
132 | } else { |
---|
133 | domClass.remove(line.domNode, "inheritBgColor light"); |
---|
134 | } |
---|
135 | } |
---|
136 | }, this); |
---|
137 | this.onSelect(item); |
---|
138 | }, |
---|
139 | _onSelectedAction: function(callback) { |
---|
140 | if (this._selectedItem && callback) { |
---|
141 | callback(this._selectedItem,this); |
---|
142 | } |
---|
143 | }, |
---|
144 | |
---|
145 | onToggle: function(evt) { |
---|
146 | if (this._folded) { |
---|
147 | var downArrowIcon = query(".rftBlockButton .rftIconHalfArrowDown", this._selectorLine.buttonsNode)[0]; |
---|
148 | if (downArrowIcon){ |
---|
149 | domClass.replace(downArrowIcon, "rftIconHalfArrowUp", "rftIconHalfArrowDown"); |
---|
150 | } |
---|
151 | fx.wipeIn({ |
---|
152 | node: this.optionsNode |
---|
153 | }).play(); |
---|
154 | this._folded = false; |
---|
155 | } else { |
---|
156 | var upArrowIcon = query(".rftBlockButton .rftIconHalfArrowUp", this._selectorLine.buttonsNode)[0]; |
---|
157 | if (upArrowIcon){ |
---|
158 | domClass.replace(upArrowIcon, "rftIconHalfArrowDown", "rftIconHalfArrowUp"); |
---|
159 | } |
---|
160 | fx.wipeOut({ |
---|
161 | node: this.optionsNode |
---|
162 | }).play(); |
---|
163 | this._folded = true; |
---|
164 | } |
---|
165 | if ( evt ) { event.stop(evt); } |
---|
166 | return false; |
---|
167 | }, |
---|
168 | |
---|
169 | addItem: function(item) { |
---|
170 | if ( this._source ) { |
---|
171 | this._source.insertNodes(false,[item]); |
---|
172 | } else { |
---|
173 | var node = this._createLineNode(item); |
---|
174 | domConstruct.place(node, this.optionsNode, "last"); |
---|
175 | } |
---|
176 | }, |
---|
177 | removeItem: function(item) { |
---|
178 | var nodes = this._source ? |
---|
179 | this._source.getAllNodes() : |
---|
180 | this.optionsNode.childNodes; |
---|
181 | baseArray.forEach(nodes, function(node) { |
---|
182 | var widget = registry.byNode(node); |
---|
183 | if ( widget && this.itemEquals ? |
---|
184 | this.itemEquals(widget.__item,item) : |
---|
185 | widget.__item === item ) { |
---|
186 | widget.destroyRecursive(); |
---|
187 | if ( this._source ) { this._source.delItem(widget.id); } |
---|
188 | } |
---|
189 | }, this); |
---|
190 | }, |
---|
191 | _createNode: function(item, hint) { |
---|
192 | var node = hint === "avatar" ? |
---|
193 | this._createAvatarNode(item) : this._createLineNode(item); |
---|
194 | return node && { |
---|
195 | node: node, |
---|
196 | data: lang.clone(item), |
---|
197 | type: [this.dndType] |
---|
198 | }; |
---|
199 | }, |
---|
200 | _createAvatarNode: function(item) { |
---|
201 | return domConstruct.create("div",{ |
---|
202 | 'class': 'dragAvatar', |
---|
203 | innerHTML: get(this.itemTitle,item) |
---|
204 | }); |
---|
205 | }, |
---|
206 | _createLineNode: function(item) { |
---|
207 | var actions = {}; |
---|
208 | var userActions = |
---|
209 | lang.isFunction(this.itemActions) ? |
---|
210 | this.itemActions(item) : |
---|
211 | this.itemActions; |
---|
212 | if (userActions) { |
---|
213 | for (var actionName in userActions) { |
---|
214 | if ( userActions.hasOwnProperty(actionName) ) { |
---|
215 | var action = userActions[actionName]; |
---|
216 | actions[actionName] = { |
---|
217 | callback: action.callback && |
---|
218 | lang.partial(action.callback,item,this), |
---|
219 | properties: { |
---|
220 | blockButton: false, |
---|
221 | showLabel: false, |
---|
222 | icon: action.icon + " black", |
---|
223 | tooltip: action.description |
---|
224 | } |
---|
225 | }; |
---|
226 | } |
---|
227 | } |
---|
228 | } |
---|
229 | var w = new LineWithActionsWidget({ |
---|
230 | title: get(this.itemTitle,item), |
---|
231 | actions: actions, |
---|
232 | __item: item |
---|
233 | }); |
---|
234 | w.startup(); |
---|
235 | this.own(w.on("click", lang.hitch(this, '_onSelect', item, w))); |
---|
236 | return w.domNode; |
---|
237 | }, |
---|
238 | |
---|
239 | onSelect: function(/*item*/) {}, |
---|
240 | |
---|
241 | destroy: function() { |
---|
242 | if ( this._source ) { this._source.destroy(); } |
---|
243 | this.inherited(arguments); |
---|
244 | } |
---|
245 | }); |
---|
246 | }); |
---|