1 | define([
|
---|
2 | 'dojo/_base/declare',
|
---|
3 | 'dojo/_base/lang',
|
---|
4 | 'dijit/form/NumberSpinner',
|
---|
5 | 'dijit/form/Textarea',
|
---|
6 | 'dijit/form/TextBox',
|
---|
7 | 'dijit/layout/StackContainer',
|
---|
8 | 'dojox/form/CheckedMultiSelect',
|
---|
9 | 'dojox/layout/TableContainer'
|
---|
10 | ],function(declare, lang, NumberSpinner, Textarea, TextBox, StackContainer, CheckedMultiSelect, TableContainer) {
|
---|
11 | var factory = declare('rft.ui.ContentWidgetFactory', [], {
|
---|
12 | /* No default type, all should be valid */
|
---|
13 | createWidget: function(/*Object*/ content) {
|
---|
14 | // options: Object
|
---|
15 | // type: "header", "text", textinput, etc.
|
---|
16 | // contents: "text"
|
---|
17 | // disabled: bool
|
---|
18 | return this['create'+content.type+'Widget'](content);
|
---|
19 | },
|
---|
20 | createHeaderWidget: function(options) {
|
---|
21 | var headerWidget = new HeaderItem();
|
---|
22 | headerWidget.setObject(options);
|
---|
23 | return headerWidget;
|
---|
24 | },
|
---|
25 | createTextWidget: function(options) {
|
---|
26 | var textWidget = new TextItem();
|
---|
27 | textWidget.setObject(options);
|
---|
28 | return textWidget;
|
---|
29 | },
|
---|
30 | createFreeTextInputWidget: function(options) {
|
---|
31 | return new FreeTextInput();
|
---|
32 | },
|
---|
33 | createIntegerInputWidget: function(options) {
|
---|
34 | var integerWidget = new IntegerInput();
|
---|
35 | integerWidget.setObject(options);
|
---|
36 | return integerWidget;
|
---|
37 | },
|
---|
38 | createMultipleChoiceInputWidget: function(options) {
|
---|
39 | var mcWidget = new MultipleChoiceInput();
|
---|
40 | mcWidget.setObject(options);
|
---|
41 | return mcWidget;
|
---|
42 | }
|
---|
43 | });
|
---|
44 |
|
---|
45 | /* Contents */
|
---|
46 | var HeaderItem = declare(TextBox, {
|
---|
47 | getObject: function() {
|
---|
48 | return { type : 'Header',
|
---|
49 | contents: this.get('value'),
|
---|
50 | disabled: this.get('disabled')
|
---|
51 | };
|
---|
52 | },
|
---|
53 | setObject: function(object) {
|
---|
54 | this.set('value', object.contents);
|
---|
55 | if (object.disabled) {
|
---|
56 | this.set('disabled', true);
|
---|
57 | }
|
---|
58 | }
|
---|
59 | });
|
---|
60 |
|
---|
61 | var TextItem = declare(Textarea, {
|
---|
62 | getObject: function() {
|
---|
63 | return { type: 'Text',
|
---|
64 | contents: this.get('value'),
|
---|
65 | disabled: this.get('disabled')
|
---|
66 | };
|
---|
67 | },
|
---|
68 | setObject: function(object) {
|
---|
69 | this.set('value', object.contents);
|
---|
70 | if (object.disabled) {
|
---|
71 | this.set('disabled', true);
|
---|
72 | }
|
---|
73 | }
|
---|
74 | });
|
---|
75 |
|
---|
76 | /* Inputs */
|
---|
77 | var FreeTextInput = declare(Textarea, {
|
---|
78 | getObject: function() {
|
---|
79 | return { type: 'FreeTextInput' };
|
---|
80 | }
|
---|
81 | });
|
---|
82 |
|
---|
83 | //Use CheckedMultiSelect
|
---|
84 | var MultipleChoiceInput = declare(StackContainer, {
|
---|
85 | _multiSelect: null,
|
---|
86 | postCreate: function() {
|
---|
87 | this.inherited(arguments);
|
---|
88 | this._multiSelect = new CheckedMultiSelect();
|
---|
89 | this._multiSelect.addOption({ label: "Hurr durr", value: 1 });
|
---|
90 | this._multiSelect.addOption({ label: "This is a choice", value: 1 });
|
---|
91 | this._multiSelect.addOption({ label: "This too", value: 1 });
|
---|
92 | this._multiSelect.addOption({ label: "So because there are more", value: 1 });
|
---|
93 | this._multiSelect.addOption({ label: "This must be multiple choice.", value: 1 });
|
---|
94 | this.addChild(this._multiSelect);
|
---|
95 | },
|
---|
96 |
|
---|
97 | setObject: function() {
|
---|
98 | return;
|
---|
99 | },
|
---|
100 |
|
---|
101 | edit: function() {
|
---|
102 | return;
|
---|
103 | }
|
---|
104 |
|
---|
105 | });
|
---|
106 |
|
---|
107 | var IntegerInput = declare(StackContainer, {
|
---|
108 | _numberSpinner: null,
|
---|
109 | _simpleTable: null,
|
---|
110 | _editWidgets: null,
|
---|
111 | _editTable: null,
|
---|
112 | postCreate: function() {
|
---|
113 | this.inherited(arguments);
|
---|
114 | this._numberSpinner = new NumberSpinner( { title: "Answer", value: 0, constraints: { min: -100, max: 100 } });
|
---|
115 | this._simpleTable = new TableContainer({ cols: 1, customClass: "labelsAndValues", labelWidth : 150} );
|
---|
116 | this._simpleTable.addChild(this._numberSpinner);
|
---|
117 | this._editTable = new TableContainer({ cols: 1, customClass: "labelsAndValues"} );
|
---|
118 | this._simpleTable.startup();
|
---|
119 | this.addChild(this._simpleTable);
|
---|
120 | },
|
---|
121 | edit: function() {
|
---|
122 | this.removeChild(this._simpleTable);
|
---|
123 | this.addChild(this._editTable);
|
---|
124 | },
|
---|
125 | save: function () {
|
---|
126 | for (var widget in this._editWidgets) {
|
---|
127 | var w = this._editWidgets[widget];
|
---|
128 | if (w.value) {
|
---|
129 | if (w.constraint)
|
---|
130 | this._numberSpinner.constraints[w.field] = w.value;
|
---|
131 | else
|
---|
132 | this._numberSpinner.set(w.field, w.value);
|
---|
133 | }
|
---|
134 | }
|
---|
135 | this.removeChild(this._editTable);
|
---|
136 | this.addChild(this._simpleTable);
|
---|
137 | this._simpleTable.layout();
|
---|
138 | },
|
---|
139 | setObject: function(object) {
|
---|
140 | if(object.contents) {
|
---|
141 | lang.mixin(this._numberSpinner, object.contents);
|
---|
142 | this._setupEditWidgets(object.contents);
|
---|
143 | }
|
---|
144 | else {
|
---|
145 | //Create widgets with default values
|
---|
146 | this._setupEditWidgets(this.getObject().contents);
|
---|
147 | }
|
---|
148 | },
|
---|
149 | _setupEditWidgets: function(contents) {
|
---|
150 | //Set up widgets which should appear once edit is pressed.
|
---|
151 | this._editWidgets = [];
|
---|
152 | this._editWidgets.push(new NumberSpinner( { field: "value", title: "Default value" }))
|
---|
153 | this._editWidgets.push(new NumberSpinner( { field: "smallDelta", title: "Increment size" }));
|
---|
154 | this._editWidgets.push(new NumberSpinner( { constraint: true, field: "max", title: "Maximum value" }));
|
---|
155 | this._editWidgets.push(new NumberSpinner( { constraint: true, field: "min", title: "Minimum value" }));
|
---|
156 | this._editWidgets.push(new TextBox ( { field: "invalidMessage", title: "Invalid message" }));
|
---|
157 | this._editWidgets.push(new TextBox ( { field: "title", title: "Label" }));
|
---|
158 | for (var widget in this._editWidgets) {
|
---|
159 | var w = this._editWidgets[widget];
|
---|
160 | if (w.constraint)
|
---|
161 | w.set('value', contents.constraints[w.field]);
|
---|
162 | else
|
---|
163 | w.set('value', contents[w.field]);
|
---|
164 | this._editTable.addChild(w);
|
---|
165 | }
|
---|
166 | this._editTable.startup();
|
---|
167 | },
|
---|
168 | getObject: function() {
|
---|
169 | return {
|
---|
170 | type: 'IntegerInput',
|
---|
171 | contents: {
|
---|
172 | value: this._numberSpinner.value,
|
---|
173 | smallDelta: this._numberSpinner.smallDelta,
|
---|
174 | constraints: this._numberSpinner.constraints,
|
---|
175 | invalidMessage: this._numberSpinner.invalidMessage,
|
---|
176 | title: this._numberSpinner.title
|
---|
177 | }
|
---|
178 | };
|
---|
179 | }
|
---|
180 | });
|
---|
181 |
|
---|
182 | return factory;
|
---|
183 | });
|
---|