source: Dev/trunk/src/client/qed-client/widgets/Selector.js @ 472

Last change on this file since 472 was 472, checked in by hendrikvanantwerpen, 12 years ago

Fixed deploy script.
Fix for ... in ... complaints by jshint.
Fix a typo.

File size: 7.4 KB
Line 
1define([
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    return declare([_WidgetBase,_TemplatedMixin,_WidgetsInTemplateMixin,_Container],{
21        templateString: templateString,
22        baseClass: 'rftSelector',
23
24        title: "",
25        selectedActions: null,
26        itemActions: null,
27        // main selector action: Object
28        //    title:
29        //    description:
30        //    icon:
31        dndType: null,
32
33        _source: null,
34        _folded: true,
35        _titleLine: null,
36        _selectorLine: null,
37        _selectedItem: null,
38
39        startup: function() {
40            if ( this._started ){ return; }
41            this.inherited(arguments);
42            domClass.add(this.selectedColorNode, "pending");
43
44            this._createTitleLine();
45            this._createSelectorLine();
46            if (this.dndType) {
47                this._createSource();
48            }
49
50            fx.wipeOut({
51                node: this.optionsNode
52            }).play();
53        },
54        _createTitleLine: function() {
55            var actions = {};
56            var action = null;
57            if ( this.selectedActions !== null ) {
58                for (var actionName in this.selectedActions) {
59                    if ( this.selectedActions.hasOwnProperty(actionName) ) {
60                        action = this.selectedActions[actionName];
61                        actions[actionName] = {
62                            callback: action.callback &&
63                                    lang.hitch(this,this._onSelectedAction,
64                                            action.callback),
65                            properties: {
66                                blockButton: true,
67                                label: action.title || actionName,
68                                icon: action.icon,
69                                tooltip: action.description
70                            }
71
72                        };
73                    }
74                }
75            }
76
77            this._titleLine = new LineWithActionsWidget({
78                title: this.title,
79                actions: actions
80            },this.titleNode);
81            this._titleLine.startup();
82        },
83        _createSelectorLine: function() {
84            this._selectorLine = new LineWithActionsWidget({
85                title: 'None',
86                actions: {
87                    "Toggle dropdown" : {
88                        callback: lang.hitch(this, this.onToggle),
89                        properties: {
90                            blockButton: true,
91                            showLabel: false,
92                            icon: "HalfArrowDown"
93                        }
94                    }
95                }
96            },this.selectedItemNode);
97            this._selectorLine.startup();
98            this._selectorLine.on('click',lang.hitch(this, this.onToggle));
99        },
100        _createSource: function() {
101            this._source = new Source(this.optionsNode, {
102                singular: true,
103                delay: 5,
104                selfAccept: false,
105                copyOnly: true,
106                withHandles: this.withHandles,
107                creator: lang.hitch(this, "_createNode")
108            });
109        },
110        _onSelect: function(item, widget) {
111            this._selectedItem = item;
112            this.onToggle();
113            this._selectorLine.set("title", item.title);
114            baseArray.forEach(this.optionsNode.childNodes, function(node){
115                var line = registry.byNode(node);
116                if (line) {
117                    if (line === widget) {
118                        domClass.add(line.domNode, "inheritBgColor light");
119                    } else {
120                        domClass.remove(line.domNode, "inheritBgColor light");
121                    }
122                }
123            }, this);
124            this.onSelect(item);
125        },
126        _onSelectedAction: function(callback) {
127            if (this._selectedItem && callback) {
128                callback(this._selectedItem);
129            }
130        },
131
132        onToggle: function(evt) {
133            if (this._folded) {
134                var downArrowIcon = query(".rftBlockButton .rftIconHalfArrowDown", this._selectorLine.buttonsNode)[0];
135                if (downArrowIcon){
136                    domClass.replace(downArrowIcon, "rftIconHalfArrowUp", "rftIconHalfArrowDown");
137                }
138                fx.wipeIn({
139                    node: this.optionsNode
140                }).play();
141                this._folded = false;
142            } else {
143                var upArrowIcon = query(".rftBlockButton .rftIconHalfArrowUp", this._selectorLine.buttonsNode)[0];
144                if (upArrowIcon){
145                    domClass.replace(upArrowIcon, "rftIconHalfArrowDown", "rftIconHalfArrowUp");
146                }
147                fx.wipeOut({
148                    node: this.optionsNode
149                }).play();
150                this._folded = true;
151            }
152            if ( evt ) { event.stop(evt); }
153            return false;
154        },
155
156        addItem: function(item) {
157            if ( this._source ) {
158                this._source.insertNodes(false,[item]);
159            } else {
160                var node = this._createLineNode(item);
161                domConstruct.place(node, this.optionsNode, "last");
162            }
163        },
164        _createNode: function(item, hint) {
165            var node = hint === "avatar" ?
166                    this._createAvatarNode(item) : this._createLineNode(item);
167            return node && {
168                node: node,
169                data: lang.clone(item),
170                type: [this.dndType]
171            };
172        },
173        _createAvatarNode: function(item) {
174            return domConstruct.create("div",{
175                'class': 'dragAvatar',
176                innerHTML: item.title
177            });
178        },
179        _createLineNode: function(item) {
180            var actions = {};
181            var action;
182            if (this.itemActions) {
183                for (var actionName in this.itemActions) {
184                    if ( this.itemActions.hasOwnProperty(actionName) ) {
185                        action = this.itemActions[actionName];
186                        actions[actionName] = {
187                            callback: action.callback && lang.partial(action.callback,item),
188                            properties: {
189                                blockButton: false,
190                                showLabel: false,
191                                icon: action.icon + " black",
192                                tooltip: action.description
193                            }
194                        };
195                    }
196                }
197            }
198            var w = new LineWithActionsWidget({
199                title: item.title,
200                actions: actions
201            });
202            w.startup();
203            w.on("click", lang.hitch(this, this._onSelect, item, w));
204            return w.domNode;
205        },
206
207        onSelect: function(/*item*/) {}
208    });
209});
Note: See TracBrowser for help on using the repository browser.