0010954: Search for CustomField type "keyField" does not work
[tine20] / tine20 / Tinebase / js / widgets / customfields / FilterModel.js
1 /*
2  * Tine 2.0
3  * 
4  * @license     http://www.gnu.org/licenses/agpl.html AGPL Version 3
5  * @author      Cornelius Weiss <c.weiss@metaways.de>
6  * @copyright   Copyright (c) 2007-2015 Metaways Infosystems GmbH (http://www.metaways.de)
7  */
8
9 Ext.ns('Tine.widgets', 'Tine.widgets.customfields');
10
11 /**
12  * @namespace   Tine.widgets.customfields
13  * @class       Tine.widgets.customfields.FilterModel
14  * @extends     Tine.widgets.grid.FilterModel
15  */
16 Tine.widgets.customfields.FilterModel = Ext.extend(Tine.widgets.grid.FilterModel, {
17     /**
18      * @cfg {Tine.Tinebase.Application} app
19      */
20     app: null,
21     
22     /**
23      * @cfg {Record} cfConfig
24      */
25     cfConfig: null,
26     
27     valueType: 'customfield',
28     
29     /**
30      * @private
31      */
32     initComponent: function() {
33         this.field = 'customfield:' + this.cfConfig.id;
34         this.cfDefinition = this.cfConfig.get('definition');
35         this.label =  this.cfDefinition.label;
36         
37         switch (this.cfDefinition.type) {
38             case 'record':
39             case 'keyField':
40                 this.operators = ['equals', 'not' /* , 'in', 'notin' */];
41                 this.defaultOperator = 'equals';
42                 this.valueRenderer = this.cfValueRenderer;
43                 break;
44             case 'integer':
45                 this.operators = ['equals', 'greater', 'less'];
46                 this.defaultOperator = 'equals';
47                 break;
48             case 'bool':
49             case 'boolean':
50                 this.valueType = 'bool';
51                 this.defaultOperator = 'equals';
52                 this.defaultValue = '0';
53                 break;
54             case 'date':
55             case 'datetime':
56                 this.valueType = 'date';
57                 this.defaultOperator = 'within';
58         }
59         
60         Tine.widgets.customfields.FilterModel.superclass.initComponent.call(this);
61     },
62     
63     /**
64      * returns valueType of given filter
65      * 
66      * added for cf of type record and keyField
67      * 
68      * @param {Record} filter
69      * @return {String}
70      */
71     getValueType: function (filter) {
72         var operator  = filter.get('operator') ? filter.get('operator') : this.defaultOperator,
73             valueType = 'selectionComboBox';
74
75         return valueType;
76     },
77     
78     /**
79      * called on operator change of a filter row
80      * 
81      * modified for cf of type record and keyField
82      * 
83      * @private
84      */
85     onOperatorChange: function (filter, newOperator) {
86         this.supr().onOperatorChange.call(this, filter, newOperator);
87         
88         if (['record', 'keyField'].indexOf(this.cfDefinition.type) !== -1) {
89             var valueType = this.getValueType(filter);
90             
91             for (var valueField in filter.valueFields) {
92                 if (filter.valueFields.hasOwnProperty(valueField)) {
93                     filter.valueFields[valueField][valueField === valueType ? 'show' : 'hide']();
94                 }
95             }
96             
97             filter.formFields.value = filter.valueFields[valueType];
98             if (valueType === 'selectionComboBox' && Ext.isArray(filter.formFields.value.value)) {
99                 filter.formFields.value.setValue(filter.formFields.value.value[0]);
100             }
101         }
102     },
103         
104     /**
105      * cf value renderer
106      * 
107      * @param {Ext.data.Record} filter line
108      * @param {Ext.Element} element to render to 
109      */
110     cfValueRenderer: function(filter, el) {        
111         var valueType   = this.getValueType(filter),
112             fieldWidth  = 200;
113                     
114         filter.valueFields = {};
115         
116         filter.valueFields.selectionComboBox = Tine.widgets.customfields.Field.get(this.app, this.cfConfig, {
117             hidden: valueType !== 'selectionComboBox',
118             width: fieldWidth,
119             minListWidth: 350,
120             value: filter.data.value ? filter.data.value : this.defaultValue,
121             resizable: true,
122             filter: filter,
123             id: 'tw-ftb-frow-valuefield-' + filter.id,
124             renderTo: el,
125             listeners: {
126                 scope: this,
127                 'specialkey': function (field, e) {
128                     if (e.getKey() === e.ENTER) {
129                         this.onFiltertrigger();
130                     }
131                 },
132                 'select': this.onFiltertrigger
133             }
134         });
135
136         return filter.valueFields[valueType];
137     }
138 });
139 Tine.widgets.grid.FilterToolbar.FILTERS['tinebase.customfield'] = Tine.widgets.customfields.FilterModel;
140