bd449a0b70164c9d6d07415556f014d199bb6327
[ccsdk/features.git] /
1 (function( $, app, i18n ) {
2
3         var ui = app.ns("ui");
4         var ut = app.ns("ut");
5
6         ui.QueryFilter = ui.AbstractWidget.extend({
7                 defaults: {
8                         metadata: null,   // (required) instanceof app.data.MetaData
9                         query: null       // (required) instanceof app.data.Query that the filters will act apon
10                 },
11                 init: function() {
12                         this._super();
13                         this.metadata = this.config.metadata;
14                         this.query = this.config.query;
15                         this.el = $(this._main_template());
16                 },
17                 helpTypeMap: {
18                         "date" : "QueryFilter.DateRangeHelp"
19                 },
20                 requestUpdate: function(jEv) {
21                         if(jEv && jEv.originalEvent) { // we only want to update on real user interaction not generated events
22                                 this.query.setPage(1);
23                                 this.query.query();
24                         }
25                 },
26                 getSpec: function(fieldName) {
27                         return this.metadata.fields[fieldName];
28                 },
29                 _selectAlias_handler: function(jEv) {
30                         var indices = (jEv.target.selectedIndex === 0) ? [] : this.metadata.getIndices($(jEv.target).val());
31                         $(".uiQueryFilter-index").each(function(i, el) {
32                                 var jEl = $(el);
33                                 if(indices.contains(jEl.text()) !== jEl.hasClass("selected")) {
34                                         jEl.click();
35                                 }
36                         });
37                         this.requestUpdate(jEv);
38                 },
39                 _selectIndex_handler: function(jEv) {
40                         var jEl = $(jEv.target).closest(".uiQueryFilter-index");
41                         jEl.toggleClass("selected");
42                         var selected = jEl.hasClass("selected");
43                         this.query.setIndex(jEl.text(), selected);
44                         if(selected) {
45                                 var types = this.metadata.getTypes(this.query.indices);
46                                 this.el.find("DIV.uiQueryFilter-type.selected").each(function(n, el) {
47                                         if(! types.contains($(el).text())) {
48                                                 $(el).click();
49                                         }
50                                 });
51                         }
52                         this.requestUpdate(jEv);
53                 },
54                 _selectType_handler: function(jEv) {
55                         var jEl = $(jEv.target).closest(".uiQueryFilter-type");
56                         jEl.toggleClass("selected");
57                         var type = jEl.text(), selected = jEl.hasClass("selected");
58                         this.query.setType(type, selected);
59                         if(selected) {
60                                 var indices = this.metadata.types[type].indices;
61                                 // es throws a 500 if searching an index for a type it does not contain - so we prevent that
62                                 this.el.find("DIV.uiQueryFilter-index.selected").each(function(n, el) {
63                                         if(! indices.contains($(el).text())) {
64                                                 $(el).click();
65                                         }
66                                 });
67                                 // es throws a 500 if you specify types from different indices with _all
68                                 jEl.siblings(".uiQueryFilter-type.selected").forEach(function(el) {
69                                         if(this.metadata.types[$(el).text()].indices.intersection(indices).length === 0) {
70                                                 $(el).click();
71                                         }
72                                 }, this);
73                         }
74                         this.requestUpdate(jEv);
75                 },
76                 _openFilter_handler: function(section) {
77                         var field_name = section.config.title;
78                         if(! section.loaded) {
79                                 var spec = this.getSpec(field_name);
80                                 if(spec.core_type === "string") {
81                                         section.body.append(this._textFilter_template(spec));
82                                 } else if(spec.core_type === "date") {
83                                         section.body.append(this._dateFilter_template(spec));
84                                         section.body.append(new ui.DateHistogram({ printEl: section.body.find("INPUT"), cluster: this.cluster, query: this.query, spec: spec }));
85                                 } else if(spec.core_type === "number") {
86                                         section.body.append(this._numericFilter_template(spec));
87                                 } else if(spec.core_type === 'boolean') {
88                                         section.body.append(this._booleanFilter_template(spec));
89                                 } else if (spec.core_type === 'multi_field') {
90                                         section.body.append(this._multiFieldFilter_template(section, spec));
91                                 } 
92                                 section.loaded = true;
93                         }
94                         section.on("animComplete", function(section) { section.body.find("INPUT").focus(); });
95                 },
96                 _textFilterChange_handler: function(jEv) {
97                         var jEl = $(jEv.target).closest("INPUT");
98                         var val = jEl.val();
99                         var spec = jEl.data("spec");
100                         var uqids = jEl.data("uqids") || [];
101                         uqids.forEach(function(uqid) {
102                                 uqid && this.query.removeClause(uqid);
103                         }, this);
104                         if(val.length) {
105                                 if(jEl[0] === document.activeElement && jEl[0].selectionStart === jEl[0].selectionEnd) {
106                                         val = val.replace(new RegExp("(.{"+jEl[0].selectionStart+"})"), "$&*");
107                                 }
108                                 uqids = val.split(/\s+/).map(function(term) {
109                                         // Figure out the actual field name - needed for multi_field, because
110                                         // querying for "field.field" will not work. Simply "field" must be used
111                                         // if nothing is aliased.
112                                         var fieldNameParts = spec.field_name.split('.');
113                                         var part = fieldNameParts.length - 1;
114                                         var name = fieldNameParts[part];
115                                         while (part >= 1) {
116                                                 if (fieldNameParts[part] !== fieldNameParts[part - 1]) {
117                                                         name = fieldNameParts[part - 1] + "." + name;
118                                                 }
119                                                 part--;
120                                         }
121                                         return term && this.query.addClause(term, name, "wildcard", "must");
122                                 }, this);
123                         }
124                         jEl.data("uqids", uqids);
125                         this.requestUpdate(jEv);
126                 },
127                 _dateFilterChange_handler: function(jEv) {
128                         var jEl = $(jEv.target).closest("INPUT");
129                         var val = jEl.val();
130                         var spec = jEl.data("spec");
131                         var uqid = jEl.data("uqid") || null;
132                         var range = window.dateRangeParser.parse(val);
133                         var lastRange = jEl.data("lastRange");
134                         if(!range || (lastRange && lastRange.start === range.start && lastRange.end === range.end)) {
135                                 return;
136                         }
137                         uqid && this.query.removeClause(uqid);
138                         if((range.start && range.end) === null) {
139                                 uqid = null;
140                         } else {
141                                 var value = {};
142                                 if( range.start ) {
143                                         value["gte"] = range.start;
144                                 }
145                                 if( range.end ) {
146                                         value["lte"] = range.end;
147                                 }
148                                 uqid = this.query.addClause( value, spec.field_name, "range", "must");
149                         }
150                         jEl.data("lastRange", range);
151                         jEl.siblings(".uiQueryFilter-rangeHintFrom")
152                                 .text(i18n.text("QueryFilter.DateRangeHint.from", range.start && new Date(range.start).toUTCString()));
153                         jEl.siblings(".uiQueryFilter-rangeHintTo")
154                                 .text(i18n.text("QueryFilter.DateRangeHint.to", range.end && new Date(range.end).toUTCString()));
155                         jEl.data("uqid", uqid);
156                         this.requestUpdate(jEv);
157                 },
158                 _numericFilterChange_handler: function(jEv) {
159                         var jEl = $(jEv.target).closest("INPUT");
160                         var val = jEl.val();
161                         var spec = jEl.data("spec");
162                         var uqid = jEl.data("uqid") || null;
163                         var lastRange = jEl.data("lastRange");
164                         var range = (function(val) {
165                                 var ops = val.split(/->|<>|</).map( function(v) { return parseInt(v.trim(), 10); });
166                                 if(/<>/.test(val)) {
167                                         return { gte: (ops[0] - ops[1]), lte: (ops[0] + ops[1]) };
168                                 } else if(/->|</.test(val)) {
169                                         return { gte: ops[0], lte: ops[1] };
170                                 } else {
171                                         return { gte: ops[0], lte: ops[0] };
172                                 }
173                         })(val || "");
174                         if(!range || (lastRange && lastRange.lte === range.lte && lastRange.gte === range.gte)) {
175                                 return;
176                         }
177                         jEl.data("lastRange", range);
178                         uqid && this.query.removeClause(uqid);
179                         uqid = this.query.addClause( range, spec.field_name, "range", "must");
180                         jEl.data("uqid", uqid);
181                         this.requestUpdate(jEv);
182                 },
183                 _booleanFilterChange_handler: function( jEv ) {
184                         var jEl = $(jEv.target).closest("SELECT");
185                         var val = jEl.val();
186                         var spec = jEl.data("spec");
187                         var uqid = jEl.data("uqid") || null;
188                         uqid && this.query.removeClause(uqid);
189                         if(val === "true" || val === "false") {
190                                 jEl.data("uqid", this.query.addClause(val, spec.field_name, "term", "must") );
191                         }
192                         this.requestUpdate(jEv);
193                 },
194                 _main_template: function() {
195                         return { tag: "DIV", id: this.id(), cls: "uiQueryFilter", children: [
196                                 this._aliasSelector_template(),
197                                 this._indexSelector_template(),
198                                 this._typesSelector_template(),
199                                 this._filters_template()
200                         ] };
201                 },
202                 _aliasSelector_template: function() {
203                         var aliases = Object.keys(this.metadata.aliases).sort();
204                         aliases.unshift( i18n.text("QueryFilter.AllIndices") );
205                         return { tag: "DIV", cls: "uiQueryFilter-section uiQueryFilter-aliases", children: [
206                                 { tag: "SELECT", onChange: this._selectAlias_handler, children: aliases.map(ut.option_template) }
207                         ] };
208                 },
209                 _indexSelector_template: function() {
210                         var indices = Object.keys( this.metadata.indices ).sort();
211                         return { tag: "DIV", cls: "uiQueryFilter-section uiQueryFilter-indices", children: [
212                                 { tag: "HEADER", text: i18n.text("QueryFilter-Header-Indices") },
213                                 { tag: "DIV", onClick: this._selectIndex_handler, children: indices.map( function( name ) {
214                                         return { tag: "DIV", cls: "uiQueryFilter-booble uiQueryFilter-index", text: name };
215                                 })}
216                         ] };
217                 },
218                 _typesSelector_template: function() {
219                         var types = Object.keys( this.metadata.types ).sort();
220                         return { tag: "DIV", cls: "uiQueryFilter-section uiQueryFilter-types", children: [
221                                 { tag: "HEADER", text: i18n.text("QueryFilter-Header-Types") },
222                                 { tag: "DIV", onClick: this._selectType_handler, children: types.map( function( name ) {
223                                         return { tag: "DIV", cls: "uiQueryFilter-booble uiQueryFilter-type", text: name };
224                                 })}
225                         ] };
226                 },
227                 _filters_template: function() {
228                         var _metadataFields = this.metadata.fields;
229                         var fields = Object.keys( _metadataFields ).sort()
230                                 .filter(function(d) { return (_metadataFields[d].core_type !== undefined); });
231                         return { tag: "DIV", cls: "uiQueryFilter-section uiQueryFilter-filters", children: [
232                                 { tag: "HEADER", text: i18n.text("QueryFilter-Header-Fields") },
233                                 { tag: "DIV", children: fields.map( function(name ) {
234                                         return new app.ui.SidebarSection({
235                                                 title: name,
236                                                 help: this.helpTypeMap[this.metadata.fields[ name ].type],
237                                                 onShow: this._openFilter_handler
238                                         });
239                                 }, this ) }
240                         ] };
241                 },
242                 _textFilter_template: function(spec) {
243                         return { tag: "INPUT", data: { spec: spec }, onKeyup: this._textFilterChange_handler };
244                 },
245                 _dateFilter_template: function(spec) {
246                         return { tag: "DIV", children: [
247                                 { tag: "INPUT", data: { spec: spec }, onKeyup: this._dateFilterChange_handler },
248                                 { tag: "PRE", cls: "uiQueryFilter-rangeHintFrom", text: i18n.text("QueryFilter.DateRangeHint.from", "")},
249                                 { tag: "PRE", cls: "uiQueryFilter-rangeHintTo", text: i18n.text("QueryFilter.DateRangeHint.to", "") }
250                         ]};
251                 },
252                 _numericFilter_template: function(spec) {
253                         return { tag: "INPUT", data: { spec: spec }, onKeyup: this._numericFilterChange_handler };
254                 },
255                 _booleanFilter_template: function(spec) {
256                         return { tag: "SELECT", data: { spec: spec }, onChange: this._booleanFilterChange_handler,
257                                 children: [ i18n.text("QueryFilter.AnyValue"), "true", "false" ].map( function( val ) {
258                                         return { tag: "OPTION", value: val, text: val };
259                                 })
260                         };
261                 },
262                 _multiFieldFilter_template: function(section, spec) {
263                         return {
264                                 tag : "DIV", cls : "uiQueryFilter-subMultiFields", children : acx.eachMap(spec.fields, function(name, data) {
265                                         if (name === spec.field_name) {
266                                                 section.config.title = spec.field_name + "." + name;
267                                                 return this._openFilter_handler(section);
268                                         }
269                                         return new app.ui.SidebarSection({
270                                                 title : data.field_name, help : this.helpTypeMap[data.type], onShow : this._openFilter_handler
271                                         });
272                                 }, this)
273                         };
274                 }       
275         });
276
277 })( this.jQuery, this.app, this.i18n );