xloadtree.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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) 1999 - 2002 Erik Arvidsson |
  12. |-----------------------------------------------------------------------------|
  13. | This software is provided "as is", without warranty of any kind, express or |
  14. | implied, including but not limited to the warranties of merchantability, |
  15. | fitness for a particular purpose and noninfringement. In no event shall the |
  16. | authors or copyright holders be liable for any claim, damages or other |
  17. | liability, whether in an action of contract, tort or otherwise, arising |
  18. | from, out of or in connection with the software or the use or other |
  19. | dealings in the software. |
  20. | - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
  21. | This software is available under the three different licenses mentioned |
  22. | below. To use this software you must chose, and qualify, for one of those. |
  23. | - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
  24. | The WebFX Non-Commercial License http://webfx.eae.net/license.html |
  25. | Permits anyone the right to use the software in a non-commercial context |
  26. | free of charge. |
  27. | - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
  28. | The WebFX Commercial license http://webfx.eae.net/commercial.html |
  29. | Permits the license holder the right to use the software in a commercial |
  30. | context. Such license must be specifically obtained, however it's valid for |
  31. | any number of implementations of the licensed software. |
  32. | - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
  33. | GPL - The GNU General Public License http://www.gnu.org/licenses/gpl.txt |
  34. | Permits anyone the right to use and modify the software without limitations |
  35. | as long as proper credits are given and the original and modified source |
  36. | code are included. Requires that the final product, software derivate from |
  37. | the original source or any software utilizing a GPL component, such as |
  38. | this, is also licensed under the GPL license. |
  39. |-----------------------------------------------------------------------------|
  40. | 2001-09-27 | Original Version Posted. |
  41. | 2002-01-19 | Added some simple error handling and string templates for |
  42. | | reporting the errors. |
  43. | 2002-01-28 | Fixed loading issues in IE50 and IE55 that made the tree load |
  44. | | twice. |
  45. | 2002-10-10 | (1.1) Added reload method that reloads the XML file from the |
  46. | | server. |
  47. / 2003-05-06 | Added support for target attribute |
  48. |-----------------------------------------------------------------------------|
  49. | Dependencies: xtree.js - original xtree library |
  50. | xtree.css - simple css styling of xtree |
  51. | xmlextras.js - provides xml http objects and xml document |
  52. | objects |
  53. |-----------------------------------------------------------------------------|
  54. | Created 2001-09-27 | All changes are in the log above. | Updated 2003-05-06 |
  55. \----------------------------------------------------------------------------*/
  56. webFXTreeConfig.loadingText = "Loading...";
  57. webFXTreeConfig.loadErrorTextTemplate = "Error loading \"%1%\"";
  58. webFXTreeConfig.emptyErrorTextTemplate = "Error \"%1%\" does not contain any tree items";
  59. /*
  60. * WebFXLoadTree class
  61. */
  62. function WebFXLoadTree(sText, sXmlSrc, sAction, sBehavior, sIcon, sOpenIcon) {
  63. // call super
  64. this.WebFXTree = WebFXTree;
  65. this.WebFXTree(sText, sAction, sBehavior, sIcon, sOpenIcon);
  66. // setup default property values
  67. this.src = sXmlSrc;
  68. this.loading = false;
  69. this.loaded = false;
  70. this.errorText = "";
  71. // check start state and load if open
  72. if (this.open)
  73. _startLoadXmlTree(this.src, this);
  74. else {
  75. // and create loading item if not
  76. this._loadingItem = new WebFXTreeItem(webFXTreeConfig.loadingText);
  77. this.add(this._loadingItem);
  78. }
  79. }
  80. WebFXLoadTree.prototype = new WebFXTree;
  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. * WebFXLoadTreeItem class
  92. */
  93. function WebFXLoadTreeItem(sText, sXmlSrc, sAction, eParent, sIcon, sOpenIcon) {
  94. // call super
  95. this.WebFXTreeItem = WebFXTreeItem;
  96. this.WebFXTreeItem(sText, sAction, eParent, sIcon, sOpenIcon);
  97. // setup default property values
  98. this.src = sXmlSrc;
  99. this.loading = false;
  100. this.loaded = false;
  101. this.errorText = "";
  102. // check start state and load if open
  103. if (this.open)
  104. _startLoadXmlTree(this.src, this);
  105. else {
  106. // and create loading item if not
  107. this._loadingItem = new WebFXTreeItem(webFXTreeConfig.loadingText);
  108. this.add(this._loadingItem);
  109. }
  110. }
  111. WebFXLoadTreeItem.prototype = new WebFXTreeItem;
  112. // override the expand method to load the xml file
  113. WebFXLoadTreeItem.prototype._webfxtreeitem_expand = WebFXTreeItem.prototype.expand;
  114. WebFXLoadTreeItem.prototype.expand = function() {
  115. if (!this.loaded && !this.loading) {
  116. // load
  117. _startLoadXmlTree(this.src, this);
  118. }
  119. this._webfxtreeitem_expand();
  120. };
  121. // reloads the src file if already loaded
  122. WebFXLoadTree.prototype.reload =
  123. WebFXLoadTreeItem.prototype.reload = function () {
  124. // if loading do nothing
  125. if (this.loaded) {
  126. var open = this.open;
  127. // remove
  128. while (this.childNodes.length > 0)
  129. this.childNodes[this.childNodes.length - 1].remove();
  130. this.loaded = false;
  131. this._loadingItem = new WebFXTreeItem(webFXTreeConfig.loadingText);
  132. this.add(this._loadingItem);
  133. if (open)
  134. this.expand();
  135. }
  136. else if (this.open && !this.loading)
  137. _startLoadXmlTree(this.src, this);
  138. };
  139. /*
  140. * Helper functions
  141. */
  142. // creates the xmlhttp object and starts the load of the xml document
  143. function _startLoadXmlTree(sSrc, jsNode) {
  144. if (jsNode.loading || jsNode.loaded)
  145. return;
  146. jsNode.loading = true;
  147. var xmlHttp = XmlHttp.create();
  148. xmlHttp.open("GET", sSrc, true); // async
  149. xmlHttp.onreadystatechange = function () {
  150. if (xmlHttp.readyState == 4) {
  151. _xmlFileLoaded(xmlHttp.responseXML, jsNode);
  152. }
  153. };
  154. // call in new thread to allow ui to update
  155. window.setTimeout(function () {
  156. xmlHttp.send(null);
  157. }, 10);
  158. }
  159. // Converts an xml tree to a js tree. See article about xml tree format
  160. function _xmlTreeToJsTree(oNode) {
  161. // retreive attributes
  162. var text = oNode.getAttribute("text");
  163. var action = oNode.getAttribute("action");
  164. var parent = null;
  165. var icon = oNode.getAttribute("icon");
  166. var openIcon = oNode.getAttribute("openIcon");
  167. var src = oNode.getAttribute("src");
  168. var target = oNode.getAttribute("target");
  169. // create jsNode
  170. var jsNode;
  171. if (src != null && src != "")
  172. jsNode = new WebFXLoadTreeItem(text, src, action, parent, icon, openIcon);
  173. else
  174. jsNode = new WebFXTreeItem(text, action, parent, icon, openIcon);
  175. if (target != "")
  176. jsNode.target = target;
  177. // go through childNOdes
  178. var cs = oNode.childNodes;
  179. var l = cs.length;
  180. for (var i = 0; i < l; i++) {
  181. if (cs[i].tagName == "tree")
  182. jsNode.add( _xmlTreeToJsTree(cs[i]), true );
  183. }
  184. return jsNode;
  185. }
  186. // Inserts an xml document as a subtree to the provided node
  187. function _xmlFileLoaded(oXmlDoc, jsParentNode) {
  188. if (jsParentNode.loaded)
  189. return;
  190. var bIndent = false;
  191. var bAnyChildren = false;
  192. jsParentNode.loaded = true;
  193. jsParentNode.loading = false;
  194. // check that the load of the xml file went well
  195. if( oXmlDoc == null || oXmlDoc.documentElement == null) {
  196. alert(oXmlDoc.xml);
  197. jsParentNode.errorText = parseTemplateString(webFXTreeConfig.loadErrorTextTemplate,
  198. jsParentNode.src);
  199. }
  200. else {
  201. // there is one extra level of tree elements
  202. var root = oXmlDoc.documentElement;
  203. // loop through all tree children
  204. var cs = root.childNodes;
  205. var l = cs.length;
  206. for (var i = 0; i < l; i++) {
  207. if (cs[i].tagName == "tree") {
  208. bAnyChildren = true;
  209. bIndent = true;
  210. jsParentNode.add( _xmlTreeToJsTree(cs[i]), true);
  211. }
  212. }
  213. // if no children we got an error
  214. if (!bAnyChildren)
  215. jsParentNode.errorText = parseTemplateString(webFXTreeConfig.emptyErrorTextTemplate,
  216. jsParentNode.src);
  217. }
  218. // remove dummy
  219. if (jsParentNode._loadingItem != null) {
  220. jsParentNode._loadingItem.remove();
  221. bIndent = true;
  222. }
  223. if (bIndent) {
  224. // indent now that all items are added
  225. jsParentNode.indent();
  226. }
  227. // show error in status bar
  228. if (jsParentNode.errorText != "")
  229. window.status = jsParentNode.errorText;
  230. }
  231. // parses a string and replaces %n% with argument nr n
  232. function parseTemplateString(sTemplate) {
  233. var args = arguments;
  234. var s = sTemplate;
  235. s = s.replace(/\%\%/g, "%");
  236. for (var i = 1; i < args.length; i++)
  237. s = s.replace( new RegExp("\%" + i + "\%", "g"), args[i] )
  238. return s;
  239. }