1 | define([ |
---|
2 | 'dojo/_base/array', |
---|
3 | 'dojo/_base/declare', |
---|
4 | 'dojo/_base/event', |
---|
5 | 'dojo/_base/lang', |
---|
6 | 'dojo/dom-class', |
---|
7 | 'dojo/fx', |
---|
8 | 'dojo/query', |
---|
9 | 'dijit/_Container', |
---|
10 | 'dijit/_TemplatedMixin', |
---|
11 | 'dijit/_WidgetBase', |
---|
12 | 'dijit/_WidgetsInTemplateMixin', |
---|
13 | 'dijit/registry', |
---|
14 | './LineWithActionsWidget', |
---|
15 | 'dojo/text!./templates/Selector.html' |
---|
16 | ],function( |
---|
17 | baseArray, |
---|
18 | declare, |
---|
19 | event, |
---|
20 | lang, |
---|
21 | domClass, |
---|
22 | fx, |
---|
23 | query, |
---|
24 | _Container, |
---|
25 | _TemplatedMixin, |
---|
26 | _WidgetBase, |
---|
27 | _WidgetsInTemplateMixin, |
---|
28 | registry, |
---|
29 | LineWithActionsWidget, |
---|
30 | templateString |
---|
31 | ){ |
---|
32 | return declare([_WidgetBase,_TemplatedMixin,_WidgetsInTemplateMixin,_Container],{ |
---|
33 | templateString: templateString, |
---|
34 | baseClass: 'rftSelector', |
---|
35 | |
---|
36 | title: "", |
---|
37 | selectedActions: null, |
---|
38 | itemActions: null, |
---|
39 | // main selector action: Object |
---|
40 | // title: |
---|
41 | // description: |
---|
42 | // icon: |
---|
43 | |
---|
44 | _folded: true, |
---|
45 | _titleLine: null, |
---|
46 | _selectorLine: null, |
---|
47 | _selectedItem: null, |
---|
48 | |
---|
49 | startup: function() { |
---|
50 | if ( this._started ){ return; } |
---|
51 | this.inherited(arguments); |
---|
52 | domClass.add(this.selectedColorNode, "pending"); |
---|
53 | |
---|
54 | this._createTitleLine(); |
---|
55 | this._createSelectorLine(); |
---|
56 | |
---|
57 | fx.wipeOut({ |
---|
58 | node: this.optionsNode |
---|
59 | }).play(); |
---|
60 | }, |
---|
61 | _createTitleLine: function() { |
---|
62 | var actions = {}; |
---|
63 | var action = null; |
---|
64 | if ( this.selectedActions !== null ) { |
---|
65 | for (var actionName in this.selectedActions) { |
---|
66 | action = this.selectedActions[actionName]; |
---|
67 | actions[actionName] = { |
---|
68 | callback: action.callback && |
---|
69 | lang.hitch(this,this._onSelectedAction, |
---|
70 | action.callback), |
---|
71 | properties: { |
---|
72 | blockButton: true, |
---|
73 | label: action.title || actionName, |
---|
74 | icon: action.icon, |
---|
75 | tooltip: action.description |
---|
76 | } |
---|
77 | |
---|
78 | }; |
---|
79 | } |
---|
80 | } |
---|
81 | |
---|
82 | this._titleLine = new LineWithActionsWidget({ |
---|
83 | title: this.title, |
---|
84 | actions: actions |
---|
85 | },this.titleNode); |
---|
86 | this._titleLine.startup(); |
---|
87 | }, |
---|
88 | _createSelectorLine: function() { |
---|
89 | this._selectorLine = new LineWithActionsWidget({ |
---|
90 | title: 'None', |
---|
91 | actions: { |
---|
92 | "Toggle dropdown" : { |
---|
93 | callback: lang.hitch(this, this.onToggle), |
---|
94 | properties: { |
---|
95 | blockButton: true, |
---|
96 | showLabel: false, |
---|
97 | icon: "HalfArrowDown" |
---|
98 | } |
---|
99 | } |
---|
100 | } |
---|
101 | },this.selectedItemNode); |
---|
102 | this._selectorLine.startup(); |
---|
103 | this._selectorLine.on('click',lang.hitch(this, this.onToggle)); |
---|
104 | }, |
---|
105 | _onSelect: function(item, widget) { |
---|
106 | this._selectedItem = item; |
---|
107 | this.onToggle(); |
---|
108 | this._selectorLine.set("title", item.title); |
---|
109 | baseArray.forEach(this.optionsNode.childNodes, function(node){ |
---|
110 | var line = registry.byNode(node); |
---|
111 | if (line) { |
---|
112 | if (line === widget) { |
---|
113 | domClass.add(line.domNode, "inheritBgColor light"); |
---|
114 | } else { |
---|
115 | domClass.remove(line.domNode, "inheritBgColor light"); |
---|
116 | } |
---|
117 | } |
---|
118 | }, this); |
---|
119 | this.onSelect(item); |
---|
120 | }, |
---|
121 | _onSelectedAction: function(callback) { |
---|
122 | if (this._selectedItem && callback) { |
---|
123 | callback(this._selectedItem); |
---|
124 | } |
---|
125 | }, |
---|
126 | |
---|
127 | onToggle: function(evt) { |
---|
128 | if (this._folded) { |
---|
129 | var downArrowIcon = query(".rftBlockButton .rftIconHalfArrowDown", this._selectorLine.buttonsNode)[0]; |
---|
130 | if (downArrowIcon){ |
---|
131 | domClass.replace(downArrowIcon, "rftIconHalfArrowUp", "rftIconHalfArrowDown"); |
---|
132 | } |
---|
133 | fx.wipeIn({ |
---|
134 | node: this.optionsNode |
---|
135 | }).play(); |
---|
136 | this._folded = false; |
---|
137 | } else { |
---|
138 | var upArrowIcon = query(".rftBlockButton .rftIconHalfArrowUp", this._selectorLine.buttonsNode)[0]; |
---|
139 | if (upArrowIcon){ |
---|
140 | domClass.replace(upArrowIcon, "rftIconHalfArrowDown", "rftIconHalfArrowUp"); |
---|
141 | } |
---|
142 | fx.wipeOut({ |
---|
143 | node: this.optionsNode |
---|
144 | }).play(); |
---|
145 | this._folded = true; |
---|
146 | } |
---|
147 | if ( evt ) { event.stop(evt); } |
---|
148 | return false; |
---|
149 | }, |
---|
150 | |
---|
151 | addItem: function(item,displayTitle) { |
---|
152 | var actions = {}; |
---|
153 | var action; |
---|
154 | if (this.itemActions) { |
---|
155 | for (var actionName in this.itemActions) { |
---|
156 | action = this.itemActions[actionName]; |
---|
157 | actions[actionName] = { |
---|
158 | callback: action.callback && lang.partial(action.callback,item), |
---|
159 | properties: { |
---|
160 | blockButton: false, |
---|
161 | showLabel: false, |
---|
162 | icon: action.icon + " black", |
---|
163 | tooltip: action.description |
---|
164 | } |
---|
165 | }; |
---|
166 | } |
---|
167 | } |
---|
168 | var w = new LineWithActionsWidget({ |
---|
169 | title: displayTitle || item.title, |
---|
170 | actions: actions |
---|
171 | }).placeAt(this.optionsNode); |
---|
172 | w.startup(); |
---|
173 | w.on("click", lang.hitch(this, this._onSelect, item, w)); |
---|
174 | }, |
---|
175 | |
---|
176 | onSelect: function(/*item*/) {} |
---|
177 | }); |
---|
178 | }); |
---|