source: Dev/trunk/client/qed/model/widgets/QuestionEditorToolkit.js @ 428

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

Allow Selector to be a drag source.

File size: 6.2 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    '../../store',
14    './CategoryListView',
15    'dojo/text!./templates/QuestionEditorToolkit.html'
16], function(declare, lang, Source, domConstruct, Memory, _Container,
17        _TemplatedMixin, _WidgetBase, _WidgetsInTemplateMixin, Button,
18        ComboBox, store, CategoryListView, template) {
19    return declare([_WidgetBase, _TemplatedMixin, _WidgetsInTemplateMixin, _Container], {
20
21        templateString: template,
22        _list: null,
23        _categorySelect: null,
24        _categoryStore: null,
25        _topicSelect: null,
26        _topicStore: null,
27        _categories : null,
28
29        _contentItems: [
30            { type: "Header" },
31            { type: "Text" },
32            { type: "Image" },
33            { type: "ExternalMedia" },
34            { type: "Divider" }
35        ],
36        _inputsItems: [
37            { type: "StringInput" },
38            { type: "TextInput" },
39            { type: "IntegerInput" },
40            { type: "ScaleInput" },
41            { type: "MultipleChoiceInput" }
42        ],
43        _labelMap: {
44            "Header": "Header",
45            "Text": "Text",
46            "Image": "Image",
47            "ExternalMedia": "External media",
48            "Divider": "Divider",
49            "StringInput": "Text line",
50            "TextInput": "Free text",
51            "IntegerInput": "Integer number",
52            "ScaleInput": "Scale",
53            "MultipleChoiceInput": "Multiple choice"
54        },
55        _iconMap: {
56            "Header": "Header",
57            "Text": "TextBox",
58            "Image": "Image",
59            "ExternalMedia": "External",
60            "Divider": "Divider",
61            "StringInput": "Text",
62            "TextInput": "Text",
63            "IntegerInput": "Integer",
64            "ScaleInput": "Scale",
65            "MultipleChoiceInput": "MultipleChoice"
66        },
67
68        postCreate: function(){
69            this.inherited(arguments);
70            var creator = lang.hitch(this,"_creator");
71            this.contentSource = new Source(this.ToolkitContentSourceNode, {
72                accept: [],
73                copyOnly: true,
74                selfAccept: false,
75                singular: true,
76                creator: creator
77            });
78            this.inputsSource = new Source(this.ToolkitInputsSourceNode, {
79                accept: [],
80                copyOnly: true,
81                selfAccept: false,
82                singular: true,
83                creator: creator
84            });
85            this.contentSource.insertNodes(false, this._contentItems);
86            this.inputsSource.insertNodes(false, this._inputsItems);
87        },
88        _setValueAttr: function(question) {
89            this.propertiesForm.set('value', question);
90            this._categories = question.categories || [];
91            this._setupListView();
92            this._setupCategories();
93            this._setupTopic(question.topic);
94        },
95        _getValueAttr: function() {
96            var value = this.propertiesForm.get('value');
97            value.categories = this._categories;
98            return value;
99        },
100        onCategoryAdd: function() {
101            this._addCategory(this._categorySelect.get('displayedValue'));
102            this._categorySelect.reset();
103        },
104        _creator: function(item, hint) {
105            var node;
106            if (hint === "avatar") {
107                node = domConstruct.create('span',{
108                    innerHTML: this._labelMap[item.type] || item.type
109                });
110            } else {
111                var w = new Button({
112                    baseClass: "rftLargeButton",
113                    iconClass: "rftIcon rftIcon"+(this._iconMap[item.type]||"Unknown"),
114                    label: this._labelMap[item.type] || item.type,
115                    showLabel: true,
116                    style: "display: block"
117                });
118                node = w.domNode;
119            }
120            return {
121                node: node,
122                data: item,
123                type: ["questionContent"]
124            };
125        },
126        _setupListView: function() {
127            this._list = new CategoryListView( {
128                removeCallback: lang.hitch(this, this._removeCategory)
129            }).placeAt(this.listNode);
130            this._list.startup();               
131            for (var category in this._categories) {
132                this._list.appendItem(this._categories[category]);
133            }   
134        },
135        _setupCategories: function() {
136            this._categoryStore = new Memory({data: [] });
137            store.query("_design/questions/_view/all", {reduce:true, group:false, group_level:1})
138            .forPairs(lang.hitch(this, function(value, key) {
139                this._categoryStore.put({ id: key[0] });
140            }));
141            this._categorySelect = new ComboBox( {
142                name: "categories",
143                store: this._categoryStore,
144                searchAttr: "id"
145            }, this.categoriesBoxNode);
146            this._supportingWidgets.push(this._categorySelect);
147
148        },
149        _setupTopic: function(topic) {
150            this._topicStore = new Memory( {data: [] });
151            store.query("_design/questions/_view/all_topics", {reduce:true, group:true})
152            .forPairs(lang.hitch(this, function(value, key) {
153                this._topicStore.put({ id: key });
154            }));
155            this._topicSelect = new ComboBox( {
156                name: "topic",
157                store: this._topicStore,
158                searchAttr: "id",
159                value: topic
160            }, this.topicBoxNode);
161            this._supportingWidgets.push(this._topicSelect);
162        },
163        _addCategory: function(item) {
164            this._categories.push(item);
165            this._list.appendItem(item);
166        },
167        _removeCategory: function(item) {
168            this._categories.splice(this._categories.indexOf(item), 1);
169        }
170
171    });
172});
Note: See TracBrowser for help on using the repository browser.