Debian Codegraph @

dojox/grid/enhanced/plugins/Search.js

raw symbols SCIP
1 define([
2 "dojo/_base/kernel",
3 "dojo/_base/lang",
4 "dojo/_base/declare",
5 "dojo/_base/array",
6 "dojo/data/util/filter",
7 "../../EnhancedGrid",
8 "../_Plugin"
9 ], function(dojo, lang, declare, array, dFilter, EnhancedGrid, _Plugin){
10
11 var Search = declare("dojox.grid.enhanced.plugins.Search", _Plugin, {
12 // summary:
13 // Search the grid using wildcard string or Regular Expression.
14
15 // name: String
16 // plugin name
17 name: "search",
18
19 constructor: function(grid, args){
20 this.grid = grid;
21 args = (args && lang.isObject(args)) ? args : {};
22 this._cacheSize = args.cacheSize || -1;
23 grid.searchRow = lang.hitch(this, "searchRow");
24 },
25 searchRow: function(/* Object|RegExp|String */searchArgs, /* function(Integer, item) */onSearched){
26 if(!lang.isFunction(onSearched)){ return; }
27 if(lang.isString(searchArgs)){
28 searchArgs = dFilter.patternToRegExp(searchArgs);
29 }
30 var isGlobal = false;
31 if(searchArgs instanceof RegExp){
32 isGlobal = true;
33 }else if(lang.isObject(searchArgs)){
34 var isEmpty = true;
35 for(var field in searchArgs){
36 if(lang.isString(searchArgs[field])){
37 searchArgs[field] = dFilter.patternToRegExp(searchArgs[field]);
38 }
39 isEmpty = false;
40 }
41 if(isEmpty){ return; }
42 }else{
43 return;
44 }
45 this._search(searchArgs, 0, onSearched, isGlobal);
46 },
47 _search: function(/* Object|RegExp */searchArgs, /* Integer */start, /* function(Integer, item) */onSearched, /* Boolean */isGlobal){
48 var _this = this,
49 cnt = this._cacheSize,
50 args = {
51 start: start,
52 query: this.grid.query,
53 sort: this.grid.getSortProps(),
54 queryOptions: this.grid.queryOptions,
55 onBegin: function(size){
56 _this._storeSize = size;
57 },
58 onComplete: function(items){
59 if(!array.some(items, function(item, i){
60 if(_this._checkRow(item, searchArgs, isGlobal)){
61 onSearched(start + i, item);
62 return true;
63 }
64 return false;
65 })){
66 if(cnt > 0 && start + cnt < _this._storeSize){
67 _this._search(searchArgs, start + cnt, onSearched, isGlobal);
68 }else{
69 onSearched(-1, null);
70 }
71 }
72 }
73 };
74 if(cnt > 0){
75 args.count = cnt;
76 }
77 this.grid._storeLayerFetch(args);
78 },
79 _checkRow: function(/* store item */item, /* Object|RegExp */searchArgs, /* Boolean */isGlobal){
80 var g = this.grid, s = g.store, i, field,
81 cells = array.filter(g.layout.cells, function(cell){
82 return !cell.hidden;
83 });
84 if(isGlobal){
85 return array.some(cells, function(cell){
86 try{
87 if(cell.field){
88 return String(s.getValue(item, cell.field)).search(searchArgs) >= 0;
89 }
90 }catch(e){
91 console.log("Search._checkRow() error: ", e);
92 }
93 return false;
94 });
95 }else{
96 for(field in searchArgs){
97 if(searchArgs[field] instanceof RegExp){
98 for(i = cells.length - 1; i >= 0; --i){
99 if(cells[i].field == field){
100 try{
101 if(String(s.getValue(item, field)).search(searchArgs[field]) < 0){
102 return false;
103 }
104 break;
105 }catch(e){
106 return false;
107 }
108 }
109 }
110 if(i < 0){ return false; }
111 }
112 }
113 return true;
114 }
115 }
116 });
117
118 EnhancedGrid.registerPlugin(Search/*name:'search'*/);
119
120 return Search;
121
122 });