-
Notifications
You must be signed in to change notification settings - Fork 259
Expand file tree
/
Copy pathform_builder.js
More file actions
251 lines (213 loc) · 7.73 KB
/
form_builder.js
File metadata and controls
251 lines (213 loc) · 7.73 KB
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
//= require cms/ajax
//= require underscore
//= require jquery.exists
/**
* The UI for dynamically creating custom forms via the UI.
* @constructor
*
*/
var FormBuilder = function() {
};
// Add a new field to the form
// (Implementation: Clone existing hidden form elements rather than build new ones via HTML).
FormBuilder.prototype.newField = function(field_type) {
this.hideNewFormInstruction();
this.addPreviewFieldToForm(field_type);
};
FormBuilder.prototype.addPreviewFieldToForm = function(field_type) {
$("#placeHolder").load($('#placeHolder').data('new-path') + '?field_type=' + field_type + ' .control-group', function() {
var newField = $("#placeHolder").find('.control-group');
newField.insertBefore('#placeHolder');
formBuilder.enableFieldButtons();
formBuilder.resetAddFieldButton();
});
};
FormBuilder.prototype.resetAddFieldButton = function() {
$("#form_new_entry_new_field").val('1');
};
FormBuilder.prototype.removeCurrentField = function() {
this.field_being_editted.remove();
this.field_being_editted = null;
};
// Function that triggers when users click the 'Delete' field button.
FormBuilder.prototype.confirmDeleteFormField = function() {
formBuilder.field_being_editted = $(this).parents('.control-group');
var path = $(this).attr('data-path');
if (path == "") {
formBuilder.removeCurrentField();
} else {
$('#modal-confirm-delete-field').modal({
show: true
});
}
};
// Function that triggers when users click the 'Edit' field button.
FormBuilder.prototype.editFormField = function() {
// This is the overall container for the entire field.
formBuilder.field_being_editted = $(this).parents('.control-group');
$('#modal-edit-field').find('.modal-content').load($(this).attr('data-edit-path'));
$('#modal-edit-field').modal({
show: true,
remote: $(this).attr('data-edit-path')
});
};
FormBuilder.prototype.hideNewFormInstruction = function() {
var no_fields = $("#no-field-instructions");
if (no_fields.exists()) {
no_fields.hide();
}
};
// Add handler to any edit field buttons.
FormBuilder.prototype.enableFieldButtons = function() {
$('.edit_form_button').unbind('click').on('click', formBuilder.editFormField);
$('.delete_field_button').unbind('click').on('click', formBuilder.confirmDeleteFormField);
};
FormBuilder.prototype.newFormField = function() {
return $('#ajax_form_field');
};
// Delete field from form, then remove it from the field
FormBuilder.prototype.deleteFormField = function() {
var element = formBuilder.field_being_editted.find('.delete_field_button');
var url = element.attr('data-path');
$.cms_ajax.delete({
url: url,
success: function(field) {
formBuilder.removeCurrentField();
formBuilder.removeFieldId(field.id);
}
});
};
// @param [Number] value The id of the field that is to be removed from the form.
FormBuilder.prototype.removeFieldId = function(value) {
var field_ids = $('#field_ids').val().split(" ");
field_ids.splice($.inArray(value.toString(), field_ids), 1);
formBuilder.setFieldIds(field_ids);
};
// @param [Array<String>] value
FormBuilder.prototype.setFieldIds = function(value) {
var spaced_string = value.join(" ");
$('#field_ids').val(spaced_string);
};
FormBuilder.prototype.addFieldIdToList = function(new_value) {
$('#field_ids').val($('#field_ids').val() + " " + new_value);
};
// Save a new Field to the database for the current form.
FormBuilder.prototype.createField = function() {
var form = formBuilder.newFormField();
var data = form.serialize();
var url = form.attr('action');
$.ajax({
type: "POST",
url: url,
data: data,
global: false,
datatype: $.cms_ajax.asJSON()
}).done(
function(field) {
formBuilder.clearFieldErrorsOnCurrentField();
formBuilder.addFieldIdToList(field.id);
formBuilder.field_being_editted.find('input').attr('data-id', field.id);
formBuilder.field_being_editted.find('label').html(field.label);
formBuilder.field_being_editted.find('a').attr('data-edit-path', field.edit_path);
formBuilder.field_being_editted.find('a.delete_field_button').attr('data-path', field.delete_path);
formBuilder.field_being_editted.find('.help-block').html(field.instructions);
}
).fail(function(xhr, textStatus, errorThrown) {
formBuilder.displayErrorOnField(formBuilder.field_being_editted, xhr.responseJSON);
});
};
FormBuilder.prototype.clearFieldErrorsOnCurrentField = function() {
var field = formBuilder.field_being_editted;
field.removeClass("error");
field.find('.help-inline').remove();
};
FormBuilder.prototype.displayErrorOnField = function(field, json) {
var error_message = json.errors[0];
// console.log(error_message);
field.addClass("error");
var input_field = field.find('.input-append');
input_field.after('<span class="help-inline">' + error_message + '</span>');
};
// Edit Field should handle Enter by submitting the form via AJAX.
// Enter within textareas should still add endlines as normal.
FormBuilder.prototype.onEnterSubmitFormViaAjax = function() {
this.newFormField().on("keypress", function(e) {
if (e.which == 13 && e.target.tagName != 'TEXTAREA') {
formBuilder.createField();
e.preventDefault();
$('#modal-edit-field').modal('hide');
return false;
}
});
};
// Attaches behavior to the proper element.
FormBuilder.prototype.setup = function() {
var select_box = $('.add-new-field');
if (select_box.exists()) {
select_box.change(function() {
formBuilder.newField($(this).val());
});
this.enableFieldButtons();
$("#delete_field").on('click', formBuilder.deleteFormField);
$('#modal-edit-field').on('hidden.bs.modal', function(e) {
$(this).removeData('bs.modal');
});
// Allow fields to be sorted.
$('#form-preview').sortable({
axis: 'y',
delay: 250,
// When form element is moved
update: function(event, ui) {
var field_id = ui.item.find('input').attr('data-id');
var new_position = ui.item.index() + 1;
formBuilder.moveFieldTo(field_id, new_position);
}
});
this.setupConfirmationBehavior();
this.enableFormCleanup();
}
};
// Since we create a form for the #new action, we need to delete it if the user doesn't save it explicitly.
FormBuilder.prototype.enableFormCleanup = function() {
var cleanup_element = $('#cleanup-before-abandoning');
if (cleanup_element.exists()) {
var cleanup_on_leave = true;
$(":submit").on('click', function() {
cleanup_on_leave = false;
});
$(window).bind('beforeunload', function() {
if (cleanup_on_leave) {
var path = cleanup_element.attr('data-path');
$.cms_ajax.delete({url: path, async: false});
}
});
}
};
// Updates the server with the new position for a given field.
FormBuilder.prototype.moveFieldTo = function(field_id, position) {
var url = '/cms/form_fields/' + field_id + '/insert_at/' + position;
var success = function(data) {
console.log("Success:", data);
};
console.log('For', field_id, 'to', position);
$.post(url, success);
};
FormBuilder.prototype.setupConfirmationBehavior = function() {
// Confirmation Behavior
$("#form_confirmation_behavior_show_text").on('click', function() {
$(".form_confirmation_text").show();
$(".form_confirmation_redirect").hide();
});
$("#form_confirmation_behavior_redirect").on('click', function() {
$(".form_confirmation_redirect").show();
$(".form_confirmation_text").hide();
});
$("#form_confirmation_behavior_show_text").trigger('click');
};
var formBuilder = new FormBuilder();
// Register FormBuilder handlers on page load.
jQuery(function($){
formBuilder.setup();
// Include a text field to start (For easier testing)
// formBuilder.newField('text_field');
});