1 /*----------------------------------------------------------------------------\
2 | XLoadTree 1.11 |
3 |-----------------------------------------------------------------------------|
4 | Created by Erik Arvidsson |
5 | (http://webfx.eae.net/contact.html#erik) |
6 | For WebFX (http://webfx.eae.net/) |
7 |-----------------------------------------------------------------------------|
8 | An extension to xTree that allows sub trees to be loaded at runtime by |
9 | reading XML files from the server. Works with IE5+ and Mozilla 1.0+ |
10 |-----------------------------------------------------------------------------|
11 | Copyright (c) 2001, 2002, 2003, 2006 Erik Arvidsson |
12 |-----------------------------------------------------------------------------|
13 | Licensed under the Apache License, Version 2.0 (the "License"); you may not |
14 | use this file except in compliance with the License. You may obtain a copy |
15 | of the License at http://www.apache.org/licenses/LICENSE-2.0 |
16 | - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
17 | Unless required by applicable law or agreed to in writing, software |
18 | distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
19 | WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
20 | License for the specific language governing permissions and limitations |
21 | under the License. |
22 |-----------------------------------------------------------------------------|
23 | Dependencies: xtree.js - original xtree library |
24 | xtree.css - simple css styling of xtree |
25 | xmlextras.js - provides xml http objects and xml document |
26 | objects |
27 |-----------------------------------------------------------------------------|
28 | 2001-09-27 | Original Version Posted. |
29 | 2002-01-19 | Added some simple error handling and string templates for |
30 | | reporting the errors. |
31 | 2002-01-28 | Fixed loading issues in IE50 and IE55 that made the tree load |
32 | | twice. |
33 | 2002-10-10 | (1.1) Added reload method that reloads the XML file from the |
34 | | server. |
35 | 2003-05-06 | Added support for target attribute |
36 | 2006-05-28 | Changed license to Apache Software License 2.0. |
37 |-----------------------------------------------------------------------------|
38 | Created 2001-09-27 | All changes are in the log above. | Updated 2006-05-28 |
39 \----------------------------------------------------------------------------*/
40
41 // 正在加载显示内容
42 webFXTreeConfig.loadingText = "Loading";
43 // 加载错误模板
44 webFXTreeConfig.loadErrorTextTemplate = "Error loading \"%1%\"";
45 //加载空文件错误模板
46 webFXTreeConfig.emptyErrorTextTemplate = "Error \"%1%\" does not contain any tree items";
47
48 /*
49 * WebFXLoadTree class
50 */
51
52 function WebFXLoadTree(sText, sXmlSrc, sAction, sBehavior, sIcon, sOpenIcon) {
53 // call super
54 this.WebFXTree = WebFXTree;
55 this.WebFXTree(sText, sAction, sBehavior, sIcon, sOpenIcon);
56
57 // setup default property values
58 // xml 文件路径或 document 对象
59 this.src = sXmlSrc;
60 //表示是否正在加载状态
61 this.loading = false;
62 //表示是否已经加载完状态
63 this.loaded = false;
64 //错误信息
65 this.errorText = "";
66
67 // check start state and load if open
68 if (this.open){
69 _startLoadXmlTree(this.src, this);
70 } else {
71 // and create loading item if not
72 this._loadingItem = new WebFXTreeItem(webFXTreeConfig.loadingText);
73 this.add(this._loadingItem);
74 }
75 }
76
77 //WebFXLoadTree 类,继承 WebFXTree 类的所有属性和方法。
78 WebFXLoadTree.prototype = new WebFXTree;
79
80 //先将 webFXTree expand method copy to WebFXTloadTree _webfxtree_expand method
81 // override the expand method to load the xml file
82 WebFXLoadTree.prototype._webfxtree_expand = WebFXTree.prototype.expand;
83 WebFXLoadTree.prototype.expand = function() {
84 if (!this.loaded && !this.loading) {
85 // load
86 _startLoadXmlTree(this.src, this);
87 }
88 this._webfxtree_expand();
89 };
90
91 /*
92 * WebFXLoadTreeItem class
93 */
94
95 function WebFXLoadTreeItem(sText, sXmlSrc, sAction, eParent, sIcon, sOpenIcon) {
96 // call super
97 this.WebFXTreeItem = WebFXTreeItem;
98 this.WebFXTreeItem(sText, sAction, eParent, sIcon, sOpenIcon);
99
100 // setup default property values
101 this.src = sXmlSrc;
102 this.loading = false;
103 this.loaded = false;
104 this.errorText = "";
105
106 // check start state and load if open
107 if (this.open){
108 _startLoadXmlTree(this.src, this);
109 }else {
110 // and create loading item if not
111 this._loadingItem = new WebFXTreeItem(webFXTreeConfig.loadingText);
112 this.add(this._loadingItem);
113 }
114 }
115
116 // WebFXLoadTreeItem 继承 WebFXTreeItem 所有的属性和方法。
117 WebFXLoadTreeItem.prototype = new WebFXTreeItem;
118
119 // override the expand method to load the xml file
120 WebFXLoadTreeItem.prototype._webfxtreeitem_expand = WebFXTreeItem.prototype.expand;
121 WebFXLoadTreeItem.prototype.expand = function() {
122 if (!this.loaded && !this.loading) {
123 // load
124 _startLoadXmlTree(this.src, this);
125 }
126 this._webfxtreeitem_expand();
127 };
128
129 // reloads the src file if already loaded
130 WebFXLoadTree.prototype.reload = WebFXLoadTreeItem.prototype.reload = function () {
131 // if loading do nothing
132 if (this.loaded) {
133 var open = this.open;
134 // remove
135 while (this.childNodes.length > 0){
136 this.childNodes[this.childNodes.length - 1].remove();
137 }
138 this.loaded = false;
139
140 this._loadingItem = new WebFXTreeItem(webFXTreeConfig.loadingText);
141 this.add(this._loadingItem);
142
143 if (open)
144 this.expand();
145 } else if (this.open && !this.loading){
146 _startLoadXmlTree(this.src, this);
147 }
148 };
149
150
151
152 /*
153 * Helper functions
154 */
155
156 // creates the xmlhttp object and starts the load of the xml document
157 function _startLoadXmlTree(sSrc, jsNode) {
158 if (jsNode.loading || jsNode.loaded)
159 return;
160 jsNode.loading = true;
161 var xmlHttp = XmlHttp.create();
162 xmlHttp.open("GET", sSrc, true); // async
163 xmlHttp.onreadystatechange = function () {
164 if (xmlHttp.readyState == 4) {
165 _xmlFileLoaded(xmlHttp.responseXML, jsNode);
166 }
167 };
168 // call in new thread to allow ui to update
169 window.setTimeout(function () {
170 xmlHttp.send(null);
171 }, 10);
172 }
173
174 // Inserts an xml document as a subtree to the provided node
175 function _xmlFileLoaded(oXmlDoc, jsParentNode) {
176 if (jsParentNode.loaded)
177 return;
178
179 var bIndent = false;
180 //是否存在子节点的状态
181 var bAnyChildren = false;
182 jsParentNode.loaded = true;
183 jsParentNode.loading = false;
184
185 // check that the load of the xml file went well
186 // 检查加载的 xml 文件是否顺利 documentELement 返回文档的根节点。
187 if( oXmlDoc == null || oXmlDoc.documentElement == null) {
188 jsParentNode.errorText = parseTemplateString(webFXTreeConfig.loadErrorTextTemplate,
189 jsParentNode.src);
190 } else {
191 // there is one extra level of tree elements
192 //树型根元素
193 var root = oXmlDoc.documentElement;
194
195 // loop through all tree children
196 var cs = root.childNodes;
197 var l = cs.length;
198 for (var i = 0; i < l; i++) {
199 if (cs[i].tagName == "tree") {
200 bAnyChildren = true;
201 bIndent = true;
202 jsParentNode.add( _xmlTreeToJsTree(cs[i]), true);
203 }
204 }
205
206 // if no children we got an error
207 if (!bAnyChildren){
208 jsParentNode.errorText = parseTemplateString(webFXTreeConfig.emptyErrorTextTemplate,
209 jsParentNode.src);
210 }
211 }
212
213 // remove dummy
214 if (jsParentNode._loadingItem != null) {
215 jsParentNode._loadingItem.remove();
216 bIndent = true;
217 }
218
219 if (bIndent) {
220 // indent now that all items are added
221 jsParentNode.indent();
222 }
223
224 // show error in status bar
225 if (jsParentNode.errorText != "")
226 //status 属性可设置或返回窗口状态栏中的文本。
227 window.status = jsParentNode.errorText;
228 }
229
230 // parses a string and replaces %n% with argument nr n
231 function parseTemplateString(sTemplate) {
232 // arguments 属性,函数可以处理可变数量的参数。
233 // arguments 对象的 length 属性包含了传递给函数的参数的数目。
234 // 对于arguments 对象所包含的单个参数,其访问方法与数组中所包含的参数的访问方法相同。
235 var args = arguments;
236 var s = sTemplate;
237
238 //replace() 方法用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串。
239 s = s.replace(/\%\%/g, "%");
240
241 for (var i = 1; i < args.length; i++){
242 // RegExp 是正则表达式的缩写.
243 s = s.replace( new RegExp("\%" + i + "\%", "g"), args[i] )
244
245 }
246 return s;
247 }
248
249 // Converts an xml tree to a js tree. See article about xml tree format
250 function _xmlTreeToJsTree(oNode) {
251 // retreive attributes
252 var text = oNode.getAttribute("text");
253 var action = oNode.getAttribute("action");
254 var parent = null;
255 var icon = oNode.getAttribute("icon");
256 var openIcon = oNode.getAttribute("openIcon");
257 var src = oNode.getAttribute("src");1
258 var target = oNode.getAttribute("target");
259 // create jsNode
260 var jsNode;
261 if (src != null && src != ""){
262 jsNode = new WebFXLoadTreeItem(text, src, action, parent, icon, openIcon);
263 } else{
264 jsNode = new WebFXTreeItem(text, action, parent, icon, openIcon);
265 }
266 if (target != ""){
267 jsNode.target = target;
268 }
269
270 // go through childNOdes
271 var cs = oNode.childNodes;
272 var l = cs.length;
273 for (var i = 0; i < l; i++) {
274 if (cs[i].tagName == "tree")
275 jsNode.add( _xmlTreeToJsTree(cs[i]), true );
276 }
277 return jsNode;
278 }
分享到:
相关推荐
`xloadtree.js` 是一个专为实现这种页面树形结构而设计的JavaScript库,它结合了`xtree.js`和`xtree.css`等组件,提供了强大的功能和良好的用户体验。下面将详细介绍`xloadtree.js`及其相关组件的工作原理和应用场景...
2. xtree.js, xloadvtree.js, xloadtree.js, xvtree.js: 这些都是JavaScript脚本,其中可能包含了实现XML树形视图的功能,比如加载XML数据、渲染树节点、处理用户交互等。 3. xmlextras.js: 可能包含了额外的XML处理...
在给定的“xloadtree.rar_javascript_xloadtree”资源中,我们可以了解到这是关于使用JavaScript实现的一种树形数据结构的加载和展示方法。在网页中,树结构常用于组织和展示层次化的信息,例如文件系统、菜单结构...
标题 "JS 动态树 异步加载树 xloadtree WebFXLoadTree" 指的是在JavaScript中实现的一种特定的树形结构组件,它具备动态加载和异步数据获取的功能。这种技术常用于构建大型数据集的层级展示,如文件系统、组织结构或...
《xloadtree:初学者的探索与学习》 在编程世界中,数据的展示和操作是至关重要的一环,尤其在大型复杂系统中,层次结构的数据处理更是不可或缺。今天我们要探讨的是一个名为"xloadtree"的项目,这是一位初学者为了...
6. `xtree.js`:JavaScript文件,可能包含了XLoadTree的部分前端逻辑,用于在浏览器环境中与用户交互和展示XML树。 综上所述,XLoadTree是一个用Java编写的XML解析工具,它能够解析扩展的XML节点和属性,并以树状...
"XLoadTree资源包"是专门为构建Web应用中的目录树视图而设计的,它集成了JavaScript(js)和CSS(css)文件,以及相关的API文档,为开发者提供了全面的工具来实现交互式的、可扩展的树状结构展示。这个资源包的核心...
《XLoadTree:Java、XML、JSP与Ajax在Web应用中的协同工作》 XLoadTree是一个基于Java、XML、JSP和Ajax技术构建的组件,主要用于在Web应用程序中实现动态加载和展示树形结构数据。它巧妙地结合了这些技术,提供了...
xloadTree.zip是一个压缩包,包含了用于实现动态加载树形结构数据的JavaScript文件和相关示例。在网页应用中,特别是在处理大量数据时,动态加载(也称为按需加载或懒加载)是一种优化性能的有效方法,因为它只在...
在IT行业中,XTREE和XLoadTree是一种广泛用于构建网页应用程序中的树形结构视图的JavaScript库。这些库主要用于展示层级关系数据,如文件系统、组织结构或菜单系统。它们通常与CSS样式表和图像资源结合使用,以提供...
<script type="text/javascript" src="xloadtree.js"> ... <div id="tree"></div> var tree = new WebFXLoadTree('tree', 'data.xml', 'xtree.css'); ``` 这里,`data.xml`是包含树形数据的XML文件,...
1. **xloadtree.js**: 这可能是xtree的核心加载模块,负责动态加载和处理树形数据。它可能包含异步加载数据的功能,比如从服务器获取数据,或者根据用户操作动态添加、删除或更新节点。 2. **xmlextras.js**: 根据...
xloadtree使用总结xloadtree使用总结xloadtree使用总结xloadtree使用总结xloadtree使用总结xloadtree使用总结xloadtree使用总结xloadtree使用总结
- `xloadvtree.js`和`xloadtree.js`:这两个文件可能用于动态加载树数据,可能是异步加载子节点,以优化初始页面加载性能,减少数据传输量。 - `xvtree.js`:可能包含xtree的扩展功能或特定版本的实现,例如,可能...
在实际应用中,开发人员需要结合HTML、CSS和JavaScript(可能包括jQuery或其他JS库)来使用xloadtree。通过事件监听、数据绑定和API调用,可以实现各种定制化需求,比如节点的展开/折叠、选中状态的改变、节点的添加...
在`xloadtree`中,Ajax调用通常由JavaScript触发,响应数据可能是XML、JSON或其他格式,然后由JavaScript解析并渲染到树结构中。 `简单说明.txt`文件可能是关于如何配置和使用`xloadtree`的指南。它可能包含了如何...
xloadtree通过JavaScript实现,利用了JS的异步处理能力,确保了用户的操作不会被阻塞。 在JavaScript中,构建动态树通常涉及到以下几个关键点: 1. **数据结构**:首先,数据需要以树形结构存储,每个节点包含自身...
"修改xloadtree支持checkbox radio鼠标右键菜单"这个主题涉及到的是对一个特定的JavaScript组件——xLoadTree的增强,使其具备更多功能,提升用户体验。下面将详细阐述这个主题所涵盖的知识点。 1. **xLoadTree**:...
将加载完后出现的子级菜单用JS保存在客户端,下载再点 + 的时候,不从数据库直接去,和CSDN的树菜单相似,主要用到的插件是:xloadtree,解决了其他常用的属性设置,如点击节点后的页面跳转,也就是target属性, ...