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

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