Little code refactoring in module search files

This commit is contained in:
Oleg Broslavsky
2014-12-18 17:47:08 +07:00
parent 294a7cb5c6
commit 9268ba9f5e
2 changed files with 48 additions and 31 deletions

View File

@@ -1,26 +1,42 @@
function search_module(module_tree, searching_string) {
var json_object_clone = new Array();
/*
* Keyword search for command module panel.
* Words in query are searched as separated queries. You can search for exact matching using double qoutes arround query
*/
function search_module(module_tree, query_string) {
if ( query_string.search(/\w/) == -1 )
return tree_array;
// copy module tree w/o ExtJS service properties
var tree_array = new Array();
for ( var i = 0; i < module_tree.length; i++ )
json_object_clone.push(module_tree[i].attributes);
if ( searching_string.search(/\w/) == -1 )
return json_object_clone;
var json_object = jQuery.extend(true, [], json_object_clone);
searching_string = searching_string.replace(/"\s*"/g, " ").replace(/\s+/g, " ").match(/"[^"]+"|\S+/g);
searching_string.forEach(prepare_searching_string);
var result = json_object.filter(form_new_modules_tree);
result.forEach(recount_modules_and_expand_directories);
return result;
tree_array.push(module_tree[i].attributes);
var json_object = jQuery.extend(true, [], tree_array);
// split query string into separate words and exact phrases
query_string = query_string.replace(/"\s*"/g, " ").replace(/\s+/g, " ").match(/"[^"]+"|\S+/g);
query_string.forEach(prepare_query_string);
function prepare_searching_string(string, index, array){
var result = json_object.filter(form_new_modules_tree);
result.forEach(recount_modules_and_expand_directories);
return result;
// remove quotes from phrases for exact match
function prepare_query_string(string, index, array){
array[index] = string.toLowerCase().replace(/"/g, "");
}
function check_name_of_module(str) {
// True if this.toString() contains str
function check_module_name(str) {
return Boolean(this.toString().toLowerCase().replace(/\s\([0-9]+\)/g,"").indexOf(str) + 1);
}
// func for JSON filter
// Build a new tree from modules which are appropriate for any part of query
function form_new_modules_tree(element) {
if ( searching_string.some(check_name_of_module, element.text) )
if ( query_string.some(check_module_name, element.text) )
return true;
if ( element.children ) {
element.children = element.children.filter(form_new_modules_tree);
@@ -32,13 +48,16 @@ function search_module(module_tree, searching_string) {
function recount_modules_and_expand_directories(element) {
if ( element.children ) {
element.expanded = true;
var count_of_modules = element.children.length;
var modules_in_directory = element.children.length;
// visit all
for ( var i = 0; i < element.children.length; i++ )
if ( element.children )
count_of_modules += recount_modules_and_expand_directories(element.children[i]);
modules_in_directory += recount_modules_and_expand_directories(element.children[i]);
// expand them
element.children.forEach(recount_modules_and_expand_directories);
element.text = element.text.replace(/([-_ 0-9a-zA-Z]+)\(([0-9]+)\)/, "$1(" + count_of_modules + ")")
return count_of_modules - 1;
// and set new number of modules in directory
element.text = element.text.replace(/([-_ 0-9a-zA-Z]+)\(([0-9]+)\)/, "$1(" + modules_in_directory + ")")
return modules_in_directory - 1;
}
return 0;
}