Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
beefproject
GitHub Repository: beefproject/beef
Path: blob/master/extensions/admin_ui/media/javascript/ui/panel/ModuleSearching.js
1154 views
1
/*
2
* Keyword search for command module panel.
3
* Words in query are searched as separated queries. You can search for exact matching using double qoutes arround query
4
*/
5
6
function search_module(module_tree, query_string) {
7
if ( query_string.search(/\w/) == -1 )
8
return tree_array;
9
10
// copy module tree w/o ExtJS service properties
11
var tree_array = new Array();
12
for ( var i = 0; i < module_tree.length; i++ )
13
tree_array.push(module_tree[i].attributes);
14
15
var json_object = jQuery.extend(true, [], tree_array);
16
17
// split query string into separate words and exact phrases
18
query_string = query_string.replace(/"\s*"/g, " ").replace(/\s+/g, " ").match(/"[^"]+"|\S+/g);
19
query_string.forEach(prepare_query_string);
20
21
var result = json_object.filter(form_new_modules_tree);
22
result.forEach(recount_modules_and_expand_directories);
23
24
return result;
25
26
// remove quotes from phrases for exact match
27
function prepare_query_string(string, index, array){
28
array[index] = string.toLowerCase().replace(/"/g, "");
29
}
30
31
// True if this.toString() contains str
32
function check_module_name(str) {
33
return Boolean(this.toString().toLowerCase().replace(/\s\([0-9]+\)/g,"").indexOf(str) + 1);
34
}
35
36
// func for JSON filter
37
// Build a new tree from modules which are appropriate for any part of query
38
function form_new_modules_tree(element) {
39
if ( query_string.some(check_module_name, element.text) )
40
return true;
41
if ( element.children ) {
42
element.children = element.children.filter(form_new_modules_tree);
43
return Boolean(element.children.length);
44
}
45
return false;
46
}
47
48
function recount_modules_and_expand_directories(element) {
49
if ( element.children ) {
50
element.expanded = true;
51
var modules_in_directory = element.children.length;
52
// visit all
53
for ( var i = 0; i < element.children.length; i++ )
54
if ( element.children )
55
modules_in_directory += recount_modules_and_expand_directories(element.children[i]);
56
// expand them
57
element.children.forEach(recount_modules_and_expand_directories);
58
// and set new number of modules in directory
59
element.text = element.text.replace(/([-_ 0-9a-zA-Z]+)\(([0-9]+)\)/, "$1(" + modules_in_directory + ")")
60
return modules_in_directory - 1;
61
}
62
return 0;
63
}
64
}
65
66