/* File: jstree.xml.js
This plugin makes it possible for jstree to use XML data sources.
*/
/* Group: jstree xml plugin */
(function ($) {
var xsl = {
'nest' : '' +
'<' + '?xml version="1.0" encoding="utf-8" ?>' +
'' +
'' +
'' +
' ' +
' ' +
' ' +
'' +
'' +
' ' +
' ' +
'' +
'',
'flat' : '' +
'<' + '?xml version="1.0" encoding="utf-8" ?>' +
'' +
'' +
'' +
' ' +
' ' + /* the last `or` may be removed */
' ' +
' ' +
' ' +
' ' +
'
' +
'' +
'' +
' ' +
' ' +
' ' +
' ' +
' ' +
' ' +
' ' +
' ' +
' ' +
' ' +
' ' +
' ' +
' ' +
' ' +
' ' +
' ' +
' ' +
' ' +
' ' +
'
' +
' ' +
' ' +
'' +
''
},
escape_xml = function(string) {
return string
.toString()
.replace(/&/g, '&')
.replace(//g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
};
$.jstree.plugin("xml", {
defaults : {
xsl : "flat",
data : false,
ajax : false
},
_fn : {
_append_xml_data : function (dom, data) {
data = $.vakata.xslt(data, xsl[this.get_settings().xml.xsl]);
if(data === false) { return false; }
data = $(data);
if(!data || !data.length || !data.is('ul, li')) { return false; }
dom = this.get_node(dom);
if(dom === -1) { dom = this.get_container(); }
if(!dom.length) { return false; }
if(!dom.children('ul').length) { dom.append('
'); }
dom.children('ul').empty().append(data.is('ul') ? data.children('li') : data);
return true;
},
_load_node : function (obj, callback) {
var d = false,
s = this.get_settings().xml;
obj = this.get_node(obj);
if(!obj) { return false; }
switch(!0) {
// data is function
case ($.isFunction(s.data)):
return s.data.call(this, obj, $.proxy(function (d) {
return callback.call(this, this._append_xml_data(obj, d));
}, this));
// data is set, ajax is not set, or both are set, but we are dealing with root node
case ((!!s.data && !s.ajax) || (!!s.data && !!s.ajax && obj === -1)):
return callback.call(this, this._append_xml_data(obj, s.data));
// data is not set, ajax is set, or both are set, but we are dealing with a normal node
case ((!s.data && !!s.ajax) || (!!s.data && !!s.ajax && obj !== -1)):
s.ajax.success = $.proxy(function (d, t, x) {
var s = this.get_settings().xml.ajax;
if($.isFunction(s.success)) {
d = s.success.call(this, d, t, x) || d;
}
callback.call(this, this._append_xml_data(obj, d));
}, this);
s.ajax.error = $.proxy(function (x, t, e) {
var s = this.get_settings().xml.ajax;
if($.isFunction(s.error)) {
s.error.call(this, x, t, e);
}
callback.call(this, false);
}, this);
if(!s.ajax.dataType) { s.ajax.dataType = "xml"; }
if($.isFunction(s.ajax.url)) { s.ajax.url = s.ajax.url.call(this, obj); }
if($.isFunction(s.ajax.data)) { s.ajax.data = s.ajax.data.call(this, obj); }
return $.ajax(s.ajax);
}
},
get_xml : function (mode, obj, is_callback) {
var r = '';
if(!mode) { mode = 'flat'; }
if(typeof is_callback === 'undefined') {
obj = this.get_json(obj);
$.each(obj, $.proxy(function (i, v) {
r += this.get_xml(mode, v, true);
}, this));
return '' +
'<' + '?xml version="1.0" encoding="utf-8" ?>' +
'' + r + '';
}
r += '- ';
r += '';
if(mode === 'flat') { r += '
'; }
if(obj.children) {
$.each(obj.children, $.proxy(function (i, v) {
r += this.get_xml(mode, v, obj.li_attr && obj.li_attr.id ? obj.li_attr.id : true);
}, this));
}
if(mode === 'nest') { r += ''; }
return r;
}
}
});
// include the html plugin by default
$.jstree.defaults.plugins.push("xml");
})(jQuery);
//*/