xloadtree_wev8.js
10.3 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
/*----------------------------------------------------------------------------\
| XLoadTree 1.11 |
|-----------------------------------------------------------------------------|
| Created by Erik Arvidsson |
| (http://webfx.eae.net/contact.html#erik) |
| For WebFX (http://webfx.eae.net/) |
|-----------------------------------------------------------------------------|
| An extension to xTree that allows sub trees to be loaded at runtime by |
| reading XML files from the server. Works with IE5+ and Mozilla 1.0+ |
|-----------------------------------------------------------------------------|
| Copyright (c) 1999 - 2002 Erik Arvidsson |
|-----------------------------------------------------------------------------|
| This software is provided "as is", without warranty of any kind, express or |
| implied, including but not limited to the warranties of merchantability, |
| fitness for a particular purpose and noninfringement. In no event shall the |
| authors or copyright holders be liable for any claim, damages or other |
| liability, whether in an action of contract, tort or otherwise, arising |
| from, out of or in connection with the software or the use or other |
| dealings in the software. |
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| This software is available under the three different licenses mentioned |
| below. To use this software you must chose, and qualify, for one of those. |
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| The WebFX Non-Commercial License http://webfx.eae.net/license.html |
| Permits anyone the right to use the software in a non-commercial context |
| free of charge. |
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| The WebFX Commercial license http://webfx.eae.net/commercial.html |
| Permits the license holder the right to use the software in a commercial |
| context. Such license must be specifically obtained, however it's valid for |
| any number of implementations of the licensed software. |
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| GPL - The GNU General Public License http://www.gnu.org/licenses/gpl.txt |
| Permits anyone the right to use and modify the software without limitations |
| as long as proper credits are given and the original and modified source |
| code are included. Requires that the final product, software derivate from |
| the original source or any software utilizing a GPL component, such as |
| this, is also licensed under the GPL license. |
|-----------------------------------------------------------------------------|
| 2001-09-27 | Original Version Posted. |
| 2002-01-19 | Added some simple error handling and string templates for |
| | reporting the errors. |
| 2002-01-28 | Fixed loading issues in IE50 and IE55 that made the tree load |
| | twice. |
| 2002-10-10 | (1.1) Added reload method that reloads the XML file from the |
| | server. |
/ 2003-05-06 | Added support for target attribute |
|-----------------------------------------------------------------------------|
| Dependencies: xtree_wev8.js - original xtree library |
| xtree_wev8.css - simple css styling of xtree |
| xmlextras_wev8.js - provides xml http objects and xml document |
| objects |
|-----------------------------------------------------------------------------|
| Created 2001-09-27 | All changes are in the log above. | Updated 2003-05-06 |
\----------------------------------------------------------------------------*/
webFXTreeConfig.loadingText = "Loading...";
webFXTreeConfig.loadErrorTextTemplate = "";
webFXTreeConfig.emptyErrorTextTemplate = "";
/*
* WebFXLoadTree class
*/
function WebFXLoadTree(sText, sXmlSrc, sAction, sBehavior, sIcon, sOpenIcon) {
// call super
this.WebFXTree = WebFXTree;
this.WebFXTree(sText, sAction, sBehavior, sIcon, sOpenIcon);
// setup default property values
this.src = sXmlSrc;
this.loading = false;
this.loaded = false;
this.errorText = "";
// check start state and load if open
if (this.open)
_startLoadXmlTree(this.src, this);
else {
// and create loading item if not
this._loadingItem = new WebFXTreeItem(webFXTreeConfig.loadingText);
this.add(this._loadingItem);
}
}
WebFXLoadTree.prototype = new WebFXTree;
// override the expand method to load the xml file
WebFXLoadTree.prototype._webfxtree_expand = WebFXTree.prototype.expand;
WebFXLoadTree.prototype.expand = function() {
if (!this.loaded && !this.loading) {
//if (!this.loading) {
// load
_startLoadXmlTree(this.src, this);
}
this._webfxtree_expand();
};
/*
* WebFXLoadTreeItem class
*/
function WebFXLoadTreeItem(sText, sXmlSrc, sAction, eParent, sIcon, sOpenIcon) {
// call super
this.WebFXTreeItem = WebFXTreeItem;
this.WebFXTreeItem(sText, sAction, eParent, sIcon, sOpenIcon);
// setup default property values
this.src = sXmlSrc;
this.loading = false;
this.loaded = false;
this.errorText = "";
// check start state and load if open
if (this.open)
_startLoadXmlTree(this.src, this);
else {
// and create loading item if not
this._loadingItem = new WebFXTreeItem(webFXTreeConfig.loadingText,'','','/images/loading2_wev8.gif','/images/loading2_wev8.gif');
this.add(this._loadingItem);
}
}
WebFXLoadTreeItem.prototype = new WebFXTreeItem;
// override the expand method to load the xml file
WebFXLoadTreeItem.prototype._webfxtreeitem_expand = WebFXTreeItem.prototype.expand;
WebFXLoadTreeItem.prototype.expand = function() {
if (!this.loaded && !this.loading) {
// load
_startLoadXmlTree(this.src, this);
}else if(window._reload && !this.loading){
this.reload();
window._reload = false;
}
this._webfxtreeitem_expand();
};
// reloads the src file if already loaded
WebFXLoadTree.prototype.reload =
WebFXLoadTreeItem.prototype.reload = function () {
// if loading do nothing
if (this.loaded) {
var open = this.open;
// remove
while (this.childNodes.length > 0)
this.childNodes[this.childNodes.length - 1].remove();
this.loaded = false;
this._loadingItem = new WebFXTreeItem(webFXTreeConfig.loadingText);
this.add(this._loadingItem);
if (true || open)
this.expand();
}
else if (this.open && !this.loading)
_startLoadXmlTree(this.src, this);
};
/*
* Helper functions
*/
// creates the xmlhttp object and starts the load of the xml document
function _startLoadXmlTree(sSrc, jsNode) {
if (jsNode.loading || jsNode.loaded)
return;
jsNode.loading = true;
var xmlHttp = XmlHttp.create();
var xmlDocument = XmlDocument.create();
xmlHttp.open("GET", sSrc, true); // async
xmlHttp.onreadystatechange = function () {
if (xmlHttp.readyState == 4) {
//alert(xmlHttp.responseText);
xmlDocument.loadXML(xmlHttp.responseText);
_xmlFileLoaded(xmlDocument, jsNode);
}
};
// call in new thread to allow ui to update
window.setTimeout(function () {
xmlHttp.send(null);
}, 10);
}
// Converts an xml tree to a js tree. See article about xml tree format
function _xmlTreeToJsTree(oNode) {
// retreive attributes
var text = oNode.getAttribute("text");
var action = oNode.getAttribute("action");
var parent = null;
var icon = oNode.getAttribute("icon");
var openIcon = oNode.getAttribute("openIcon");
var src = oNode.getAttribute("src");
var target = oNode.getAttribute("target");
var _id = oNode.getAttribute("_id");
var _num = oNode.getAttribute("num");
// create jsNode
var jsNode;
if (src != null && src != "")
jsNode = new WebFXLoadTreeItem(text, src, action, parent, icon, openIcon);
else
jsNode = new WebFXTreeItem(text, action, parent, icon, openIcon);
if (target != "")
jsNode.target = target;
if(!!_id){
jsNode._id = _id;
}
if(!!_num || _num==0){
jsNode._num = _num;
}else{
jsNode._num = "";
}
// go through childNOdes
var cs = oNode.childNodes;
var l = cs.length;
for (var i = 0; i < l; i++) {
if (cs[i].tagName == "tree")
jsNode.add( _xmlTreeToJsTree(cs[i]), true );
}
return jsNode;
}
// Inserts an xml document as a subtree to the provided node
function _xmlFileLoaded(oXmlDoc, jsParentNode) {
if (jsParentNode.loaded)
return;
var bIndent = false;
var bAnyChildren = false;
jsParentNode.loaded = true;
jsParentNode.loading = false;
// check that the load of the xml file went well
if( oXmlDoc == null || oXmlDoc.documentElement == null) {
//alert(oXmlDoc.xml);
jsParentNode.errorText = parseTemplateString(webFXTreeConfig.loadErrorTextTemplate,
jsParentNode.src);
}
else {
// there is one extra level of tree elements
var root = oXmlDoc.documentElement;
// loop through all tree children
var cs = root.childNodes;
var l = cs.length;
for (var i = 0; i < l; i++) {
if (cs[i].tagName == "tree") {
bAnyChildren = true;
bIndent = true;
jsParentNode.add( _xmlTreeToJsTree(cs[i]), true);
}
}
// if no children we got an error
if (!bAnyChildren)
jsParentNode.errorText = parseTemplateString(webFXTreeConfig.emptyErrorTextTemplate,
jsParentNode.src);
}
// remove dummy
if (jsParentNode._loadingItem != null) {
jsParentNode._loadingItem.remove();
bIndent = true;
}
if (bIndent) {
// indent now that all items are added
jsParentNode.indent();
}
// show error in status bar
if (jsParentNode.errorText != "")
window.status = jsParentNode.errorText;
window._treeLoaded = true;
}
// parses a string and replaces %n% with argument nr n
function parseTemplateString(sTemplate) {
var args = arguments;
var s = sTemplate;
s = s.replace(/\%\%/g, "%");
for (var i = 1; i < args.length; i++)
s = s.replace( new RegExp("\%" + i + "\%", "g"), args[i] )
return s;
}