source: Dev/trunk/src/client/dojox/grid/enhanced/plugins/Filter.js @ 483

Last change on this file since 483 was 483, checked in by hendrikvanantwerpen, 10 years ago

Added Dojo 1.9.3 release.

File size: 6.8 KB
Line 
1define([
2        "dojo/_base/declare",
3        "dojo/_base/lang",
4        "../_Plugin",
5        "./Dialog",
6        "./filter/FilterLayer",
7        "./filter/FilterBar",
8        "./filter/FilterDefDialog",
9        "./filter/FilterStatusTip",
10        "./filter/ClearFilterConfirm",
11        "../../EnhancedGrid",
12        "dojo/i18n!../nls/Filter"
13], function(declare, lang, _Plugin, Dialog, layers, FilterBar, FilterDefDialog, FilterStatusTip, ClearFilterConfirm, EnhancedGrid, nls){
14
15        var Filter = declare("dojox.grid.enhanced.plugins.Filter", _Plugin, {
16                // summary:
17                //              Provide filter functionality for grid.
18                //
19                //              Acceptable plugin parameters:
20                //
21                //              1. itemsName: string:
22                //                      the name shown on the filter bar.
23                //              2. statusTipTimeout: number:
24                //                      when does the status tip show.
25                //              3. ruleCount: number:
26                //                      default to 3, should not change to more. The Claro theme limits it.
27                //              4. disabledConditions: object:
28                //                      If you don't need all of the conditions provided for a data type,
29                //                      you can explicitly declare them here:
30                //                      e.g.: disabledConditions: {string: ["contains", "is"], number: ["equalto"], ...}
31                //              5. isServerSide: boolean:
32                //                      Whether to use server side filtering. Default to false.
33                //              6. isStateful: boolean:
34                //                      If isServerSide is true, set the server side filter to be stateful or not. default to false.
35                //              7. url: string:
36                //                      If using stateful, this is the url to send commands. default to store.url.
37                //              8. ruleCountToConfirmClearFilter: Integer | null |Infinity:
38                //                      If the filter rule count is larger than or equal to this value, then a confirm dialog will show when clearing filter.
39                //                      If set to less than 1 or null, then always show the confirm dialog.
40                //                      If set to Infinity, then never show the confirm dialog.
41                //                      Default value is 2.
42                //
43                //              Acceptable cell parameters defined in layout:
44                //
45                //              1. filterable: boolean:
46                //                      The column is not filterable only when this is set to false explicitly.
47                //              2. datatype: string:
48                //                      The data type of this column. Can be "string", "number", "date", "time", "boolean".
49                //                      Default to "string".
50                //              3. autoComplete: boolean:
51                //                      If need auto-complete in the ComboBox for String type, set this to true.
52                //              4. dataTypeArgs: object:
53                //                      Some arguments helping convert store data to something the filter UI understands.
54                //                      Different data type arguments can be provided to different data types.
55                //                      For date/time, this is a dojo.date.locale.__FormatOptions, so the DataTimeBox can understand the store data.
56                //                      For boolean, this object contains:
57                //
58                //                      - trueLabel: string:
59                //                              A label to display in the filter definition dialog for true value. Default to "True".
60                //                      - falseLabel: string:
61                //                              A label to display in the filter definition dialog for false value. Default to "False".
62                //
63                //              5. disabledConditions: object:
64                //                      If you don't need all of the conditions provided by the filter UI on this column, you can explicitly say it out here.
65                //                      e.g.: disabledConditions: ["contains", "is"]
66                //                      This will disable the "contains" condition for this column, if this column is of string type.
67                //                      For full set of conditions, please refer to dojox.grid.enhanced.plugins.filter.FilterDefDialog._setupData.
68                // example:
69                //      |       <div dojoType="dojox.grid.EnhancedGrid" plugins="{GridFilter: true}" ...></div>
70                //      |       or provide some parameters:
71                //      |       <div dojoType="dojox.grid.EnhancedGrid" plugins="{GridFilter: {itemsName: 'songs'}}" ...></div>
72                //      |       Customize columns for filter:
73                //      |       var layout = [
74                //      |               ...
75                //      |               //define a column to be un-filterable in layout/structure
76                //      |               {field: "Genre", filterable: false, ...}
77                //      |               //define a column of type string and supports autoComplete when you type in filter conditions.
78                //      |               {field: "Writer", datatype: "string", autoCommplete: true, ...}
79                //      |               //define a column of type date and the data in store has format: "yyyy/M/d"
80                //      |               {field: "Publish Date", datatype: "date", dataTypeArgs: {datePattern: "yyyy/M/d"}, ...}
81                //      |               //disable some conditions for a column
82                //      |               {field: "Track", disabledConditions: ["equalto","notequalto"], ...}
83                //      |               ...
84                //      |       ];
85               
86                // name: String
87                //              plugin name
88                name: "filter",
89               
90                constructor: function(grid, args){
91                        // summary:
92                        //              See constructor of dojox.grid.enhanced._Plugin.
93                        this.grid = grid;
94                        this.nls = nls;
95                       
96                        args = this.args = lang.isObject(args) ? args : {};
97                        if(typeof args.ruleCount != 'number' || args.ruleCount < 0){
98                                args.ruleCount = 3;
99                        }
100                        var rc = this.ruleCountToConfirmClearFilter = args.ruleCountToConfirmClearFilter;
101                        if(rc === undefined){
102                                this.ruleCountToConfirmClearFilter = 2;
103                        }
104                       
105                        //Install filter layer
106                        this._wrapStore();
107                       
108                        //Install UI components
109                        var obj = { "plugin": this };
110                        this.clearFilterDialog = new Dialog({
111                                refNode: this.grid.domNode,
112                                title: this.nls["clearFilterDialogTitle"],
113                                content: new ClearFilterConfirm(obj)
114                        });
115                        this.filterDefDialog = new FilterDefDialog(obj);
116                        this.filterBar = new FilterBar(obj);
117                        this.filterStatusTip = new FilterStatusTip(obj);
118                       
119                        //Expose the layer event to grid.
120                        grid.onFilterDefined = function(){};
121                        this.connect(grid.layer("filter"), "onFilterDefined", function(filter){
122                                grid.onFilterDefined(grid.getFilter(), grid.getFilterRelation());
123                        });
124                },
125                destroy: function(){
126                        this.inherited(arguments);
127                        try{
128                                this.grid.unwrap("filter");
129                                this.filterBar.destroyRecursive();
130                                this.filterBar = null;
131                                this.clearFilterDialog.destroyRecursive();
132                                this.clearFilterDialog = null;
133                                this.filterStatusTip.destroy();
134                                this.filterStatusTip = null;
135                                this.filterDefDialog.destroy();
136                                this.filterDefDialog = null;
137                                this.grid = null;
138                                this.args = null;
139                        }catch(e){
140                                console.warn("Filter.destroy() error:",e);
141                        }
142                },
143                _wrapStore: function(){
144                        var g = this.grid;
145                        var args = this.args;
146                        var filterLayer = args.isServerSide ? new layers.ServerSideFilterLayer(args) :
147                                new layers.ClientSideFilterLayer({
148                                        cacheSize: args.filterCacheSize,
149                                        fetchAll: args.fetchAllOnFirstFilter,
150                                        getter: this._clientFilterGetter
151                                });
152                        layers.wrap(g, "_storeLayerFetch", filterLayer);
153                       
154                        this.connect(g, "_onDelete", lang.hitch(filterLayer, "invalidate"));
155                },
156                onSetStore: function(store){
157                        this.filterDefDialog.clearFilter(true);
158                },
159                _clientFilterGetter: function(/* data item */ datarow,/* cell */cell, /* int */rowIndex){
160                        // summary:
161                        //              Define the grid-specific way to get data from a row.
162                        //              Argument "cell" is provided by FilterDefDialog when defining filter expressions.
163                        //              Argument "rowIndex" is provided by FilterLayer when checking a row.
164                        //              FilterLayer also provides a forth argument: "store", which is grid.store,
165                        //              but we don't need it here.
166                        return cell.get(rowIndex, datarow);
167                }
168        });
169
170        EnhancedGrid.registerPlugin(Filter/*name:'filter'*/);
171
172        return Filter;
173
174});
Note: See TracBrowser for help on using the repository browser.