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

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

Quick hack to allow responding despite not being authenticated. Something like tokes need to be added.

File size: 7.7 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    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            if ( this.selectedActions !== null ) {
67                for (var actionName in this.selectedActions) {
68                    if ( this.selectedActions.hasOwnProperty(actionName) ) {
69                        action = this.selectedActions[actionName];
70                        actions[actionName] = {
71                            callback: action.callback &&
72                                    lang.hitch(this,this._onSelectedAction,
73                                            action.callback),
74                            properties: {
75                                blockButton: true,
76                                label: action.title || actionName,
77                                icon: action.icon,
78                                tooltip: action.description
79                            }
80
81                        };
82                    }
83                }
84            }
85
86            this._titleLine = new LineWithActionsWidget({
87                title: this.title,
88                actions: actions
89            },this.titleNode);
90            this._titleLine.startup();
91        },
92        _createSelectorLine: function() {
93            this._selectorLine = new LineWithActionsWidget({
94                title: 'None',
95                actions: {
96                    "Toggle dropdown" : {
97                        callback: lang.hitch(this, this.onToggle),
98                        properties: {
99                            blockButton: true,
100                            showLabel: false,
101                            icon: "HalfArrowDown"
102                        }
103                    }
104                }
105            },this.selectedItemNode);
106            this._selectorLine.startup();
107            this._selectorLine.on('click',lang.hitch(this, this.onToggle));
108        },
109        _createSource: function() {
110            this._source = new Source(this.optionsNode, {
111                singular: true,
112                delay: 5,
113                selfAccept: false,
114                copyOnly: true,
115                withHandles: this.withHandles,
116                creator: lang.hitch(this, "_createNode")
117            });
118        },
119        _onSelect: function(item, widget) {
120            this._selectedItem = item;
121            this.onToggle();
122            this._selectorLine.set("title", get(this.itemTitle,item));
123            baseArray.forEach(this.optionsNode.childNodes, function(node){
124                var line = registry.byNode(node);
125                if (line) {
126                    if (line === widget) {
127                        domClass.add(line.domNode, "inheritBgColor light");
128                    } else {
129                        domClass.remove(line.domNode, "inheritBgColor light");
130                    }
131                }
132            }, this);
133            this.onSelect(item);
134        },
135        _onSelectedAction: function(callback) {
136            if (this._selectedItem && callback) {
137                callback(this._selectedItem);
138            }
139        },
140
141        onToggle: function(evt) {
142            if (this._folded) {
143                var downArrowIcon = query(".rftBlockButton .rftIconHalfArrowDown", this._selectorLine.buttonsNode)[0];
144                if (downArrowIcon){
145                    domClass.replace(downArrowIcon, "rftIconHalfArrowUp", "rftIconHalfArrowDown");
146                }
147                fx.wipeIn({
148                    node: this.optionsNode
149                }).play();
150                this._folded = false;
151            } else {
152                var upArrowIcon = query(".rftBlockButton .rftIconHalfArrowUp", this._selectorLine.buttonsNode)[0];
153                if (upArrowIcon){
154                    domClass.replace(upArrowIcon, "rftIconHalfArrowDown", "rftIconHalfArrowUp");
155                }
156                fx.wipeOut({
157                    node: this.optionsNode
158                }).play();
159                this._folded = true;
160            }
161            if ( evt ) { event.stop(evt); }
162            return false;
163        },
164
165        addItem: function(item) {
166            if ( this._source ) {
167                this._source.insertNodes(false,[item]);
168            } else {
169                var node = this._createLineNode(item);
170                domConstruct.place(node, this.optionsNode, "last");
171            }
172        },
173        _createNode: function(item, hint) {
174            var node = hint === "avatar" ?
175                    this._createAvatarNode(item) : this._createLineNode(item);
176            return node && {
177                node: node,
178                data: lang.clone(item),
179                type: [this.dndType]
180            };
181        },
182        _createAvatarNode: function(item) {
183            return domConstruct.create("div",{
184                'class': 'dragAvatar',
185                innerHTML: get(this.itemTitle,item)
186            });
187        },
188        _createLineNode: function(item) {
189            var actions = {};
190            var action;
191            if (this.itemActions) {
192                for (var actionName in this.itemActions) {
193                    if ( this.itemActions.hasOwnProperty(actionName) ) {
194                        action = this.itemActions[actionName];
195                        actions[actionName] = {
196                            callback: action.callback && lang.partial(action.callback,item),
197                            properties: {
198                                blockButton: false,
199                                showLabel: false,
200                                icon: action.icon + " black",
201                                tooltip: action.description
202                            }
203                        };
204                    }
205                }
206            }
207            var w = new LineWithActionsWidget({
208                title: get(this.itemTitle,item),
209                actions: actions
210            });
211            w.startup();
212            w.on("click", lang.hitch(this, this._onSelect, item, w));
213            return w.domNode;
214        },
215
216        onSelect: function(/*item*/) {}
217    });
218});
Note: See TracBrowser for help on using the repository browser.