/openshift/adei

To get this branch, use:
bzr branch http://darksoft.org/webbzr/openshift/adei

« back to all changes in this revision

Viewing changes to includes/dhtmlx/ext/dhtmlxtree_json.js

  • Committer: Suren A. Chilingaryan
  • Date: 2008-10-29 03:02:24 UTC
  • Revision ID: csa@dside.dyndns.org-20081029030224-yc3wy2dti4htlfxf
New version of dhtmlxmenu and dhtmlxtree is added

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//v.2.0 build 81009
 
2
 
 
3
/*
 
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
 
7
*/
 
8
function jsonPointer(data,parent){
 
9
        this.d=data;
 
10
        this.dp=parent;
 
11
}
 
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;   } }
 
26
}
 
27
 
 
28
/**
 
29
*     @desc: load tree from js array file|stream
 
30
*     @type: public
 
31
*     @param: file - link to JSArray file
 
32
*     @param: afterCall - function which will be called after xml loading
 
33
*     @topic: 0
 
34
*/
 
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;
 
37
      var that=this;
 
38
      
 
39
      this.XMLLoader=new dtmlXMLLoaderObject(function(){
 
40
        eval("var z="+arguments[4].xmlDoc.responseText);
 
41
        that.loadJSArray(z);
 
42
      },this,true,this.no_cashe);
 
43
          if (afterCall) this.XMLLoader.waitCall=afterCall;
 
44
      this.XMLLoader.loadXML(file);
 
45
   };
 
46
   
 
47
/**
 
48
*     @desc: load tree from csv file|stream
 
49
*     @type: public
 
50
*     @param: file - link to CSV file
 
51
*     @param: afterCall - function which will be called after xml loading
 
52
*     @topic: 0
 
53
*/
 
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;
 
56
      var that=this;
 
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);
 
62
   };
 
63
   
 
64
/**
 
65
*     @desc: load tree from js array object
 
66
*     @type: public
 
67
*     @param: ar - js array
 
68
*     @param: afterCall - function which will be called after xml loading
 
69
*     @topic: 0
 
70
*/  
 
71
dhtmlXTreeObject.prototype.loadJSArray=function(ar,afterCall){
 
72
        //array id,parentid,text
 
73
        var z=[];
 
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]});
 
77
        }
 
78
        
 
79
        var top={id: this.rootId};
 
80
        var f=function(top,f){
 
81
                if (z[top.id]){
 
82
                        top.item=z[top.id];
 
83
                        for (var j=0; j<top.item.length; j++)
 
84
                                f(top.item[j],f);
 
85
                }
 
86
        }
 
87
        f(top,f);
 
88
        this.loadJSONObject(top,afterCall);
 
89
}
 
90
 
 
91
 
 
92
/**
 
93
*     @desc: load tree from csv string
 
94
*     @type: public
 
95
*     @param: csv - csv string 
 
96
*     @param: afterCall - function which will be called after xml loading
 
97
*     @topic: 0
 
98
*/
 
99
dhtmlXTreeObject.prototype.loadCSVString=function(csv,afterCall){
 
100
        //array id,parentid,text
 
101
        var z=[];
 
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]});
 
107
        }
 
108
        
 
109
        var top={id: this.rootId};
 
110
        var f=function(top,f){
 
111
                if (z[top.id]){
 
112
                        top.item=z[top.id];
 
113
                        for (var j=0; j<top.item.length; j++)
 
114
                                f(top.item[j],f);
 
115
                }
 
116
        }
 
117
        f(top,f);
 
118
        this.loadJSONObject(top,afterCall);
 
119
}
 
120
 
 
121
 
 
122
/**
 
123
*     @desc: load tree from json object
 
124
*     @type: public
 
125
*     @param: json - json object
 
126
*     @param: afterCall - function which will be called after xml loading
 
127
*     @topic: 0
 
128
*/
 
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);
 
132
          this._parse(p);
 
133
          this._p=p;
 
134
      if (afterCall) afterCall();
 
135
   };
 
136
   
 
137
 
 
138
/**   
 
139
*     @desc: load tree from json file
 
140
*     @type: public
 
141
*     @param: file - link to JSON file
 
142
*     @param: afterCall - function which will be called after xml loading
 
143
*     @topic: 0
 
144
*/
 
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;
 
147
      var that=this;
 
148
      
 
149
      this.XMLLoader=new dtmlXMLLoaderObject(function(){
 
150
        eval("var t="+arguments[4].xmlDoc.responseText);
 
151
        var p=new jsonPointer(t);
 
152
        that._parse(p);
 
153
        that._p=p;
 
154
      },this,true,this.no_cashe);
 
155
      
 
156
          if (afterCall) this.XMLLoader.waitCall=afterCall;
 
157
      this.XMLLoader.loadXML(file);
 
158
   };   
 
159
   
 
160
   
 
161
/**   
 
162
*     @desc: return tree as json string
 
163
*     @type: public
 
164
*     @topic: 0
 
165
*/
 
166
dhtmlXTreeObject.prototype.serializeTreeToJSON=function(){
 
167
        var out=['{"id":"'+this.rootId+'", "item":['];
 
168
        var p=[];
 
169
                for (var i=0; i<this.htmlNode.childsCount; i++)
 
170
                        p.push(this._serializeItemJSON(this.htmlNode.childNodes[i]));
 
171
        out.push(p.join(","));
 
172
        out.push("]}"); 
 
173
        return out.join("");
 
174
};
 
175
dhtmlXTreeObject.prototype._serializeItemJSON=function(itemNode){
 
176
        var out=[];
 
177
        if (itemNode.unParsed)
 
178
                        return (itemNode.unParsed.text());
 
179
  
 
180
        if (this._selected.length)
 
181
                var lid=this._selected[0].id;
 
182
        else lid="";
 
183
    var text=itemNode.span.innerHTML;
 
184
 
 
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]);
 
188
 
 
189
        if (!this._xfullXML)
 
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" ':''));
 
191
        else
 
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" ':''));
 
193
 
 
194
        if ((this._xuserData)&&(itemNode._userdatalist))
 
195
                {
 
196
                        out.push(', "userdata":[');
 
197
                        var names=itemNode._userdatalist.split(",");
 
198
                        var p=[];
 
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("]");
 
202
                }
 
203
                
 
204
                if (itemNode.childsCount){
 
205
                        out.push(', "item":[');
 
206
                        var p=[];
 
207
                for (var i=0; i<itemNode.childsCount; i++)
 
208
                        p.push(this._serializeItemJSON(itemNode.childNodes[i]));
 
209
                        out.push(p.join(","));
 
210
                        out.push("]\n");
 
211
                }
 
212
                        
 
213
                out.push("}\n")
 
214
        return out.join("");
 
215
}   
 
216
//(c)dhtmlx ltd. www.dhtmlx.com
 
 
b'\\ No newline at end of file'