forked from josdejong/jsoneditor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsoneditor.js
More file actions
2487 lines (2195 loc) · 69.6 KB
/
jsoneditor.js
File metadata and controls
2487 lines (2195 loc) · 69.6 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
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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file jsoneditor.js
*
* @brief
* JSONEditor is an editor to display and edit JSON data in a treeview.
*
* Supported browsers: Chrome, Firefox, Safari, Opera, Internet Explorer 8+
*
* @license
* This json editor is open sourced with the intention to use the editor as
* a component in your own application. Not to just copy and monetize the editor
* as it is.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy
* of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*
* Copyright (c) 2011-2012 Jos de Jong, http://jsoneditoronline.org
*
* @author Jos de Jong, <wjosdejong@gmail.com>
* @date 2012-05-30
*/
// Internet Explorer 8 and older does not support Array.indexOf,
// so we define it here in that case
// http://soledadpenades.com/2007/05/17/arrayindexof-in-internet-explorer/
if(!Array.prototype.indexOf) {
Array.prototype.indexOf = function(obj){
for(var i = 0; i < this.length; i++){
if(this[i] == obj){
return i;
}
}
return -1;
}
}
// define variable JSON, needed for correct error handling on IE7 and older
var JSON;
/**
* JSONEditor
* @param {Element} container Container element
* @param {Object} json JSON object
*/
JSONEditor = function (container, json) {
// check availability of JSON parser (not available in IE7 and older)
if (!JSON) {
throw new Error ('Your browser does not support JSON. \n\n' +
'Please install the newest version of your browser.\n' +
'(all modern browsers support JSON).');
}
if (!container) {
throw new Error('No container element provided.');
}
this.container = container;
this._createFrame();
this._createTable();
this.set(json || {});
};
// node currently being edited
JSONEditor.focusNode = undefined;
/**
* Set JSON object in editor
* @param {Object} json
*/
JSONEditor.prototype.set = function (json) {
this.content.removeChild(this.table); // Take the table offline
// replace the root node
var node = new JSONEditor.Node({
'value': json
});
this._setRoot(node);
// expand
var recurse = false;
this.node.expand(recurse);
this.content.appendChild(this.table); // Put the table online again
};
/**
* Get JSON object from editor
* @return {Object} json
*/
JSONEditor.prototype.get = function () {
// remove focus from currently edited node
if (JSONEditor.focusNode) {
JSONEditor.focusNode.blur();
}
if (this.node) {
return this.node.getValue();
}
else {
return {};
}
};
/**
* Remove the root node from the editor
*/
JSONEditor.prototype.clear = function () {
if (this.node) {
this.node.collapse();
this.tbody.removeChild(this.node.getDom());
delete this.node;
}
};
/**
* Set the root node for the json editor
* @param {JSONEditor.Node} node
*/
JSONEditor.prototype._setRoot = function (node) {
this.clear();
this.node = node;
this.tbody.appendChild(node.getDom());
};
/**
* Expand all nodes
*/
JSONEditor.prototype.expandAll = function () {
if (this.node) {
this.content.removeChild(this.table); // Take the table offline
this.node.expand();
this.content.appendChild(this.table); // Put the table online again
}
};
/**
* Collapse all nodes
*/
JSONEditor.prototype.collapseAll = function () {
if (this.node) {
this.content.removeChild(this.table); // Take the table offline
this.node.collapse();
this.content.appendChild(this.table); // Put the table online again
}
};
/**
* @constructor JSONEditor.Node
* Create a new Node
* @param {Object} params Can contain parameters: field, fieldEditable, value.
*/
JSONEditor.Node = function (params) {
this.dom = {};
this.expanded = false;
if(params && (params instanceof Object)) {
this.setField(params.field, params.fieldEditable);
this.setValue(params.value);
}
else {
this.setField();
this.setValue();
}
};
/**
* Set field
* @param {String} field
* @param {boolean} fieldEditable
*/
JSONEditor.Node.prototype.setField = function(field, fieldEditable) {
this.field = field;
this.fieldEditable = (fieldEditable == true);
};
/**
* Get field
* @return {String}
*/
JSONEditor.Node.prototype.getField = function() {
if (this.field === undefined) {
this.field = this._getDomField();
}
return this.field;
};
/**
* Set value. Value is a JSON structure or an element String, Boolean, etc.
* @param {*} value
*/
JSONEditor.Node.prototype.setValue = function(value) {
// first clear all current childs (if any)
var childs = this.childs;
if (childs) {
while (childs.length) {
this.removeChild(childs[0]);
}
}
// TODO: remove the DOM of this Node
this.type = this._getType(value);
if (this.type == 'array') {
// array
this.childs = [];
for (var i = 0, iMax = value.length; i < iMax; i++) {
var child = new JSONEditor.Node({
// 'field': i, // TODO: cleanup
'value': value[i]
});
this.appendChild(child);
}
this.value = '';
}
else if (this.type == 'object') {
// object
this.childs = [];
for (var childField in value) {
if (value.hasOwnProperty(childField)) {
var child = new JSONEditor.Node({
'field': childField,
'value': value[childField]
});
this.appendChild(child);
}
}
this.value = '';
}
else {
// value
this.childs = undefined;
this.value = value;
/* TODO
if (typeof(value) == 'string') {
var escValue = JSON.stringify(value);
this.value = escValue.substring(1, escValue.length - 1);
console.log('check', value, this.value);
}
else {
this.value = value;
}
*/
}
};
/**
* Get value. Value is a JSON structure
* @return {*} value
*/
JSONEditor.Node.prototype.getValue = function() {
//this._getDomValue(); // TODO: cleanup
if (this.type == 'array') {
var arr = [];
var childs = this.childs;
for (var i = 0, iMax = childs.length; i < iMax; i++) {
arr.push(childs[i].getValue());
}
return arr;
}
else if (this.type == 'object') {
var obj = {};
var childs = this.childs;
for (var i = 0, iMax = childs.length; i < iMax; i++) {
var child = childs[i];
obj[child.getField()] = child.getValue();
}
return obj;
}
else {
if (this.value === undefined) {
this.value = this._getDomValue();
}
return this.value;
}
};
/**
* Get the nesting level of this node
* @return {Number} level
*/
JSONEditor.Node.prototype.getLevel = function() {
return (this.parent ? this.parent.getLevel() + 1 : 0);
};
/**
* Create a clone of a node
* The complete state of a clone is copied, including whether it is expanded or
* not. The DOM elements are not cloned.
* @return {JSONEditor.Node} clone
*/
JSONEditor.Node.prototype.clone = function() {
var clone = new JSONEditor.Node();
clone.type = this.type;
clone.field = this.field;
clone.fieldHTML = this.fieldHTML;
clone.fieldEditable = this.fieldEditable;
clone.value = this.value;
clone.valueHTML = this.valueHTML;
clone.expanded = this.expanded;
if (this.childs) {
// an object or array
var childs = this.childs;
var cloneChilds = [];
for (var i = 0, iMax = childs.length; i < iMax; i++) {
var childClone = childs[i].clone();
childClone.parent = clone;
cloneChilds.push(childClone);
}
clone.childs = cloneChilds;
}
else {
// a value
clone.childs = undefined;
}
return clone;
};
/**
* Expand this node and optionally its childs.
* @param {boolean} recurse Optional recursion, true by default. When
* true, all childs will be expanded recursively
*/
JSONEditor.Node.prototype.expand = function(recurse) {
if (!this.childs) {
return;
}
// set this node expanded
this.expanded = true;
if (this.dom.expand) {
this.dom.expand.className = 'jsoneditor-expanded';
}
this.showChilds();
var childs = this.childs;
if (recurse != false) {
for (var i = 0, iMax = childs.length; i < iMax; i++) {
childs[i].expand(recurse);
}
}
};
/**
* Collapse this node and optionally its childs.
* @param {Number} recurse Optional recursion, true by default. When
* true, all childs will be collapsed recursively
*/
JSONEditor.Node.prototype.collapse = function(recurse) {
if (!this.childs) {
return;
}
this.hideChilds();
// collapse childs in case of recurse
var childs = this.childs;
if (recurse != false) {
for (var i = 0, iMax = childs.length; i < iMax; i++) {
childs[i].collapse(recurse);
}
}
// make this node collapsed
if (this.dom.expand) {
this.dom.expand.className = 'jsoneditor-collapsed';
}
this.expanded = false;
};
/**
* Recursively show all childs when they are expanded
*/
JSONEditor.Node.prototype.showChilds = function() {
var childs = this.childs;
if (!childs) {
return;
}
if (!this.expanded) {
return;
}
var tr = this.dom.tr;
var table = tr.parentNode;
if (table) {
// show row with append button
var append = this.getAppend();
var nextTr = tr.nextSibling;
if (nextTr) {
table.insertBefore(append, nextTr);
}
else {
table.appendChild(append);
}
// show childs
for (var i = 0, iMax = childs.length; i < iMax; i++) {
var child = childs[i];
table.insertBefore(child.getDom(), append);
child.showChilds();
}
}
};
/**
* Hide the node with all its childs
*/
JSONEditor.Node.prototype.hide = function() {
var tr = this.dom.tr;
var table = tr ? tr.parentNode : undefined;
if (table) {
table.removeChild(tr);
}
this.hideChilds();
};
/**
* Recursively hide all childs
*/
JSONEditor.Node.prototype.hideChilds = function() {
var childs = this.childs;
if (!childs) {
return;
}
if (!this.expanded) {
return;
}
// hide append row
var append = this.getAppend();
if (append.parentNode) {
append.parentNode.removeChild(append);
}
// hide childs
for (var i = 0, iMax = childs.length; i < iMax; i++) {
childs[i].hide();
}
};
/**
* Add a new child to the node.
* Only applicable when Node value is of type array or object
* @param {JSONEditor.Node} node
*/
JSONEditor.Node.prototype.appendChild = function(node) {
if (this.type == 'array' || this.type == 'object') {
// adjust the link to the parent
node.parent = this;
node.fieldEditable = (this.type == 'object');
if (this.type == 'array') {
node.index = this.childs.length;
}
this.childs.push(node);
if (this.expanded) {
// insert into the DOM, before the appendRow
var newtr = node.getDom();
var appendTr = this.getAppend();
var table = appendTr ? appendTr.parentNode : undefined;
if (appendTr && table) {
table.insertBefore(newtr, appendTr);
}
this._updateStatus(node.index);
node.showChilds();
}
node.updateDom();
}
};
/**
* Move an existing child from its current parent to this node
* Only applicable when Node value is of type array or object
* @param {JSONEditor.Node} node
* @param {JSONEditor.Node} beforeNode
*/
JSONEditor.Node.prototype.moveBefore = function(node, beforeNode) {
if (this.type == 'array' || this.type == 'object') {
// create a temporary row, to prevent the scroll position from jumping
// when removing the node
var tbody = (this.dom.tr) ? this.dom.tr.parentNode : undefined;
if (tbody) {
var trTemp = document.createElement('tr');
trTemp.style.height = tbody.clientHeight + 'px';
tbody.appendChild(trTemp);
}
var parent = node.parent;
if (parent) {
parent.removeChild(node);
}
if (beforeNode instanceof JSONEditor.AppendNode) {
this.appendChild(node);
}
else {
this.insertBefore(node, beforeNode);
}
if (tbody) {
tbody.removeChild(trTemp);
}
}
};
/**
* Insert a new child before a given node
* Only applicable when Node value is of type array or object
* @param {JSONEditor.Node} node
* @param {JSONEditor.Node} beforeNode
*/
JSONEditor.Node.prototype.insertBefore = function(node, beforeNode) {
if (this.type == 'array' || this.type == 'object') {
if (beforeNode == this.append) {
// append to the child nodes
// adjust the link to the parent
node.parent = this;
node.fieldEditable = (this.type == 'object');
this.childs.push(node);
}
else {
// insert before a child node
var index = this.childs.indexOf(beforeNode);
if (index == -1) {
throw new Error('Node not found');
}
// adjust the link to the parent
node.parent = this;
node.fieldEditable = (this.type == 'object');
//node.index = index; // TODO: redundant?
this.childs.splice(index, 0, node);
}
if (this.expanded) {
// insert into the DOM
var newTr = node.getDom();
var nextTr = beforeNode.getDom();
var table = nextTr ? nextTr.parentNode : undefined;
if (nextTr && table) {
table.insertBefore(newTr, nextTr);
}
node.showChilds();
}
node.updateDom();
this._updateStatus(index);
}
};
/**
* Set focus to the value of this node
*/
JSONEditor.Node.prototype.focus = function() {
if (this.dom.tr && this.dom.tr.parentNode) {
if (this.fieldEditable) {
var domField = this.dom.field;
if (domField) {
domField.focus();
}
}
else {
var domValue = this.dom.value;
if (domValue) {
domValue.focus();
}
}
}
};
/**
* Remove focus from the value or field of this node
*/
JSONEditor.Node.prototype.blur = function() {
this._getDomValue(true);
this._getDomField(true);
};
/**
* Duplicate given child node
* new structure will be added right before the cloned node
* @param {JSONEditor.Node} node the childNode to be duplicated
*/
JSONEditor.Node.prototype._duplicate = function(node) {
var clone = node.clone();
/* TODO: adjust the field name (to prevent equal field names)
if (this.type == 'object') {
}
*/
// TODO: insert after instead of insert before
this.insertBefore(clone, node);
};
/**
* Check if given node is a child. The method will check recursively to find
* this node.
* @param {JSONEditor.Node} node
* @return {boolean} containsNode
*/
JSONEditor.Node.prototype.containsNode = function(node) {
if (this == node) {
return true;
}
var childs = this.childs;
if (childs) {
for (var i = 0, iMax = childs.length; i < iMax; i++) {
if (childs[i].containsNode(node)) {
return true;
}
}
}
return false;
};
/**
* Move given node into this node
* @param {JSONEditor.Node} node the childNode to be moved
* @param {JSONEditor.Node} beforeNode node will be inserted before given
* node. If no beforeNode is given,
* the node is appended at the end
*/
JSONEditor.Node.prototype._move = function(node, beforeNode) {
if (node == beforeNode) {
// nothing to do...
return;
}
// check if this node is not a child of the node to be moved here
if (node.containsNode(this)) {
throw new Error('Cannot move a field into a child of itself');
}
// remove the original node
if (node.parent) {
node.parent.removeChild(node);
}
// create a clone of the node
var clone = node.clone();
node.clearDom();
// insert or append the node
if (beforeNode) {
this.insertBefore(clone, beforeNode);
}
else {
this.appendChild(clone);
}
/* TODO: adjust the field name (to prevent equal field names)
if (this.type == 'object') {
}
*/
};
/**
* Remove a child from the node.
* Only applicable when Node value is of type array or object
* @param {JSONEditor.Node} node The child node to be removed;
* @return {JSONEditor.Node | undefined} node The removed node on success,
* else undefined
*/
JSONEditor.Node.prototype.removeChild = function(node) {
if (this.childs) {
var index = this.childs.indexOf(node);
if (index != -1) {
node.hide();
var removedNode = this.childs.splice(index, 1)[0];
this._updateStatus(index);
return removedNode;
}
}
return undefined;
};
/**
* change the type of the value of this Node
* @param {String} newType
*/
JSONEditor.Node.prototype._changeType = function (newType) {
var oldType = this.type;
if ((newType == 'string' || newType == 'auto') &&
(oldType == 'string' || oldType == 'auto')) {
// this is an easy change
this.type = newType;
}
else {
// change from array to object, or from string/auto to object/array
var table = this.dom.tr ? this.dom.tr.parentNode : undefined;
var lastTr;
if (this.expanded) {
lastTr = this.getAppend();
}
else {
lastTr = this.getDom();
}
var nextTr = (lastTr && lastTr.parentNode) ? lastTr.nextSibling : undefined;
// hide current field and all its childs
this.hide();
this.clearDom();
// adjust the field and the value
this.type = newType;
// adjust childs
if (newType == 'object') {
if (!this.childs) {
this.childs = [];
}
var childs = this.childs;
for (var i = 0, iMax = childs.length; i < iMax; i++) {
var child = childs[i];
child.clearDom();
delete child.index;
child.fieldEditable = true;
if (child.field == undefined) {
child.field = i;
}
}
if (oldType == 'string' || oldType == 'auto') {
this.expanded = true;
}
}
else if (newType == 'array') {
if (!this.childs) {
this.childs = [];
}
var childs = this.childs;
var fieldEditable = false;
for (var i = 0, iMax = childs.length; i < iMax; i++) {
var child = childs[i];
child.clearDom();
child.fieldEditable = false;
child.index = i;
}
this._updateStatus();
if (oldType == 'string' || oldType == 'auto') {
this.expanded = true;
}
}
else {
this.expanded = false;
}
// create new DOM
if (table) {
if (nextTr) {
table.insertBefore(this.getDom(), nextTr);
}
else {
table.appendChild(this.getDom());
}
}
this.showChilds();
}
if (newType == 'auto' || newType == 'string') {
// cast value to the correct type
if (newType == 'string') {
this.value = String(this.value);
}
else {
this.value = this._stringCast(String(this.value));
}
this.focus();
}
// TODO: test if things are updated twice...
this.updateDom();
};
/**
* Retrieve value from DOM
* @param {boolean} silent. If true (default), no errors will be thrown in
* case of invalid data
*/
JSONEditor.Node.prototype._getDomValue = function(silent) {
if (this.dom.value && this.type != 'array' && this.type != 'object') {
this.valueHTML = this.dom.value.innerHTML;
}
if (this.valueHTML != undefined) {
try {
// retrieve the value
if (this.type == 'string') {
this.value = this._unescape(this._stripHTML(this.valueHTML));
}
else {
var value = this._unescape(this._stripHTML(this.valueHTML));
this.value = this._stringCast(value);
}
}
catch (err) {
this.value = undefined;
if (silent != true) {
throw err;
}
}
}
};
/**
* Update dom value:
* - the text color of the value, depending on the type of the value
* - the height of the field, depending on the width
* - background color in case it is empty
*/
JSONEditor.Node.prototype._updateDomValue = function () {
var domValue = this.dom.value;
if (domValue) {
// set text color depending on value type
var v = this.value;
var t = (this.type == 'auto') ? typeof(v) : this.type;
var color = '';
if (t == 'string') {
color = 'green';
}
else if (t == 'number') {
color = 'red';
}
else if (t == 'boolean') {
color = 'blue';
}
else if (this.type == 'object' || this.type == 'array') {
// note: typeof(null)=="object", therefore check this.type instead of t
color = '';
}
else if (v === null) {
color = 'purple';
}
else if (v === undefined) {
// invalid value
color = 'green';
}
domValue.style.color = color;
// make backgound color lightgray when empty
var isEmpty = (String(this.value) == '' && this.type != 'array' && this.type != 'object');
if (isEmpty) {
JSONEditor.addClassName(domValue, 'jsoneditor-empty');
}
else {
JSONEditor.removeClassName(domValue, 'jsoneditor-empty');
}
// strip formatting from the contents of the editable div
JSONEditor.stripFormatting(domValue);
}
};
/**
* Update dom field:
* - the text color of the field, depending on the text
* - the height of the field, depending on the width
* - background color in case it is empty
*/
JSONEditor.Node.prototype._updateDomField = function () {
var domField = this.dom.field;
if (domField) {
// make backgound color lightgray when empty
var isEmpty = (String(this.field) == '');
if (isEmpty) {
JSONEditor.addClassName(domField, 'jsoneditor-empty');
}
else {
JSONEditor.removeClassName(domField, 'jsoneditor-empty');
}
// strip formatting from the contents of the editable div
JSONEditor.stripFormatting(domField);
}
};
/**
* Retrieve field from DOM
* @param {boolean} silent. If true (default), no errors will be thrown in
* case of invalid data
*/
JSONEditor.Node.prototype._getDomField = function(silent) {
if (this.dom.field && this.fieldEditable) {
this.fieldHTML = this.dom.field.innerHTML;
}
if (this.fieldHTML != undefined) {
try {
this.field = this._unescape(this._stripHTML(this.fieldHTML));
}
catch (err) {
this.field = undefined;
if (silent != true) {
throw err;
}
}
}
};
/**
* Clear the dom of the node
*/
JSONEditor.Node.prototype.clearDom = function() {
// TODO: hide the node first?
//this.hide();
// TOOD: recursively clear dom?
this.dom = {};
};
/**
* Get the HTML DOM TR element of the node.
* The dom will be generated when not yet created
* @return {Element} tr HTML DOM TR Element
*/
JSONEditor.Node.prototype.getDom = function() {
var dom = this.dom;
if (dom.tr) {
return dom.tr;
}
// create row
dom.tr = document.createElement('tr');
dom.tr.className = 'jsoneditor-tr';
dom.tr.node = this;
// create draggable area
var tdDrag = document.createElement('td');
tdDrag.className = 'jsoneditor-td';
tdDrag.title = 'Move field (drag and drop)';
dom.drag = this._createDomDragArea();
if (dom.drag) {
tdDrag.appendChild(dom.drag);
}
dom.tr.appendChild(tdDrag);
// create tree and field
var tdField = document.createElement('td');
tdField.className = 'jsoneditor-td';
dom.tr.appendChild(tdField);
dom.expand = this._createDomExpandButton();
dom.field = this._createDomField();
dom.value = this._createDomValue();
dom.tree = this._createDomTree(dom.expand, dom.field, dom.value);
tdField.appendChild(dom.tree);
// create type select box
var tdType = document.createElement('td');
tdType.className = 'jsoneditor-td jsoneditor-td-edit';
dom.tr.appendChild(tdType);
dom.type = this._createDomTypeButton();
tdType.appendChild(dom.type);
// create duplicate button
var tdDuplicate = document.createElement('td');
tdDuplicate.className = 'jsoneditor-td jsoneditor-td-edit';
dom.tr.appendChild(tdDuplicate);
dom.duplicate = this._createDomDuplicateButton();
if (dom.duplicate) {
tdDuplicate.appendChild(dom.duplicate);