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

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

Can now edit basic question properties and store them.

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