-
Notifications
You must be signed in to change notification settings - Fork 0
/
ObjMaker.js
316 lines (312 loc) · 14.2 KB
/
ObjMaker.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
308
309
310
311
312
313
314
315
316
//Created: Mark A. Burgess - 5/10/2015
(function (document, $) {
context = function(container, config) {
var class_names, drop_labels, include_spacers, refreshHandler, modifiable_form
// set configuration variables to config dictionary/object
function resetConfig(config) {
if (!config) {
config = {}
}
class_names = config['class_names']
if (class_names == undefined) {
class_names = {string_content:"string_content",
numeric_content:"numeric_content",
array_content:"array_content",
object_content:"object_content",
sub_element:"sub_element",
content:"content",
container:"container",
spacer:"spacer",
additional:"additional",
needed:"needed",
duplicate:"duplicate",
name_box:"name_box"}
}
drop_labels = config['drop_labels']
if (drop_labels == undefined) {
drop_labels = {None:"",
String:"String",
Number:"Number",
Array:"Array",
Object:"Object"}
}
include_spacers = config['include_spacers']
refreshHandler = config['refreshHandler']
modifiable_form = config['modifiable_form']
if (modifiable_form == undefined) {
modifiable_form = true
}
}
//call it
resetConfig(config)
// manually set the refresh handler hook
function setRefreshHandler(handler) {
refreshHandler = handler
}
// Given the 'content' DOM object of a presumed string field, return the string otherwise empty string
function getString(element) {
element = $(element)
if (element && element.attr("class") && element.attr("class").indexOf(class_names["string_content"]) > -1) {
return element.children("input").val()
} else {
return ""
}
}
// Given the 'content' DOM object of a presumed numeric field, return the string otherwise zero
function getNumeric(element) {
element = $(element)
if (element && element.attr("class") && element.attr("class").indexOf(class_names["numeric_content"]) > -1) {
return parseFloat(element.children("input[type=number]").val())
} else {
return 0
}
}
// Given the 'content' DOM object of a presumed Array field/s, return the array otherwise empty-array
function getArray(element) {
element = $(element)
if (element && element.attr("class") && element.attr("class").indexOf(class_names["array_content"]) > -1) {
var obj_elements = element.children("."+class_names["sub_element"])
var array = []
for (var i = 0; i < obj_elements.length; i++) {
var obj_children = $(obj_elements[i]).children()
var type = obj_children.filter("select").val()
if (type != drop_labels["None"]) {
var content = obj_children.filter("."+class_names['content'])
if (type == drop_labels["String"]) {
array.push(getString(content))
} else if (type == drop_labels["Number"]) {
array.push(getNumeric(content))
} else if (type == drop_labels["Array"]) {
array.push(getArray(content))
} else if (type == drop_labels["Object"]) {
array.push(getObject(content))
} else {
throw "wtf"
}
}
}
return array
} else {
return []
}
}
// Given the 'content' DOM object of a presumed Object field, return the processed object otherwise empty-object, NOTE: throws 'duplicate_presnt' if there exists a duplicate name in the inputs
function getObject(element) {
element = $(element)
if (element && element.attr("class") && element.attr("class").indexOf(class_names["object_content"]) > -1) {
var obj_elements = element.children("."+class_names['sub_element'])
var obj = {}
for (var i = 0; i < obj_elements.length; i++) {
var obj_children = $(obj_elements[i]).children()
var type = obj_children.filter("select").val()
if (type != drop_labels["None"]) {
var label = obj_children.filter("input").val()
if (label == undefined) {
label = ""
}
var content = obj_children.filter("."+class_names['content'])
if (obj[label] != undefined) {
obj_children.filter("input").addClass(class_names["duplicate"])
throw "duplicate_present"
}
if (type == drop_labels["String"]) {
obj[label] = getString(content)
} else if (type == drop_labels["Number"]) {
obj[label] = getNumeric(content)
} else if (type == drop_labels["Array"]) {
obj[label] = getArray(content)
} else if (type == drop_labels["Object"]) {
obj[label] = getObject(content)
} else {
throw "wtf"
}
}
}
return obj
} else {
return {}
}
}
//for a given DOM element, make a selection box of possible types
function makeTypeSelect(element) {
element = $(element)
var sel = $("<select></select")
$("<option>"+drop_labels["None"]+"</option>").appendTo(sel)
$("<option>"+drop_labels["String"]+"</option>").appendTo(sel)
$("<option>"+drop_labels["Number"]+"</option>").appendTo(sel)
$("<option>"+drop_labels["Array"]+"</option>").appendTo(sel)
$("<option>"+drop_labels["Object"]+"</option>").appendTo(sel)
sel.appendTo(element)
return sel
}
//for a give 'string_content' DOM div, populate with string field
function makeString(element, entity) {
element = $(element)
var input = $("<input></input>")
input.val(entity).change(refresh)
input.appendTo(element)
}
//for a give 'numeric_content' DOM div, populate with numeric field
function makeNumeric(element, entity) {
element = $(element)
var input = $("<input type=number></input>")
input.val(entity).change(refresh)
input.appendTo(element)
}
//for a give 'array_content' DOM div, populate with all fields from the array
function makeArray(element, entity) {
element = $(element)
for (var i = 0; i < entity.length; i++) {
var value = entity[i]
if (include_spacers) {
$('<div class="'+class_names["spacer"]+'"></div>').appendTo(element)
}
var sub_element = $('<div class="'+class_names["sub_element"]+'"></div>')
sub_element.appendTo(element)
var type_select = makeTypeSelect(sub_element)
if (modifiable_form) {
type_select.change(refresh)
} else {
type_select.prop("disabled", true)
}
type_select.val(makeEntity(sub_element, value))
}
if (modifiable_form) {
if (include_spacers) {
$('<div class="'+class_names["spacer"]+'"></div>').appendTo(element)
}
var new_sub_element = $("<div class='"+class_names["sub_element"]+"'></div>")
new_sub_element.appendTo(element)
makeTypeSelect(new_sub_element).addClass(class_names["additional"]).change(refresh)
}
}
//for a give 'object_content' DOM div, populate with all fields from the object
function makeObject(element, entity) {
element = $(element)
var keys = Object.keys(entity)
var has_undefined_key = false
for (var i = 0; i < keys.length; i++) {
var key = keys[i]
var value = entity[key]
if (include_spacers) {
$('<div class="'+class_names["spacer"]+'"></div>').appendTo(element)
}
var sub_element = $('<div class="'+class_names["sub_element"]+'"></div>')
sub_element.appendTo(element)
var type_select = makeTypeSelect(sub_element)
var name_box = $("<input class='"+class_names["name_box"]+"'></input>")
name_box.val(key)
name_box.appendTo(sub_element)
if (modifiable_form) {
type_select.change(refresh)
name_box.data("prev", key)
name_box.change(revert_refresh)
} else {
type_select.prop("disabled", true)
name_box.prop("disabled", true)
}
if (key == "") {
has_undefined_key = true
name_box.addClass(class_names["needed"])
}
type_select.val(makeEntity(sub_element, value))
}
if ((!has_undefined_key) && (modifiable_form)) {
if (include_spacers) {
$('<div class="'+class_names["spacer"]+'"></div>').appendTo(element)
}
var new_sub_element = $("<div class='"+class_names["sub_element"]+"'></div>")
new_sub_element.appendTo(element)
makeTypeSelect(new_sub_element).addClass(class_names["additional"]).change(refresh)
}
}
//for a given DOM element, and an arbitary -simple- entity, make appropriate 'content' div, and populate
function makeEntity(element, entity) {
element = $(element)
var new_content
if (typeof entity == "string") {
new_content = $("<div class='"+class_names['string_content']+" "+class_names["content"]+"'></div>")
new_content.appendTo(element)
makeString(new_content, entity)
return drop_labels["String"]
} else if ($.isNumeric(entity)) {
new_content = $("<div class='"+class_names['numeric_content']+" "+class_names["content"]+"'></div>")
new_content.appendTo(element)
makeNumeric(new_content, entity)
return drop_labels["Number"]
} else if ($.isArray(entity)) {
new_content = $("<div class='"+class_names['array_content']+" "+class_names["content"]+"'></div>")
new_content.appendTo(element)
makeArray(new_content, entity)
return drop_labels["Array"]
} else if ($.isPlainObject(entity)) {
new_content = $("<div class='"+class_names['object_content']+" "+class_names["content"]+"'></div>")
new_content.appendTo(element)
makeObject(new_content, entity)
return drop_labels["Object"]
} else {
throw "oops cannot parse-in " + entity
}
}
// try to refresh, and if duplicte-name-error is thrown then alert and revert the change
function revert_refresh() {
try {
refresh()
} catch(e) {
if (e == "duplicate_present") {
alert("cannot create object with duplicate named entries")
$(this).val($(this).data("prev"))
revert_refresh()
} else {
throw e
}
}
}
//refresh everything about the graph and call refresh_handler
function refresh() {
var obj = getObject(container.children("."+class_names["content"]))
container.empty()
makeEditor(obj)
if (refreshHandler) {
refreshHandler(obj)
}
}
//under the assumption that the initial container is empty, set the class of the container appropriate and create the DOM graph from argument.
function makeEditor(initial) {
if (initial == undefined) {
initial = {}
}
element = $(container).addClass(class_names["container"])
makeEntity(container, initial)
}
//set the DOM graph to given object, and call refresh_handler
function set(obj) {
container.empty()
makeEditor(obj)
if (refreshHandler) {
refreshHandler(obj)
}
}
//get the javascript-object from the DOM fields
function get() {
return getObject(container.children("."+class_names["content"]))
}
//return control
return {
"set": set,
"get": get,
"setRefreshHandler": setRefreshHandler,
"resetConfig": resetConfig
}
}
$.makeObjEditor = function(element, initial, config) {
var ctx = context(element, config)
ctx.set(initial)
return ctx
}
$.fn.makeObjEditor = function(initial, config) {
var ctx = context(this, config)
ctx.set(initial)
return ctx
}
})(document, jQuery);