source: Dev/branches/rest-dojo-ui/client/rft/ui/QuestionEditorToolkit.js @ 378

Last change on this file since 378 was 378, checked in by jkraaijeveld, 13 years ago

Created a factory for the inner widgets on the Question edit screen - can be easily made more generic if necessary too.

File size: 5.9 KB
Line 
1define([
2        'dojo/_base/declare',
3        'dojo/_base/lang',
4        'dojo/_base/array',
5        'dijit/_WidgetBase',
6        'dijit/_Container',
7        'dojo/dom-construct',
8        'rft/store',
9        'rft/ui/QuestionEditorPreviewItem',
10        'rft/ui/CategoryListView',
11        'dijit/_TemplatedMixin',
12        'dijit/_WidgetsInTemplateMixin',
13        'dijit/form/ComboBox',
14        'dojo/text!./templates/QuestionEditorToolkit.html'
15        ], function(declare, lang, baseArray, _WidgetBase, _Container, domConstruct, store, QuestionEditorPreviewItem, CategoryListView, _TemplatedMixin, _WidgetsInTemplateMixin, ComboBox, template) {
16                return declare("rft.ui.QuestionEditorToolkit", [_WidgetBase, _TemplatedMixin, _WidgetsInTemplateMixin, _Container], {
17
18                        templateString: template,
19                        _list: null,
20                        _categorySelect: null,
21                        _categoryStore: null,
22                        _topicSelect: null,
23                        _topicStore: null,
24                        _categories : null,
25
26//                      constructor: function() {
27//                              lang.mixin(this, arguments);
28//                      },
29
30                        postCreate: function(){
31                                this.contentSource = new dojo.dnd.Source(this.ToolkitContentSourceNode, {
32                                        accept: [],
33                                        horizontal: false,
34                                        withHandles: false,
35                                        copyOnly: true,
36                                        selfCopy: false,
37                                        selfAccept: false,
38                                        singular: true,
39                                        creator: this._creator
40                                });
41                                this.inputsSource = new dojo.dnd.Source(this.ToolkitInputsSourceNode, {
42                                        accept: [],
43                                        horizontal: false,
44                                        withHandles: false,
45                                        copyOnly: true,
46                                        selfCopy: false,
47                                        selfAccept: false,
48                                        singular: true,
49                                        creator: this._creator
50                                });
51                                var contentItems = this._contentItems();
52                                var inputsItems = this._inputsItems();
53                                this.contentSource.insertNodes(false, contentItems);
54                                this.inputsSource.insertNodes(false, inputsItems);
55                        },
56                        loadQuestion: function(question) {
57                                this.propertiesForm.set('value', question);
58                                this._categories = question.categories;
59                                this._setupListView();
60                                this._setupCategories();
61                                this._setupTopic(question.topic);
62                        },
63                        onCategoryAdd: function() {
64                                this._addCategory(this._categorySelect.get('displayedValue'));
65                                this._categorySelect.reset();
66                        },
67                        _creator: function(item, hint) {
68                                var node;
69                                if (hint == "avatar") {
70                                        node = document.createElement("span");
71                                        node.innerHTML = item.widgetType;
72                                        return {node: node, data: item, type: "ToolkitItem"};
73                                } else {
74                                        var w = new dijit.form.Button({
75                                                baseClass: "rftLargeButton",
76                                                iconClass: "rftIcon rftIcon"+item.icon,
77                                                label: item.label,
78                                                showLabel: true,
79                                                class: "newline"
80                                        });
81                                        return {node: w.domNode, data: item.objectData, type: "ToolkitItem"};
82                                }
83                        },
84                        _setupListView: function() {
85                                this._list = new CategoryListView( {
86                                        controller: this,
87                                        removeCallback: lang.hitch(this, this._removeCategory),
88                                }).placeAt(this.listNode);
89                                for (category in this._categories) {
90                                        this._list.insertItem(this._categories[category]);
91                                }       
92                                this._list.startup();
93                        },
94                        _setupCategories: function() {
95                                this._categoryStore = new dojo.store.Memory({data: [] });
96                                store.query("_design/default/_view/questions", {reduce:true, group:false, group_level:1})
97                                        .forPairs(lang.hitch(this, function(value, key) {
98                                                this._categoryStore.put({ id: key[0] });
99                                        }));
100                                this._categorySelect = new ComboBox( {
101                                        id: "categoriesBox",
102                                        name: "categories",
103                                        store: this._categoryStore,
104                                        searchAttr: "id"
105                                }, "categoriesBox");
106                                this._supportingWidgets.push(this._categorySelect);
107
108            },
109            _setupTopic: function(topic) {
110                                this._topicStore = new dojo.store.Memory( {data: [] });
111                                store.query("_design/default/_view/topics", {reduce:true, group:true})
112                                        .forPairs(lang.hitch(this, function(value, key) {
113                                                this._topicStore.put({ id: key });
114                                        }));
115                                this._topicSelect = new ComboBox( {
116                                        id: "topicBox",
117                                        name: "topic",
118                                        store: this._topicStore,
119                                        searchAttr: "id",
120                                        value: topic
121                                }, "topicBox");
122                                this._supportingWidgets.push(this._topicSelect);
123            },
124                        _addCategory: function(item) {
125                                this._categories.push(item);
126                                this._list.insertItem(item);
127                        },
128                        _removeCategory: function(item) {
129                                this._categories.splice(this._categories.indexOf(item), 1);
130                        },
131                        _contentItems: function() {
132                                // Returns an array of objects, from which to generate buttons to populate the ToolkitContentSource list.
133                                return [
134                                {
135                                        label: "Header",
136                                        objectData: {
137                                                widgetType: "Header",
138                                        },
139                                        icon: "Header"
140                                },
141                                {
142                                        label: "Text",
143                                        objectData: {
144                                                widgetType: "Text",
145                                        },
146                                        icon: "TextBox"
147                                },
148                                {
149                                        label: "Image",
150                                        objectData: {
151                                                widgetType: "Image",
152                                        },
153                                        icon: "Image"
154                                },
155                                {
156                                        label: "External media",
157                                        objectData: {
158                                                widgetType: "ExternalMedia",
159                                        },
160                                        icon: "External"
161                                },
162                                {
163                                        label: "Divider",
164                                        objectData: {
165                                                widgetType: "Divider",
166                                        },
167                                        icon: "Divider"
168                                }];
169                        },
170                        _inputsItems: function() {
171                                // Returns an array of objects, from which to generate buttons to populate the ToolkitInputsSource list.
172                                return [
173                                {
174                                        label: "Free text",
175                                        objectData: {
176                                                widgetType: "FreeTextInput",
177                                        },
178                                        icon: "Text"
179                                },
180                                {
181                                        label: "Integer",
182                                        objectData: {
183                                                _id: null,
184                                                widgetType: "IntegerInput",
185                                                widgetProps: {}
186                                        },
187                                        icon: "Integer"
188                                },
189                                {
190                                        label: "Scale",
191                                        objectData: {
192                                                _id: null,
193                                                widgetType: "ScaleInput",
194                                                widgetProps: {}
195                                        },
196                                        icon: "Scale"
197                                },
198                                {
199                                        label: "Cards",
200                                        objectData: {
201                                                _id: null,
202                                                widgetType: "CardsInput",
203                                                widgetProps: {}
204                                        },
205                                        icon: "Cards"
206                                },
207                                {
208                                        label: "Multiple Choice",
209                                        objectData: {
210                                                _id: null,
211                                                widgetType: "MultipleChoiceInput",
212                                                widgetProps: {}
213                                        },
214                                        icon: "MultipleChoice"
215                                }
216                                ];
217                        }
218
219                });
220});
Note: See TracBrowser for help on using the repository browser.