-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathobjeditor.js
41 lines (38 loc) · 1.33 KB
/
objeditor.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
// Object renderer
var ObjEditor = function(elem, callback) {
r = this;
this.elem = elem;
this.callback = callback;
$(elem).delegate('.property.new .key', "change", function(e) {
e.stopImmediatePropagation();
$this = $(this);
$prop = $this.parents('.property');
$this.replaceWith('<label class="key">' + $this.val() + '</label>');
$prop
.removeClass('new')
.trigger('change');
});
$(elem).delegate('.new', "change", function(e) {
e.stopImmediatePropagation();
});
$(elem).bind('change', function(e){ callback() });
}
ObjEditor.prototype.render = function(obj) {
$list = $('<ul id="object">');
for ( k in obj ) {
$('<li class="property">').html('<label class="key">' + String(k) + '</label> <input class="value" name="' + String(k) + '"type="text" value="' + String(obj[k]) + '"></input>').appendTo($list);
}
$(this.elem).empty().append($list);
this.newProp();
}
ObjEditor.prototype.decode = function() {
obj = {}
$(this.elem).find('.property:not(.new)').each(function(e) {
$node = $(this);
obj[$node.find("label").text()] = $node.find("input").val();
});
return obj;
};
ObjEditor.prototype.newProp = function() {
$(this.elem).find('#object').append('<li class="new property"><input class="key" value="Key..."></input><input class="value" value="Value..."></input></li>');
};