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

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

Reorganized for Node --- the SVN gods hate us all!

Lost all historical info on moved files, because SVN is a f *.

Also we have Node now, serving both the static content and forwarding
database requests.

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