Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Cantino authored and Andrew Cantino committed Oct 15, 2008
1 parent ceefbf8 commit ebc1696
Showing 1 changed file with 48 additions and 16 deletions.
64 changes: 48 additions & 16 deletions index.html
Expand Up @@ -80,7 +80,7 @@
this.container = $(this.wrapped.parent());

this.builderTab = $('<li><a href="#">builder</a></li>').click(function(e) {
self.rebuild();
self.rebuild(true);
self.builder.show();
self.wrapped.hide();
return false;
Expand Down Expand Up @@ -131,18 +131,39 @@
});
};

JSONEditor.prototype.DELETE = function(key, struct) {
JSONEditor.prototype.DELETE = function(key, struct, layerOnly) {
var self = this;
return $('<a class="icon" href="#"><img src="delete.png" /></a>').click(function(e) {
delete struct[key];
return $('<a class="icon" href="#"><img src="delete.png" border=0/></a>').click(function(e) {
var didSomething = false;
if (struct[key] instanceof Array) {
if(struct[key].length > 0) {
struct[key] = struct[key][0];
didSomething = true;
}
} else if (struct[key] instanceof Object) {
for (var i in struct[key]) {
struct[key] = struct[key][i];
didSomething = true;
break;
}
}
if (didSomething) {
self.rebuild();
return false;
}
if (struct instanceof Array) {
struct.splice(key, 1);
} else {
delete struct[key];
}
self.rebuild();
return false;
});
};

JSONEditor.prototype.ADD = function(struct) {
var self = this;
return $('<a class="icon" href="#"><img src="add.png" /></a>').click(function(e) {
return $('<a class="icon" href="#"><img src="add.png" border=0/></a>').click(function(e) {
struct['??'] = '??';
self.rebuild();
return false;
Expand All @@ -153,10 +174,11 @@
this.wrapped.get(0).value = JSON.stringify(this.json, null, 2);
};

JSONEditor.prototype.rebuild = function() {
if (this.json) this.storeToText();
JSONEditor.prototype.rebuild = function(doNotRefreshText) {
if (this.json && !doNotRefreshText) this.storeToText();
this.cleanBuilder();
this.json = JSON.parse(this.wrapped.get(0).value);
this.alreadyFocused = false;
this.build(this.json, this.builder);
};

Expand All @@ -180,9 +202,10 @@

JSONEditor.prototype.edit = function(e, key, struct, kind){
var self = this;
var form = $("<form></form>").css('display', 'inline');
var input = document.createElement("INPUT");
input.value = e.text();
input.onblur = function() {
var onblur = function() {
var val = input.value;
if(kind == 'key') {
struct[val] = struct[key];
Expand All @@ -192,8 +215,12 @@
}
e.text(val);
e.get(0).editing = false;
}
$(e).html(input);
if (key != val) self.rebuild();
return false;
};
$(input).blur(onblur);
$(form).submit(onblur).append(input);
$(e).html(form);
input.focus();
};

Expand All @@ -219,11 +246,12 @@
return true;
});

// Auto-edit '??' keys.
if (kind == 'key' && text == '??') {
// Auto-edit '??' keys and values.
if (text == '??' && !this.alreadyFocused) {
this.alreadyFocused = true;
elem.click();
$(this).oneTime(100, function() { // Because JavaScript is annoying and we need to focus once the current stuff is done.
elem.get(0).firstChild.focus();
elem.find('input').focus().select();
});
}
return elem;
Expand All @@ -234,19 +262,22 @@
var bq = $(document.createElement("BLOCKQUOTE"));
var bq2 = $(document.createElement("BLOCKQUOTE"));
bq.append($("<div>[</div>"));
if (parent) bq.prepend(this.DELETE(key, parent));
bq.append(bq2);
for(var i = 0; i < json.length; i++) {
this.build(json[0], bq2, i, json);
// if (parent) bq.prepend(this.DELETE(key, parent));
this.build(json[0], bq2, json, i);
}
bq.append($("<div>]</div>"));
node.append(bq);
} else if (json instanceof Object) {
node.append($('<div>{</div>').prepend(this.DELETE(key, parent)).prepend(this.ADD(json)));
node.append($('<div>{</div>'));
if (parent) node.prepend(this.DELETE(key, parent, true));
node.prepend(this.ADD(json));
var bq = $(document.createElement("BLOCKQUOTE"));
node.append(bq);
for(var i in json){
var div = $('<div></div>');
div.append(this.DELETE(i.toString(), json));
div.append(this.BRACE(i.toString(), json));
div.append(this.BRACKET(i.toString(), json));
div.append(this.editable(i.toString(), i.toString(), json, 'key').wrap('<b class="key"></b>').parent());
Expand All @@ -256,6 +287,7 @@
}
node.append($('<div>}</div>'));
} else {
if (parent) node.prepend(this.DELETE(key, parent));
node.append(this.editable(json.toString(), key, parent, 'value').wrap('<span class="val"></span>').parent());
}
};
Expand Down

0 comments on commit ebc1696

Please sign in to comment.