1 (function( $, app, i18n ) {
6 ui.QueryFilter = ui.AbstractWidget.extend({
8 metadata: null, // (required) instanceof app.data.MetaData
9 query: null // (required) instanceof app.data.Query that the filters will act apon
13 this.metadata = this.config.metadata;
14 this.query = this.config.query;
15 this.el = $(this._main_template());
18 "date" : "QueryFilter.DateRangeHelp"
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);
26 getSpec: function(fieldName) {
27 return this.metadata.fields[fieldName];
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) {
33 if(indices.contains(jEl.text()) !== jEl.hasClass("selected")) {
37 this.requestUpdate(jEv);
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);
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())) {
52 this.requestUpdate(jEv);
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);
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())) {
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) {
74 this.requestUpdate(jEv);
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));
92 section.loaded = true;
94 section.on("animComplete", function(section) { section.body.find("INPUT").focus(); });
96 _textFilterChange_handler: function(jEv) {
97 var jEl = $(jEv.target).closest("INPUT");
99 var spec = jEl.data("spec");
100 var uqids = jEl.data("uqids") || [];
101 uqids.forEach(function(uqid) {
102 uqid && this.query.removeClause(uqid);
105 if(jEl[0] === document.activeElement && jEl[0].selectionStart === jEl[0].selectionEnd) {
106 val = val.replace(new RegExp("(.{"+jEl[0].selectionStart+"})"), "$&*");
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];
116 if (fieldNameParts[part] !== fieldNameParts[part - 1]) {
117 name = fieldNameParts[part - 1] + "." + name;
121 return term && this.query.addClause(term, name, "wildcard", "must");
124 jEl.data("uqids", uqids);
125 this.requestUpdate(jEv);
127 _dateFilterChange_handler: function(jEv) {
128 var jEl = $(jEv.target).closest("INPUT");
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)) {
137 uqid && this.query.removeClause(uqid);
138 if((range.start && range.end) === null) {
143 value["gte"] = range.start;
146 value["lte"] = range.end;
148 uqid = this.query.addClause( value, spec.field_name, "range", "must");
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);
158 _numericFilterChange_handler: function(jEv) {
159 var jEl = $(jEv.target).closest("INPUT");
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); });
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] };
171 return { gte: ops[0], lte: ops[0] };
174 if(!range || (lastRange && lastRange.lte === range.lte && lastRange.gte === range.gte)) {
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);
183 _booleanFilterChange_handler: function( jEv ) {
184 var jEl = $(jEv.target).closest("SELECT");
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") );
192 this.requestUpdate(jEv);
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()
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) }
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 };
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 };
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({
236 help: this.helpTypeMap[this.metadata.fields[ name ].type],
237 onShow: this._openFilter_handler
242 _textFilter_template: function(spec) {
243 return { tag: "INPUT", data: { spec: spec }, onKeyup: this._textFilterChange_handler };
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", "") }
252 _numericFilter_template: function(spec) {
253 return { tag: "INPUT", data: { spec: spec }, onKeyup: this._numericFilterChange_handler };
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 };
262 _multiFieldFilter_template: function(section, spec) {
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);
269 return new app.ui.SidebarSection({
270 title : data.field_name, help : this.helpTypeMap[data.type], onShow : this._openFilter_handler
277 })( this.jQuery, this.app, this.i18n );