-
Notifications
You must be signed in to change notification settings - Fork 7
/
editor.js
307 lines (270 loc) · 7.73 KB
/
editor.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
// Constants
var Tabs = {
ADD_FIELD_TAB: 0,
FIELD_SETTINGS_TAB: 1,
FORM_SETTINGS_TAB: 2
};
var FIELD_TYPES = [
"text", "number", "textarea", "checkbox", "radio", "select", "section",
"page", "shortname", "file", "address", "date", "email", "time", "phone",
"url", "money", "likert"
];
// Root view model
var EditorViewModel = function(data) {
var self = this;
// Data
this.form = new FormViewModel(data);
this.currentTab = ko.observable(Tabs.ADD_FIELD_TAB);
this.selectedField = ko.observable(null);
// Helper data
this.formSettingsSelected = ko.computed(function(){
return this.currentTab() === Tabs.FORM_SETTINGS_TAB
}, this);
this.selectedFieldIndex = ko.computed(function() {
return self.form.fields.indexOf(self.selectedField());
}, this);
this.formJSON = ko.computed(function(){
return ko.toJSON(self.form);
}, this);
// Behaviour
this.showFormSettings = function() {
self.currentTab(Tabs.FORM_SETTINGS_TAB);
};
this.selectField = function(field) {
self.selectedField(field);
self.currentTab(Tabs.FIELD_SETTINGS_TAB);
};
this.selectFieldAtIndex = function(index) {
if (index >= 0 && index < self.form.fields().length) {
self.selectField(self.form.fields()[index]);
}
};
// Duplicates the given field by delegating to FormViewModel
this.duplicateField = function(field) {
var field = self.form.duplicateField(field);
if (field) {
self.selectedField(field);
}
};
// Adds a new field from given data. event is used to get the type of the
// new field. Actually delegates to the FormViewModel
this.addField = function(data, event) {
var type = $(event.target).data("type");
data.type = type;
var field = self.form.addField(data);
// Scroll to the field
// TODO This should not be here
if (field) {
$("html, body").animate({
scrollTop: $(".field-wrapper:last").offset().top
}, 1000);
}
};
// Removes the given field. Actually delegates to the FormViewModel
this.removeField = function(field) {
if (field === self.selectedField()) {
self.selectedField(null);
}
self.form.removeField(field);
if (!self.form.hasFields() && self.currentTab() === Tabs.FIELD_SETTINGS_TAB) {
self.currentTab(Tabs.ADD_FIELD_TAB);
}
};
this.createFirstField = function() {
var field = self.form.addField({type: 'textarea'});
field.title("This is my first field, yeay!");
self.selectField(field);
};
this.removeselectedField = function() {
var selectedField = self.selectedField();
if (selectedField !== null) {
self.removeField(selectedField);
}
};
this.selectNextField = function(){
self.selectFieldAtIndex(self.selectedFieldIndex() + 1);
};
this.selectPrevField = function(){
self.selectFieldAtIndex(self.selectedFieldIndex() - 1);
};
// Setup keyboard shortcuts
$(document)
.bind('keydown', 'backspace', this.removeselectedField)
.bind('keydown', 'del', this.removeselectedField)
.bind('keydown', 'j', this.selectNextField)
.bind('keydown', 'k', this.selectPrevField);
};
var getDefaultDataForType = function(type) {
switch (type) {
case 'text': return {};
case 'number':
return {
title: "Number"
};
case 'textarea': return {};
case 'checkbox':
return {
title: "Check all that apply",
choices: [
{choice: "First Choice"},
{choice: "Second Choice"},
{choice: "Third Choice"}
]
};
case 'radio':
return {
title: "Select a Choice",
choices: [
{choice: "First Choice"},
{choice: "Second Choice"},
{choice: "Third Choice"}
]
};
case 'select':
return {
title: "Select a Choice",
choices: [
{choice: "First Choice"},
{choice: "Second Choice"},
{choice: "Third Choice"}
]
};
case 'section':
return {
title: "Section Break",
instructions: "A description of the section goes here."
};
case 'page': return {};
case 'shortname':
return {
title: "Name"
};
case "phone":
return {
title: "Phone"
};
case 'file':
case 'address':
case "date":
case "email":
case "time":
case "url":
case "money":
case "likert":
default: return {}
}
};
var FormViewModel = function(data) {
var self = this;
var mapping = {
'fields': {
create: function(options) {
return new FieldViewModel(options.data || []);
}
}
};
this.name = ko.observable("Untitled");
this.description = ko.observable("");
this.fields = ko.observableArray([]);
ko.mapping.fromJS(data, mapping, this);
this.hasFields = ko.computed(function(){
return this.fields().length !== 0;
}, this);
// Behaviour
this.duplicateField = function(field) {
var newFieldData = ko.toJS(field),
index = ko.utils.arrayIndexOf(self.fields(), field),
newField = new FieldViewModel(newFieldData);
self.fields.splice(index, 0, newField);
return newField;
};
this.addField = function(data) {
if (data.type === undefined) {
return false;
}
var newField = new FieldViewModel({type: data.type});
ko.mapping.fromJS(getDefaultDataForType(data.type), {}, newField);
self.fields.push(newField);
return newField;
};
this.removeField = function(field) {
self.fields.remove(field);
};
};
// Helper
var traverse = function(o, func) {
for (i in o) {
func.apply(this, [o, i, o[i]]);
if (typeof(o[i])=="object") {
traverse(o[i],func);
}
}
};
FormViewModel.prototype.toJSON = function() {
var obj = {
name: ko.utils.unwrapObservable(this.name),
description: ko.utils.unwrapObservable(this.description),
fields: ko.utils.unwrapObservable(this.fields)
};
// Remove ko mappings from the object recursively
traverse(obj, function(object, key, value) {
if (key === '__ko_mapping__') {
delete object[key];
};
});
return obj;
};
var FieldViewModel = function(data) {
var self = this;
this.type = ko.observable();
this.title = ko.observable("Untitled");
this.is_required = ko.observable(false);
this.instructions = ko.observable("");
this.choices = ko.observableArray([]);
this.is_randomized = ko.observableArray(null);
ko.mapping.fromJS(data, {}, this);
// Data
this.previewTemplateName = ko.computed(function(){
return "tmp-field-preview-" + this.type();
}, this);
this.settingsTemplateName = ko.computed(function(){
return "tmp-field-settings-" + this.type();
}, this);
this.hasChoices = ko.computed(function() {
return this.choices && this.choices().length !== 0;
}, this);
// Behaviour
this.addChoice = function() {
self.choices.push(ko.mapping.fromJS({"choice": ""}));
};
this.removeChoice = function(choice) {
self.choices.remove(choice);
};
};
FieldViewModel.prototype.toJSON = function() {
return {
type: ko.utils.unwrapObservable(this.type),
title: ko.utils.unwrapObservable(this.title),
is_required: ko.utils.unwrapObservable(this.is_required),
instructions: ko.utils.unwrapObservable(this.instructions),
choices: ko.utils.unwrapObservable(this.choices),
is_randomized: ko.utils.unwrapObservable(this.is_randomized)
};
};
// Custom ko bindings
ko.bindingHandlers.tab = {
init: function(element, valueAccessor) {
var currentTab = valueAccessor();
$(element).find('a').click(function() {
currentTab($(this).parent().index());
});
},
update: function(element, valueAccessor, allBindingsAccessor, viewModel) {
var currentTab = valueAccessor()();
$(element).find('li:nth(' + currentTab + ') a:first').trigger('click');
// Dirty hack
if (currentTab !== Tabs.FIELD_SETTINGS_TAB) {
viewModel.selectedField && viewModel.selectedField(null);
}
}
};