[MERGE] Forward-port of latest 7.0 bugfixes, up to rev. 4179 revid:dle@openerp.com...
[odoo/odoo.git] / addons / web / static / src / js / search.js
1
2 (function() {
3
4 var instance = openerp;
5 openerp.web.search = {};
6
7 var QWeb = instance.web.qweb,
8       _t =  instance.web._t,
9      _lt = instance.web._lt;
10 _.mixin({
11     sum: function (obj) { return _.reduce(obj, function (a, b) { return a + b; }, 0); }
12 });
13
14 /** @namespace */
15 var my = instance.web.search = {};
16
17 var B = Backbone;
18 my.FacetValue = B.Model.extend({
19
20 });
21 my.FacetValues = B.Collection.extend({
22     model: my.FacetValue
23 });
24 my.Facet = B.Model.extend({
25     initialize: function (attrs) {
26         var values = attrs.values;
27         delete attrs.values;
28
29         B.Model.prototype.initialize.apply(this, arguments);
30
31         this.values = new my.FacetValues(values || []);
32         this.values.on('add remove change reset', function () {
33             this.trigger('change', this);
34         }, this);
35     },
36     get: function (key) {
37         if (key !== 'values') {
38             return B.Model.prototype.get.call(this, key);
39         }
40         return this.values.toJSON();
41     },
42     set: function (key, value) {
43         if (key !== 'values') {
44             return B.Model.prototype.set.call(this, key, value);
45         }
46         this.values.reset(value);
47     },
48     toJSON: function () {
49         var out = {};
50         var attrs = this.attributes;
51         for(var att in attrs) {
52             if (!attrs.hasOwnProperty(att) || att === 'field') {
53                 continue;
54             }
55             out[att] = attrs[att];
56         }
57         out.values = this.values.toJSON();
58         return out;
59     }
60 });
61 my.SearchQuery = B.Collection.extend({
62     model: my.Facet,
63     initialize: function () {
64         B.Collection.prototype.initialize.apply(
65             this, arguments);
66         this.on('change', function (facet) {
67             if(!facet.values.isEmpty()) { return; }
68
69             this.remove(facet, {silent: true});
70         }, this);
71     },
72     add: function (values, options) {
73         options = options || {};
74
75         if (!values) {
76             values = [];
77         } else if (!(values instanceof Array)) {
78             values = [values];
79         }
80
81         _(values).each(function (value) {
82             var model = this._prepareModel(value, options);
83             var previous = this.detect(function (facet) {
84                 return facet.get('category') === model.get('category')
85                     && facet.get('field') === model.get('field');
86             });
87             if (previous) {
88                 previous.values.add(model.get('values'), _.omit(options, 'at', 'merge'));
89                 return;
90             }
91             B.Collection.prototype.add.call(this, model, options);
92         }, this);
93         // warning: in backbone 1.0+ add is supposed to return the added models,
94         // but here toggle may delegate to add and return its value directly.
95         // return value of neither seems actually used but should be tested
96         // before change, probably
97         return this;
98     },
99     toggle: function (value, options) {
100         options = options || {};
101
102         var facet = this.detect(function (facet) {
103             return facet.get('category') === value.category
104                 && facet.get('field') === value.field;
105         });
106         if (!facet) {
107             return this.add(value, options);
108         }
109
110         var changed = false;
111         _(value.values).each(function (val) {
112             var already_value = facet.values.detect(function (v) {
113                 return v.get('value') === val.value
114                     && v.get('label') === val.label;
115             });
116             // toggle value
117             if (already_value) {
118                 facet.values.remove(already_value, {silent: true});
119             } else {
120                 facet.values.add(val, {silent: true});
121             }
122             changed = true;
123         });
124         // "Commit" changes to values array as a single call, so observers of
125         // change event don't get misled by intermediate incomplete toggling
126         // states
127         facet.trigger('change', facet);
128         return this;
129     }
130 });
131
132 function assert(condition, message) {
133     if(!condition) {
134         throw new Error(message);
135     }
136 }
137 my.InputView = instance.web.Widget.extend({
138     template: 'SearchView.InputView',
139     events: {
140         focus: function () { this.trigger('focused', this); },
141         blur: function () { this.$el.text(''); this.trigger('blurred', this); },
142         keydown: 'onKeydown',
143         paste: 'onPaste',
144     },
145     getSelection: function () {
146         this.el.normalize();
147         // get Text node
148         var root = this.el.childNodes[0];
149         if (!root || !root.textContent) {
150             // if input does not have a child node, or the child node is an
151             // empty string, then the selection can only be (0, 0)
152             return {start: 0, end: 0};
153         }
154         var range = window.getSelection().getRangeAt(0);
155         // In Firefox, depending on the way text is selected (drag, double- or
156         // triple-click) the range may start or end on the parent of the
157         // selected text node‽ Check for this condition and fixup the range
158         // note: apparently with C-a this can go even higher?
159         if (range.startContainer === this.el && range.startOffset === 0) {
160             range.setStart(root, 0);
161         }
162         if (range.endContainer === this.el && range.endOffset === 1) {
163             range.setEnd(root, root.length);
164         }
165         assert(range.startContainer === root,
166                "selection should be in the input view");
167         assert(range.endContainer === root,
168                "selection should be in the input view");
169         return {
170             start: range.startOffset,
171             end: range.endOffset
172         };
173     },
174     onKeydown: function (e) {
175         this.el.normalize();
176         var sel;
177         switch (e.which) {
178         // Do not insert newline, but let it bubble so searchview can use it
179         case $.ui.keyCode.ENTER:
180             e.preventDefault();
181             break;
182
183         // FIXME: may forget content if non-empty but caret at index 0, ok?
184         case $.ui.keyCode.BACKSPACE:
185             sel = this.getSelection();
186             if (sel.start === 0 && sel.start === sel.end) {
187                 e.preventDefault();
188                 var preceding = this.getParent().siblingSubview(this, -1);
189                 if (preceding && (preceding instanceof my.FacetView)) {
190                     preceding.model.destroy();
191                 }
192             }
193             break;
194
195         // let left/right events propagate to view if caret is at input border
196         // and not a selection
197         case $.ui.keyCode.LEFT:
198             sel = this.getSelection();
199             if (sel.start !== 0 || sel.start !== sel.end) {
200                 e.stopPropagation();
201             }
202             break;
203         case $.ui.keyCode.RIGHT:
204             sel = this.getSelection();
205             var len = this.$el.text().length;
206             if (sel.start !== len || sel.start !== sel.end) {
207                 e.stopPropagation();
208             }
209             break;
210         }
211     },
212     setCursorAtEnd: function () {
213         this.el.normalize();
214         var sel = window.getSelection();
215         sel.removeAllRanges();
216         var range = document.createRange();
217         // in theory, range.selectNodeContents should work here. In practice,
218         // MSIE9 has issues from time to time, instead of selecting the inner
219         // text node it would select the reference node instead (e.g. in demo
220         // data, company news, copy across the "Company News" link + the title,
221         // from about half the link to half the text, paste in search box then
222         // hit the left arrow key, getSelection would blow up).
223         //
224         // Explicitly selecting only the inner text node (only child node
225         // since we've normalized the parent) avoids the issue
226         range.selectNode(this.el.childNodes[0]);
227         range.collapse(false);
228         sel.addRange(range);
229     },
230     onPaste: function () {
231         this.el.normalize();
232         // In MSIE and Webkit, it is possible to get various representations of
233         // the clipboard data at this point e.g.
234         // window.clipboardData.getData('Text') and
235         // event.clipboardData.getData('text/plain') to ensure we have a plain
236         // text representation of the object (and probably ensure the object is
237         // pastable as well, so nobody puts an image in the search view)
238         // (nb: since it's not possible to alter the content of the clipboard
239         // — at least in Webkit — to ensure only textual content is available,
240         // using this would require 1. getting the text data; 2. manually
241         // inserting the text data into the content; and 3. cancelling the
242         // paste event)
243         //
244         // But Firefox doesn't support the clipboard API (as of FF18)
245         // although it correctly triggers the paste event (Opera does not even
246         // do that) => implement lowest-denominator system where onPaste
247         // triggers a followup "cleanup" pass after the data has been pasted
248         setTimeout(function () {
249             // Read text content (ignore pasted HTML)
250             var data = this.$el.text();
251             if (!data)
252                 return; 
253             // paste raw text back in
254             this.$el.empty().text(data);
255             this.el.normalize();
256             // Set the cursor at the end of the text, so the cursor is not lost
257             // in some kind of error-spawning limbo.
258             this.setCursorAtEnd();
259         }.bind(this), 0);
260     }
261 });
262 my.FacetView = instance.web.Widget.extend({
263     template: 'SearchView.FacetView',
264     events: {
265         'focus': function () { this.trigger('focused', this); },
266         'blur': function () { this.trigger('blurred', this); },
267         'click': function (e) {
268             if ($(e.target).is('.oe_facet_remove')) {
269                 this.model.destroy();
270                 return false;
271             }
272             this.$el.focus();
273             e.stopPropagation();
274         },
275         'keydown': function (e) {
276             var keys = $.ui.keyCode;
277             switch (e.which) {
278             case keys.BACKSPACE:
279             case keys.DELETE:
280                 this.model.destroy();
281                 return false;
282             }
283         }
284     },
285     init: function (parent, model) {
286         this._super(parent);
287         this.model = model;
288         this.model.on('change', this.model_changed, this);
289     },
290     destroy: function () {
291         this.model.off('change', this.model_changed, this);
292         this._super();
293     },
294     start: function () {
295         var self = this;
296         var $e = this.$('> span:last-child');
297         return $.when(this._super()).then(function () {
298             return $.when.apply(null, self.model.values.map(function (value) {
299                 return new my.FacetValueView(self, value).appendTo($e);
300             }));
301         });
302     },
303     model_changed: function () {
304         this.$el.text(this.$el.text() + '*');
305     }
306 });
307 my.FacetValueView = instance.web.Widget.extend({
308     template: 'SearchView.FacetView.Value',
309     init: function (parent, model) {
310         this._super(parent);
311         this.model = model;
312         this.model.on('change', this.model_changed, this);
313     },
314     destroy: function () {
315         this.model.off('change', this.model_changed, this);
316         this._super();
317     },
318     model_changed: function () {
319         this.$el.text(this.$el.text() + '*');
320     }
321 });
322
323 instance.web.SearchView = instance.web.Widget.extend(/** @lends instance.web.SearchView# */{
324     template: "SearchView",
325     events: {
326         // focus last input if view itself is clicked
327         'click': function (e) {
328             if (e.target === this.$('.oe_searchview_facets')[0]) {
329                 this.$('.oe_searchview_input:last').focus();
330             }
331         },
332         // search button
333         'click button.oe_searchview_search': function (e) {
334             e.stopImmediatePropagation();
335             this.do_search();
336         },
337         'click .oe_searchview_clear': function (e) {
338             e.stopImmediatePropagation();
339             this.query.reset();
340         },
341         'click .oe_searchview_unfold_drawer': function (e) {
342             e.stopImmediatePropagation();
343             this.$el.toggleClass('oe_searchview_open_drawer');
344         },
345         'keydown .oe_searchview_input, .oe_searchview_facet': function (e) {
346             switch(e.which) {
347             case $.ui.keyCode.LEFT:
348                 this.focusPreceding(this);
349                 e.preventDefault();
350                 break;
351             case $.ui.keyCode.RIGHT:
352                 this.focusFollowing(this);
353                 e.preventDefault();
354                 break;
355             }
356         },
357         'autocompleteopen': function () {
358             this.$el.autocomplete('widget').css('z-index', 9999);
359         },
360     },
361     /**
362      * @constructs instance.web.SearchView
363      * @extends instance.web.Widget
364      *
365      * @param parent
366      * @param dataset
367      * @param view_id
368      * @param defaults
369      * @param {Object} [options]
370      * @param {Boolean} [options.hidden=false] hide the search view
371      * @param {Boolean} [options.disable_custom_filters=false] do not load custom filters from ir.filters
372      */
373     init: function(parent, dataset, view_id, defaults, options) {
374         this.options = _.defaults(options || {}, {
375             hidden: false,
376             disable_custom_filters: false,
377         });
378         this._super(parent);
379         this.dataset = dataset;
380         this.model = dataset.model;
381         this.view_id = view_id;
382
383         this.defaults = defaults || {};
384         this.has_defaults = !_.isEmpty(this.defaults);
385
386         this.inputs = [];
387         this.controls = [];
388
389         this.headless = this.options.hidden && !this.has_defaults;
390
391         this.input_subviews = [];
392
393         this.ready = $.Deferred();
394     },
395     start: function() {
396         var self = this;
397         var p = this._super();
398
399         this.setup_global_completion();
400         this.query = new my.SearchQuery()
401                 .on('add change reset remove', this.proxy('do_search'))
402                 .on('add change reset remove', this.proxy('renderFacets'));
403
404         if (this.options.hidden) {
405             this.$el.hide();
406         }
407         if (this.headless) {
408             this.ready.resolve();
409         } else {
410             var load_view = instance.web.fields_view_get({
411                 model: this.dataset._model,
412                 view_id: this.view_id,
413                 view_type: 'search',
414                 context: this.dataset.get_context(),
415             });
416
417             this.alive($.when(load_view)).then(function (r) {
418                 return self.search_view_loaded(r);
419             }).fail(function () {
420                 self.ready.reject.apply(null, arguments);
421             });
422         }
423
424         instance.web.bus.on('click', this, function(ev) {
425             if ($(ev.target).parents('.oe_searchview').length === 0) {
426                 self.$el.removeClass('oe_searchview_open_drawer');
427             }
428         });
429
430         return $.when(p, this.ready);
431     },
432     show: function () {
433         this.$el.show();
434     },
435     hide: function () {
436         this.$el.hide();
437     },
438
439     subviewForRoot: function (subview_root) {
440         return _(this.input_subviews).detect(function (subview) {
441             return subview.$el[0] === subview_root;
442         });
443     },
444     siblingSubview: function (subview, direction, wrap_around) {
445         var index = _(this.input_subviews).indexOf(subview) + direction;
446         if (wrap_around && index < 0) {
447             index = this.input_subviews.length - 1;
448         } else if (wrap_around && index >= this.input_subviews.length) {
449             index = 0;
450         }
451         return this.input_subviews[index];
452     },
453     focusPreceding: function (subview_root) {
454         return this.siblingSubview(
455             this.subviewForRoot(subview_root), -1, true)
456                 .$el.focus();
457     },
458     focusFollowing: function (subview_root) {
459         return this.siblingSubview(
460             this.subviewForRoot(subview_root), +1, true)
461                 .$el.focus();
462     },
463
464     /**
465      * Sets up thingie where all the mess is put?
466      */
467     select_for_drawer: function () {
468         return _(this.inputs).filter(function (input) {
469             return input.in_drawer();
470         });
471     },
472     /**
473      * Sets up search view's view-wide auto-completion widget
474      */
475     setup_global_completion: function () {
476         var autocomplete = this.$el.autocomplete({
477             source: this.proxy('complete_global_search'),
478             select: this.proxy('select_completion'),
479             focus: function (e) { e.preventDefault(); },
480             html: true,
481             autoFocus: true,
482             minLength: 1,
483             delay: 250,
484         }).data('autocomplete');
485
486         this.$el.on('input', function () {
487             this.$el.autocomplete('close');
488         }.bind(this));
489
490         // MonkeyPatch autocomplete instance
491         _.extend(autocomplete, {
492             _renderItem: function (ul, item) {
493                 // item of completion list
494                 var $item = $( "<li></li>" )
495                     .data( "item.autocomplete", item )
496                     .appendTo( ul );
497
498                 if (item.facet !== undefined) {
499                     // regular completion item
500                     if (item.first) {
501                         $item.css('borderTop', '1px solid #cccccc');
502                     }
503                     return $item.append(
504                         (item.label)
505                             ? $('<a>').html(item.label)
506                             : $('<a>').text(item.value));
507                 }
508                 return $item.text(item.label)
509                     .css({
510                         borderTop: '1px solid #cccccc',
511                         margin: 0,
512                         padding: 0,
513                         zoom: 1,
514                         'float': 'left',
515                         clear: 'left',
516                         width: '100%'
517                     });
518             },
519             _value: function() {
520                 return self.$('div.oe_searchview_input').text();
521             },
522         });
523     },
524     /**
525      * Provide auto-completion result for req.term (an array to `resp`)
526      *
527      * @param {Object} req request to complete
528      * @param {String} req.term searched term to complete
529      * @param {Function} resp response callback
530      */
531     complete_global_search:  function (req, resp) {
532         $.when.apply(null, _(this.inputs).chain()
533             .filter(function (input) { return input.visible(); })
534             .invoke('complete', req.term)
535             .value()).then(function () {
536                 resp(_(arguments).chain()
537                     .compact()
538                     .map(function (completion) {
539                         if (completion.length && completion[0].facet !== undefined) {
540                             completion[0].first = true;
541                         }
542                         return completion;
543                     })
544                     .flatten(true)
545                     .value());
546                 });
547     },
548
549     /**
550      * Action to perform in case of selection: create a facet (model)
551      * and add it to the search collection
552      *
553      * @param {Object} e selection event, preventDefault to avoid setting value on object
554      * @param {Object} ui selection information
555      * @param {Object} ui.item selected completion item
556      */
557     select_completion: function (e, ui) {
558         e.preventDefault();
559
560         var input_index = _(this.input_subviews).indexOf(
561             this.subviewForRoot(
562                 this.$('div.oe_searchview_input:focus')[0]));
563         this.query.add(ui.item.facet, {at: input_index / 2});
564     },
565     childFocused: function () {
566         this.$el.addClass('oe_focused');
567     },
568     childBlurred: function () {
569         var val = this.$el.val();
570         this.$el.val('');
571         var complete = this.$el.data('autocomplete');
572         if ((val && complete.term === undefined) || complete.previous) {
573             throw new Error("new jquery.ui version altering implementation" +
574                             " details relied on");
575         }
576         delete complete.term;
577         this.$el.removeClass('oe_focused')
578                      .trigger('blur');
579     },
580     /**
581      *
582      * @param {openerp.web.search.SearchQuery | openerp.web.search.Facet} _1
583      * @param {openerp.web.search.Facet} [_2]
584      * @param {Object} [options]
585      */
586     renderFacets: function (_1, _2, options) {
587         // _1: model if event=change, otherwise collection
588         // _2: undefined if event=change, otherwise model
589         var self = this;
590         var started = [];
591         var $e = this.$('div.oe_searchview_facets');
592         _.invoke(this.input_subviews, 'destroy');
593         this.input_subviews = [];
594
595         var i = new my.InputView(this);
596         started.push(i.appendTo($e));
597         this.input_subviews.push(i);
598         this.query.each(function (facet) {
599             var f = new my.FacetView(this, facet);
600             started.push(f.appendTo($e));
601             self.input_subviews.push(f);
602
603             var i = new my.InputView(this);
604             started.push(i.appendTo($e));
605             self.input_subviews.push(i);
606         }, this);
607         _.each(this.input_subviews, function (childView) {
608             childView.on('focused', self, self.proxy('childFocused'));
609             childView.on('blurred', self, self.proxy('childBlurred'));
610         });
611
612         $.when.apply(null, started).then(function () {
613             var input_to_focus;
614             // options.at: facet inserted at given index, focus next input
615             // otherwise just focus last input
616             if (!options || typeof options.at !== 'number') {
617                 input_to_focus = _.last(self.input_subviews);
618             } else {
619                 input_to_focus = self.input_subviews[(options.at + 1) * 2];
620             }
621
622             input_to_focus.$el.focus();
623         });
624     },
625
626     /**
627      * Builds a list of widget rows (each row is an array of widgets)
628      *
629      * @param {Array} items a list of nodes to convert to widgets
630      * @param {Object} fields a mapping of field names to (ORM) field attributes
631      * @param {Object} [group] group to put the new controls in
632      */
633     make_widgets: function (items, fields, group) {
634         if (!group) {
635             group = new instance.web.search.Group(
636                 this, 'q', {attrs: {string: _t("Filters")}});
637         }
638         var self = this;
639         var filters = [];
640         _.each(items, function (item) {
641             if (filters.length && item.tag !== 'filter') {
642                 group.push(new instance.web.search.FilterGroup(filters, group));
643                 filters = [];
644             }
645
646             switch (item.tag) {
647             case 'separator': case 'newline':
648                 break;
649             case 'filter':
650                 filters.push(new instance.web.search.Filter(item, group));
651                 break;
652             case 'group':
653                 self.make_widgets(item.children, fields,
654                     new instance.web.search.Group(group, 'w', item));
655                 break;
656             case 'field':
657                 var field = this.make_field(
658                     item, fields[item['attrs'].name], group);
659                 group.push(field);
660                 // filters
661                 self.make_widgets(item.children, fields, group);
662                 break;
663             }
664         }, this);
665
666         if (filters.length) {
667             group.push(new instance.web.search.FilterGroup(filters, this));
668         }
669     },
670     /**
671      * Creates a field for the provided field descriptor item (which comes
672      * from fields_view_get)
673      *
674      * @param {Object} item fields_view_get node for the field
675      * @param {Object} field fields_get result for the field
676      * @param {Object} [parent]
677      * @returns instance.web.search.Field
678      */
679     make_field: function (item, field, parent) {
680         // M2O combined with selection widget is pointless and broken in search views,
681         // but has been used in the past for unsupported hacks -> ignore it
682         if (field.type === "many2one" && item.attrs.widget === "selection"){
683             item.attrs.widget = undefined;
684         }
685         var obj = instance.web.search.fields.get_any( [item.attrs.widget, field.type]);
686         if(obj) {
687             return new (obj) (item, field, parent || this);
688         } else {
689             console.group('Unknown field type ' + field.type);
690             console.error('View node', item);
691             console.info('View field', field);
692             console.info('In view', this);
693             console.groupEnd();
694             return null;
695         }
696     },
697
698     add_common_inputs: function() {
699         // add Filters to this.inputs, need view.controls filled
700         (new instance.web.search.Filters(this));
701         // add custom filters to this.inputs
702         this.custom_filters = new instance.web.search.CustomFilters(this);
703         // add Advanced to this.inputs
704         (new instance.web.search.Advanced(this));
705     },
706
707     search_view_loaded: function(data) {
708         var self = this;
709         this.fields_view = data;
710         if (data.type !== 'search' ||
711             data.arch.tag !== 'search') {
712                 throw new Error(_.str.sprintf(
713                     "Got non-search view after asking for a search view: type %s, arch root %s",
714                     data.type, data.arch.tag));
715         }
716         this.make_widgets(
717             data['arch'].children,
718             data.fields);
719
720         this.add_common_inputs();
721
722         // build drawer
723         var drawer_started = $.when.apply(
724             null, _(this.select_for_drawer()).invoke(
725                 'appendTo', this.$('.oe_searchview_drawer')));
726
727         
728         // load defaults
729         var defaults_fetched = $.when.apply(null, _(this.inputs).invoke(
730                 'facet_for_defaults', this.defaults))
731             .then(this.proxy('setup_default_query'));
732
733         return $.when(drawer_started, defaults_fetched)
734             .then(function () { 
735                 self.trigger("search_view_loaded", data);
736                 self.ready.resolve();
737             });
738     },
739     setup_default_query: function () {
740         // Hacky implementation of CustomFilters#facet_for_defaults ensure
741         // CustomFilters will be ready (and CustomFilters#filters will be
742         // correctly filled) by the time this method executes.
743         var custom_filters = this.custom_filters.filters;
744         if (!this.options.disable_custom_filters && !_(custom_filters).isEmpty()) {
745             // Check for any is_default custom filter
746             var personal_filter = _(custom_filters).find(function (filter) {
747                 return filter.user_id && filter.is_default;
748             });
749             if (personal_filter) {
750                 this.custom_filters.toggle_filter(personal_filter, true);
751                 return;
752             }
753
754             var global_filter = _(custom_filters).find(function (filter) {
755                 return !filter.user_id && filter.is_default;
756             });
757             if (global_filter) {
758                 this.custom_filters.toggle_filter(global_filter, true);
759                 return;
760             }
761         }
762         // No custom filter, or no is_default custom filter, apply view defaults
763         this.query.reset(_(arguments).compact(), {preventSearch: true});
764     },
765     /**
766      * Extract search data from the view's facets.
767      *
768      * Result is an object with 4 (own) properties:
769      *
770      * errors
771      *     An array of any error generated during data validation and
772      *     extraction, contains the validation error objects
773      * domains
774      *     Array of domains
775      * contexts
776      *     Array of contexts
777      * groupbys
778      *     Array of domains, in groupby order rather than view order
779      *
780      * @return {Object}
781      */
782     build_search_data: function () {
783         var domains = [], contexts = [], groupbys = [], errors = [];
784
785         this.query.each(function (facet) {
786             var field = facet.get('field');
787             try {
788                 var domain = field.get_domain(facet);
789                 if (domain) {
790                     domains.push(domain);
791                 }
792                 var context = field.get_context(facet);
793                 if (context) {
794                     contexts.push(context);
795                 }
796                 var group_by = field.get_groupby(facet);
797                 if (group_by) {
798                     groupbys.push.apply(groupbys, group_by);
799                 }
800             } catch (e) {
801                 if (e instanceof instance.web.search.Invalid) {
802                     errors.push(e);
803                 } else {
804                     throw e;
805                 }
806             }
807         });
808         return {
809             domains: domains,
810             contexts: contexts,
811             groupbys: groupbys,
812             errors: errors
813         };
814     }, 
815     /**
816      * Performs the search view collection of widget data.
817      *
818      * If the collection went well (all fields are valid), then triggers
819      * :js:func:`instance.web.SearchView.on_search`.
820      *
821      * If at least one field failed its validation, triggers
822      * :js:func:`instance.web.SearchView.on_invalid` instead.
823      *
824      * @param [_query]
825      * @param {Object} [options]
826      */
827     do_search: function (_query, options) {
828         if (options && options.preventSearch) {
829             return;
830         }
831         var search = this.build_search_data();
832         if (!_.isEmpty(search.errors)) {
833             this.on_invalid(search.errors);
834             return;
835         }
836         this.trigger('search_data', search.domains, search.contexts, search.groupbys);
837     },
838     /**
839      * Triggered after the SearchView has collected all relevant domains and
840      * contexts.
841      *
842      * It is provided with an Array of domains and an Array of contexts, which
843      * may or may not be evaluated (each item can be either a valid domain or
844      * context, or a string to evaluate in order in the sequence)
845      *
846      * It is also passed an array of contexts used for group_by (they are in
847      * the correct order for group_by evaluation, which contexts may not be)
848      *
849      * @event
850      * @param {Array} domains an array of literal domains or domain references
851      * @param {Array} contexts an array of literal contexts or context refs
852      * @param {Array} groupbys ordered contexts which may or may not have group_by keys
853      */
854     /**
855      * Triggered after a validation error in the SearchView fields.
856      *
857      * Error objects have three keys:
858      * * ``field`` is the name of the invalid field
859      * * ``value`` is the invalid value
860      * * ``message`` is the (in)validation message provided by the field
861      *
862      * @event
863      * @param {Array} errors a never-empty array of error objects
864      */
865     on_invalid: function (errors) {
866         this.do_notify(_t("Invalid Search"), _t("triggered from search view"));
867         this.trigger('invalid_search', errors);
868     }
869 });
870
871 /**
872  * Registry of search fields, called by :js:class:`instance.web.SearchView` to
873  * find and instantiate its field widgets.
874  */
875 instance.web.search.fields = new instance.web.Registry({
876     'char': 'instance.web.search.CharField',
877     'text': 'instance.web.search.CharField',
878     'html': 'instance.web.search.CharField',
879     'boolean': 'instance.web.search.BooleanField',
880     'integer': 'instance.web.search.IntegerField',
881     'id': 'instance.web.search.IntegerField',
882     'float': 'instance.web.search.FloatField',
883     'selection': 'instance.web.search.SelectionField',
884     'datetime': 'instance.web.search.DateTimeField',
885     'date': 'instance.web.search.DateField',
886     'many2one': 'instance.web.search.ManyToOneField',
887     'many2many': 'instance.web.search.CharField',
888     'one2many': 'instance.web.search.CharField'
889 });
890 instance.web.search.Invalid = instance.web.Class.extend( /** @lends instance.web.search.Invalid# */{
891     /**
892      * Exception thrown by search widgets when they hold invalid values,
893      * which they can not return when asked.
894      *
895      * @constructs instance.web.search.Invalid
896      * @extends instance.web.Class
897      *
898      * @param field the name of the field holding an invalid value
899      * @param value the invalid value
900      * @param message validation failure message
901      */
902     init: function (field, value, message) {
903         this.field = field;
904         this.value = value;
905         this.message = message;
906     },
907     toString: function () {
908         return _.str.sprintf(
909             _t("Incorrect value for field %(fieldname)s: [%(value)s] is %(message)s"),
910             {fieldname: this.field, value: this.value, message: this.message}
911         );
912     }
913 });
914 instance.web.search.Widget = instance.web.Widget.extend( /** @lends instance.web.search.Widget# */{
915     template: null,
916     /**
917      * Root class of all search widgets
918      *
919      * @constructs instance.web.search.Widget
920      * @extends instance.web.Widget
921      *
922      * @param parent parent of this widget
923      */
924     init: function (parent) {
925         this._super(parent);
926         var ancestor = parent;
927         do {
928             this.view = ancestor;
929         } while (!(ancestor instanceof instance.web.SearchView)
930                && (ancestor = (ancestor.getParent && ancestor.getParent())));
931     }
932 });
933
934 instance.web.search.add_expand_listener = function($root) {
935     $root.find('a.searchview_group_string').click(function (e) {
936         $root.toggleClass('folded expanded');
937         e.stopPropagation();
938         e.preventDefault();
939     });
940 };
941 instance.web.search.Group = instance.web.search.Widget.extend({
942     init: function (parent, icon, node) {
943         this._super(parent);
944         var attrs = node.attrs;
945         this.modifiers = attrs.modifiers =
946             attrs.modifiers ? JSON.parse(attrs.modifiers) : {};
947         this.attrs = attrs;
948         this.icon = icon;
949         this.name = attrs.string;
950         this.children = [];
951
952         this.view.controls.push(this);
953     },
954     push: function (input) {
955         this.children.push(input);
956     },
957     visible: function () {
958         return !this.modifiers.invisible;
959     },
960 });
961
962 instance.web.search.Input = instance.web.search.Widget.extend( /** @lends instance.web.search.Input# */{
963     _in_drawer: false,
964     /**
965      * @constructs instance.web.search.Input
966      * @extends instance.web.search.Widget
967      *
968      * @param parent
969      */
970     init: function (parent) {
971         this._super(parent);
972         this.load_attrs({});
973         this.view.inputs.push(this);
974     },
975     /**
976      * Fetch auto-completion values for the widget.
977      *
978      * The completion values should be an array of objects with keys category,
979      * label, value prefixed with an object with keys type=section and label
980      *
981      * @param {String} value value to complete
982      * @returns {jQuery.Deferred<null|Array>}
983      */
984     complete: function (value) {
985         return $.when(null);
986     },
987     /**
988      * Returns a Facet instance for the provided defaults if they apply to
989      * this widget, or null if they don't.
990      *
991      * This default implementation will try calling
992      * :js:func:`instance.web.search.Input#facet_for` if the widget's name
993      * matches the input key
994      *
995      * @param {Object} defaults
996      * @returns {jQuery.Deferred<null|Object>}
997      */
998     facet_for_defaults: function (defaults) {
999         if (!this.attrs ||
1000             !(this.attrs.name in defaults && defaults[this.attrs.name])) {
1001             return $.when(null);
1002         }
1003         return this.facet_for(defaults[this.attrs.name]);
1004     },
1005     in_drawer: function () {
1006         return !!this._in_drawer;
1007     },
1008     get_context: function () {
1009         throw new Error(
1010             "get_context not implemented for widget " + this.attrs.type);
1011     },
1012     get_groupby: function () {
1013         throw new Error(
1014             "get_groupby not implemented for widget " + this.attrs.type);
1015     },
1016     get_domain: function () {
1017         throw new Error(
1018             "get_domain not implemented for widget " + this.attrs.type);
1019     },
1020     load_attrs: function (attrs) {
1021         attrs.modifiers = attrs.modifiers ? JSON.parse(attrs.modifiers) : {};
1022         this.attrs = attrs;
1023     },
1024     /**
1025      * Returns whether the input is "visible". The default behavior is to
1026      * query the ``modifiers.invisible`` flag on the input's description or
1027      * view node.
1028      *
1029      * @returns {Boolean}
1030      */
1031     visible: function () {
1032         if (this.attrs.modifiers.invisible) {
1033             return false;
1034         }
1035         var parent = this;
1036         while ((parent = parent.getParent()) &&
1037                (   (parent instanceof instance.web.search.Group)
1038                 || (parent instanceof instance.web.search.Input))) {
1039             if (!parent.visible()) {
1040                 return false;
1041             }
1042         }
1043         return true;
1044     },
1045 });
1046 instance.web.search.FilterGroup = instance.web.search.Input.extend(/** @lends instance.web.search.FilterGroup# */{
1047     template: 'SearchView.filters',
1048     icon: 'q',
1049     completion_label: _lt("Filter on: %s"),
1050     /**
1051      * Inclusive group of filters, creates a continuous "button" with clickable
1052      * sections (the normal display for filters is to be a self-contained button)
1053      *
1054      * @constructs instance.web.search.FilterGroup
1055      * @extends instance.web.search.Input
1056      *
1057      * @param {Array<instance.web.search.Filter>} filters elements of the group
1058      * @param {instance.web.SearchView} parent parent in which the filters are contained
1059      */
1060     init: function (filters, parent) {
1061         // If all filters are group_by and we're not initializing a GroupbyGroup,
1062         // create a GroupbyGroup instead of the current FilterGroup
1063         if (!(this instanceof instance.web.search.GroupbyGroup) &&
1064               _(filters).all(function (f) {
1065                   if (!f.attrs.context) { return false; }
1066                   var c = instance.web.pyeval.eval('context', f.attrs.context);
1067                   return !_.isEmpty(c.group_by);})) {
1068             return new instance.web.search.GroupbyGroup(filters, parent);
1069         }
1070         this._super(parent);
1071         this.filters = filters;
1072         this.view.query.on('add remove change reset', this.proxy('search_change'));
1073     },
1074     start: function () {
1075         this.$el.on('click', 'li', this.proxy('toggle_filter'));
1076         return $.when(null);
1077     },
1078     /**
1079      * Handles change of the search query: any of the group's filter which is
1080      * in the search query should be visually checked in the drawer
1081      */
1082     search_change: function () {
1083         var self = this;
1084         var $filters = this.$('> li').removeClass('oe_selected');
1085         var facet = this.view.query.find(_.bind(this.match_facet, this));
1086         if (!facet) { return; }
1087         facet.values.each(function (v) {
1088             var i = _(self.filters).indexOf(v.get('value'));
1089             if (i === -1) { return; }
1090             $filters.filter(function () {
1091                 return Number($(this).data('index')) === i;
1092             }).addClass('oe_selected');
1093         });
1094     },
1095     /**
1096      * Matches the group to a facet, in order to find if the group is
1097      * represented in the current search query
1098      */
1099     match_facet: function (facet) {
1100         return facet.get('field') === this;
1101     },
1102     make_facet: function (values) {
1103         return {
1104             category: _t("Filter"),
1105             icon: this.icon,
1106             values: values,
1107             field: this
1108         };
1109     },
1110     make_value: function (filter) {
1111         return {
1112             label: filter.attrs.string || filter.attrs.help || filter.attrs.name,
1113             value: filter
1114         };
1115     },
1116     facet_for_defaults: function (defaults) {
1117         var self = this;
1118         var fs = _(this.filters).chain()
1119             .filter(function (f) {
1120                 return f.attrs && f.attrs.name && !!defaults[f.attrs.name];
1121             }).map(function (f) {
1122                 return self.make_value(f);
1123             }).value();
1124         if (_.isEmpty(fs)) { return $.when(null); }
1125         return $.when(this.make_facet(fs));
1126     },
1127     /**
1128      * Fetches contexts for all enabled filters in the group
1129      *
1130      * @param {openerp.web.search.Facet} facet
1131      * @return {*} combined contexts of the enabled filters in this group
1132      */
1133     get_context: function (facet) {
1134         var contexts = facet.values.chain()
1135             .map(function (f) { return f.get('value').attrs.context; })
1136             .without('{}')
1137             .reject(_.isEmpty)
1138             .value();
1139
1140         if (!contexts.length) { return; }
1141         if (contexts.length === 1) { return contexts[0]; }
1142         return _.extend(new instance.web.CompoundContext(), {
1143             __contexts: contexts
1144         });
1145     },
1146     /**
1147      * Fetches group_by sequence for all enabled filters in the group
1148      *
1149      * @param {VS.model.SearchFacet} facet
1150      * @return {Array} enabled filters in this group
1151      */
1152     get_groupby: function (facet) {
1153         return  facet.values.chain()
1154             .map(function (f) { return f.get('value').attrs.context; })
1155             .without('{}')
1156             .reject(_.isEmpty)
1157             .value();
1158     },
1159     /**
1160      * Handles domains-fetching for all the filters within it: groups them.
1161      *
1162      * @param {VS.model.SearchFacet} facet
1163      * @return {*} combined domains of the enabled filters in this group
1164      */
1165     get_domain: function (facet) {
1166         var domains = facet.values.chain()
1167             .map(function (f) { return f.get('value').attrs.domain; })
1168             .without('[]')
1169             .reject(_.isEmpty)
1170             .value();
1171
1172         if (!domains.length) { return; }
1173         if (domains.length === 1) { return domains[0]; }
1174         for (var i=domains.length; --i;) {
1175             domains.unshift(['|']);
1176         }
1177         return _.extend(new instance.web.CompoundDomain(), {
1178             __domains: domains
1179         });
1180     },
1181     toggle_filter: function (e) {
1182         this.toggle(this.filters[Number($(e.target).data('index'))]);
1183     },
1184     toggle: function (filter) {
1185         this.view.query.toggle(this.make_facet([this.make_value(filter)]));
1186     },
1187     complete: function (item) {
1188         var self = this;
1189         item = item.toLowerCase();
1190         var facet_values = _(this.filters).chain()
1191             .filter(function (filter) { return filter.visible(); })
1192             .filter(function (filter) {
1193                 var at = {
1194                     string: filter.attrs.string || '',
1195                     help: filter.attrs.help || '',
1196                     name: filter.attrs.name || ''
1197                 };
1198                 var include = _.str.include;
1199                 return include(at.string.toLowerCase(), item)
1200                     || include(at.help.toLowerCase(), item)
1201                     || include(at.name.toLowerCase(), item);
1202             })
1203             .map(this.make_value)
1204             .value();
1205         if (_(facet_values).isEmpty()) { return $.when(null); }
1206         return $.when(_.map(facet_values, function (facet_value) {
1207             return {
1208                 label: _.str.sprintf(self.completion_label.toString(),
1209                                      _.escape(facet_value.label)),
1210                 facet: self.make_facet([facet_value])
1211             };
1212         }));
1213     }
1214 });
1215 instance.web.search.GroupbyGroup = instance.web.search.FilterGroup.extend({
1216     icon: 'w',
1217     completion_label: _lt("Group by: %s"),
1218     init: function (filters, parent) {
1219         this._super(filters, parent);
1220         // Not flanders: facet unicity is handled through the
1221         // (category, field) pair of facet attributes. This is all well and
1222         // good for regular filter groups where a group matches a facet, but for
1223         // groupby we want a single facet. So cheat: add an attribute on the
1224         // view which proxies to the first GroupbyGroup, so it can be used
1225         // for every GroupbyGroup and still provides the various methods needed
1226         // by the search view. Use weirdo name to avoid risks of conflicts
1227         if (!this.view._s_groupby) {
1228             this.view._s_groupby = {
1229                 help: "See GroupbyGroup#init",
1230                 get_context: this.proxy('get_context'),
1231                 get_domain: this.proxy('get_domain'),
1232                 get_groupby: this.proxy('get_groupby')
1233             };
1234         }
1235     },
1236     match_facet: function (facet) {
1237         return facet.get('field') === this.view._s_groupby;
1238     },
1239     make_facet: function (values) {
1240         return {
1241             category: _t("GroupBy"),
1242             icon: this.icon,
1243             values: values,
1244             field: this.view._s_groupby
1245         };
1246     }
1247 });
1248 instance.web.search.Filter = instance.web.search.Input.extend(/** @lends instance.web.search.Filter# */{
1249     template: 'SearchView.filter',
1250     /**
1251      * Implementation of the OpenERP filters (button with a context and/or
1252      * a domain sent as-is to the search view)
1253      *
1254      * Filters are only attributes holder, the actual work (compositing
1255      * domains and contexts, converting between facets and filters) is
1256      * performed by the filter group.
1257      *
1258      * @constructs instance.web.search.Filter
1259      * @extends instance.web.search.Input
1260      *
1261      * @param node
1262      * @param parent
1263      */
1264     init: function (node, parent) {
1265         this._super(parent);
1266         this.load_attrs(node.attrs);
1267     },
1268     facet_for: function () { return $.when(null); },
1269     get_context: function () { },
1270     get_domain: function () { },
1271 });
1272 instance.web.search.Field = instance.web.search.Input.extend( /** @lends instance.web.search.Field# */ {
1273     template: 'SearchView.field',
1274     default_operator: '=',
1275     /**
1276      * @constructs instance.web.search.Field
1277      * @extends instance.web.search.Input
1278      *
1279      * @param view_section
1280      * @param field
1281      * @param parent
1282      */
1283     init: function (view_section, field, parent) {
1284         this._super(parent);
1285         this.load_attrs(_.extend({}, field, view_section.attrs));
1286     },
1287     facet_for: function (value) {
1288         return $.when({
1289             field: this,
1290             category: this.attrs.string || this.attrs.name,
1291             values: [{label: String(value), value: value}]
1292         });
1293     },
1294     value_from: function (facetValue) {
1295         return facetValue.get('value');
1296     },
1297     get_context: function (facet) {
1298         var self = this;
1299         // A field needs a context to send when active
1300         var context = this.attrs.context;
1301         if (_.isEmpty(context) || !facet.values.length) {
1302             return;
1303         }
1304         var contexts = facet.values.map(function (facetValue) {
1305             return new instance.web.CompoundContext(context)
1306                 .set_eval_context({self: self.value_from(facetValue)});
1307         });
1308
1309         if (contexts.length === 1) { return contexts[0]; }
1310
1311         return _.extend(new instance.web.CompoundContext(), {
1312             __contexts: contexts
1313         });
1314     },
1315     get_groupby: function () { },
1316     /**
1317      * Function creating the returned domain for the field, override this
1318      * methods in children if you only need to customize the field's domain
1319      * without more complex alterations or tests (and without the need to
1320      * change override the handling of filter_domain)
1321      *
1322      * @param {String} name the field's name
1323      * @param {String} operator the field's operator (either attribute-specified or default operator for the field
1324      * @param {Number|String} facet parsed value for the field
1325      * @returns {Array<Array>} domain to include in the resulting search
1326      */
1327     make_domain: function (name, operator, facet) {
1328         return [[name, operator, this.value_from(facet)]];
1329     },
1330     get_domain: function (facet) {
1331         if (!facet.values.length) { return; }
1332
1333         var value_to_domain;
1334         var self = this;
1335         var domain = this.attrs['filter_domain'];
1336         if (domain) {
1337             value_to_domain = function (facetValue) {
1338                 return new instance.web.CompoundDomain(domain)
1339                     .set_eval_context({self: self.value_from(facetValue)});
1340             };
1341         } else {
1342             value_to_domain = function (facetValue) {
1343                 return self.make_domain(
1344                     self.attrs.name,
1345                     self.attrs.operator || self.default_operator,
1346                     facetValue);
1347             };
1348         }
1349         var domains = facet.values.map(value_to_domain);
1350
1351         if (domains.length === 1) { return domains[0]; }
1352         for (var i = domains.length; --i;) {
1353             domains.unshift(['|']);
1354         }
1355
1356         return _.extend(new instance.web.CompoundDomain(), {
1357             __domains: domains
1358         });
1359     }
1360 });
1361 /**
1362  * Implementation of the ``char`` OpenERP field type:
1363  *
1364  * * Default operator is ``ilike`` rather than ``=``
1365  *
1366  * * The Javascript and the HTML values are identical (strings)
1367  *
1368  * @class
1369  * @extends instance.web.search.Field
1370  */
1371 instance.web.search.CharField = instance.web.search.Field.extend( /** @lends instance.web.search.CharField# */ {
1372     default_operator: 'ilike',
1373     complete: function (value) {
1374         if (_.isEmpty(value)) { return $.when(null); }
1375         var label = _.str.sprintf(_.str.escapeHTML(
1376             _t("Search %(field)s for: %(value)s")), {
1377                 field: '<em>' + _.escape(this.attrs.string) + '</em>',
1378                 value: '<strong>' + _.escape(value) + '</strong>'});
1379         return $.when([{
1380             label: label,
1381             facet: {
1382                 category: this.attrs.string,
1383                 field: this,
1384                 values: [{label: value, value: value}]
1385             }
1386         }]);
1387     }
1388 });
1389 instance.web.search.NumberField = instance.web.search.Field.extend(/** @lends instance.web.search.NumberField# */{
1390     complete: function (value) {
1391         var val = this.parse(value);
1392         if (isNaN(val)) { return $.when(); }
1393         var label = _.str.sprintf(
1394             _t("Search %(field)s for: %(value)s"), {
1395                 field: '<em>' + _.escape(this.attrs.string) + '</em>',
1396                 value: '<strong>' + _.escape(value) + '</strong>'});
1397         return $.when([{
1398             label: label,
1399             facet: {
1400                 category: this.attrs.string,
1401                 field: this,
1402                 values: [{label: value, value: val}]
1403             }
1404         }]);
1405     },
1406 });
1407 /**
1408  * @class
1409  * @extends instance.web.search.NumberField
1410  */
1411 instance.web.search.IntegerField = instance.web.search.NumberField.extend(/** @lends instance.web.search.IntegerField# */{
1412     error_message: _t("not a valid integer"),
1413     parse: function (value) {
1414         try {
1415             return instance.web.parse_value(value, {'widget': 'integer'});
1416         } catch (e) {
1417             return NaN;
1418         }
1419     }
1420 });
1421 /**
1422  * @class
1423  * @extends instance.web.search.NumberField
1424  */
1425 instance.web.search.FloatField = instance.web.search.NumberField.extend(/** @lends instance.web.search.FloatField# */{
1426     error_message: _t("not a valid number"),
1427     parse: function (value) {
1428         try {
1429             return instance.web.parse_value(value, {'widget': 'float'});
1430         } catch (e) {
1431             return NaN;
1432         }
1433     }
1434 });
1435
1436 /**
1437  * Utility function for m2o & selection fields taking a selection/name_get pair
1438  * (value, name) and converting it to a Facet descriptor
1439  *
1440  * @param {instance.web.search.Field} field holder field
1441  * @param {Array} pair pair value to convert
1442  */
1443 function facet_from(field, pair) {
1444     return {
1445         field: field,
1446         category: field['attrs'].string,
1447         values: [{label: pair[1], value: pair[0]}]
1448     };
1449 }
1450
1451 /**
1452  * @class
1453  * @extends instance.web.search.Field
1454  */
1455 instance.web.search.SelectionField = instance.web.search.Field.extend(/** @lends instance.web.search.SelectionField# */{
1456     // This implementation is a basic <select> field, but it may have to be
1457     // altered to be more in line with the GTK client, which uses a combo box
1458     // (~ jquery.autocomplete):
1459     // * If an option was selected in the list, behave as currently
1460     // * If something which is not in the list was entered (via the text input),
1461     //   the default domain should become (`ilike` string_value) but **any
1462     //   ``context`` or ``filter_domain`` becomes falsy, idem if ``@operator``
1463     //   is specified. So at least get_domain needs to be quite a bit
1464     //   overridden (if there's no @value and there is no filter_domain and
1465     //   there is no @operator, return [[name, 'ilike', str_val]]
1466     template: 'SearchView.field.selection',
1467     init: function () {
1468         this._super.apply(this, arguments);
1469         // prepend empty option if there is no empty option in the selection list
1470         this.prepend_empty = !_(this.attrs.selection).detect(function (item) {
1471             return !item[1];
1472         });
1473     },
1474     complete: function (needle) {
1475         var self = this;
1476         var results = _(this.attrs.selection).chain()
1477             .filter(function (sel) {
1478                 var value = sel[0], label = sel[1];
1479                 if (!value) { return false; }
1480                 return label.toLowerCase().indexOf(needle.toLowerCase()) !== -1;
1481             })
1482             .map(function (sel) {
1483                 return {
1484                     label: _.escape(sel[1]),
1485                     facet: facet_from(self, sel)
1486                 };
1487             }).value();
1488         if (_.isEmpty(results)) { return $.when(null); }
1489         return $.when.call(null, [{
1490             label: _.escape(this.attrs.string)
1491         }].concat(results));
1492     },
1493     facet_for: function (value) {
1494         var match = _(this.attrs.selection).detect(function (sel) {
1495             return sel[0] === value;
1496         });
1497         if (!match) { return $.when(null); }
1498         return $.when(facet_from(this, match));
1499     }
1500 });
1501 instance.web.search.BooleanField = instance.web.search.SelectionField.extend(/** @lends instance.web.search.BooleanField# */{
1502     /**
1503      * @constructs instance.web.search.BooleanField
1504      * @extends instance.web.search.BooleanField
1505      */
1506     init: function () {
1507         this._super.apply(this, arguments);
1508         this.attrs.selection = [
1509             [true, _t("Yes")],
1510             [false, _t("No")]
1511         ];
1512     }
1513 });
1514 /**
1515  * @class
1516  * @extends instance.web.search.DateField
1517  */
1518 instance.web.search.DateField = instance.web.search.Field.extend(/** @lends instance.web.search.DateField# */{
1519     value_from: function (facetValue) {
1520         return instance.web.date_to_str(facetValue.get('value'));
1521     },
1522     complete: function (needle) {
1523         var d = Date.parse(needle);
1524         if (!d) { return $.when(null); }
1525         var date_string = instance.web.format_value(d, this.attrs);
1526         var label = _.str.sprintf(_.str.escapeHTML(
1527             _t("Search %(field)s at: %(value)s")), {
1528                 field: '<em>' + _.escape(this.attrs.string) + '</em>',
1529                 value: '<strong>' + date_string + '</strong>'});
1530         return $.when([{
1531             label: label,
1532             facet: {
1533                 category: this.attrs.string,
1534                 field: this,
1535                 values: [{label: date_string, value: d}]
1536             }
1537         }]);
1538     }
1539 });
1540 /**
1541  * Implementation of the ``datetime`` openerp field type:
1542  *
1543  * * Uses the same widget as the ``date`` field type (a simple date)
1544  *
1545  * * Builds a slighly more complex, it's a datetime range (includes time)
1546  *   spanning the whole day selected by the date widget
1547  *
1548  * @class
1549  * @extends instance.web.DateField
1550  */
1551 instance.web.search.DateTimeField = instance.web.search.DateField.extend(/** @lends instance.web.search.DateTimeField# */{
1552     value_from: function (facetValue) {
1553         return instance.web.datetime_to_str(facetValue.get('value'));
1554     }
1555 });
1556 instance.web.search.ManyToOneField = instance.web.search.CharField.extend({
1557     default_operator: {},
1558     init: function (view_section, field, parent) {
1559         this._super(view_section, field, parent);
1560         this.model = new instance.web.Model(this.attrs.relation);
1561     },
1562     complete: function (needle) {
1563         var self = this;
1564         // FIXME: "concurrent" searches (multiple requests, mis-ordered responses)
1565         var context = instance.web.pyeval.eval(
1566             'contexts', [this.view.dataset.get_context()]);
1567         return this.model.call('name_search', [], {
1568             name: needle,
1569             args: instance.web.pyeval.eval(
1570                 'domains', this.attrs.domain ? [this.attrs.domain] : [], context),
1571             limit: 8,
1572             context: context
1573         }).then(function (results) {
1574             if (_.isEmpty(results)) { return null; }
1575             return [{label: self.attrs.string}].concat(
1576                 _(results).map(function (result) {
1577                     return {
1578                         label: _.escape(result[1]),
1579                         facet: facet_from(self, result)
1580                     };
1581                 }));
1582         });
1583     },
1584     facet_for: function (value) {
1585         var self = this;
1586         if (value instanceof Array) {
1587             if (value.length === 2 && _.isString(value[1])) {
1588                 return $.when(facet_from(this, value));
1589             }
1590             assert(value.length <= 1,
1591                    _t("M2O search fields do not currently handle multiple default values"));
1592             // there are many cases of {search_default_$m2ofield: [id]}, need
1593             // to handle this as if it were a single value.
1594             value = value[0];
1595         }
1596         return this.model.call('name_get', [value]).then(function (names) {
1597             if (_(names).isEmpty()) { return null; }
1598             return facet_from(self, names[0]);
1599         });
1600     },
1601     value_from: function (facetValue) {
1602         return facetValue.get('label');
1603     },
1604     make_domain: function (name, operator, facetValue) {
1605         switch(operator){
1606         case this.default_operator:
1607             return [[name, '=', facetValue.get('value')]];
1608         case 'child_of':
1609             return [[name, 'child_of', facetValue.get('value')]];
1610         }
1611         return this._super(name, operator, facetValue);
1612     },
1613     get_context: function (facet) {
1614         var values = facet.values;
1615         if (_.isEmpty(this.attrs.context) && values.length === 1) {
1616             var c = {};
1617             c['default_' + this.attrs.name] = values.at(0).get('value');
1618             return c;
1619         }
1620         return this._super(facet);
1621     }
1622 });
1623
1624 instance.web.search.CustomFilters = instance.web.search.Input.extend({
1625     template: 'SearchView.CustomFilters',
1626     _in_drawer: true,
1627     init: function () {
1628         this.is_ready = $.Deferred();
1629         this._super.apply(this, arguments);
1630     },
1631     start: function () {
1632         var self = this;
1633         this.model = new instance.web.Model('ir.filters');
1634         this.filters = {};
1635         this.$filters = {};
1636         this.view.query
1637             .on('remove', function (facet) {
1638                 if (!facet.get('is_custom_filter')) {
1639                     return;
1640                 }
1641                 self.clear_selection();
1642             })
1643             .on('reset', this.proxy('clear_selection'));
1644         this.$el.on('submit', 'form', this.proxy('save_current'));
1645         this.$el.on('click', 'input[type=checkbox]', function() {
1646             $(this).siblings('input[type=checkbox]').prop('checked', false);
1647         });
1648         this.$el.on('click', 'h4', function () {
1649             self.$el.toggleClass('oe_opened');
1650         });
1651         return this.model.call('get_filters', [this.view.model])
1652             .then(this.proxy('set_filters'))
1653             .done(function () { self.is_ready.resolve(); })
1654             .fail(function () { self.is_ready.reject.apply(self.is_ready, arguments); });
1655     },
1656     /**
1657      * Special implementation delaying defaults until CustomFilters is loaded
1658      */
1659     facet_for_defaults: function () {
1660         return this.is_ready;
1661     },
1662     /**
1663      * Generates a mapping key (in the filters and $filter mappings) for the
1664      * filter descriptor object provided (as returned by ``get_filters``).
1665      *
1666      * The mapping key is guaranteed to be unique for a given (user_id, name)
1667      * pair.
1668      *
1669      * @param {Object} filter
1670      * @param {String} filter.name
1671      * @param {Number|Pair<Number, String>} [filter.user_id]
1672      * @return {String} mapping key corresponding to the filter
1673      */
1674     key_for: function (filter) {
1675         var user_id = filter.user_id;
1676         var uid = (user_id instanceof Array) ? user_id[0] : user_id;
1677         return _.str.sprintf('(%s)%s', uid, filter.name);
1678     },
1679     /**
1680      * Generates a :js:class:`~instance.web.search.Facet` descriptor from a
1681      * filter descriptor
1682      *
1683      * @param {Object} filter
1684      * @param {String} filter.name
1685      * @param {Object} [filter.context]
1686      * @param {Array} [filter.domain]
1687      * @return {Object}
1688      */
1689     facet_for: function (filter) {
1690         return {
1691             category: _t("Custom Filter"),
1692             icon: 'M',
1693             field: {
1694                 get_context: function () { return filter.context; },
1695                 get_groupby: function () { return [filter.context]; },
1696                 get_domain: function () { return filter.domain; }
1697             },
1698             _id: filter['id'],
1699             is_custom_filter: true,
1700             values: [{label: filter.name, value: null}]
1701         };
1702     },
1703     clear_selection: function () {
1704         this.$('li.oe_selected').removeClass('oe_selected');
1705     },
1706     append_filter: function (filter) {
1707         var self = this;
1708         var key = this.key_for(filter);
1709         var warning = _t("This filter is global and will be removed for everybody if you continue.");
1710
1711         var $filter;
1712         if (key in this.$filters) {
1713             $filter = this.$filters[key];
1714         } else {
1715             var id = filter.id;
1716             this.filters[key] = filter;
1717             $filter = this.$filters[key] = $('<li></li>')
1718                 .appendTo(this.$('.oe_searchview_custom_list'))
1719                 .addClass(filter.user_id ? 'oe_searchview_custom_private'
1720                                          : 'oe_searchview_custom_public')
1721                 .toggleClass('oe_searchview_custom_default', filter.is_default)
1722                 .text(filter.name);
1723
1724             $('<a class="oe_searchview_custom_delete">x</a>')
1725                 .click(function (e) {
1726                     e.stopPropagation();
1727                     if (!(filter.user_id || confirm(warning))) {
1728                         return;
1729                     }
1730                     self.model.call('unlink', [id]).done(function () {
1731                         $filter.remove();
1732                         delete self.$filters[key];
1733                         delete self.filters[key];
1734                     });
1735                 })
1736                 .appendTo($filter);
1737         }
1738
1739         $filter.unbind('click').click(function () {
1740             self.toggle_filter(filter);
1741         });
1742     },
1743     toggle_filter: function (filter, preventSearch) {
1744         var current = this.view.query.find(function (facet) {
1745             return facet.get('_id') === filter.id;
1746         });
1747         if (current) {
1748             this.view.query.remove(current);
1749             this.$filters[this.key_for(filter)].removeClass('oe_selected');
1750             return;
1751         }
1752         this.view.query.reset([this.facet_for(filter)], {
1753             preventSearch: preventSearch || false});
1754         this.$filters[this.key_for(filter)].addClass('oe_selected');
1755     },
1756     set_filters: function (filters) {
1757         _(filters).map(_.bind(this.append_filter, this));
1758     },
1759     save_current: function () {
1760         var self = this;
1761         var $name = this.$('input:first');
1762         var private_filter = !this.$('#oe_searchview_custom_public').prop('checked');
1763         var set_as_default = this.$('#oe_searchview_custom_default').prop('checked');
1764         if (_.isEmpty($name.val())){
1765             this.do_warn(_t("Error"), _t("Filter name is required."));
1766             return false;
1767         }
1768         var search = this.view.build_search_data();
1769         instance.web.pyeval.eval_domains_and_contexts({
1770             domains: search.domains,
1771             contexts: search.contexts,
1772             group_by_seq: search.groupbys || []
1773         }).done(function (results) {
1774             if (!_.isEmpty(results.group_by)) {
1775                 results.context.group_by = results.group_by;
1776             }
1777             // Don't save user_context keys in the custom filter, otherwise end
1778             // up with e.g. wrong uid or lang stored *and used in subsequent
1779             // reqs*
1780             var ctx = results.context;
1781             _(_.keys(instance.session.user_context)).each(function (key) {
1782                 delete ctx[key];
1783             });
1784             var filter = {
1785                 name: $name.val(),
1786                 user_id: private_filter ? instance.session.uid : false,
1787                 model_id: self.view.model,
1788                 context: results.context,
1789                 domain: results.domain,
1790                 is_default: set_as_default
1791             };
1792             // FIXME: current context?
1793             return self.model.call('create_or_replace', [filter]).done(function (id) {
1794                 filter.id = id;
1795                 self.append_filter(filter);
1796                 self.$el
1797                     .removeClass('oe_opened')
1798                     .find('form')[0].reset();
1799             });
1800         });
1801         return false;
1802     }
1803 });
1804
1805 instance.web.search.Filters = instance.web.search.Input.extend({
1806     template: 'SearchView.Filters',
1807     _in_drawer: true,
1808     start: function () {
1809         var self = this;
1810         var running_count = 0;
1811         // get total filters count
1812         var is_group = function (i) { return i instanceof instance.web.search.FilterGroup; };
1813         var visible_filters = _(this.view.controls).chain().reject(function (group) {
1814             return _(_(group.children).filter(is_group)).isEmpty()
1815                 || group.modifiers.invisible;
1816         });
1817         var filters_count = visible_filters
1818             .pluck('children')
1819             .flatten()
1820             .filter(is_group)
1821             .map(function (i) { return i.filters.length; })
1822             .sum()
1823             .value();
1824
1825         var col1 = [], col2 = visible_filters.map(function (group) {
1826                 var filters = _(group.children).filter(is_group);
1827                 return {
1828                     name: _.str.sprintf("<span class='oe_i'>%s</span> %s",
1829                             group.icon, group.name),
1830                     filters: filters,
1831                     length: _(filters).chain().map(function (i) {
1832                         return i.filters.length; }).sum().value()
1833                 };
1834             }).value();
1835
1836         while (col2.length) {
1837             // col1 + group should be smaller than col2 + group
1838             if ((running_count + col2[0].length) <= (filters_count - running_count)) {
1839                 running_count += col2[0].length;
1840                 col1.push(col2.shift());
1841             } else {
1842                 break;
1843             }
1844         }
1845
1846         return $.when(
1847             this.render_column(col1, $('<div>').appendTo(this.$el)),
1848             this.render_column(col2, $('<div>').appendTo(this.$el)));
1849     },
1850     render_column: function (column, $el) {
1851         return $.when.apply(null, _(column).map(function (group) {
1852             $('<h3>').html(group.name).appendTo($el);
1853             return $.when.apply(null,
1854                 _(group.filters).invoke('appendTo', $el));
1855         }));
1856     }
1857 });
1858
1859 instance.web.search.Advanced = instance.web.search.Input.extend({
1860     template: 'SearchView.advanced',
1861     _in_drawer: true,
1862     start: function () {
1863         var self = this;
1864         this.$el
1865             .on('keypress keydown keyup', function (e) { e.stopPropagation(); })
1866             .on('click', 'h4', function () {
1867                 self.$el.toggleClass('oe_opened');
1868             }).on('click', 'button.oe_add_condition', function () {
1869                 self.append_proposition();
1870             }).on('submit', 'form', function (e) {
1871                 e.preventDefault();
1872                 self.commit_search();
1873             });
1874         return $.when(
1875             this._super(),
1876             new instance.web.Model(this.view.model).call('fields_get', {
1877                     context: this.view.dataset.context
1878                 }).done(function(data) {
1879                     self.fields = {
1880                         id: { string: 'ID', type: 'id' }
1881                     };
1882                     _.each(data, function(field_def, field_name) {
1883                         if (field_def.selectable !== false && field_name != 'id') {
1884                             self.fields[field_name] = field_def;
1885                         }
1886                     });
1887         })).done(function () {
1888             self.append_proposition();
1889         });
1890     },
1891     append_proposition: function () {
1892         var self = this;
1893         return (new instance.web.search.ExtendedSearchProposition(this, this.fields))
1894             .appendTo(this.$('ul')).done(function () {
1895                 self.$('button.oe_apply').prop('disabled', false);
1896             });
1897     },
1898     remove_proposition: function (prop) {
1899         // removing last proposition, disable apply button
1900         if (this.getChildren().length <= 1) {
1901             this.$('button.oe_apply').prop('disabled', true);
1902         }
1903         prop.destroy();
1904     },
1905     commit_search: function () {
1906         // Get domain sections from all propositions
1907         var children = this.getChildren();
1908         var propositions = _.invoke(children, 'get_proposition');
1909         var domain = _(propositions).pluck('value');
1910         for (var i = domain.length; --i;) {
1911             domain.unshift('|');
1912         }
1913
1914         this.view.query.add({
1915             category: _t("Advanced"),
1916             values: propositions,
1917             field: {
1918                 get_context: function () { },
1919                 get_domain: function () { return domain;},
1920                 get_groupby: function () { }
1921             }
1922         });
1923
1924         // remove all propositions
1925         _.invoke(children, 'destroy');
1926         // add new empty proposition
1927         this.append_proposition();
1928         // TODO: API on searchview
1929         this.view.$el.removeClass('oe_searchview_open_drawer');
1930     }
1931 });
1932
1933 instance.web.search.ExtendedSearchProposition = instance.web.Widget.extend(/** @lends instance.web.search.ExtendedSearchProposition# */{
1934     template: 'SearchView.extended_search.proposition',
1935     events: {
1936         'change .searchview_extended_prop_field': 'changed',
1937         'change .searchview_extended_prop_op': 'operator_changed',
1938         'click .searchview_extended_delete_prop': function (e) {
1939             e.stopPropagation();
1940             this.getParent().remove_proposition(this);
1941         }
1942     },
1943     /**
1944      * @constructs instance.web.search.ExtendedSearchProposition
1945      * @extends instance.web.Widget
1946      *
1947      * @param parent
1948      * @param fields
1949      */
1950     init: function (parent, fields) {
1951         this._super(parent);
1952         this.fields = _(fields).chain()
1953             .map(function(val, key) { return _.extend({}, val, {'name': key}); })
1954             .filter(function (field) { return !field.deprecated && (field.store === void 0 || field.store || field.fnct_search); })
1955             .sortBy(function(field) {return field.string;})
1956             .value();
1957         this.attrs = {_: _, fields: this.fields, selected: null};
1958         this.value = null;
1959     },
1960     start: function () {
1961         return this._super().done(this.proxy('changed'));
1962     },
1963     changed: function() {
1964         var nval = this.$(".searchview_extended_prop_field").val();
1965         if(this.attrs.selected === null || this.attrs.selected === undefined || nval != this.attrs.selected.name) {
1966             this.select_field(_.detect(this.fields, function(x) {return x.name == nval;}));
1967         }
1968     },
1969     operator_changed: function (e) {
1970         var $value = this.$('.searchview_extended_prop_value');
1971         switch ($(e.target).val()) {
1972         case '∃':
1973         case '∄':
1974             $value.hide();
1975             break;
1976         default:
1977             $value.show();
1978         }
1979     },
1980     /**
1981      * Selects the provided field object
1982      *
1983      * @param field a field descriptor object (as returned by fields_get, augmented by the field name)
1984      */
1985     select_field: function(field) {
1986         var self = this;
1987         if(this.attrs.selected !== null && this.attrs.selected !== undefined) {
1988             this.value.destroy();
1989             this.value = null;
1990             this.$('.searchview_extended_prop_op').html('');
1991         }
1992         this.attrs.selected = field;
1993         if(field === null || field === undefined) {
1994             return;
1995         }
1996
1997         var type = field.type;
1998         var Field = instance.web.search.custom_filters.get_object(type);
1999         if(!Field) {
2000             Field = instance.web.search.custom_filters.get_object("char");
2001         }
2002         this.value = new Field(this, field);
2003         _.each(this.value.operators, function(operator) {
2004             $('<option>', {value: operator.value})
2005                 .text(String(operator.text))
2006                 .appendTo(self.$('.searchview_extended_prop_op'));
2007         });
2008         var $value_loc = this.$('.searchview_extended_prop_value').show().empty();
2009         this.value.appendTo($value_loc);
2010
2011     },
2012     get_proposition: function() {
2013         if (this.attrs.selected === null || this.attrs.selected === undefined)
2014             return null;
2015         var field = this.attrs.selected;
2016         var op_select = this.$('.searchview_extended_prop_op')[0];
2017         var operator = op_select.options[op_select.selectedIndex];
2018
2019         return {
2020             label: this.value.get_label(field, operator),
2021             value: this.value.get_domain(field, operator),
2022         };
2023     }
2024 });
2025
2026 instance.web.search.ExtendedSearchProposition.Field = instance.web.Widget.extend({
2027     init: function (parent, field) {
2028         this._super(parent);
2029         this.field = field;
2030     },
2031     get_label: function (field, operator) {
2032         var format;
2033         switch (operator.value) {
2034         case '∃': case '∄': format = _t('%(field)s %(operator)s'); break;
2035         default: format = _t('%(field)s %(operator)s "%(value)s"'); break;
2036         }
2037         return this.format_label(format, field, operator);
2038     },
2039     format_label: function (format, field, operator) {
2040         return _.str.sprintf(format, {
2041             field: field.string,
2042             // According to spec, HTMLOptionElement#label should return
2043             // HTMLOptionElement#text when not defined/empty, but it does
2044             // not in older Webkit (between Safari 5.1.5 and Chrome 17) and
2045             // Gecko (pre Firefox 7) browsers, so we need a manual fallback
2046             // for those
2047             operator: operator.label || operator.text,
2048             value: this
2049         });
2050     },
2051     get_domain: function (field, operator) {
2052         switch (operator.value) {
2053         case '∃': return this.make_domain(field.name, '!=', false);
2054         case '∄': return this.make_domain(field.name, '=', false);
2055         default: return this.make_domain(
2056             field.name, operator.value, this.get_value());
2057         }
2058     },
2059     make_domain: function (field, operator, value) {
2060         return [field, operator, value];
2061     },
2062     /**
2063      * Returns a human-readable version of the value, in case the "logical"
2064      * and the "semantic" values of a field differ (as for selection fields,
2065      * for instance).
2066      *
2067      * The default implementation simply returns the value itself.
2068      *
2069      * @return {String} human-readable version of the value
2070      */
2071     toString: function () {
2072         return this.get_value();
2073     }
2074 });
2075 instance.web.search.ExtendedSearchProposition.Char = instance.web.search.ExtendedSearchProposition.Field.extend({
2076     template: 'SearchView.extended_search.proposition.char',
2077     operators: [
2078         {value: "ilike", text: _lt("contains")},
2079         {value: "not ilike", text: _lt("doesn't contain")},
2080         {value: "=", text: _lt("is equal to")},
2081         {value: "!=", text: _lt("is not equal to")},
2082         {value: "∃", text: _lt("is set")},
2083         {value: "∄", text: _lt("is not set")}
2084     ],
2085     get_value: function() {
2086         return this.$el.val();
2087     }
2088 });
2089 instance.web.search.ExtendedSearchProposition.DateTime = instance.web.search.ExtendedSearchProposition.Field.extend({
2090     template: 'SearchView.extended_search.proposition.empty',
2091     operators: [
2092         {value: "=", text: _lt("is equal to")},
2093         {value: "!=", text: _lt("is not equal to")},
2094         {value: ">", text: _lt("greater than")},
2095         {value: "<", text: _lt("less than")},
2096         {value: ">=", text: _lt("greater or equal than")},
2097         {value: "<=", text: _lt("less or equal than")},
2098         {value: "∃", text: _lt("is set")},
2099         {value: "∄", text: _lt("is not set")}
2100     ],
2101     /**
2102      * Date widgets live in view_form which is not yet loaded when this is
2103      * initialized -_-
2104      */
2105     widget: function () { return instance.web.DateTimeWidget; },
2106     get_value: function() {
2107         return this.datewidget.get_value();
2108     },
2109     toString: function () {
2110         return instance.web.format_value(this.get_value(), { type:"datetime" });
2111     },
2112     start: function() {
2113         var ready = this._super();
2114         this.datewidget = new (this.widget())(this);
2115         this.datewidget.appendTo(this.$el);
2116         return ready;
2117     }
2118 });
2119 instance.web.search.ExtendedSearchProposition.Date = instance.web.search.ExtendedSearchProposition.DateTime.extend({
2120     widget: function () { return instance.web.DateWidget; },
2121     toString: function () {
2122         return instance.web.format_value(this.get_value(), { type:"date" });
2123     }
2124 });
2125 instance.web.search.ExtendedSearchProposition.Integer = instance.web.search.ExtendedSearchProposition.Field.extend({
2126     template: 'SearchView.extended_search.proposition.integer',
2127     operators: [
2128         {value: "=", text: _lt("is equal to")},
2129         {value: "!=", text: _lt("is not equal to")},
2130         {value: ">", text: _lt("greater than")},
2131         {value: "<", text: _lt("less than")},
2132         {value: ">=", text: _lt("greater or equal than")},
2133         {value: "<=", text: _lt("less or equal than")},
2134         {value: "∃", text: _lt("is set")},
2135         {value: "∄", text: _lt("is not set")}
2136     ],
2137     toString: function () {
2138         return this.$el.val();
2139     },
2140     get_value: function() {
2141         try {
2142             var val =this.$el.val();
2143             return instance.web.parse_value(val === "" ? 0 : val, {'widget': 'integer'});
2144         } catch (e) {
2145             return "";
2146         }
2147     }
2148 });
2149 instance.web.search.ExtendedSearchProposition.Id = instance.web.search.ExtendedSearchProposition.Integer.extend({
2150     operators: [{value: "=", text: _lt("is")}]
2151 });
2152 instance.web.search.ExtendedSearchProposition.Float = instance.web.search.ExtendedSearchProposition.Field.extend({
2153     template: 'SearchView.extended_search.proposition.float',
2154     operators: [
2155         {value: "=", text: _lt("is equal to")},
2156         {value: "!=", text: _lt("is not equal to")},
2157         {value: ">", text: _lt("greater than")},
2158         {value: "<", text: _lt("less than")},
2159         {value: ">=", text: _lt("greater or equal than")},
2160         {value: "<=", text: _lt("less or equal than")},
2161         {value: "∃", text: _lt("is set")},
2162         {value: "∄", text: _lt("is not set")}
2163     ],
2164     toString: function () {
2165         return this.$el.val();
2166     },
2167     get_value: function() {
2168         try {
2169             var val =this.$el.val();
2170             return instance.web.parse_value(val === "" ? 0.0 : val, {'widget': 'float'});
2171         } catch (e) {
2172             return "";
2173         }
2174     }
2175 });
2176 instance.web.search.ExtendedSearchProposition.Selection = instance.web.search.ExtendedSearchProposition.Field.extend({
2177     template: 'SearchView.extended_search.proposition.selection',
2178     operators: [
2179         {value: "=", text: _lt("is")},
2180         {value: "!=", text: _lt("is not")},
2181         {value: "∃", text: _lt("is set")},
2182         {value: "∄", text: _lt("is not set")}
2183     ],
2184     toString: function () {
2185         var select = this.$el[0];
2186         var option = select.options[select.selectedIndex];
2187         return option.label || option.text;
2188     },
2189     get_value: function() {
2190         return this.$el.val();
2191     }
2192 });
2193 instance.web.search.ExtendedSearchProposition.Boolean = instance.web.search.ExtendedSearchProposition.Field.extend({
2194     template: 'SearchView.extended_search.proposition.empty',
2195     operators: [
2196         {value: "=", text: _lt("is true")},
2197         {value: "!=", text: _lt("is false")}
2198     ],
2199     get_label: function (field, operator) {
2200         return this.format_label(
2201             _t('%(field)s %(operator)s'), field, operator);
2202     },
2203     get_value: function() {
2204         return true;
2205     }
2206 });
2207
2208 instance.web.search.custom_filters = new instance.web.Registry({
2209     'char': 'instance.web.search.ExtendedSearchProposition.Char',
2210     'text': 'instance.web.search.ExtendedSearchProposition.Char',
2211     'one2many': 'instance.web.search.ExtendedSearchProposition.Char',
2212     'many2one': 'instance.web.search.ExtendedSearchProposition.Char',
2213     'many2many': 'instance.web.search.ExtendedSearchProposition.Char',
2214
2215     'datetime': 'instance.web.search.ExtendedSearchProposition.DateTime',
2216     'date': 'instance.web.search.ExtendedSearchProposition.Date',
2217     'integer': 'instance.web.search.ExtendedSearchProposition.Integer',
2218     'float': 'instance.web.search.ExtendedSearchProposition.Float',
2219     'boolean': 'instance.web.search.ExtendedSearchProposition.Boolean',
2220     'selection': 'instance.web.search.ExtendedSearchProposition.Selection',
2221
2222     'id': 'instance.web.search.ExtendedSearchProposition.Id'
2223 });
2224
2225 })();
2226
2227 // vim:et fdc=0 fdl=0 foldnestmax=3 fdm=syntax: