Skip to content

Commit

Permalink
added initial get_json function to core (needs more work though!) - n…
Browse files Browse the repository at this point in the history
…ow all JSON operations are in the core
  • Loading branch information
vakata committed Jul 13, 2011
1 parent e32eb85 commit 40f9890
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 14 deletions.
11 changes: 11 additions & 0 deletions index.html
Expand Up @@ -8,6 +8,7 @@
<script type="text/javascript" src="_lib/jquery.js"></script>
<script type="text/javascript" src="_lib/jquery.hotkeys.js"></script>
<script type="text/javascript" src="vakata.js"></script>
<!--
<script type="text/javascript" src="jstree.core.js"></script>
<script type="text/javascript" src="jstree.themes.js"></script>
<script type="text/javascript" src="jstree.ui.js"></script>
Expand All @@ -17,6 +18,7 @@
<script type="text/javascript" src="jstree.sort.js"></script>
<script type="text/javascript" src="jstree.contextmenu.js"></script>
<script type="text/javascript" src="jstree.hotkeys.js"></script>
-->
<style type="text/css">
html, body { margin:0; padding:0; font-family:Verdana; font-size:10px; }
#container { width:800px; margin:0 auto; }
Expand All @@ -28,6 +30,15 @@
STATE = $('#jstree').jstree('get_state');
}
$(function () {
return;
/*
var m = $("#jstree-json")[0].attributes, r = '';
$.each(m, function (i, v) {
if(v.nodeValue !== null && v.nodeValue != '') r += v.nodeName + "=" + v.nodeValue + "; ";
});
alert(r);
*/

$("#jstree")
.jstree()
.bind('__ready.jstree', function () {
Expand Down
71 changes: 70 additions & 1 deletion jstree.core.js
Expand Up @@ -1042,7 +1042,7 @@ Some static functions and variables, unless you know exactly what you are doing
Function: clean_node
This function converts inserted nodes to the required by jsTree format. It takes care of converting a simple unodreder list to the internally used markup.
The core calls this function automatically when new data arrives (by binding to the <load_node> event).
Each plugin may override this function to include its own markup, but keep in mind to do it like that:
Each plugin may override this function to include its own source, but keep in mind to do it like that:
> clean_node : function(obj) {
> obj = this.__call_old();
> obj.each(function () {
Expand Down Expand Up @@ -1390,6 +1390,75 @@ Some static functions and variables, unless you know exactly what you are doing
}
return li;
},
/*
Function: get_json
This function returns the whole tree (or a single node) in JSON format.
Each plugin may override this function to include its own source, but keep in mind to do it like that:
> get_json : function(obj, li_attr, a_attr, is_callback) {
> var r = this.__call_old();
> if(is_callback && obj && obj.length && obj.is && obj.is('li')) {
> if(<some-condition>) { r.data.jstree.<some-key> = <some-value-this-plugin-will-process>; }
> }
> return r;
> }
Parameters:
obj - *mixed* the input to parse
li_attr - *array* the attributes to collect from the li node (defaults to [ 'id', 'class', 'rel' ])
a_attr - *array* the attributes to collect from the a node (defaults to [ 'href', 'title' ])
is_callback - do not modify this, jstree uses this parameter to keep track of the recursion
Returns:
Array - an array consisting of objects (one for each node)
*/
get_json : function (obj, li_attr, a_attr, is_callback) {
// TODO: li_attr, a_attr - remove in favour of collection the attributes automatically
obj = this.get_node(obj);
if(!is_callback) {
li_attr = $.isArray(li_attr) ? li_attr : [ "id", "class", "rel" ];
a_attr = $.isArray(a_attr) ? a_attr : [ "href", "title" ];
if(!obj || obj === -1) { obj = this.get_container_ul().children("li"); }
}
var r, t;
if(!obj || obj.length) { return false; }
if(obj.length > 1 || !is_callback) {
r = [];
t = this;
obj.each(function () {
r.push(t.get_json(this, li_attr, a_attr, true));
});
return r;
}
r = { title : this.get_text(obj), data : $.extend(true, {}, obj.data() || {}), children : false };
if(li_attr.length) {
r.li_attr = {};
$.each(li_attr, function (i, v) {
// TODO: if ID - do not replace
r.li_attr[v] = obj.attr(v) ? $.trim((' ' + obj.attr(v)).replace(/ jstree[^ ]*/ig,'').replace(/\s+$/ig," ")) : '';
});
}
if(a_attr.length) {
r.a_attr = {};
t = obj.children('a:eq(0)');
$.each(a_attr, function (i, v) {
r.a_attr[v] = t.attr(v) ? $.trim((' ' + t.attr(v)).replace(/ jstree[^ ]*/ig,'').replace(/\s+$/ig," ")) : '';
});
}

if(!r.data.jstree) { r.data.jstree = {}; }
if(this.is_open(obj)) { r.data.jstree.opened = true; }
if(this.is_closed(obj)) { r.data.jstree.closed = true; }

obj = obj.find('> ul > li');
if(obj.length) {
r.children = [];
t = this;
obj.each(function () {
r.children.push(t.get_json(this, li_attr, a_attr, true));
});
}
return r;
},
/*
Function: create_node
This function creates a new node.
Expand Down
14 changes: 3 additions & 11 deletions jstree.json.js
Expand Up @@ -8,11 +8,11 @@ This plugin makes it possible for jstree to use JSON data sources.
this.get_container()
.bind("__after_close.jstree", $.proxy(function (e, data) {
var t = $(data.rslt.obj);
if(this.get_settings(true).json.progressive_unload && t.data('jstree').parsed_children) {
t.data('jstree').children = t.data('jstree').parsed_children;
if(this.get_settings(true).json.progressive_unload) {
t.data('jstree').children = this.get_json(t)[0].children;
t.children("ul").remove();
}
}, this))
}, this));
},
defaults : {
data : false,
Expand All @@ -30,11 +30,6 @@ This plugin makes it possible for jstree to use JSON data sources.
node.data.jstree.children = node.children;
node.children = true;
}
if(!s.progressive_render && s.progressive_unload) {
if(!node.data) { node.data = {}; }
if(!node.data.jstree) { node.data.jstree = {}; }
node.data.jstree.parsed_children = node.children;
}
}
return this.__call_old(true, node);
},
Expand Down Expand Up @@ -63,9 +58,6 @@ This plugin makes it possible for jstree to use JSON data sources.
case (obj !== -1 && obj.length && obj.data('jstree') && $.isArray(obj.data('jstree').children)):
d = obj.data('jstree').children;
obj.data('jstree').children = null;
if(this.get_settings(true).json.progressive_unload) {
obj.data('jstree').parsed_children = d;
}
return callback.call(this, this._append_json_data(obj, d));
// no settings
case (!s.data && !s.ajax):
Expand Down
4 changes: 2 additions & 2 deletions vakata.js
Expand Up @@ -1181,12 +1181,12 @@ Functions for dealing with localStorage with fallback to userData or cookies. A
}
else {
_storage_elm = document.createElement('link');
if(_storage_elm.addBehavior){
if(_storage_elm.addBehavior) {
_storage_elm.style.behavior = 'url(#default#userData)';
document.getElementsByTagName('head')[0].appendChild(_storage_elm);
_storage_elm.load("jStorage");
var data = "{}";
try{
try {
data = _storage_elm.getAttribute("jStorage");
} catch(E5) {}
_storage_service.jStorage = data;
Expand Down

0 comments on commit 40f9890

Please sign in to comment.