-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdynaform.jquery.js
More file actions
201 lines (166 loc) · 5.28 KB
/
dynaform.jquery.js
File metadata and controls
201 lines (166 loc) · 5.28 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
var Dynaform = {};
function DynaformType(){
this.name = function(){
return 'textbox';
};
this.previewHtml = function(){
return "<input type=\"text\" value=\"(textbox)\" disabled />";
};
this.defaultModel = function(){
return {type: this.name(), label: ""};
};
this.edit = function(state){
state.label = prompt("Please enter label:", state.label);
if(state.label != '' && state.label != null){
state.save();
}
};
this.add = function(state){
this.edit(state);
}
this.confirmDeleteMessage = function(){
return "Are you sure you want to delete this " + this.name() + "?";
};
this.confirmDelete = function(){
return confirm(this.confirmDeleteMessage());
};
}
(function(){
var types = [];
Dynaform.addType = function(type){
types[type.name()] = type;
};
Dynaform.type = function(name){
return types[name];
};
function InputState(model, fn){
for(var i in model){
this[i] = model[i];
}
this.save = function(){
out = {};
for(var i in model){
out[i] = this[i];
}
fn(out);
}
}
function to_json_string(data){
if(typeof data == 'number' || typeof data == 'boolean'){
return data;
}
if(typeof data == 'string'){
return "\"" + data.replace(/\"/, "\\\"") + "\"";
}
if(data instanceof Array){
var items = [];
for(var i in data){
items.push(to_json_string(data[i]));
}
return "[" + items.join(",") + "]";
}
var items = [];
for(var i in data){
items.push(to_json_string(i) + ":" + to_json_string(data[i]));
}
return "{" + items.join(",") + "}";
}
function html_escape(data){
return data.replace(/&/, "&").replace(/</, "<").replace(/>/, ">").replace(/\"/, """);
}
function Editor(form_input){
var that = this;
var container = $('<div class="dynaform_editor">');
$(form_input).hide();
container.insertAfter(form_input);
var model;
try {model = jQuery.parseJSON($(form_input).val());} catch(e){};
if(!model){
model = {inputs: []};
}
function update(){
$(container).html("");
var table = $("<table>");
$(container).append(table);
var inputs = model['inputs'];
if(inputs.length > 0 ){
table.html("<tr><th>Label</th><th>Preview</th></tr>");
}
for(var i in inputs){
var input = inputs[i];
var row = $("<tr>");
$(table).append(row);
(function(i){
var cell = $("<td valign=\"top\" class=\"label\">"+html_escape(input.label)+"</td><td class=\"preview\">"+types[input.type].previewHtml()+"</td>");
$(row).append(cell);
var cell = $("<td valign=\"top\"><a href=\"javascript:;\" class=\"button edit\">Edit</a></td>");
$(row).append(cell);
$(cell).find("a").click(function(){
types[inputs[i].type].edit(new InputState(inputs[i], function(input_model){
inputs[i] = input_model;
update();
}));
});
var cell = $("<td valign=\"top\"><a href=\"javascript:;\" class=\"button delete\">Delete</a></td>");
$(row).append(cell);
$(cell).find("a").click(function(){
if(types[inputs[i].type].confirmDelete()){
var new_inputs = [];
for(var j in inputs){
if(j != i){
new_inputs.push(inputs[j]);
}
}
model['inputs'] = new_inputs;
update();
}
});
var cell = $("<td valign=\"top\"><a href=\"javascript:;\" class=\"button up\">Up</a></td>");
$(row).append(cell);
if(i > 0){
$(cell).find("a").click(function(){
var current = inputs[i];
inputs[i] = inputs[i - 1];
inputs[i - 1] = current;
update();
});
}
var cell = $("<td valign=\"top\"><a href=\"javascript:;\" class=\"button down\">Down</a></td>");
$(row).append(cell);
if(i < inputs.length - 1){
$(cell).find("a").click(function(){
var current = inputs[i];
inputs[i] = inputs[i + 1];
inputs[i + 1] = current;
update();
});
}
})(parseInt(i));
}
var row = $("<tr>");
$(table).append(row);
var cell = $("<td colspan=\"6\">");
$(row).append(cell);
var insert_select = $("<select>");
$(cell).append(insert_select);
$(insert_select).append($("<option selected> --- Add --- </option>"));
for(var i in types){
var type = types[i];
$(insert_select).append($("<option value=\""+i+"\">"+i+"</option>"));
}
$(insert_select).change(function(){
var type = $(this).val();
$(this).val("");
types[type].add(new InputState(types[type].defaultModel(), function(input_model){
model.inputs.push(input_model);
update();
}));
});
$(form_input).val(to_json_string(model));
}
update();
}
$(function(){
$(".dynaform").each(function(){new Editor(this);});
});
})();