4
Copyright DHTMLX LTD. http://www.dhtmlx.com
5
You allowed to use this component or parts of it under GPL terms
6
To use it on other terms or get Professional edition of the component please contact us at sales@dhtmlx.com
8
function jsonPointer(data,parent){
12
jsonPointer.prototype={
13
text:function(){ var afff=function(n){ var p=[]; for(var i=0; i<n.length; i++) p.push("{"+sfff(n[i])+"}"); return p.join(","); }; var sfff=function(n){ var p=[]; for (var a in n) if (typeof(n[a])=="object"){ if (a.length) p.push('"'+a+'":['+afff(n[a])+"]"); else p.push('"'+a+'":{'+sfff(n[a])+"}"); }else p.push('"'+a+'":"'+n[a]+'"'); return p.join(","); }; return "{"+sfff(this.d)+"}"; },
14
get:function(name){return this.d[name]; },
15
exists:function(){return !!this.d },
16
content:function(){return this.d.content; },
17
each:function(name,f,t){ var a=this.d[name]; var c=new jsonPointer(); if (a) for (var i=0; i<a.length; i++) { c.d=a[i]; f.apply(t,[c,i]); } },
18
get_all:function(){ return this.d; },
19
sub:function(name){ return new jsonPointer(this.d[name],this.d) },
20
sub_exists:function(name){ return !!this.d[name]; },
21
each_x:function(name,rule,f,t,i){ var a=this.d[name]; var c=new jsonPointer(0,this.d); if (a) for (i=i||0; i<a.length; i++) if (a[i][rule]) { c.d=a[i]; if(f.apply(t,[c,i])==-1) return; } },
22
up:function(name){ return new jsonPointer(this.d.parentNode,this.d); },
23
set:function(name,val){ this.d[name]=val; },
24
clone:function(name){ return new jsonPointer(this.d,this.dp); },
25
through:function(name,rule,v,f,t){ var a=this.d[name]; if (a.length) for (var i=0; i<a.length; i++) { if (a[i][rule]!=null && a[i][rule]!="" && (!v || a[i][rule]==v )) { var c=new jsonPointer(a[i],this.d); f.apply(t,[c,i]); } var w=this.d; this.d=a[i]; if (this.sub_exists(name)) this.through(name,rule,v,f,t); this.d=w; } }
29
* @desc: load tree from js array file|stream
31
* @param: file - link to JSArray file
32
* @param: afterCall - function which will be called after xml loading
35
dhtmlXTreeObject.prototype.loadJSArrayFile=function(file,afterCall){
36
if (!this.parsCount) this.callEvent("onXLS",[this,this._ld_id]); this._ld_id=null; this.xmlstate=1;
39
this.XMLLoader=new dtmlXMLLoaderObject(function(){
40
eval("var z="+arguments[4].xmlDoc.responseText);
42
},this,true,this.no_cashe);
43
if (afterCall) this.XMLLoader.waitCall=afterCall;
44
this.XMLLoader.loadXML(file);
48
* @desc: load tree from csv file|stream
50
* @param: file - link to CSV file
51
* @param: afterCall - function which will be called after xml loading
54
dhtmlXTreeObject.prototype.loadCSV=function(file,afterCall){
55
if (!this.parsCount) this.callEvent("onXLS",[this,this._ld_id]); this._ld_id=null; this.xmlstate=1;
57
this.XMLLoader=new dtmlXMLLoaderObject(function(){
58
that.loadCSVString(arguments[4].xmlDoc.responseText);
59
},this,true,this.no_cashe);
60
if (afterCall) this.XMLLoader.waitCall=afterCall;
61
this.XMLLoader.loadXML(file);
65
* @desc: load tree from js array object
67
* @param: ar - js array
68
* @param: afterCall - function which will be called after xml loading
71
dhtmlXTreeObject.prototype.loadJSArray=function(ar,afterCall){
72
//array id,parentid,text
74
for (var i=0; i<ar.length; i++){
75
if (!z[ar[i][1]]) z[ar[i][1]]=[];
76
z[ar[i][1]].push({id:ar[i][0],text:ar[i][2]});
79
var top={id: this.rootId};
80
var f=function(top,f){
83
for (var j=0; j<top.item.length; j++)
88
this.loadJSONObject(top,afterCall);
93
* @desc: load tree from csv string
95
* @param: csv - csv string
96
* @param: afterCall - function which will be called after xml loading
99
dhtmlXTreeObject.prototype.loadCSVString=function(csv,afterCall){
100
//array id,parentid,text
102
var ar=csv.split("\n");
103
for (var i=0; i<ar.length; i++){
104
var t=ar[i].split(",");
105
if (!z[t[1]]) z[t[1]]=[];
106
z[t[1]].push({id:t[0],text:t[2]});
109
var top={id: this.rootId};
110
var f=function(top,f){
113
for (var j=0; j<top.item.length; j++)
118
this.loadJSONObject(top,afterCall);
123
* @desc: load tree from json object
125
* @param: json - json object
126
* @param: afterCall - function which will be called after xml loading
129
dhtmlXTreeObject.prototype.loadJSONObject=function(json,afterCall){
130
if (!this.parsCount) this.callEvent("onXLS",[this,null]);this.xmlstate=1;
131
var p=new jsonPointer(json);
134
if (afterCall) afterCall();
139
* @desc: load tree from json file
141
* @param: file - link to JSON file
142
* @param: afterCall - function which will be called after xml loading
145
dhtmlXTreeObject.prototype.loadJSON=function(file,afterCall){
146
if (!this.parsCount) this.callEvent("onXLS",[this,this._ld_id]); this._ld_id=null; this.xmlstate=1;
149
this.XMLLoader=new dtmlXMLLoaderObject(function(){
150
eval("var t="+arguments[4].xmlDoc.responseText);
151
var p=new jsonPointer(t);
154
},this,true,this.no_cashe);
156
if (afterCall) this.XMLLoader.waitCall=afterCall;
157
this.XMLLoader.loadXML(file);
162
* @desc: return tree as json string
166
dhtmlXTreeObject.prototype.serializeTreeToJSON=function(){
167
var out=['{"id":"'+this.rootId+'", "item":['];
169
for (var i=0; i<this.htmlNode.childsCount; i++)
170
p.push(this._serializeItemJSON(this.htmlNode.childNodes[i]));
171
out.push(p.join(","));
175
dhtmlXTreeObject.prototype._serializeItemJSON=function(itemNode){
177
if (itemNode.unParsed)
178
return (itemNode.unParsed.text());
180
if (this._selected.length)
181
var lid=this._selected[0].id;
183
var text=itemNode.span.innerHTML;
185
if (this._xescapeEntities)
186
for (var i=0; i<this._serEnts.length; i++)
187
text=text.replace(this._serEnts[i][2],this._serEnts[i][1]);
190
out.push('{ "id":"'+itemNode.id+'", '+(this._getOpenState(itemNode)==1?' "open":"1", ':'')+(lid==itemNode.id?' "select":"1",':'')+' "text":"'+text+'"'+( ((this.XMLsource)&&(itemNode.XMLload==0))?', "child":"1" ':''));
192
out.push('{ "id":"'+itemNode.id+'", '+(this._getOpenState(itemNode)==1?' "open":"1", ':'')+(lid==itemNode.id?' "select":"1",':'')+' "text":"'+text+'", "im0":"'+itemNode.images[0]+'", "im1":"'+itemNode.images[1]+'", "im2":"'+itemNode.images[2]+'" '+(itemNode.acolor?(', "aCol":"'+itemNode.acolor+'" '):'')+(itemNode.scolor?(', "sCol":"'+itemNode.scolor+'" '):'')+(itemNode.checkstate==1?', "checked":"1" ':(itemNode.checkstate==2?', "checked":"-1"':''))+(itemNode.closeable?', "closeable":"1" ':''));
194
if ((this._xuserData)&&(itemNode._userdatalist))
196
out.push(', "userdata":[');
197
var names=itemNode._userdatalist.split(",");
199
for (var i=0; i<names.length; i++)
200
p.push('{ "name":"'+names[i]+'" , "content":"'+itemNode.userData["t_"+names[i]]+'" }');
201
out.push(p.join(",")); out.push("]");
204
if (itemNode.childsCount){
205
out.push(', "item":[');
207
for (var i=0; i<itemNode.childsCount; i++)
208
p.push(this._serializeItemJSON(itemNode.childNodes[i]));
209
out.push(p.join(","));
216
//(c)dhtmlx ltd. www.dhtmlx.com
b'\\ No newline at end of file'