ultimix
jquery.tree.xml_nested.js
Go to the documentation of this file.
1 (function ($) {
2  if(typeof Sarissa == "undefined") throw "jsTree xml_nested: Sarissa is not included.";
3 
4  $.extend($.tree.datastores, {
5  "xml_nested" : function () {
6  return {
7  get : function(obj, tree, opts) {
8  var str = "";
9  if(!obj || $(obj).size() == 0) {
10  obj = tree.container.children("ul").children("li");
11  }
12  else obj = $(obj);
13 
14  if(obj.size() > 1) {
15  var _this = this;
16  var str = '<root>';
17  obj.each(function () {
18  str += _this.get(this, tree, $.extend(true, {}, opts, { callback : true }));
19  });
20  str += '</root>';
21  return str;
22  }
23 
24  if(!opts) var opts = {};
25  if(!opts.outer_attrib) opts.outer_attrib = [ "id", "rel", "class" ];
26  if(!opts.inner_attrib) opts.inner_attrib = [ ];
27  if(!opts.callback) str += '<root>';
28 
29  str += '<item ';
30  for(var i in opts.outer_attrib) {
31  if(!opts.outer_attrib.hasOwnProperty(i)) continue;
32  var val = (opts.outer_attrib[i] == "class") ? obj.attr(opts.outer_attrib[i]).replace(/(^| )last( |$)/ig," ").replace(/(^| )(leaf|closed|open)( |$)/ig," ") : obj.attr(opts.outer_attrib[i]);
33  if(typeof val != "undefined" && val.toString().replace(" ","").length > 0) str += ' ' + opts.outer_attrib[i] + '="' + val.toString() + '" ';
34  delete val;
35  }
36  str += '>';
37 
38  str += '<content>';
39  if(tree.settings.languages.length) {
40  for(var i in tree.settings.languages) {
41  if(!tree.settings.languages.hasOwnProperty(i)) continue;
42  str += this.process_inner(obj.children("a." + tree.settings.languages[i]), tree, opts, tree.settings.languages[i]);
43  }
44  }
45  else {
46  str += this.process_inner(obj.children("a"), tree, opts);
47  }
48  str += '</content>';
49 
50  if(obj.children("ul").size() > 0) {
51  var _this = this;
52  obj.children("ul").children("li").each(function () {
53  str += _this.get(this, tree, $.extend(true, {}, opts, { callback : true }));
54  });
55  }
56  str += '</item>';
57 
58  if(!opts.callback) str += '</root>';
59  return str;
60  },
61  process_inner : function(obj, tree, opts, lang) {
62  var str = '<name ';
63  if(lang) str += ' lang="' + lang + '" ';
64  if(opts.inner_attrib.length || obj.children("ins").get(0).style.backgroundImage.toString().length || obj.children("ins").get(0).className.length) {
65  if(obj.children("ins").get(0).style.className.length) {
66  str += ' icon="' + obj.children("ins").get(0).style.className + '" ';
67  }
68  if(obj.children("ins").get(0).style.backgroundImage.length) {
69  str += ' icon="' + obj.children("ins").get(0).style.backgroundImage.replace("url(","").replace(")","") + '" ';
70  }
71  if(opts.inner_attrib.length) {
72  for(var j in opts.inner_attrib) {
73  if(!opts.inner_attrib.hasOwnProperty(j)) continue;
74  var val = obj.attr(opts.inner_attrib[j]);
75  if(typeof val != "undefined" && val.toString().replace(" ","").length > 0) str += ' ' + opts.inner_attrib[j] + '="' + val.toString() + '" ';
76  delete val;
77  }
78  }
79  }
80  str += '><![CDATA[' + tree.get_text(obj,lang) + ']]></name>';
81  return str;
82  },
83  parse : function(data, tree, opts, callback) {
84  var processor = new XSLTProcessor();
85  processor.importStylesheet($.tree.datastores.xml_nested.xsl);
86 
87  var result = $((new XMLSerializer()).serializeToString(processor.transformToDocument(data)).replace('<?xml version="1.0"?>',''));
88  // Opera bug
89  if(result.size() > 1) result = result.eq(1);
90  if(result.is("ul")) result = result.html();
91  else result = result.find("ul:eq(0)").html();
92  if(callback) callback.call(null,result);
93 
94  // Disabled because of Chrome issues
95  // if(callback) callback.call(null,(new XMLSerializer()).serializeToString(processor.transformToDocument(data)).replace(/^<ul[^>]*>/i,"").replace(/<\/ul>$/i,""));
96  },
97  load : function(data, tree, opts, callback) {
98  if(opts.staticData) {
99  callback.call(null, (new DOMParser()).parseFromString(opts.staticData,'text/xml'));
100  }
101  else {
102  $.ajax({
103  'type' : opts.method,
104  'url' : opts.url,
105  'data' : data,
106  'dataType' : "xml",
107  'success' : function (d, textStatus) {
108  callback.call(null, d);
109  },
110  'error' : function (xhttp, textStatus, errorThrown) {
111  callback.call(null, false);
112  tree.error(errorThrown + " " + textStatus);
113  }
114  });
115  }
116  }
117  }
118  }
119  });
120  $.tree.datastores.xml_nested.xsl = (new DOMParser()).parseFromString('<?xml version="1.0" encoding="utf-8" ?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" ><xsl:output method="html" encoding="utf-8" omit-xml-declaration="yes" standalone="no" indent="no" media-type="text/html" /><xsl:template match="/"><xsl:call-template name="nodes"><xsl:with-param name="node" select="/root" /></xsl:call-template></xsl:template><xsl:template name="nodes"><xsl:param name="node" /><ul><xsl:for-each select="$node/item"><xsl:variable name="children" select="count(./item) &gt; 0" /><li><xsl:attribute name="class"><xsl:if test="position() = last()"> last </xsl:if><xsl:choose><xsl:when test="@state = \'open\'"> open </xsl:when><xsl:when test="$children or @hasChildren"> closed </xsl:when><xsl:otherwise> leaf </xsl:otherwise></xsl:choose><xsl:value-of select="@class" /></xsl:attribute><xsl:for-each select="@*"><xsl:if test="name() != \'class\' and name() != \'state\' and name() != \'hasChildren\'"><xsl:attribute name="{name()}"><xsl:value-of select="." /></xsl:attribute></xsl:if></xsl:for-each><xsl:for-each select="content/name"><a href=""><xsl:attribute name="class"><xsl:value-of select="@lang" /><xsl:value-of select="@class" /></xsl:attribute><xsl:attribute name="style"><xsl:value-of select="@style" /></xsl:attribute><xsl:for-each select="@*"><xsl:if test="name() != \'style\' and name() != \'class\'"><xsl:attribute name="{name()}"><xsl:value-of select="." /></xsl:attribute></xsl:if></xsl:for-each><ins><xsl:if test="string-length(attribute::icon) > 0"><xsl:choose><xsl:when test="not(contains(@icon,\'/\'))"><xsl:attribute name="class"><xsl:value-of select="@icon" /></xsl:attribute></xsl:when><xsl:otherwise><xsl:attribute name="style">background-image:url(<xsl:value-of select="@icon" />);</xsl:attribute></xsl:otherwise></xsl:choose></xsl:if><xsl:text>&#xa0;</xsl:text></ins><xsl:value-of select="current()" /></a></xsl:for-each><xsl:if test="$children or @hasChildren"><xsl:call-template name="nodes"><xsl:with-param name="node" select="current()" /></xsl:call-template></xsl:if></li></xsl:for-each></ul></xsl:template></xsl:stylesheet>','text/xml');
121 })(jQuery);