From 9268ba9f5e6bb01eab9edd5ae031f1c7ca62571a Mon Sep 17 00:00:00 2001 From: Oleg Broslavsky Date: Thu, 18 Dec 2014 17:47:08 +0700 Subject: [PATCH] Little code refactoring in module search files --- .../javascript/ui/panel/ModuleSearching.js | 55 +++++++++++++------ .../ui/panel/tabs/ZombieTabCommands.js | 24 ++++---- 2 files changed, 48 insertions(+), 31 deletions(-) diff --git a/extensions/admin_ui/media/javascript/ui/panel/ModuleSearching.js b/extensions/admin_ui/media/javascript/ui/panel/ModuleSearching.js index 9970df7e1..ffdbe12e1 100644 --- a/extensions/admin_ui/media/javascript/ui/panel/ModuleSearching.js +++ b/extensions/admin_ui/media/javascript/ui/panel/ModuleSearching.js @@ -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; } diff --git a/extensions/admin_ui/media/javascript/ui/panel/tabs/ZombieTabCommands.js b/extensions/admin_ui/media/javascript/ui/panel/tabs/ZombieTabCommands.js index 19fa6a96b..81a5c149a 100644 --- a/extensions/admin_ui/media/javascript/ui/panel/tabs/ZombieTabCommands.js +++ b/extensions/admin_ui/media/javascript/ui/panel/tabs/ZombieTabCommands.js @@ -104,16 +104,14 @@ ZombieTab_Commands = function(zombie) { specialkey : function(field,e){ if(e.getKey() == e.ENTER){ if( field.getValue() ){ - console.log(originalRoot); var root = { text: "Search results", children: search_module(originalRoot, field.getValue()) }; - console.log(originalRoot); - console.log(root); command_module_tree.setRootNode(root); - }else { command_module_tree.setRootNode(originalRoot); - } + } else + command_module_tree.setRootNode(originalRoot); + } } } @@ -126,14 +124,14 @@ ZombieTab_Commands = function(zombie) { minSize: 190, maxSize: 500, region: 'north' -}); + }); -var command_module_tree = new Ext.tree.TreePanel({ - id: "zombie-command-modules"+zombie.session, - region: 'center', + var command_module_tree = new Ext.tree.TreePanel({ + id: "zombie-command-modules"+zombie.session, + region: 'center', width: 190, minSize: 190, - maxSize: 500, // if some command module names are even longer, adjust this value + maxSize: 500, useArrows: true, autoScroll: true, animate: true, @@ -182,18 +180,18 @@ var command_module_tree = new Ext.tree.TreePanel({ } }); -var command_module_tree_container = new Ext.Panel({ + var command_module_tree_container = new Ext.Panel({ id: "zombie-command-modules-container"+zombie.session, title: "Module Tree", border: true, width: 190, minSize: 190, - maxSize: 500, + maxSize: 500, // if some command module names are even longer, adjust this value layout: 'border', region: 'west', split: true, items: [ command_module_tree_search_panel,command_module_tree ], -}); + }); var commands_statusbar = new Beef_StatusBar(zombie.session);