Subversion-Projekte lars-tiefland.ci

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
875 lars 1
/**
2
 * ### Massload plugin
3
 *
4
 * Adds massload functionality to jsTree, so that multiple nodes can be loaded in a single request (only useful with lazy loading).
5
 */
6
/*globals jQuery, define, exports, require, document */
7
(function (factory) {
8
	"use strict";
9
	if (typeof define === 'function' && define.amd) {
10
		define('jstree.massload', ['jquery','jstree'], factory);
11
	}
12
	else if(typeof exports === 'object') {
13
		factory(require('jquery'), require('jstree'));
14
	}
15
	else {
16
		factory(jQuery, jQuery.jstree);
17
	}
18
}(function ($, jstree, undefined) {
19
	"use strict";
20
 
21
	if($.jstree.plugins.massload) { return; }
22
 
23
	/**
24
	 * massload configuration
25
	 *
26
	 * It is possible to set this to a standard jQuery-like AJAX config.
27
	 * In addition to the standard jQuery ajax options here you can supply functions for `data` and `url`, the functions will be run in the current instance's scope and a param will be passed indicating which node IDs need to be loaded, the return value of those functions will be used.
28
	 *
29
	 * You can also set this to a function, that function will receive the node IDs being loaded as argument and a second param which is a function (callback) which should be called with the result.
30
	 *
31
	 * Both the AJAX and the function approach rely on the same return value - an object where the keys are the node IDs, and the value is the children of that node as an array.
32
	 *
33
	 *	{
34
	 *		"id1" : [{ "text" : "Child of ID1", "id" : "c1" }, { "text" : "Another child of ID1", "id" : "c2" }],
35
	 *		"id2" : [{ "text" : "Child of ID2", "id" : "c3" }]
36
	 *	}
37
	 *
38
	 * @name $.jstree.defaults.massload
39
	 * @plugin massload
40
	 */
41
	$.jstree.defaults.massload = null;
42
	$.jstree.plugins.massload = function (options, parent) {
43
		this.init = function (el, options) {
44
			parent.init.call(this, el, options);
45
			this._data.massload = {};
46
		};
47
		this._load_nodes = function (nodes, callback, is_callback) {
48
			var s = this.settings.massload;
49
			if(is_callback && !$.isEmptyObject(this._data.massload)) {
50
				return parent._load_nodes.call(this, nodes, callback, is_callback);
51
			}
52
			if($.isFunction(s)) {
53
				return s.call(this, nodes, $.proxy(function (data) {
54
					if(data) {
55
						for(var i in data) {
56
							if(data.hasOwnProperty(i)) {
57
								this._data.massload[i] = data[i];
58
							}
59
						}
60
					}
61
					parent._load_nodes.call(this, nodes, callback, is_callback);
62
				}, this));
63
			}
64
			if(typeof s === 'object' && s && s.url) {
65
				s = $.extend(true, {}, s);
66
				if($.isFunction(s.url)) {
67
					s.url = s.url.call(this, nodes);
68
				}
69
				if($.isFunction(s.data)) {
70
					s.data = s.data.call(this, nodes);
71
				}
72
				return $.ajax(s)
73
					.done($.proxy(function (data,t,x) {
74
							if(data) {
75
								for(var i in data) {
76
									if(data.hasOwnProperty(i)) {
77
										this._data.massload[i] = data[i];
78
									}
79
								}
80
							}
81
							parent._load_nodes.call(this, nodes, callback, is_callback);
82
						}, this))
83
					.fail($.proxy(function (f) {
84
							parent._load_nodes.call(this, nodes, callback, is_callback);
85
						}, this));
86
			}
87
			return parent._load_nodes.call(this, nodes, callback, is_callback);
88
		};
89
		this._load_node = function (obj, callback) {
90
			var d = this._data.massload[obj.id];
91
			if(d) {
92
				return this[typeof d === 'string' ? '_append_html_data' : '_append_json_data'](obj, typeof d === 'string' ? $($.parseHTML(d)).filter(function () { return this.nodeType !== 3; }) : d, function (status) {
93
					callback.call(this, status);
94
					delete this._data.massload[obj.id];
95
				});
96
			}
97
			return parent._load_node.call(this, obj, callback);
98
		};
99
	};
100
}));