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