Commit 6bd6d207 authored by Nigel Kukard's avatar Nigel Kukard
Browse files

* Updated GridFilters

parent 13171ed2
/*
* Ext JS Library 2.1
* Copyright(c) 2006-2008, Ext JS, LLC.
* licensing@extjs.com
*
* http://extjs.com/license
*/
/**
* Ext.ux.grid.GridFilters v0.2.8
**/
Ext.grid.GridFilters = function(config){
Ext.namespace("Ext.ux.grid");
Ext.ux.grid.GridFilters = function(config){
this.filters = new Ext.util.MixedCollection();
this.filters.getKey = function(o) {return o ? o.dataIndex : null};
this.filters.getKey = function(o){return o ? o.dataIndex : null};
for(var i=0, len=config.filters.length; i<len; i++) {
for(var i=0, len=config.filters.length; i<len; i++)
this.addFilter(config.filters[i]);
}
this.deferredUpdate = new Ext.util.DelayedTask(this.reload, this);
delete config.filters;
Ext.apply(this, config);
};
Ext.extend(Ext.grid.GridFilters, Ext.util.Observable, {
Ext.extend(Ext.ux.grid.GridFilters, Ext.util.Observable, {
/**
* @cfg {Integer} updateBuffer
* Number of milisecond to defer store updates since the last filter change.
......@@ -57,61 +52,61 @@ Ext.extend(Ext.grid.GridFilters, Ext.util.Observable, {
*/
showMenu: true,
menuFilterText: 'Filters',
init: function(grid){
if(grid instanceof Ext.grid.GridPanel){
this.grid = grid;
this.store = this.grid.getStore();
if(this.local){
this.store.on('load', function(store) {
store.filterBy(this.getRecordFilter());
}, this);
} else {
this.store.on('beforeload', this.onBeforeLoad, this);
}
this.grid.filters = this;
this.grid.addEvents('filterupdate');
grid.on("render", this.onRender, this);
grid.on("beforestaterestore", this.applyState, this);
grid.on("beforestatesave", this.saveState, this);
} else if(grid instanceof Ext.PagingToolbar) {
this.toolbar = grid;
}
if(grid instanceof Ext.grid.GridPanel){
this.grid = grid;
this.store = this.grid.getStore();
if(this.local){
this.store.on('load', function(store){
store.filterBy(this.getRecordFilter());
}, this);
} else {
this.store.on('beforeload', this.onBeforeLoad, this);
}
this.grid.filters = this;
this.grid.addEvents({"filterupdate": true});
grid.on("render", this.onRender, this);
grid.on("beforestaterestore", this.applyState, this);
grid.on("beforestatesave", this.saveState, this);
} else if(grid instanceof Ext.PagingToolbar){
this.toolbar = grid;
}
},
/** private **/
applyState: function(grid, state) {
applyState: function(grid, state){
this.suspendStateStore = true;
this.clearFilters();
if(state.filters) {
for(var key in state.filters) {
if(state.filters)
for(var key in state.filters){
var filter = this.filters.get(key);
if(filter) {
if(filter){
filter.setValue(state.filters[key]);
filter.setActive(true);
}
}
}
this.deferredUpdate.cancel();
if(this.local) {
if(this.local)
this.reload();
}
this.suspendStateStore = false;
},
/** private **/
saveState: function(grid, state){
var filters = {};
this.filters.each(function(filter) {
if(filter.active) {
this.filters.each(function(filter){
if(filter.active)
filters[filter.dataIndex] = filter.getValue();
}
});
return state.filters = filters;
},
......@@ -120,12 +115,12 @@ Ext.extend(Ext.grid.GridFilters, Ext.util.Observable, {
onRender: function(){
var hmenu;
if(this.showMenu) {
if(this.showMenu){
hmenu = this.grid.getView().hmenu;
this.sep = hmenu.addSeparator();
this.menu = hmenu.add(new Ext.menu.CheckItem({
text: 'Filters',
text: this.menuFilterText,
menu: new Ext.menu.Menu()
}));
this.menu.on('checkchange', this.onCheckChange, this);
......@@ -139,9 +134,9 @@ Ext.extend(Ext.grid.GridFilters, Ext.util.Observable, {
},
/** private **/
onMenu: function(filterMenu) {
onMenu: function(filterMenu){
var filter = this.getMenuFilter();
if(filter) {
if(filter){
this.menu.menu = filter.menu;
this.menu.setChecked(filter.active, false);
}
......@@ -151,28 +146,24 @@ Ext.extend(Ext.grid.GridFilters, Ext.util.Observable, {
},
/** private **/
onCheckChange: function(item, value) {
onCheckChange: function(item, value){
this.getMenuFilter().setActive(value);
},
/** private **/
onBeforeCheck: function(check, value) {
onBeforeCheck: function(check, value){
return !value || this.getMenuFilter().isActivatable();
},
/** private **/
onStateChange: function(event, filter) {
if(event == "serialize") {
return;
}
onStateChange: function(event, filter){
if(event == "serialize") return;
if(filter == this.getMenuFilter()) {
if(filter == this.getMenuFilter())
this.menu.setChecked(filter.active, false);
}
if(this.autoReload || this.local) {
if(this.autoReload || this.local)
this.deferredUpdate.delay(this.updateBuffer);
}
var view = this.grid.getView();
this.updateColumnHeadings(view);
......@@ -183,7 +174,7 @@ Ext.extend(Ext.grid.GridFilters, Ext.util.Observable, {
},
/** private **/
onBeforeLoad: function(store, options) {
onBeforeLoad: function(store, options){
options.params = options.params || {};
this.cleanParams(options.params);
var params = this.buildQuery(this.getFilterData());
......@@ -191,48 +182,44 @@ Ext.extend(Ext.grid.GridFilters, Ext.util.Observable, {
},
/** private **/
onRefresh: function(view) {
onRefresh: function(view){
this.updateColumnHeadings(view);
},
/** private **/
getMenuFilter: function() {
getMenuFilter: function(){
var view = this.grid.getView();
if(!view || view.hdCtxIndex === undefined) {
if(!view || view.hdCtxIndex === undefined)
return null;
}
return this.filters.get(view.cm.config[view.hdCtxIndex].dataIndex);
return this.filters.get(
view.cm.config[view.hdCtxIndex].dataIndex);
},
/** private **/
updateColumnHeadings: function(view) {
if(!view || !view.mainHd) {
return;
}
updateColumnHeadings: function(view){
if(!view || !view.mainHd) return;
var hds = view.mainHd.select('td').removeClass(this.filterCls);
for(var i=0, len=view.cm.config.length; i<len; i++) {
for(var i=0, len=view.cm.config.length; i<len; i++){
var filter = this.getFilter(view.cm.config[i].dataIndex);
if(filter && filter.active) {
if(filter && filter.active)
hds.item(i).addClass(this.filterCls);
}
}
},
/** private **/
reload: function() {
reload: function(){
if(this.local){
this.grid.store.clearFilter(true);
this.grid.store.filterBy(this.getRecordFilter());
} else {
this.deferredUpdate.cancel();
var store = this.grid.store;
if(this.toolbar) {
if(this.toolbar){
var start = this.toolbar.paramNames.start;
if(store.lastOptions && store.lastOptions.params && store.lastOptions.params[start]) {
if(store.lastOptions && store.lastOptions.params && store.lastOptions.params[start])
store.lastOptions.params[start] = 0;
}
}
store.reload();
}
......@@ -244,21 +231,18 @@ Ext.extend(Ext.grid.GridFilters, Ext.util.Observable, {
*
* @private
*/
getRecordFilter: function() {
getRecordFilter: function(){
var f = [];
this.filters.each(function(filter) {
if(filter.active) {
f.push(filter);
}
this.filters.each(function(filter){
if(filter.active) f.push(filter);
});
var len = f.length;
return function(record) {
for(var i=0; i<len; i++) {
if(!f[i].validateRecord(record)) {
return function(record){
for(var i=0; i<len; i++)
if(!f[i].validateRecord(record))
return false;
}
}
return true;
};
},
......@@ -266,12 +250,13 @@ Ext.extend(Ext.grid.GridFilters, Ext.util.Observable, {
/**
* Adds a filter to the collection.
*
* @param {Object/Ext.grid.filter.Filter} config A filter configuration or a filter object.
* @param {Object/Ext.ux.grid.filter.Filter} config A filter configuration or a filter object.
*
* @return {Ext.grid.filter.Filter} The existing or newly created filter object.
* @return {Ext.ux.grid.filter.Filter} The existing or newly created filter object.
*/
addFilter: function(config) {
var filter = config.menu ? config : new (this.getFilterClass(config.type))(config);
addFilter: function(config){
var filter = config.menu ? config :
new (this.getFilterClass(config.type))(config);
this.filters.add(filter);
Ext.util.Observable.capture(filter, this.onStateChange, this);
......@@ -283,7 +268,7 @@ Ext.extend(Ext.grid.GridFilters, Ext.util.Observable, {
*
* @param {String} dataIndex The dataIndex of the desired filter object.
*
* @return {Ext.grid.filter.Filter}
* @return {Ext.ux.grid.filter.Filter}
*/
getFilter: function(dataIndex){
return this.filters.get(dataIndex);
......@@ -292,22 +277,25 @@ Ext.extend(Ext.grid.GridFilters, Ext.util.Observable, {
/**
* Turns all filters off. This does not clear the configuration information.
*/
clearFilters: function() {
this.filters.each(function(filter) {
clearFilters: function(){
this.filters.each(function(filter){
filter.setActive(false);
});
},
/** private **/
getFilterData: function() {
var filters = [];
getFilterData: function(){
var filters = [],
fields = this.grid.getStore().fields;
this.filters.each(function(f) {
if(f.active) {
this.filters.each(function(f){
if(f.active){
var d = [].concat(f.serialize());
for(var i=0, len=d.length; i<len; i++) {
filters.push({field: f.dataIndex, data: d[i]});
}
for(var i=0, len=d.length; i<len; i++)
filters.push({
field: f.dataIndex,
data: d[i]
});
}
});
......@@ -325,17 +313,16 @@ Ext.extend(Ext.grid.GridFilters, Ext.util.Observable, {
*
* @return {Object} Query keys and values
*/
buildQuery: function(filters) {
buildQuery: function(filters){
var p = {};
for(var i=0, len=filters.length; i<len; i++) {
var f = filters[i];
for(var i=0, len=filters.length; i<len; i++){
var f = filters[i];
var root = [this.paramPrefix, '[', i, ']'].join('');
p[root + '[field]'] = f.field;
var dataPrefix = root + '[data]';
for(var key in f.data) {
for(var key in f.data)
p[[dataPrefix, '[', key, ']'].join('')] = f.data[key];
}
}
return p;
......@@ -346,13 +333,11 @@ Ext.extend(Ext.grid.GridFilters, Ext.util.Observable, {
*
* @param {Object} p Query parameters that may contain filter related fields.
*/
cleanParams: function(p) {
cleanParams: function(p){
var regex = new RegExp("^" + this.paramPrefix + "\[[0-9]+\]");
for(var key in p) {
if(regex.test(key)) {
for(var key in p)
if(regex.test(key))
delete p[key];
}
}
},
/**
......@@ -364,6 +349,6 @@ Ext.extend(Ext.grid.GridFilters, Ext.util.Observable, {
* @return {Class}
*/
getFilterClass: function(type){
return Ext.grid.filter[type.substr(0, 1).toUpperCase() + type.substr(1) + 'Filter'];
return Ext.ux.grid.filter[type.substr(0, 1).toUpperCase() + type.substr(1) + 'Filter'];
}
});
});
\ No newline at end of file
/*
* Ext JS Library 2.1
* Copyright(c) 2006-2008, Ext JS, LLC.
* licensing@extjs.com
*
* http://extjs.com/license
*/
Ext.grid.filter.BooleanFilter = Ext.extend(Ext.grid.filter.Filter, {
defaultValue: false,
Ext.ux.grid.filter.BooleanFilter = Ext.extend(Ext.ux.grid.filter.Filter, {
defaultValue: false,
yesText: "Yes",
noText: "No",
init: function(){
var gId = Ext.id();
this.options = [
new Ext.menu.CheckItem({text: "Yes", group: gId, checked: this.defaultValue === true}),
new Ext.menu.CheckItem({text: "No", group: gId, checked: this.defaultValue === false})
];
new Ext.menu.CheckItem({text: this.yesText, group: gId, checked: this.defaultValue === true}),
new Ext.menu.CheckItem({text: this.noText, group: gId, checked: this.defaultValue === false})];
this.menu.add(this.options[0], this.options[1]);
for(var i=0; i<this.options.length; i++) {
for(var i=0; i<this.options.length; i++){
this.options[i].on('click', this.fireUpdate, this);
this.options[i].on('checkchange', this.fireUpdate, this);
}
},
isActivatable: function() {
isActivatable: function(){
return true;
},
fireUpdate: function() {
fireUpdate: function(){
this.fireEvent("update", this);
this.setActive(true);
},
setValue: function(value) {
setValue: function(value){
this.options[value ? 0 : 1].setChecked(true);
},
getValue: function() {
getValue: function(){
return this.options[0].checked;
},
serialize: function() {
serialize: function(){
var args = {type: 'boolean', value: this.getValue()};
this.fireEvent('serialize', args, this);
return args;
},
validateRecord: function(record) {
validateRecord: function(record){
return record.get(this.dataIndex) == this.getValue();
}
});
\ No newline at end of file
/*
* Ext JS Library 2.1
* Copyright(c) 2006-2008, Ext JS, LLC.
* licensing@extjs.com
*
* http://extjs.com/license
*/
Ext.grid.filter.DateFilter = Ext.extend(Ext.grid.filter.Filter, {
dateFormat: 'Y-m-d', // ISO 8601 standard date format
Ext.ux.grid.filter.DateFilter = Ext.extend(Ext.ux.grid.filter.Filter, {
dateFormat: 'Y-m-d',
pickerOpts: {},
init: function() {
beforeText: 'Before',
afterText: 'After',
onText: 'On',
init: function(){
var opts = Ext.apply(this.pickerOpts, {
minDate: this.minDate,
maxDate: this.maxDate,
format: this.dateFormat
});
var dates = this.dates = {
'after': new Ext.menu.CheckItem({text: "From", menu: new Ext.menu.DateMenu(opts)}),
'before': new Ext.menu.CheckItem({text: "To", menu: new Ext.menu.DateMenu(opts)}),
'on': new Ext.menu.CheckItem({text: "On", menu: new Ext.menu.DateMenu(opts)})
};
'before': new Ext.menu.CheckItem({text: this.beforeText, menu: new Ext.menu.DateMenu(opts)}),
'after': new Ext.menu.CheckItem({text: this.afterText, menu: new Ext.menu.DateMenu(opts)}),
'on': new Ext.menu.CheckItem({text: this.onText, menu: new Ext.menu.DateMenu(opts)})};
this.menu.add(dates.after, dates.before, "-", dates.on);
this.menu.add(dates.before, dates.after, "-", dates.on);
for(var key in dates) {
for(var key in dates){
var date = dates[key];
date.menu.on('select', this.onSelect.createDelegate(this, [date]), this);
date.menu.on('select', function(date, menuItem, value, picker){
date.setChecked(true);
if(date == dates.on){
dates.before.setChecked(false, true);
dates.after.setChecked(false, true);
} else {
dates.on.setChecked(false, true);
if(date == dates.after && dates.before.menu.picker.value < value)
dates.before.setChecked(false, true);
else if (date == dates.before && dates.after.menu.picker.value > value)
dates.after.setChecked(false, true);
}
this.fireEvent("update", this);
}.createDelegate(this, [date], 0));
date.on('checkchange', function(){
this.setActive(this.isActivatable());
this.setActive(this.isActivatable());
}, this);
};
},
onSelect: function(date, menuItem, value, picker) {
date.setChecked(true);
var dates = this.dates;
if(date == dates.on) {
dates.before.setChecked(false, true);
dates.after.setChecked(false, true);
} else {
dates.on.setChecked(false, true);
if(date == dates.after && dates.before.menu.picker.value < value) {
dates.before.setChecked(false, true);
} else if (date == dates.before && dates.after.menu.picker.value > value) {
dates.after.setChecked(false, true);
}
}
this.fireEvent("update", this);
},
getFieldValue: function(field) {
getFieldValue: function(field){
return this.dates[field].menu.picker.getValue();
},
getPicker: function(field) {
getPicker: function(field){
return this.dates[field].menu.picker;
},
isActivatable: function() {
isActivatable: function(){
return this.dates.on.checked || this.dates.after.checked || this.dates.before.checked;
},
setValue: function(value) {
for(var key in this.dates) {
if(value[key]) {
setValue: function(value){
for(var key in this.dates)
if(value[key]){
this.dates[key].menu.picker.setValue(value[key]);
this.dates[key].setChecked(true);
} else {
this.dates[key].setChecked(false);
}
}
},
getValue: function() {
getValue: function(){
var result = {};
for(var key in this.dates) {
if(this.dates[key].checked) {
for(var key in this.dates)
if(this.dates[key].checked)
result[key] = this.dates[key].menu.picker.getValue();
}
}
return result;
},
seria