-
Notifications
You must be signed in to change notification settings - Fork 21
/
hulk.js
509 lines (449 loc) · 16.9 KB
/
hulk.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
(function($) {
// ie hack, http://stackoverflow.com/a/10183573/329700
if(!(window.console && console.log)) {
console = {
log: function(){},
debug: function(){},
info: function(){},
warn: function(){},
error: function(){}
};
}
var DEFAULT_SEPARATOR = "=>";
/**
* Return a jQuery element for a save button
*/
var getSaveButton = function() {
var button = document.createElement('button');
button.setAttribute('class', 'hulk-save');
button.innerHTML = 'Save';
return button;
};
/********************** Handlers ******************************/
var attachSaveHandler = function(button, callback) {
$(button).on('click', callback);
};
var attachCollapseHandler = function(button) {
$(button).on('click', function(e) {
e.preventDefault();
var value = $(button).next();
if ($(button).hasClass('collapsed')) {
// Expand it
$(button).text('Collapse');
$(value).slideDown('slow', function() {
$(button).removeClass('collapsed');
});
} else {
$(value).slideUp('slow', function() {
$(button).addClass('collapsed');
$(button).text('Expand');
});
}
});
};
/**
* Add a handler to append a new element as HTML to the list
*/
var attachAddArrayElementHandler = function(button, options) {
$(button).on('click', function(e) {
e.preventDefault();
var elementHTML = createArrayElementHTML("", options);
$(button).before(elementHTML);
});
};
/**
* Add a handler to append a new dictionary as HTML to the list
*
* XXX: consider refactoring this with the function above
*/
var attachAddKeyValuePairElementHandler = function(button, options) {
$(button).on('click', function(e) {
e.preventDefault();
var elementHTML = createArrayElementHTML({"": ""}, options);
$(button).before(elementHTML);
});
};
var attachKeyValuePairHandler = function(button, options) {
$(button).on('click', function(e) {
e.preventDefault();
var elementHTML = createKeyValuePairHTML("", "", options);
$(button).before(elementHTML);
});
};
/********************** Utilities ******************************/
var getOptionOrDefault = function(options, optionName, defaultValue) {
if (typeof options === "undefined") {
return defaultValue;
}
if (typeof options[optionName] === "undefined") {
return defaultValue;
}
return options[optionName];
};
/**
* Detect whether a string is a number-ish value
*
* @param n string
* @return boolean Whether the string is a number
*/
var isNumber = function(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
};
/**
* Detect whether an object is a dictionary (eg, {}) instead of a list or
* another data type
*
* Implementation taken from goog.typeOf here:
* docs.closure-library.googlecode.com/git/closure_goog_base.js.source.html
*/
var isDictionary = function(obj) {
return typeof obj === "object" && ! (obj instanceof Array);
};
/**
* Detect whether a list is a list of dictionaries
*
* Of course in Javascript a list can combine any types, in this case we are
* strict and care only if the list is made up entirely of dictionaries. You
* shouldn't mix types in a list.
*/
var isArrayOfDictionaries = function(list) {
try {
return list.filter(isDictionary).length === list.length;
} catch (TypeError) {
return false;
}
};
/********* These functions take JSON and return HTML **********/
/**
* Create HTML for a map key/value pair
*/
var createKeyValuePairHTML = function(key, value, options) {
var separator = getOptionOrDefault(
options, "separator", DEFAULT_SEPARATOR);
var depth = getOptionOrDefault(options, "depth", -1);
var pair = $(document.createElement('div'));
pair.addClass('hulk-map-pair');
var keyHTML = $(document.createElement('input'));
keyHTML.addClass('hulk-map-key');
var valuesOnly = getOptionOrDefault(options, "permissions", []);
if ($.inArray("values-only", valuesOnly) !== -1) {
keyHTML.prop("readonly", true);
keyHTML.css("border", "0px");
}
keyHTML.attr('value', key);
pair.append(keyHTML);
var separatorElement = $(document.createElement('p'));
separatorElement.addClass('hulk-separator');
separatorElement.text(separator);
pair.append(separatorElement);
var optionsCopy = jQuery.extend(true, {}, options);
if (depth > 0) {
optionsCopy.depth = depth - 1;
}
var valueHTML = convertJSONToHTML(value, optionsCopy, key);
var hideToggle = getOptionOrDefault(options, "permissions", []);
valueHTML.addClass('hulk-map-value-container');
if (valueHTML.children('.hulk-map-pair, .hulk-array-element').length > 0) {
var button = $(document.createElement('button'));
button.addClass('hulk-collapse-item');
if (depth !== 0) {
button.text("Collapse");
attachCollapseHandler(button);
if ($.inArray("hide-toggle", hideToggle) === -1) {
pair.append(button);
}
} else {
button.addClass('collapsed');
button.text("Expand");
valueHTML.hide();
attachCollapseHandler(button);
if ($.inArray("hide-toggle", hideToggle) === -1) {
pair.append(button);
}
}
}
pair.append(valueHTML);
return pair;
};
var createMapHTML = function(map, options, optionalKey) {
var mapHTML = $(document.createElement('div'));
mapHTML.addClass('hulk-map');
// ugh, http://stackoverflow.com/q/5467129/329700
var key;
var keys = [];
for (key in map) {
if (map.hasOwnProperty(key)) {
keys.push(key);
}
}
keys.sort();
for (var j = 0; j < keys.length; j++) {
key = keys[j];
var value = map[key];
var pair = createKeyValuePairHTML(key, value, options);
mapHTML.append(pair);
}
var addPairElement = $(document.createElement('button'));
addPairElement.addClass('hulk-map-add-pair');
var text = getAddElementText("key/value pair", optionalKey);
addPairElement.text(text);
attachKeyValuePairHandler(addPairElement, options);
var noAppend = getOptionOrDefault(options, "permissions", []);
if ($.inArray("no-append", noAppend) !== -1) {
return mapHTML;
}
mapHTML.append(addPairElement);
return mapHTML;
};
/**
* Create HTML for a JSON array element
*
* @return a jQuery object for the element, plus what's inside the element
*/
var createArrayElementHTML = function(element, options) {
var elementHTML = $(document.createElement('div'));
elementHTML.addClass('hulk-array-element');
elementHTML.html(convertJSONToHTML(element, options));
return elementHTML;
};
var getAddElementText = function(elementName, optionalKey) {
var type = typeof optionalKey;
var maxKeyLength = 30;
if (type === "undefined" || type !== "string") {
return ["Add new ", elementName].join("");
}
if (optionalKey.length > maxKeyLength) {
return [
"Add new ",
elementName,
" to ",
optionalKey.substring(0, maxKeyLength),
"..."
].join("");
} else {
return [
"Add new ",
elementName,
" to ",
optionalKey
].join("");
}
};
var createArrayHTML = function(data, options, optionalKey) {
var array = $(document.createElement('div'));
array.addClass('hulk-array');
for (var i = 0; i < data.length; i++) {
var elementHTML = createArrayElementHTML(data[i], options);
array.append(elementHTML);
}
// If it's a list of dictionaries, add an option to add
// a dictionary.
var text;
if (isArrayOfDictionaries(data)) {
var addPairElement = $(document.createElement('button'));
addPairElement.addClass('hulk-array-add-pair');
text = getAddElementText("object", optionalKey);
addPairElement.text(text);
attachAddKeyValuePairElementHandler(addPairElement, options);
array.append(addPairElement);
} else {
// Otherwise, you can only add new values.
var addRowElement = $(document.createElement('button'));
addRowElement.addClass('hulk-array-add-row');
text = getAddElementText("element", optionalKey);
addRowElement.text(text);
attachAddArrayElementHandler(addRowElement, options);
array.append(addRowElement);
}
return array;
};
/**
* Converts a simple JSON value (string, boolean, number) into HTML
*
* @return jQuery
*/
var createInputHTML = function(input, type) {
var maxInputTextLength = 80;
var valueInput;
if (type === "string" && input.length > maxInputTextLength) {
valueInput = $(document.createElement('textarea'));
// XXX Make this configurable?
valueInput.attr('rows', 7);
valueInput.attr('cols', 80);
valueInput.addClass('hulk-input-textarea');
} else if (type === "boolean") {
valueInput = $(document.createElement('select'));
var trueOption = $(document.createElement('option'));
trueOption.attr('value', 'true').text('true');
var falseOption = $(document.createElement('option'));
falseOption.attr('value', 'false').text('false');
valueInput.append(trueOption);
valueInput.append(falseOption);
valueInput.addClass('hulk-input-select');
} else {
valueInput = $(document.createElement('input'));
valueInput.addClass('hulk-input-text');
}
valueInput.addClass('hulk-map-value');
if (type === "boolean") {
valueInput.val(input.toString());
} else {
valueInput.val(input);
}
return valueInput;
};
/**
* Convert a JSON object into HTML
*
* This should handle most JSON objects, per the specification here:
* http://www.json.org/
*
* This function calls itself recursively
*
* @param object data the JSON object to convert
* @param object|undefined options the user specified options
* @param string optionalKey for dictionary values, pass in the associated key
*/
var convertJSONToHTML = function(data, options, optionalKey) {
var type = typeof data;
// typeof null === "object", so we compare directly against null
if (type === "string" || type === "number" || type === "boolean" || data === null) {
return createInputHTML(data, type);
}
// XXX: the JSON specification allows for fractions and exponents.
// Handle them here.
// javascript you're drunk. http://stackoverflow.com/a/4775737/329700
if (Object.prototype.toString.call(data) === '[object Array]') {
return createArrayHTML(data, options, optionalKey);
}
// Now that we've covered the other cases, only dictionaries should be
// left, in theory.
return createMapHTML(data, options, optionalKey);
};
/************ these functions take HTML and return JSON ***************/
/**
* Take a value and try to parse it into JSON data types
*
* For example, "true" => true
*
* @param string value
* @param object|undefined options
* @return The returned data type
*/
var parseTextInput = function(value, options) {
// XXX: the JSON specification allows for fractions and exponents,
// handle them here.
if (isNumber(value)) {
return parseFloat(value);
}
if (value === "true") {
return true;
}
if (value === "false") {
return false;
}
/**
* Note: there's some data loss here as we cannot detect between the
* empty string and null. In theory we could attach a data-* attribute
* to the input and use that but you'd still break if the user voided a
* field while editing the JSON.
*/
if (value === null || value === "null") {
return null;
}
if (value.length === 0) {
var emptyString = getOptionOrDefault(options, "emptyString", false);
return emptyString === true ? "" : null;
}
return value;
};
/**
* this function calls itself recursively
*
* input: a JQuery object (the editor in JSON)
* output: a JSON object
*/
var reassembleJSON = function(html, options) {
var mapChildren = html.children('.hulk-map');
if (mapChildren.length > 0) {
return reassembleJSON(mapChildren, options);
}
var mapItems = html.children('.hulk-map-pair');
if (mapItems.length > 0) {
var d = {};
mapItems.each(function(index, element) {
var $element = $(element);
var key = $element.children('.hulk-map-key');
// XXX if multiple elements have the same key, last one wins.
// what should actually be done here? warn?
if (key.val() === "") {
return;
}
d[key.val()] = reassembleJSON(
$element.children('.hulk-map-value-container'), options);
});
return d;
}
var arrayChildren = html.children('.hulk-array');
if (arrayChildren.length > 0) {
return reassembleJSON(arrayChildren, options);
}
if (html.hasClass('hulk-array')) {
var array = [];
html.children('.hulk-array-element').each(function(index, element) {
array.push(reassembleJSON($(element), options));
});
return array;
}
if (html.hasClass('hulk-map-value')) {
var smartParsing = getOptionOrDefault(options, "smartParsing", true);
if (!smartParsing) {
return html.val();
}
return parseTextInput(html.val(), options);
}
// hack, merge this with the above conditional
var valueChild = html.children('.hulk-map-value');
if (valueChild.length) {
return reassembleJSON(valueChild, options);
}
if (html.hasClass('hulk-map-value-container')) {
return reassembleJSON(html.children('.hulk-map-value'), options);
}
return {};
};
/********************* Exported Functions **********************/
$.hulk = function(selector, data, callback, options) {
if (isDictionary(callback)) {
console.warn("Dictionary " + JSON.stringify(callback) + " passed " +
"as the callback (3rd) argument, probably meant to pass it " +
"as the options (4th) argument");
};
// get option settings
var $element = $(selector);
$element.addClass('hulk');
if ($element.length === 0) {
// XXX console doesn't always exist
console.error("Attempting to hulk-ify element with selector " +
selector + " failed because the element does not exist. " +
"Quitting");
return;
}
var html = convertJSONToHTML(data, options);
$element.html(html);
var showSaveButton = getOptionOrDefault(options, "showSaveButton", true);
if (showSaveButton) {
var button = getSaveButton();
attachSaveHandler(button, function(event) {
var newData = reassembleJSON($element.children(), options);
callback(newData);
event.preventDefault();
});
$element.append(button);
}
return $element;
};
$.hulkSmash = function(selector, options) {
return reassembleJSON($(selector), options);
};
}(jQuery));