| 24 |
lars |
1 |
/**
|
|
|
2 |
* ### Search plugin
|
|
|
3 |
*
|
|
|
4 |
* Adds search functionality to jsTree.
|
|
|
5 |
*/
|
|
|
6 |
/*globals jQuery, define, exports, require, document */
|
|
|
7 |
(function (factory) {
|
|
|
8 |
"use strict";
|
|
|
9 |
if (typeof define === 'function' && define.amd) {
|
|
|
10 |
define('jstree.search', ['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.search) { return; }
|
|
|
22 |
|
|
|
23 |
/**
|
|
|
24 |
* stores all defaults for the search plugin
|
|
|
25 |
* @name $.jstree.defaults.search
|
|
|
26 |
* @plugin search
|
|
|
27 |
*/
|
|
|
28 |
$.jstree.defaults.search = {
|
|
|
29 |
/**
|
|
|
30 |
* a jQuery-like AJAX config, which jstree uses if a server should be queried for results.
|
|
|
31 |
*
|
|
|
32 |
* A `str` (which is the search string) parameter will be added with the request, an optional `inside` parameter will be added if the search is limited to a node id. The expected result is a JSON array with nodes that need to be opened so that matching nodes will be revealed.
|
|
|
33 |
* Leave this setting as `false` to not query the server. You can also set this to a function, which will be invoked in the instance's scope and receive 3 parameters - the search string, the callback to call with the array of nodes to load, and the optional node ID to limit the search to
|
|
|
34 |
* @name $.jstree.defaults.search.ajax
|
|
|
35 |
* @plugin search
|
|
|
36 |
*/
|
|
|
37 |
ajax : false,
|
|
|
38 |
/**
|
|
|
39 |
* Indicates if the search should be fuzzy or not (should `chnd3` match `child node 3`). Default is `false`.
|
|
|
40 |
* @name $.jstree.defaults.search.fuzzy
|
|
|
41 |
* @plugin search
|
|
|
42 |
*/
|
|
|
43 |
fuzzy : false,
|
|
|
44 |
/**
|
|
|
45 |
* Indicates if the search should be case sensitive. Default is `false`.
|
|
|
46 |
* @name $.jstree.defaults.search.case_sensitive
|
|
|
47 |
* @plugin search
|
|
|
48 |
*/
|
|
|
49 |
case_sensitive : false,
|
|
|
50 |
/**
|
|
|
51 |
* Indicates if the tree should be filtered (by default) to show only matching nodes (keep in mind this can be a heavy on large trees in old browsers).
|
|
|
52 |
* This setting can be changed at runtime when calling the search method. Default is `false`.
|
|
|
53 |
* @name $.jstree.defaults.search.show_only_matches
|
|
|
54 |
* @plugin search
|
|
|
55 |
*/
|
|
|
56 |
show_only_matches : false,
|
|
|
57 |
/**
|
|
|
58 |
* Indicates if the children of matched element are shown (when show_only_matches is true)
|
|
|
59 |
* This setting can be changed at runtime when calling the search method. Default is `false`.
|
|
|
60 |
* @name $.jstree.defaults.search.show_only_matches_children
|
|
|
61 |
* @plugin search
|
|
|
62 |
*/
|
|
|
63 |
show_only_matches_children : false,
|
|
|
64 |
/**
|
|
|
65 |
* Indicates if all nodes opened to reveal the search result, should be closed when the search is cleared or a new search is performed. Default is `true`.
|
|
|
66 |
* @name $.jstree.defaults.search.close_opened_onclear
|
|
|
67 |
* @plugin search
|
|
|
68 |
*/
|
|
|
69 |
close_opened_onclear : true,
|
|
|
70 |
/**
|
|
|
71 |
* Indicates if only leaf nodes should be included in search results. Default is `false`.
|
|
|
72 |
* @name $.jstree.defaults.search.search_leaves_only
|
|
|
73 |
* @plugin search
|
|
|
74 |
*/
|
|
|
75 |
search_leaves_only : false,
|
|
|
76 |
/**
|
|
|
77 |
* If set to a function it wil be called in the instance's scope with two arguments - search string and node (where node will be every node in the structure, so use with caution).
|
|
|
78 |
* If the function returns a truthy value the node will be considered a match (it might not be displayed if search_only_leaves is set to true and the node is not a leaf). Default is `false`.
|
|
|
79 |
* @name $.jstree.defaults.search.search_callback
|
|
|
80 |
* @plugin search
|
|
|
81 |
*/
|
|
|
82 |
search_callback : false
|
|
|
83 |
};
|
|
|
84 |
|
|
|
85 |
$.jstree.plugins.search = function (options, parent) {
|
|
|
86 |
this.bind = function () {
|
|
|
87 |
parent.bind.call(this);
|
|
|
88 |
|
|
|
89 |
this._data.search.str = "";
|
|
|
90 |
this._data.search.dom = $();
|
|
|
91 |
this._data.search.res = [];
|
|
|
92 |
this._data.search.opn = [];
|
|
|
93 |
this._data.search.som = false;
|
|
|
94 |
this._data.search.smc = false;
|
|
|
95 |
this._data.search.hdn = [];
|
|
|
96 |
|
|
|
97 |
this.element
|
|
|
98 |
.on("search.jstree", $.proxy(function (e, data) {
|
|
|
99 |
if(this._data.search.som && data.res.length) {
|
|
|
100 |
var m = this._model.data, i, j, p = [];
|
|
|
101 |
for(i = 0, j = data.res.length; i < j; i++) {
|
|
|
102 |
if(m[data.res[i]] && !m[data.res[i]].state.hidden) {
|
|
|
103 |
p.push(data.res[i]);
|
|
|
104 |
p = p.concat(m[data.res[i]].parents);
|
|
|
105 |
if(this._data.search.smc) {
|
|
|
106 |
p = p.concat(m[data.res[i]].children_d);
|
|
|
107 |
}
|
|
|
108 |
}
|
|
|
109 |
}
|
|
|
110 |
p = $.vakata.array_remove_item($.vakata.array_unique(p), $.jstree.root);
|
|
|
111 |
this._data.search.hdn = this.hide_all(true);
|
|
|
112 |
this.show_node(p);
|
|
|
113 |
}
|
|
|
114 |
}, this))
|
|
|
115 |
.on("clear_search.jstree", $.proxy(function (e, data) {
|
|
|
116 |
if(this._data.search.som && data.res.length) {
|
|
|
117 |
this.show_node(this._data.search.hdn);
|
|
|
118 |
}
|
|
|
119 |
}, this));
|
|
|
120 |
};
|
|
|
121 |
/**
|
|
|
122 |
* used to search the tree nodes for a given string
|
|
|
123 |
* @name search(str [, skip_async])
|
|
|
124 |
* @param {String} str the search string
|
|
|
125 |
* @param {Boolean} skip_async if set to true server will not be queried even if configured
|
|
|
126 |
* @param {Boolean} show_only_matches if set to true only matching nodes will be shown (keep in mind this can be very slow on large trees or old browsers)
|
|
|
127 |
* @param {mixed} inside an optional node to whose children to limit the search
|
|
|
128 |
* @param {Boolean} append if set to true the results of this search are appended to the previous search
|
|
|
129 |
* @plugin search
|
|
|
130 |
* @trigger search.jstree
|
|
|
131 |
*/
|
|
|
132 |
this.search = function (str, skip_async, show_only_matches, inside, append, show_only_matches_children) {
|
|
|
133 |
if(str === false || $.trim(str.toString()) === "") {
|
|
|
134 |
return this.clear_search();
|
|
|
135 |
}
|
|
|
136 |
inside = this.get_node(inside);
|
|
|
137 |
inside = inside && inside.id ? inside.id : null;
|
|
|
138 |
str = str.toString();
|
|
|
139 |
var s = this.settings.search,
|
|
|
140 |
a = s.ajax ? s.ajax : false,
|
|
|
141 |
m = this._model.data,
|
|
|
142 |
f = null,
|
|
|
143 |
r = [],
|
|
|
144 |
p = [], i, j;
|
|
|
145 |
if(this._data.search.res.length && !append) {
|
|
|
146 |
this.clear_search();
|
|
|
147 |
}
|
|
|
148 |
if(show_only_matches === undefined) {
|
|
|
149 |
show_only_matches = s.show_only_matches;
|
|
|
150 |
}
|
|
|
151 |
if(show_only_matches_children === undefined) {
|
|
|
152 |
show_only_matches_children = s.show_only_matches_children;
|
|
|
153 |
}
|
|
|
154 |
if(!skip_async && a !== false) {
|
|
|
155 |
if($.isFunction(a)) {
|
|
|
156 |
return a.call(this, str, $.proxy(function (d) {
|
|
|
157 |
if(d && d.d) { d = d.d; }
|
|
|
158 |
this._load_nodes(!$.isArray(d) ? [] : $.vakata.array_unique(d), function () {
|
|
|
159 |
this.search(str, true, show_only_matches, inside, append);
|
|
|
160 |
}, true);
|
|
|
161 |
}, this), inside);
|
|
|
162 |
}
|
|
|
163 |
else {
|
|
|
164 |
a = $.extend({}, a);
|
|
|
165 |
if(!a.data) { a.data = {}; }
|
|
|
166 |
a.data.str = str;
|
|
|
167 |
if(inside) {
|
|
|
168 |
a.data.inside = inside;
|
|
|
169 |
}
|
|
|
170 |
return $.ajax(a)
|
|
|
171 |
.fail($.proxy(function () {
|
|
|
172 |
this._data.core.last_error = { 'error' : 'ajax', 'plugin' : 'search', 'id' : 'search_01', 'reason' : 'Could not load search parents', 'data' : JSON.stringify(a) };
|
|
|
173 |
this.settings.core.error.call(this, this._data.core.last_error);
|
|
|
174 |
}, this))
|
|
|
175 |
.done($.proxy(function (d) {
|
|
|
176 |
if(d && d.d) { d = d.d; }
|
|
|
177 |
this._load_nodes(!$.isArray(d) ? [] : $.vakata.array_unique(d), function () {
|
|
|
178 |
this.search(str, true, show_only_matches, inside, append);
|
|
|
179 |
}, true);
|
|
|
180 |
}, this));
|
|
|
181 |
}
|
|
|
182 |
}
|
|
|
183 |
if(!append) {
|
|
|
184 |
this._data.search.str = str;
|
|
|
185 |
this._data.search.dom = $();
|
|
|
186 |
this._data.search.res = [];
|
|
|
187 |
this._data.search.opn = [];
|
|
|
188 |
this._data.search.som = show_only_matches;
|
|
|
189 |
this._data.search.smc = show_only_matches_children;
|
|
|
190 |
}
|
|
|
191 |
|
|
|
192 |
f = new $.vakata.search(str, true, { caseSensitive : s.case_sensitive, fuzzy : s.fuzzy });
|
|
|
193 |
$.each(m[inside ? inside : $.jstree.root].children_d, function (ii, i) {
|
|
|
194 |
var v = m[i];
|
|
|
195 |
if(v.text && (!s.search_leaves_only || (v.state.loaded && v.children.length === 0)) && ( (s.search_callback && s.search_callback.call(this, str, v)) || (!s.search_callback && f.search(v.text).isMatch) ) ) {
|
|
|
196 |
r.push(i);
|
|
|
197 |
p = p.concat(v.parents);
|
|
|
198 |
}
|
|
|
199 |
});
|
|
|
200 |
if(r.length) {
|
|
|
201 |
p = $.vakata.array_unique(p);
|
|
|
202 |
for(i = 0, j = p.length; i < j; i++) {
|
|
|
203 |
if(p[i] !== $.jstree.root && m[p[i]] && this.open_node(p[i], null, 0) === true) {
|
|
|
204 |
this._data.search.opn.push(p[i]);
|
|
|
205 |
}
|
|
|
206 |
}
|
|
|
207 |
if(!append) {
|
|
|
208 |
this._data.search.dom = $(this.element[0].querySelectorAll('#' + $.map(r, function (v) { return "0123456789".indexOf(v[0]) !== -1 ? '\\3' + v[0] + ' ' + v.substr(1).replace($.jstree.idregex,'\\$&') : v.replace($.jstree.idregex,'\\$&'); }).join(', #')));
|
|
|
209 |
this._data.search.res = r;
|
|
|
210 |
}
|
|
|
211 |
else {
|
|
|
212 |
this._data.search.dom = this._data.search.dom.add($(this.element[0].querySelectorAll('#' + $.map(r, function (v) { return "0123456789".indexOf(v[0]) !== -1 ? '\\3' + v[0] + ' ' + v.substr(1).replace($.jstree.idregex,'\\$&') : v.replace($.jstree.idregex,'\\$&'); }).join(', #'))));
|
|
|
213 |
this._data.search.res = $.vakata.array_unique(this._data.search.res.concat(r));
|
|
|
214 |
}
|
|
|
215 |
this._data.search.dom.children(".jstree-anchor").addClass('jstree-search');
|
|
|
216 |
}
|
|
|
217 |
/**
|
|
|
218 |
* triggered after search is complete
|
|
|
219 |
* @event
|
|
|
220 |
* @name search.jstree
|
|
|
221 |
* @param {jQuery} nodes a jQuery collection of matching nodes
|
|
|
222 |
* @param {String} str the search string
|
|
|
223 |
* @param {Array} res a collection of objects represeing the matching nodes
|
|
|
224 |
* @plugin search
|
|
|
225 |
*/
|
|
|
226 |
this.trigger('search', { nodes : this._data.search.dom, str : str, res : this._data.search.res, show_only_matches : show_only_matches });
|
|
|
227 |
};
|
|
|
228 |
/**
|
|
|
229 |
* used to clear the last search (removes classes and shows all nodes if filtering is on)
|
|
|
230 |
* @name clear_search()
|
|
|
231 |
* @plugin search
|
|
|
232 |
* @trigger clear_search.jstree
|
|
|
233 |
*/
|
|
|
234 |
this.clear_search = function () {
|
|
|
235 |
if(this.settings.search.close_opened_onclear) {
|
|
|
236 |
this.close_node(this._data.search.opn, 0);
|
|
|
237 |
}
|
|
|
238 |
/**
|
|
|
239 |
* triggered after search is complete
|
|
|
240 |
* @event
|
|
|
241 |
* @name clear_search.jstree
|
|
|
242 |
* @param {jQuery} nodes a jQuery collection of matching nodes (the result from the last search)
|
|
|
243 |
* @param {String} str the search string (the last search string)
|
|
|
244 |
* @param {Array} res a collection of objects represeing the matching nodes (the result from the last search)
|
|
|
245 |
* @plugin search
|
|
|
246 |
*/
|
|
|
247 |
this.trigger('clear_search', { 'nodes' : this._data.search.dom, str : this._data.search.str, res : this._data.search.res });
|
|
|
248 |
if(this._data.search.res.length) {
|
|
|
249 |
this._data.search.dom = $(this.element[0].querySelectorAll('#' + $.map(this._data.search.res, function (v) {
|
|
|
250 |
return "0123456789".indexOf(v[0]) !== -1 ? '\\3' + v[0] + ' ' + v.substr(1).replace($.jstree.idregex,'\\$&') : v.replace($.jstree.idregex,'\\$&');
|
|
|
251 |
}).join(', #')));
|
|
|
252 |
this._data.search.dom.children(".jstree-anchor").removeClass("jstree-search");
|
|
|
253 |
}
|
|
|
254 |
this._data.search.str = "";
|
|
|
255 |
this._data.search.res = [];
|
|
|
256 |
this._data.search.opn = [];
|
|
|
257 |
this._data.search.dom = $();
|
|
|
258 |
};
|
|
|
259 |
|
|
|
260 |
this.redraw_node = function(obj, deep, callback, force_render) {
|
|
|
261 |
obj = parent.redraw_node.apply(this, arguments);
|
|
|
262 |
if(obj) {
|
|
|
263 |
if($.inArray(obj.id, this._data.search.res) !== -1) {
|
|
|
264 |
var i, j, tmp = null;
|
|
|
265 |
for(i = 0, j = obj.childNodes.length; i < j; i++) {
|
|
|
266 |
if(obj.childNodes[i] && obj.childNodes[i].className && obj.childNodes[i].className.indexOf("jstree-anchor") !== -1) {
|
|
|
267 |
tmp = obj.childNodes[i];
|
|
|
268 |
break;
|
|
|
269 |
}
|
|
|
270 |
}
|
|
|
271 |
if(tmp) {
|
|
|
272 |
tmp.className += ' jstree-search';
|
|
|
273 |
}
|
|
|
274 |
}
|
|
|
275 |
}
|
|
|
276 |
return obj;
|
|
|
277 |
};
|
|
|
278 |
};
|
|
|
279 |
|
|
|
280 |
// helpers
|
|
|
281 |
(function ($) {
|
|
|
282 |
// from http://kiro.me/projects/fuse.html
|
|
|
283 |
$.vakata.search = function(pattern, txt, options) {
|
|
|
284 |
options = options || {};
|
|
|
285 |
options = $.extend({}, $.vakata.search.defaults, options);
|
|
|
286 |
if(options.fuzzy !== false) {
|
|
|
287 |
options.fuzzy = true;
|
|
|
288 |
}
|
|
|
289 |
pattern = options.caseSensitive ? pattern : pattern.toLowerCase();
|
|
|
290 |
var MATCH_LOCATION = options.location,
|
|
|
291 |
MATCH_DISTANCE = options.distance,
|
|
|
292 |
MATCH_THRESHOLD = options.threshold,
|
|
|
293 |
patternLen = pattern.length,
|
|
|
294 |
matchmask, pattern_alphabet, match_bitapScore, search;
|
|
|
295 |
if(patternLen > 32) {
|
|
|
296 |
options.fuzzy = false;
|
|
|
297 |
}
|
|
|
298 |
if(options.fuzzy) {
|
|
|
299 |
matchmask = 1 << (patternLen - 1);
|
|
|
300 |
pattern_alphabet = (function () {
|
|
|
301 |
var mask = {},
|
|
|
302 |
i = 0;
|
|
|
303 |
for (i = 0; i < patternLen; i++) {
|
|
|
304 |
mask[pattern.charAt(i)] = 0;
|
|
|
305 |
}
|
|
|
306 |
for (i = 0; i < patternLen; i++) {
|
|
|
307 |
mask[pattern.charAt(i)] |= 1 << (patternLen - i - 1);
|
|
|
308 |
}
|
|
|
309 |
return mask;
|
|
|
310 |
}());
|
|
|
311 |
match_bitapScore = function (e, x) {
|
|
|
312 |
var accuracy = e / patternLen,
|
|
|
313 |
proximity = Math.abs(MATCH_LOCATION - x);
|
|
|
314 |
if(!MATCH_DISTANCE) {
|
|
|
315 |
return proximity ? 1.0 : accuracy;
|
|
|
316 |
}
|
|
|
317 |
return accuracy + (proximity / MATCH_DISTANCE);
|
|
|
318 |
};
|
|
|
319 |
}
|
|
|
320 |
search = function (text) {
|
|
|
321 |
text = options.caseSensitive ? text : text.toLowerCase();
|
|
|
322 |
if(pattern === text || text.indexOf(pattern) !== -1) {
|
|
|
323 |
return {
|
|
|
324 |
isMatch: true,
|
|
|
325 |
score: 0
|
|
|
326 |
};
|
|
|
327 |
}
|
|
|
328 |
if(!options.fuzzy) {
|
|
|
329 |
return {
|
|
|
330 |
isMatch: false,
|
|
|
331 |
score: 1
|
|
|
332 |
};
|
|
|
333 |
}
|
|
|
334 |
var i, j,
|
|
|
335 |
textLen = text.length,
|
|
|
336 |
scoreThreshold = MATCH_THRESHOLD,
|
|
|
337 |
bestLoc = text.indexOf(pattern, MATCH_LOCATION),
|
|
|
338 |
binMin, binMid,
|
|
|
339 |
binMax = patternLen + textLen,
|
|
|
340 |
lastRd, start, finish, rd, charMatch,
|
|
|
341 |
score = 1,
|
|
|
342 |
locations = [];
|
|
|
343 |
if (bestLoc !== -1) {
|
|
|
344 |
scoreThreshold = Math.min(match_bitapScore(0, bestLoc), scoreThreshold);
|
|
|
345 |
bestLoc = text.lastIndexOf(pattern, MATCH_LOCATION + patternLen);
|
|
|
346 |
if (bestLoc !== -1) {
|
|
|
347 |
scoreThreshold = Math.min(match_bitapScore(0, bestLoc), scoreThreshold);
|
|
|
348 |
}
|
|
|
349 |
}
|
|
|
350 |
bestLoc = -1;
|
|
|
351 |
for (i = 0; i < patternLen; i++) {
|
|
|
352 |
binMin = 0;
|
|
|
353 |
binMid = binMax;
|
|
|
354 |
while (binMin < binMid) {
|
|
|
355 |
if (match_bitapScore(i, MATCH_LOCATION + binMid) <= scoreThreshold) {
|
|
|
356 |
binMin = binMid;
|
|
|
357 |
} else {
|
|
|
358 |
binMax = binMid;
|
|
|
359 |
}
|
|
|
360 |
binMid = Math.floor((binMax - binMin) / 2 + binMin);
|
|
|
361 |
}
|
|
|
362 |
binMax = binMid;
|
|
|
363 |
start = Math.max(1, MATCH_LOCATION - binMid + 1);
|
|
|
364 |
finish = Math.min(MATCH_LOCATION + binMid, textLen) + patternLen;
|
|
|
365 |
rd = new Array(finish + 2);
|
|
|
366 |
rd[finish + 1] = (1 << i) - 1;
|
|
|
367 |
for (j = finish; j >= start; j--) {
|
|
|
368 |
charMatch = pattern_alphabet[text.charAt(j - 1)];
|
|
|
369 |
if (i === 0) {
|
|
|
370 |
rd[j] = ((rd[j + 1] << 1) | 1) & charMatch;
|
|
|
371 |
} else {
|
|
|
372 |
rd[j] = ((rd[j + 1] << 1) | 1) & charMatch | (((lastRd[j + 1] | lastRd[j]) << 1) | 1) | lastRd[j + 1];
|
|
|
373 |
}
|
|
|
374 |
if (rd[j] & matchmask) {
|
|
|
375 |
score = match_bitapScore(i, j - 1);
|
|
|
376 |
if (score <= scoreThreshold) {
|
|
|
377 |
scoreThreshold = score;
|
|
|
378 |
bestLoc = j - 1;
|
|
|
379 |
locations.push(bestLoc);
|
|
|
380 |
if (bestLoc > MATCH_LOCATION) {
|
|
|
381 |
start = Math.max(1, 2 * MATCH_LOCATION - bestLoc);
|
|
|
382 |
} else {
|
|
|
383 |
break;
|
|
|
384 |
}
|
|
|
385 |
}
|
|
|
386 |
}
|
|
|
387 |
}
|
|
|
388 |
if (match_bitapScore(i + 1, MATCH_LOCATION) > scoreThreshold) {
|
|
|
389 |
break;
|
|
|
390 |
}
|
|
|
391 |
lastRd = rd;
|
|
|
392 |
}
|
|
|
393 |
return {
|
|
|
394 |
isMatch: bestLoc >= 0,
|
|
|
395 |
score: score
|
|
|
396 |
};
|
|
|
397 |
};
|
|
|
398 |
return txt === true ? { 'search' : search } : search(txt);
|
|
|
399 |
};
|
|
|
400 |
$.vakata.search.defaults = {
|
|
|
401 |
location : 0,
|
|
|
402 |
distance : 100,
|
|
|
403 |
threshold : 0.6,
|
|
|
404 |
fuzzy : false,
|
|
|
405 |
caseSensitive : false
|
|
|
406 |
};
|
|
|
407 |
}($));
|
|
|
408 |
|
|
|
409 |
// include the search plugin by default
|
|
|
410 |
// $.jstree.defaults.plugins.push("search");
|
|
|
411 |
}));
|