[FIX] fields: inherited fields get their attribute 'state' from their base field
[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 (_, options) {
33             this.trigger('change', this, options);
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             if (this.drawer) 
344                 this.drawer.toggle();
345         },
346         'keydown .oe_searchview_input, .oe_searchview_facet': function (e) {
347             switch(e.which) {
348             case $.ui.keyCode.LEFT:
349                 this.focusPreceding(e.target);
350                 e.preventDefault();
351                 break;
352             case $.ui.keyCode.RIGHT:
353                 if (!this.autocomplete.is_expandable()) {
354                     this.focusFollowing(e.target);
355                 }
356                 e.preventDefault();
357                 break;
358             }
359         },
360         'autocompleteopen': function () {
361             this.$el.autocomplete('widget').css('z-index', 9999);
362         },
363     },
364     /**
365      * @constructs instance.web.SearchView
366      * @extends instance.web.Widget
367      *
368      * @param parent
369      * @param dataset
370      * @param view_id
371      * @param defaults
372      * @param {Object} [options]
373      * @param {Boolean} [options.hidden=false] hide the search view
374      * @param {Boolean} [options.disable_custom_filters=false] do not load custom filters from ir.filters
375      */
376     init: function(parent, dataset, view_id, defaults, options) {
377         this.options = _.defaults(options || {}, {
378             hidden: false,
379             disable_custom_filters: false,
380         });
381         this._super(parent);
382         this.dataset = dataset;
383         this.model = dataset.model;
384         this.view_id = view_id;
385
386         this.defaults = defaults || {};
387         this.has_defaults = !_.isEmpty(this.defaults);
388
389         this.headless = this.options.hidden && !this.has_defaults;
390
391         this.input_subviews = [];
392         this.view_manager = null;
393         this.$view_manager_header = null;
394
395         this.ready = $.Deferred();
396         this.drawer_ready = $.Deferred();
397         this.fields_view_get = $.Deferred();
398         this.drawer = new instance.web.SearchViewDrawer(parent, this);
399
400     },
401     start: function() {
402         var self = this;
403         var p = this._super();
404
405         this.$view_manager_header = this.$el.parents(".oe_view_manager_header").first();
406
407         this.setup_global_completion();
408         this.query = new my.SearchQuery()
409                 .on('add change reset remove', this.proxy('do_search'))
410                 .on('change', this.proxy('renderChangedFacets'))
411                 .on('add reset remove', this.proxy('renderFacets'));
412
413         if (this.options.hidden) {
414             this.$el.hide();
415         }
416         if (this.headless) {
417             this.ready.resolve();
418         } else {
419             var load_view = instance.web.fields_view_get({
420                 model: this.dataset._model,
421                 view_id: this.view_id,
422                 view_type: 'search',
423                 context: this.dataset.get_context(),
424             });
425
426             this.alive($.when(load_view)).then(function (r) {
427                 self.fields_view_get.resolve(r);
428                 return self.search_view_loaded(r);
429             }).fail(function () {
430                 self.ready.reject.apply(null, arguments);
431             });
432         }
433
434         var view_manager = this.getParent();
435         while (!(view_manager instanceof instance.web.ViewManager) &&
436                 view_manager && view_manager.getParent) {
437             view_manager = view_manager.getParent();
438         }
439
440         if (view_manager) {
441             this.view_manager = view_manager;
442             view_manager.on('switch_mode', this, function (e) {
443                 self.drawer.toggle(e === 'graph');
444             });
445         }
446         return $.when(p, this.ready);
447     },
448
449     set_drawer: function (drawer) {
450         this.drawer = drawer;
451     },
452
453     show: function () {
454         this.$el.show();
455     },
456     hide: function () {
457         this.$el.hide();
458     },
459
460     subviewForRoot: function (subview_root) {
461         return _(this.input_subviews).detect(function (subview) {
462             return subview.$el[0] === subview_root;
463         });
464     },
465     siblingSubview: function (subview, direction, wrap_around) {
466         var index = _(this.input_subviews).indexOf(subview) + direction;
467         if (wrap_around && index < 0) {
468             index = this.input_subviews.length - 1;
469         } else if (wrap_around && index >= this.input_subviews.length) {
470             index = 0;
471         }
472         return this.input_subviews[index];
473     },
474     focusPreceding: function (subview_root) {
475         return this.siblingSubview(
476             this.subviewForRoot(subview_root), -1, true)
477                 .$el.focus();
478     },
479     focusFollowing: function (subview_root) {
480         return this.siblingSubview(
481             this.subviewForRoot(subview_root), +1, true)
482                 .$el.focus();
483     },
484
485     /**
486      * Sets up search view's view-wide auto-completion widget
487      */
488     setup_global_completion: function () {
489         var self = this;
490         this.autocomplete = new instance.web.search.AutoComplete(this, {
491             source: this.proxy('complete_global_search'),
492             select: this.proxy('select_completion'),
493             delay: 0,
494             get_search_string: function () {
495                 return self.$('div.oe_searchview_input').text();
496             },
497             width: this.$el.width(),
498         });
499         this.autocomplete.appendTo(this.$el);
500     },
501     /**
502      * Provide auto-completion result for req.term (an array to `resp`)
503      *
504      * @param {Object} req request to complete
505      * @param {String} req.term searched term to complete
506      * @param {Function} resp response callback
507      */
508     complete_global_search:  function (req, resp) {
509         $.when.apply(null, _(this.drawer.inputs).chain()
510             .filter(function (input) { return input.visible(); })
511             .invoke('complete', req.term)
512             .value()).then(function () {
513                 resp(_(arguments).chain()
514                     .compact()
515                     .flatten(true)
516                     .value());
517                 });
518     },
519
520     /**
521      * Action to perform in case of selection: create a facet (model)
522      * and add it to the search collection
523      *
524      * @param {Object} e selection event, preventDefault to avoid setting value on object
525      * @param {Object} ui selection information
526      * @param {Object} ui.item selected completion item
527      */
528     select_completion: function (e, ui) {
529         e.preventDefault();
530
531         var input_index = _(this.input_subviews).indexOf(
532             this.subviewForRoot(
533                 this.$('div.oe_searchview_input:focus')[0]));
534         this.query.add(ui.item.facet, {at: input_index / 2});
535     },
536     childFocused: function () {
537         this.$el.addClass('oe_focused');
538     },
539     childBlurred: function () {
540         var val = this.$el.val();
541         this.$el.val('');
542         this.$el.removeClass('oe_focused')
543                      .trigger('blur');
544         this.autocomplete.close();
545     },
546     /**
547      * Call the renderFacets method with the correct arguments.
548      * This is due to the fact that change events are called with two arguments
549      * (model, options) while add, reset and remove events are called with
550      * (collection, model, options) as arguments
551      */
552     renderChangedFacets: function (model, options) {
553         this.renderFacets(undefined, model, options);
554     },
555     /**
556      * @param {openerp.web.search.SearchQuery | undefined} Undefined if event is change
557      * @param {openerp.web.search.Facet} 
558      * @param {Object} [options]
559      */
560     renderFacets: function (collection, model, options) {
561         var self = this;
562         var started = [];
563         var $e = this.$('div.oe_searchview_facets');
564         _.invoke(this.input_subviews, 'destroy');
565         this.input_subviews = [];
566
567         var i = new my.InputView(this);
568         started.push(i.appendTo($e));
569         this.input_subviews.push(i);
570         this.query.each(function (facet) {
571             var f = new my.FacetView(this, facet);
572             started.push(f.appendTo($e));
573             self.input_subviews.push(f);
574
575             var i = new my.InputView(this);
576             started.push(i.appendTo($e));
577             self.input_subviews.push(i);
578         }, this);
579         _.each(this.input_subviews, function (childView) {
580             childView.on('focused', self, self.proxy('childFocused'));
581             childView.on('blurred', self, self.proxy('childBlurred'));
582         });
583
584         $.when.apply(null, started).then(function () {
585             if (options && options.focus_input === false) return;
586             var input_to_focus;
587             // options.at: facet inserted at given index, focus next input
588             // otherwise just focus last input
589             if (!options || typeof options.at !== 'number') {
590                 input_to_focus = _.last(self.input_subviews);
591             } else {
592                 input_to_focus = self.input_subviews[(options.at + 1) * 2];
593             }
594             input_to_focus.$el.focus();
595         });
596     },
597
598     search_view_loaded: function(data) {
599         var self = this;
600         this.fields_view = data;
601         this.view_id = this.view_id || data.view_id;
602         if (data.type !== 'search' ||
603             data.arch.tag !== 'search') {
604                 throw new Error(_.str.sprintf(
605                     "Got non-search view after asking for a search view: type %s, arch root %s",
606                     data.type, data.arch.tag));
607         }
608
609         return this.drawer_ready
610             .then(this.proxy('setup_default_query'))
611             .then(function () { 
612                 self.trigger("search_view_loaded", data);
613                 self.ready.resolve();
614             });
615     },
616     setup_default_query: function () {
617         // Hacky implementation of CustomFilters#facet_for_defaults ensure
618         // CustomFilters will be ready (and CustomFilters#filters will be
619         // correctly filled) by the time this method executes.
620         var custom_filters = this.drawer.custom_filters.filters;
621         if (!this.options.disable_custom_filters && !_(custom_filters).isEmpty()) {
622             // Check for any is_default custom filter
623             var personal_filter = _(custom_filters).find(function (filter) {
624                 return filter.user_id && filter.is_default;
625             });
626             if (personal_filter) {
627                 this.drawer.custom_filters.toggle_filter(personal_filter, true);
628                 return;
629             }
630
631             var global_filter = _(custom_filters).find(function (filter) {
632                 return !filter.user_id && filter.is_default;
633             });
634             if (global_filter) {
635                 this.drawer.custom_filters.toggle_filter(global_filter, true);
636                 return;
637             }
638         }
639         // No custom filter, or no is_default custom filter, apply view defaults
640         this.query.reset(_(arguments).compact(), {preventSearch: true});
641     },
642     /**
643      * Extract search data from the view's facets.
644      *
645      * Result is an object with 4 (own) properties:
646      *
647      * errors
648      *     An array of any error generated during data validation and
649      *     extraction, contains the validation error objects
650      * domains
651      *     Array of domains
652      * contexts
653      *     Array of contexts
654      * groupbys
655      *     Array of domains, in groupby order rather than view order
656      *
657      * @return {Object}
658      */
659     build_search_data: function () {
660         var domains = [], contexts = [], groupbys = [], errors = [];
661
662         this.query.each(function (facet) {
663             var field = facet.get('field');
664             try {
665                 var domain = field.get_domain(facet);
666                 if (domain) {
667                     domains.push(domain);
668                 }
669                 var context = field.get_context(facet);
670                 if (context) {
671                     contexts.push(context);
672                 }
673                 var group_by = field.get_groupby(facet);
674                 if (group_by) {
675                     groupbys.push.apply(groupbys, group_by);
676                 }
677             } catch (e) {
678                 if (e instanceof instance.web.search.Invalid) {
679                     errors.push(e);
680                 } else {
681                     throw e;
682                 }
683             }
684         });
685         return {
686             domains: domains,
687             contexts: contexts,
688             groupbys: groupbys,
689             errors: errors
690         };
691     }, 
692     /**
693      * Performs the search view collection of widget data.
694      *
695      * If the collection went well (all fields are valid), then triggers
696      * :js:func:`instance.web.SearchView.on_search`.
697      *
698      * If at least one field failed its validation, triggers
699      * :js:func:`instance.web.SearchView.on_invalid` instead.
700      *
701      * @param [_query]
702      * @param {Object} [options]
703      */
704     do_search: function (_query, options) {
705         if (options && options.preventSearch) {
706             return;
707         }
708         var search = this.build_search_data();
709         if (!_.isEmpty(search.errors)) {
710             this.on_invalid(search.errors);
711             return;
712         }
713         this.trigger('search_data', search.domains, search.contexts, search.groupbys);
714     },
715     /**
716      * Triggered after the SearchView has collected all relevant domains and
717      * contexts.
718      *
719      * It is provided with an Array of domains and an Array of contexts, which
720      * may or may not be evaluated (each item can be either a valid domain or
721      * context, or a string to evaluate in order in the sequence)
722      *
723      * It is also passed an array of contexts used for group_by (they are in
724      * the correct order for group_by evaluation, which contexts may not be)
725      *
726      * @event
727      * @param {Array} domains an array of literal domains or domain references
728      * @param {Array} contexts an array of literal contexts or context refs
729      * @param {Array} groupbys ordered contexts which may or may not have group_by keys
730      */
731     /**
732      * Triggered after a validation error in the SearchView fields.
733      *
734      * Error objects have three keys:
735      * * ``field`` is the name of the invalid field
736      * * ``value`` is the invalid value
737      * * ``message`` is the (in)validation message provided by the field
738      *
739      * @event
740      * @param {Array} errors a never-empty array of error objects
741      */
742     on_invalid: function (errors) {
743         this.do_notify(_t("Invalid Search"), _t("triggered from search view"));
744         this.trigger('invalid_search', errors);
745     },
746
747     // The method appendTo is overwrited to be able to insert the drawer anywhere
748     appendTo: function ($searchview_parent, $searchview_drawer_node) {
749         var $searchview_drawer_node = $searchview_drawer_node || $searchview_parent;
750
751         return $.when(
752             this._super($searchview_parent),
753             this.drawer.appendTo($searchview_drawer_node)
754         );
755     },
756
757     destroy: function () {
758         this.drawer.destroy();
759         this.getParent().destroy.call(this);
760     }
761 });
762
763 instance.web.SearchViewDrawer = instance.web.Widget.extend({
764     template: "SearchViewDrawer",
765
766     init: function(parent, searchview) {
767         this._super(parent);
768         this.searchview = searchview;
769         this.searchview.set_drawer(this);
770         this.ready = searchview.drawer_ready;
771         this.controls = [];
772         this.inputs = [];
773     },
774
775     toggle: function (visibility) {
776         this.$el.toggle(visibility);
777         var $view_manager_body = this.$el.closest('.oe_view_manager_body');
778         if ($view_manager_body.length) {
779             $view_manager_body.scrollTop(0);
780         }
781     },
782
783     start: function() {
784         var self = this;
785         if (this.searchview.headless) return $.when(this._super(), this.searchview.ready);
786         var filters_ready = this.searchview.fields_view_get
787                                 .then(this.proxy('prepare_filters'));
788         return $.when(this._super(), filters_ready).then(function () {
789             var defaults = arguments[1][0];
790             self.ready.resolve.apply(null, defaults);
791         });
792     },
793     prepare_filters: function (data) {
794         this.make_widgets(
795             data['arch'].children,
796             data.fields);
797
798         this.add_common_inputs();
799
800         // build drawer
801         var in_drawer = this.select_for_drawer();
802
803         var $first_col = this.$(".col-md-7"),
804             $snd_col = this.$(".col-md-5");
805
806         var add_custom_filters = in_drawer[0].appendTo($first_col),
807             add_filters = in_drawer[1].appendTo($first_col),
808             add_rest = $.when.apply(null, _(in_drawer.slice(2)).invoke('appendTo', $snd_col)),
809             defaults_fetched = $.when.apply(null, _(this.inputs).invoke(
810                 'facet_for_defaults', this.searchview.defaults));
811
812         return $.when(defaults_fetched, add_custom_filters, add_filters, add_rest);
813     },
814     /**
815      * Sets up thingie where all the mess is put?
816      */
817     select_for_drawer: function () {
818         return _(this.inputs).filter(function (input) {
819             return input.in_drawer();
820         });
821     },
822
823     /**
824      * Builds a list of widget rows (each row is an array of widgets)
825      *
826      * @param {Array} items a list of nodes to convert to widgets
827      * @param {Object} fields a mapping of field names to (ORM) field attributes
828      * @param {Object} [group] group to put the new controls in
829      */
830     make_widgets: function (items, fields, group) {
831         if (!group) {
832             group = new instance.web.search.Group(
833                 this, 'q', {attrs: {string: _t("Filters")}});
834         }
835         var self = this;
836         var filters = [];
837         _.each(items, function (item) {
838             if (filters.length && item.tag !== 'filter') {
839                 group.push(new instance.web.search.FilterGroup(filters, group));
840                 filters = [];
841             }
842
843             switch (item.tag) {
844             case 'separator': case 'newline':
845                 break;
846             case 'filter':
847                 filters.push(new instance.web.search.Filter(item, group));
848                 break;
849             case 'group':
850                 self.add_separator();
851                 self.make_widgets(item.children, fields,
852                     new instance.web.search.Group(group, 'w', item));
853                 self.add_separator();
854                 break;
855             case 'field':
856                 var field = this.make_field(
857                     item, fields[item['attrs'].name], group);
858                 group.push(field);
859                 // filters
860                 self.make_widgets(item.children, fields, group);
861                 break;
862             }
863         }, this);
864
865         if (filters.length) {
866             group.push(new instance.web.search.FilterGroup(filters, this));
867         }
868     },
869
870     add_separator: function () {
871         if (!(_.last(this.inputs) instanceof instance.web.search.Separator))
872             new instance.web.search.Separator(this);
873     },
874     /**
875      * Creates a field for the provided field descriptor item (which comes
876      * from fields_view_get)
877      *
878      * @param {Object} item fields_view_get node for the field
879      * @param {Object} field fields_get result for the field
880      * @param {Object} [parent]
881      * @returns instance.web.search.Field
882      */
883     make_field: function (item, field, parent) {
884         // M2O combined with selection widget is pointless and broken in search views,
885         // but has been used in the past for unsupported hacks -> ignore it
886         if (field.type === "many2one" && item.attrs.widget === "selection"){
887             item.attrs.widget = undefined;
888         }
889         var obj = instance.web.search.fields.get_any( [item.attrs.widget, field.type]);
890         if(obj) {
891             return new (obj) (item, field, parent || this);
892         } else {
893             console.group('Unknown field type ' + field.type);
894             console.error('View node', item);
895             console.info('View field', field);
896             console.info('In view', this);
897             console.groupEnd();
898             return null;
899         }
900     },
901
902     add_common_inputs: function() {
903         // add custom filters to this.inputs
904         this.custom_filters = new instance.web.search.CustomFilters(this);
905         // add Filters to this.inputs, need view.controls filled
906         (new instance.web.search.Filters(this));
907         (new instance.web.search.SaveFilter(this, this.custom_filters));
908         // add Advanced to this.inputs
909         (new instance.web.search.Advanced(this));
910     },
911
912 });
913
914 /**
915  * Registry of search fields, called by :js:class:`instance.web.SearchView` to
916  * find and instantiate its field widgets.
917  */
918 instance.web.search.fields = new instance.web.Registry({
919     'char': 'instance.web.search.CharField',
920     'text': 'instance.web.search.CharField',
921     'html': 'instance.web.search.CharField',
922     'boolean': 'instance.web.search.BooleanField',
923     'integer': 'instance.web.search.IntegerField',
924     'id': 'instance.web.search.IntegerField',
925     'float': 'instance.web.search.FloatField',
926     'selection': 'instance.web.search.SelectionField',
927     'datetime': 'instance.web.search.DateTimeField',
928     'date': 'instance.web.search.DateField',
929     'many2one': 'instance.web.search.ManyToOneField',
930     'many2many': 'instance.web.search.CharField',
931     'one2many': 'instance.web.search.CharField'
932 });
933 instance.web.search.Invalid = instance.web.Class.extend( /** @lends instance.web.search.Invalid# */{
934     /**
935      * Exception thrown by search widgets when they hold invalid values,
936      * which they can not return when asked.
937      *
938      * @constructs instance.web.search.Invalid
939      * @extends instance.web.Class
940      *
941      * @param field the name of the field holding an invalid value
942      * @param value the invalid value
943      * @param message validation failure message
944      */
945     init: function (field, value, message) {
946         this.field = field;
947         this.value = value;
948         this.message = message;
949     },
950     toString: function () {
951         return _.str.sprintf(
952             _t("Incorrect value for field %(fieldname)s: [%(value)s] is %(message)s"),
953             {fieldname: this.field, value: this.value, message: this.message}
954         );
955     }
956 });
957 instance.web.search.Widget = instance.web.Widget.extend( /** @lends instance.web.search.Widget# */{
958     template: null,
959     /**
960      * Root class of all search widgets
961      *
962      * @constructs instance.web.search.Widget
963      * @extends instance.web.Widget
964      *
965      * @param parent parent of this widget
966      */
967     init: function (parent) {
968         this._super(parent);
969         var ancestor = parent;
970         do {
971             this.drawer = ancestor;
972         } while (!(ancestor instanceof instance.web.SearchViewDrawer)
973                && (ancestor = (ancestor.getParent && ancestor.getParent())));
974         this.view = this.drawer.searchview || this.drawer;
975     }
976 });
977
978 instance.web.search.add_expand_listener = function($root) {
979     $root.find('a.searchview_group_string').click(function (e) {
980         $root.toggleClass('folded expanded');
981         e.stopPropagation();
982         e.preventDefault();
983     });
984 };
985 instance.web.search.Group = instance.web.search.Widget.extend({
986     init: function (parent, icon, node) {
987         this._super(parent);
988         var attrs = node.attrs;
989         this.modifiers = attrs.modifiers =
990             attrs.modifiers ? JSON.parse(attrs.modifiers) : {};
991         this.attrs = attrs;
992         this.icon = icon;
993         this.name = attrs.string;
994         this.children = [];
995
996         this.drawer.controls.push(this);
997     },
998     push: function (input) {
999         this.children.push(input);
1000     },
1001     visible: function () {
1002         return !this.modifiers.invisible;
1003     },
1004 });
1005
1006 instance.web.search.Input = instance.web.search.Widget.extend( /** @lends instance.web.search.Input# */{
1007     _in_drawer: false,
1008     /**
1009      * @constructs instance.web.search.Input
1010      * @extends instance.web.search.Widget
1011      *
1012      * @param parent
1013      */
1014     init: function (parent) {
1015         this._super(parent);
1016         this.load_attrs({});
1017         this.drawer.inputs.push(this);
1018     },
1019     /**
1020      * Fetch auto-completion values for the widget.
1021      *
1022      * The completion values should be an array of objects with keys category,
1023      * label, value prefixed with an object with keys type=section and label
1024      *
1025      * @param {String} value value to complete
1026      * @returns {jQuery.Deferred<null|Array>}
1027      */
1028     complete: function (value) {
1029         return $.when(null);
1030     },
1031     /**
1032      * Returns a Facet instance for the provided defaults if they apply to
1033      * this widget, or null if they don't.
1034      *
1035      * This default implementation will try calling
1036      * :js:func:`instance.web.search.Input#facet_for` if the widget's name
1037      * matches the input key
1038      *
1039      * @param {Object} defaults
1040      * @returns {jQuery.Deferred<null|Object>}
1041      */
1042     facet_for_defaults: function (defaults) {
1043         if (!this.attrs ||
1044             !(this.attrs.name in defaults && defaults[this.attrs.name])) {
1045             return $.when(null);
1046         }
1047         return this.facet_for(defaults[this.attrs.name]);
1048     },
1049     in_drawer: function () {
1050         return !!this._in_drawer;
1051     },
1052     get_context: function () {
1053         throw new Error(
1054             "get_context not implemented for widget " + this.attrs.type);
1055     },
1056     get_groupby: function () {
1057         throw new Error(
1058             "get_groupby not implemented for widget " + this.attrs.type);
1059     },
1060     get_domain: function () {
1061         throw new Error(
1062             "get_domain not implemented for widget " + this.attrs.type);
1063     },
1064     load_attrs: function (attrs) {
1065         attrs.modifiers = attrs.modifiers ? JSON.parse(attrs.modifiers) : {};
1066         this.attrs = attrs;
1067     },
1068     /**
1069      * Returns whether the input is "visible". The default behavior is to
1070      * query the ``modifiers.invisible`` flag on the input's description or
1071      * view node.
1072      *
1073      * @returns {Boolean}
1074      */
1075     visible: function () {
1076         if (this.attrs.modifiers.invisible) {
1077             return false;
1078         }
1079         var parent = this;
1080         while ((parent = parent.getParent()) &&
1081                (   (parent instanceof instance.web.search.Group)
1082                 || (parent instanceof instance.web.search.Input))) {
1083             if (!parent.visible()) {
1084                 return false;
1085             }
1086         }
1087         return true;
1088     },
1089 });
1090 instance.web.search.FilterGroup = instance.web.search.Input.extend(/** @lends instance.web.search.FilterGroup# */{
1091     template: 'SearchView.filters',
1092     icon: 'q',
1093     completion_label: _lt("Filter on: %s"),
1094     /**
1095      * Inclusive group of filters, creates a continuous "button" with clickable
1096      * sections (the normal display for filters is to be a self-contained button)
1097      *
1098      * @constructs instance.web.search.FilterGroup
1099      * @extends instance.web.search.Input
1100      *
1101      * @param {Array<instance.web.search.Filter>} filters elements of the group
1102      * @param {instance.web.SearchView} parent parent in which the filters are contained
1103      */
1104     init: function (filters, parent) {
1105         // If all filters are group_by and we're not initializing a GroupbyGroup,
1106         // create a GroupbyGroup instead of the current FilterGroup
1107         if (!(this instanceof instance.web.search.GroupbyGroup) &&
1108               _(filters).all(function (f) {
1109                   if (!f.attrs.context) { return false; }
1110                   var c = instance.web.pyeval.eval('context', f.attrs.context);
1111                   return !_.isEmpty(c.group_by);})) {
1112             return new instance.web.search.GroupbyGroup(filters, parent);
1113         }
1114         this._super(parent);
1115         this.filters = filters;
1116         this.view.query.on('add remove change reset', this.proxy('search_change'));
1117     },
1118     start: function () {
1119         this.$el.on('click', 'li', this.proxy('toggle_filter'));
1120         return $.when(null);
1121     },
1122     /**
1123      * Handles change of the search query: any of the group's filter which is
1124      * in the search query should be visually checked in the drawer
1125      */
1126     search_change: function () {
1127         var self = this;
1128         var $filters = this.$('> li').removeClass('badge');
1129         var facet = this.view.query.find(_.bind(this.match_facet, this));
1130         if (!facet) { return; }
1131         facet.values.each(function (v) {
1132             var i = _(self.filters).indexOf(v.get('value'));
1133             if (i === -1) { return; }
1134             $filters.filter(function () {
1135                 return Number($(this).data('index')) === i;
1136             }).addClass('badge');
1137         });
1138     },
1139     /**
1140      * Matches the group to a facet, in order to find if the group is
1141      * represented in the current search query
1142      */
1143     match_facet: function (facet) {
1144         return facet.get('field') === this;
1145     },
1146     make_facet: function (values) {
1147         return {
1148             category: _t("Filter"),
1149             icon: this.icon,
1150             values: values,
1151             field: this
1152         };
1153     },
1154     make_value: function (filter) {
1155         return {
1156             label: filter.attrs.string || filter.attrs.help || filter.attrs.name,
1157             value: filter
1158         };
1159     },
1160     facet_for_defaults: function (defaults) {
1161         var self = this;
1162         var fs = _(this.filters).chain()
1163             .filter(function (f) {
1164                 return f.attrs && f.attrs.name && !!defaults[f.attrs.name];
1165             }).map(function (f) {
1166                 return self.make_value(f);
1167             }).value();
1168         if (_.isEmpty(fs)) { return $.when(null); }
1169         return $.when(this.make_facet(fs));
1170     },
1171     /**
1172      * Fetches contexts for all enabled filters in the group
1173      *
1174      * @param {openerp.web.search.Facet} facet
1175      * @return {*} combined contexts of the enabled filters in this group
1176      */
1177     get_context: function (facet) {
1178         var contexts = facet.values.chain()
1179             .map(function (f) { return f.get('value').attrs.context; })
1180             .without('{}')
1181             .reject(_.isEmpty)
1182             .value();
1183
1184         if (!contexts.length) { return; }
1185         if (contexts.length === 1) { return contexts[0]; }
1186         return _.extend(new instance.web.CompoundContext(), {
1187             __contexts: contexts
1188         });
1189     },
1190     /**
1191      * Fetches group_by sequence for all enabled filters in the group
1192      *
1193      * @param {VS.model.SearchFacet} facet
1194      * @return {Array} enabled filters in this group
1195      */
1196     get_groupby: function (facet) {
1197         return  facet.values.chain()
1198             .map(function (f) { return f.get('value').attrs.context; })
1199             .without('{}')
1200             .reject(_.isEmpty)
1201             .value();
1202     },
1203     /**
1204      * Handles domains-fetching for all the filters within it: groups them.
1205      *
1206      * @param {VS.model.SearchFacet} facet
1207      * @return {*} combined domains of the enabled filters in this group
1208      */
1209     get_domain: function (facet) {
1210         var domains = facet.values.chain()
1211             .map(function (f) { return f.get('value').attrs.domain; })
1212             .without('[]')
1213             .reject(_.isEmpty)
1214             .value();
1215
1216         if (!domains.length) { return; }
1217         if (domains.length === 1) { return domains[0]; }
1218         for (var i=domains.length; --i;) {
1219             domains.unshift(['|']);
1220         }
1221         return _.extend(new instance.web.CompoundDomain(), {
1222             __domains: domains
1223         });
1224     },
1225     toggle_filter: function (e) {
1226         this.toggle(this.filters[Number($(e.target).data('index'))]);
1227     },
1228     toggle: function (filter) {
1229         this.view.query.toggle(this.make_facet([this.make_value(filter)]));
1230     },
1231     complete: function (item) {
1232         var self = this;
1233         item = item.toLowerCase();
1234         var facet_values = _(this.filters).chain()
1235             .filter(function (filter) { return filter.visible(); })
1236             .filter(function (filter) {
1237                 var at = {
1238                     string: filter.attrs.string || '',
1239                     help: filter.attrs.help || '',
1240                     name: filter.attrs.name || ''
1241                 };
1242                 var include = _.str.include;
1243                 return include(at.string.toLowerCase(), item)
1244                     || include(at.help.toLowerCase(), item)
1245                     || include(at.name.toLowerCase(), item);
1246             })
1247             .map(this.make_value)
1248             .value();
1249         if (_(facet_values).isEmpty()) { return $.when(null); }
1250         return $.when(_.map(facet_values, function (facet_value) {
1251             return {
1252                 label: _.str.sprintf(self.completion_label.toString(),
1253                                      _.escape(facet_value.label)),
1254                 facet: self.make_facet([facet_value])
1255             };
1256         }));
1257     }
1258 });
1259 instance.web.search.GroupbyGroup = instance.web.search.FilterGroup.extend({
1260     icon: 'w',
1261     completion_label: _lt("Group by: %s"),
1262     init: function (filters, parent) {
1263         this._super(filters, parent);
1264         // Not flanders: facet unicity is handled through the
1265         // (category, field) pair of facet attributes. This is all well and
1266         // good for regular filter groups where a group matches a facet, but for
1267         // groupby we want a single facet. So cheat: add an attribute on the
1268         // view which proxies to the first GroupbyGroup, so it can be used
1269         // for every GroupbyGroup and still provides the various methods needed
1270         // by the search view. Use weirdo name to avoid risks of conflicts
1271         if (!this.view._s_groupby) {
1272             this.view._s_groupby = {
1273                 help: "See GroupbyGroup#init",
1274                 get_context: this.proxy('get_context'),
1275                 get_domain: this.proxy('get_domain'),
1276                 get_groupby: this.proxy('get_groupby')
1277             };
1278         }
1279     },
1280     match_facet: function (facet) {
1281         return facet.get('field') === this.view._s_groupby;
1282     },
1283     make_facet: function (values) {
1284         return {
1285             category: _t("GroupBy"),
1286             icon: this.icon,
1287             values: values,
1288             field: this.view._s_groupby
1289         };
1290     }
1291 });
1292 instance.web.search.Filter = instance.web.search.Input.extend(/** @lends instance.web.search.Filter# */{
1293     template: 'SearchView.filter',
1294     /**
1295      * Implementation of the OpenERP filters (button with a context and/or
1296      * a domain sent as-is to the search view)
1297      *
1298      * Filters are only attributes holder, the actual work (compositing
1299      * domains and contexts, converting between facets and filters) is
1300      * performed by the filter group.
1301      *
1302      * @constructs instance.web.search.Filter
1303      * @extends instance.web.search.Input
1304      *
1305      * @param node
1306      * @param parent
1307      */
1308     init: function (node, parent) {
1309         this._super(parent);
1310         this.load_attrs(node.attrs);
1311     },
1312     facet_for: function () { return $.when(null); },
1313     get_context: function () { },
1314     get_domain: function () { },
1315 });
1316
1317 instance.web.search.Separator = instance.web.search.Input.extend({
1318     _in_drawer: false,
1319
1320     complete: function () {
1321         return {is_separator: true};
1322     }
1323 });
1324
1325 instance.web.search.Field = instance.web.search.Input.extend( /** @lends instance.web.search.Field# */ {
1326     template: 'SearchView.field',
1327     default_operator: '=',
1328     /**
1329      * @constructs instance.web.search.Field
1330      * @extends instance.web.search.Input
1331      *
1332      * @param view_section
1333      * @param field
1334      * @param parent
1335      */
1336     init: function (view_section, field, parent) {
1337         this._super(parent);
1338         this.load_attrs(_.extend({}, field, view_section.attrs));
1339     },
1340     facet_for: function (value) {
1341         return $.when({
1342             field: this,
1343             category: this.attrs.string || this.attrs.name,
1344             values: [{label: String(value), value: value}]
1345         });
1346     },
1347     value_from: function (facetValue) {
1348         return facetValue.get('value');
1349     },
1350     get_context: function (facet) {
1351         var self = this;
1352         // A field needs a context to send when active
1353         var context = this.attrs.context;
1354         if (_.isEmpty(context) || !facet.values.length) {
1355             return;
1356         }
1357         var contexts = facet.values.map(function (facetValue) {
1358             return new instance.web.CompoundContext(context)
1359                 .set_eval_context({self: self.value_from(facetValue)});
1360         });
1361
1362         if (contexts.length === 1) { return contexts[0]; }
1363
1364         return _.extend(new instance.web.CompoundContext(), {
1365             __contexts: contexts
1366         });
1367     },
1368     get_groupby: function () { },
1369     /**
1370      * Function creating the returned domain for the field, override this
1371      * methods in children if you only need to customize the field's domain
1372      * without more complex alterations or tests (and without the need to
1373      * change override the handling of filter_domain)
1374      *
1375      * @param {String} name the field's name
1376      * @param {String} operator the field's operator (either attribute-specified or default operator for the field
1377      * @param {Number|String} facet parsed value for the field
1378      * @returns {Array<Array>} domain to include in the resulting search
1379      */
1380     make_domain: function (name, operator, facet) {
1381         return [[name, operator, this.value_from(facet)]];
1382     },
1383     get_domain: function (facet) {
1384         if (!facet.values.length) { return; }
1385
1386         var value_to_domain;
1387         var self = this;
1388         var domain = this.attrs['filter_domain'];
1389         if (domain) {
1390             value_to_domain = function (facetValue) {
1391                 return new instance.web.CompoundDomain(domain)
1392                     .set_eval_context({self: self.value_from(facetValue)});
1393             };
1394         } else {
1395             value_to_domain = function (facetValue) {
1396                 return self.make_domain(
1397                     self.attrs.name,
1398                     self.attrs.operator || self.default_operator,
1399                     facetValue);
1400             };
1401         }
1402         var domains = facet.values.map(value_to_domain);
1403
1404         if (domains.length === 1) { return domains[0]; }
1405         for (var i = domains.length; --i;) {
1406             domains.unshift(['|']);
1407         }
1408
1409         return _.extend(new instance.web.CompoundDomain(), {
1410             __domains: domains
1411         });
1412     }
1413 });
1414 /**
1415  * Implementation of the ``char`` OpenERP field type:
1416  *
1417  * * Default operator is ``ilike`` rather than ``=``
1418  *
1419  * * The Javascript and the HTML values are identical (strings)
1420  *
1421  * @class
1422  * @extends instance.web.search.Field
1423  */
1424 instance.web.search.CharField = instance.web.search.Field.extend( /** @lends instance.web.search.CharField# */ {
1425     default_operator: 'ilike',
1426     complete: function (value) {
1427         if (_.isEmpty(value)) { return $.when(null); }
1428         var label = _.str.sprintf(_.str.escapeHTML(
1429             _t("Search %(field)s for: %(value)s")), {
1430                 field: '<em>' + _.escape(this.attrs.string) + '</em>',
1431                 value: '<strong>' + _.escape(value) + '</strong>'});
1432         return $.when([{
1433             label: label,
1434             facet: {
1435                 category: this.attrs.string,
1436                 field: this,
1437                 values: [{label: value, value: value}]
1438             }
1439         }]);
1440     }
1441 });
1442 instance.web.search.NumberField = instance.web.search.Field.extend(/** @lends instance.web.search.NumberField# */{
1443     complete: function (value) {
1444         var val = this.parse(value);
1445         if (isNaN(val)) { return $.when(); }
1446         var label = _.str.sprintf(
1447             _t("Search %(field)s for: %(value)s"), {
1448                 field: '<em>' + _.escape(this.attrs.string) + '</em>',
1449                 value: '<strong>' + _.escape(value) + '</strong>'});
1450         return $.when([{
1451             label: label,
1452             facet: {
1453                 category: this.attrs.string,
1454                 field: this,
1455                 values: [{label: value, value: val}]
1456             }
1457         }]);
1458     },
1459 });
1460 /**
1461  * @class
1462  * @extends instance.web.search.NumberField
1463  */
1464 instance.web.search.IntegerField = instance.web.search.NumberField.extend(/** @lends instance.web.search.IntegerField# */{
1465     error_message: _t("not a valid integer"),
1466     parse: function (value) {
1467         try {
1468             return instance.web.parse_value(value, {'widget': 'integer'});
1469         } catch (e) {
1470             return NaN;
1471         }
1472     }
1473 });
1474 /**
1475  * @class
1476  * @extends instance.web.search.NumberField
1477  */
1478 instance.web.search.FloatField = instance.web.search.NumberField.extend(/** @lends instance.web.search.FloatField# */{
1479     error_message: _t("not a valid number"),
1480     parse: function (value) {
1481         try {
1482             return instance.web.parse_value(value, {'widget': 'float'});
1483         } catch (e) {
1484             return NaN;
1485         }
1486     }
1487 });
1488
1489 /**
1490  * Utility function for m2o & selection fields taking a selection/name_get pair
1491  * (value, name) and converting it to a Facet descriptor
1492  *
1493  * @param {instance.web.search.Field} field holder field
1494  * @param {Array} pair pair value to convert
1495  */
1496 function facet_from(field, pair) {
1497     return {
1498         field: field,
1499         category: field['attrs'].string,
1500         values: [{label: pair[1], value: pair[0]}]
1501     };
1502 }
1503
1504 /**
1505  * @class
1506  * @extends instance.web.search.Field
1507  */
1508 instance.web.search.SelectionField = instance.web.search.Field.extend(/** @lends instance.web.search.SelectionField# */{
1509     // This implementation is a basic <select> field, but it may have to be
1510     // altered to be more in line with the GTK client, which uses a combo box
1511     // (~ jquery.autocomplete):
1512     // * If an option was selected in the list, behave as currently
1513     // * If something which is not in the list was entered (via the text input),
1514     //   the default domain should become (`ilike` string_value) but **any
1515     //   ``context`` or ``filter_domain`` becomes falsy, idem if ``@operator``
1516     //   is specified. So at least get_domain needs to be quite a bit
1517     //   overridden (if there's no @value and there is no filter_domain and
1518     //   there is no @operator, return [[name, 'ilike', str_val]]
1519     template: 'SearchView.field.selection',
1520     init: function () {
1521         this._super.apply(this, arguments);
1522         // prepend empty option if there is no empty option in the selection list
1523         this.prepend_empty = !_(this.attrs.selection).detect(function (item) {
1524             return !item[1];
1525         });
1526     },
1527     complete: function (needle) {
1528         var self = this;
1529         var results = _(this.attrs.selection).chain()
1530             .filter(function (sel) {
1531                 var value = sel[0], label = sel[1];
1532                 if (!value) { return false; }
1533                 return label.toLowerCase().indexOf(needle.toLowerCase()) !== -1;
1534             })
1535             .map(function (sel) {
1536                 return {
1537                     label: _.escape(sel[1]),
1538                     facet: facet_from(self, sel)
1539                 };
1540             }).value();
1541         if (_.isEmpty(results)) { return $.when(null); }
1542         return $.when.call(null, [{
1543             label: _.escape(this.attrs.string)
1544         }].concat(results));
1545     },
1546     facet_for: function (value) {
1547         var match = _(this.attrs.selection).detect(function (sel) {
1548             return sel[0] === value;
1549         });
1550         if (!match) { return $.when(null); }
1551         return $.when(facet_from(this, match));
1552     }
1553 });
1554 instance.web.search.BooleanField = instance.web.search.SelectionField.extend(/** @lends instance.web.search.BooleanField# */{
1555     /**
1556      * @constructs instance.web.search.BooleanField
1557      * @extends instance.web.search.BooleanField
1558      */
1559     init: function () {
1560         this._super.apply(this, arguments);
1561         this.attrs.selection = [
1562             [true, _t("Yes")],
1563             [false, _t("No")]
1564         ];
1565     }
1566 });
1567 /**
1568  * @class
1569  * @extends instance.web.search.DateField
1570  */
1571 instance.web.search.DateField = instance.web.search.Field.extend(/** @lends instance.web.search.DateField# */{
1572     value_from: function (facetValue) {
1573         return instance.web.date_to_str(facetValue.get('value'));
1574     },
1575     complete: function (needle) {
1576         var d;
1577         try {
1578             var t = (this.attrs && this.attrs.type === 'datetime') ? 'datetime' : 'date';
1579             var v = instance.web.parse_value(needle, {'widget': t});
1580             if (t === 'datetime'){
1581                 d = instance.web.str_to_datetime(v);
1582             }
1583             else{
1584                 d = instance.web.str_to_date(v);
1585             }
1586         } catch (e) {
1587             // pass
1588         }
1589         if (!d) { return $.when(null); }
1590         var date_string = instance.web.format_value(d, this.attrs);
1591         var label = _.str.sprintf(_.str.escapeHTML(
1592             _t("Search %(field)s at: %(value)s")), {
1593                 field: '<em>' + _.escape(this.attrs.string) + '</em>',
1594                 value: '<strong>' + date_string + '</strong>'});
1595         return $.when([{
1596             label: label,
1597             facet: {
1598                 category: this.attrs.string,
1599                 field: this,
1600                 values: [{label: date_string, value: d}]
1601             }
1602         }]);
1603     }
1604 });
1605 /**
1606  * Implementation of the ``datetime`` openerp field type:
1607  *
1608  * * Uses the same widget as the ``date`` field type (a simple date)
1609  *
1610  * * Builds a slighly more complex, it's a datetime range (includes time)
1611  *   spanning the whole day selected by the date widget
1612  *
1613  * @class
1614  * @extends instance.web.DateField
1615  */
1616 instance.web.search.DateTimeField = instance.web.search.DateField.extend(/** @lends instance.web.search.DateTimeField# */{
1617     value_from: function (facetValue) {
1618         return instance.web.datetime_to_str(facetValue.get('value'));
1619     }
1620 });
1621 instance.web.search.ManyToOneField = instance.web.search.CharField.extend({
1622     default_operator: {},
1623     init: function (view_section, field, parent) {
1624         this._super(view_section, field, parent);
1625         this.model = new instance.web.Model(this.attrs.relation);
1626     },
1627
1628     complete: function (value) {
1629         if (_.isEmpty(value)) { return $.when(null); }
1630         var label = _.str.sprintf(_.str.escapeHTML(
1631             _t("Search %(field)s for: %(value)s")), {
1632                 field: '<em>' + _.escape(this.attrs.string) + '</em>',
1633                 value: '<strong>' + _.escape(value) + '</strong>'});
1634         return $.when([{
1635             label: label,
1636             facet: {
1637                 category: this.attrs.string,
1638                 field: this,
1639                 values: [{label: value, value: value, operator: 'ilike'}]
1640             },
1641             expand: this.expand.bind(this),
1642         }]);
1643     },
1644
1645     expand: function (needle) {
1646         var self = this;
1647         // FIXME: "concurrent" searches (multiple requests, mis-ordered responses)
1648         var context = instance.web.pyeval.eval(
1649             'contexts', [this.view.dataset.get_context()]);
1650         return this.model.call('name_search', [], {
1651             name: needle,
1652             args: (typeof this.attrs.domain === 'string') ? [] : this.attrs.domain,
1653             limit: 8,
1654             context: context
1655         }).then(function (results) {
1656             if (_.isEmpty(results)) { return null; }
1657             return _(results).map(function (result) {
1658                 return {
1659                     label: _.escape(result[1]),
1660                     facet: facet_from(self, result)
1661                 };
1662             });
1663         });
1664     },
1665     facet_for: function (value) {
1666         var self = this;
1667         if (value instanceof Array) {
1668             if (value.length === 2 && _.isString(value[1])) {
1669                 return $.when(facet_from(this, value));
1670             }
1671             assert(value.length <= 1,
1672                    _t("M2O search fields do not currently handle multiple default values"));
1673             // there are many cases of {search_default_$m2ofield: [id]}, need
1674             // to handle this as if it were a single value.
1675             value = value[0];
1676         }
1677         return this.model.call('name_get', [value]).then(function (names) {
1678             if (_(names).isEmpty()) { return null; }
1679             return facet_from(self, names[0]);
1680         });
1681     },
1682     value_from: function (facetValue) {
1683         return facetValue.get('label');
1684     },
1685     make_domain: function (name, operator, facetValue) {
1686         operator = facetValue.get('operator') || operator;
1687
1688         switch(operator){
1689         case this.default_operator:
1690             return [[name, '=', facetValue.get('value')]];
1691         case 'ilike':
1692             return [[name, 'ilike', facetValue.get('value')]];
1693         case 'child_of':
1694             return [[name, 'child_of', facetValue.get('value')]];
1695         }
1696         return this._super(name, operator, facetValue);
1697     },
1698     get_context: function (facet) {
1699         var values = facet.values;
1700         if (_.isEmpty(this.attrs.context) && values.length === 1) {
1701             var c = {};
1702             var v = values.at(0);
1703             if (v.get('operator') !== 'ilike') {
1704                 c['default_' + this.attrs.name] = v.get('value');
1705             }
1706             return c;
1707         }
1708         return this._super(facet);
1709     }
1710 });
1711
1712 instance.web.search.CustomFilters = instance.web.search.Input.extend({
1713     template: 'SearchView.Custom',
1714     _in_drawer: true,
1715     init: function () {
1716         this.is_ready = $.Deferred();
1717         this._super.apply(this,arguments);
1718     },
1719     start: function () {
1720         var self = this;
1721         this.model = new instance.web.Model('ir.filters');
1722         this.filters = {};
1723         this.$filters = {};
1724         this.view.query
1725             .on('remove', function (facet) {
1726                 if (!facet.get('is_custom_filter')) {
1727                     return;
1728                 }
1729                 self.clear_selection();
1730             })
1731             .on('reset', this.proxy('clear_selection'));
1732         return this.model.call('get_filters', [this.view.model, this.get_action_id()])
1733             .then(this.proxy('set_filters'))
1734             .done(function () { self.is_ready.resolve(); })
1735             .fail(function () { self.is_ready.reject.apply(self.is_ready, arguments); });
1736     },
1737     get_action_id: function(){
1738         var action = instance.client.action_manager.inner_action;
1739         if (action) return action.id;
1740     },
1741     /**
1742      * Special implementation delaying defaults until CustomFilters is loaded
1743      */
1744     facet_for_defaults: function () {
1745         return this.is_ready;
1746     },
1747     /**
1748      * Generates a mapping key (in the filters and $filter mappings) for the
1749      * filter descriptor object provided (as returned by ``get_filters``).
1750      *
1751      * The mapping key is guaranteed to be unique for a given (user_id, name)
1752      * pair.
1753      *
1754      * @param {Object} filter
1755      * @param {String} filter.name
1756      * @param {Number|Pair<Number, String>} [filter.user_id]
1757      * @return {String} mapping key corresponding to the filter
1758      */
1759     key_for: function (filter) {
1760         var user_id = filter.user_id,
1761             action_id = filter.action_id;
1762         var uid = (user_id instanceof Array) ? user_id[0] : user_id;
1763         var act_id = (action_id instanceof Array) ? action_id[0] : action_id;
1764         return _.str.sprintf('(%s)(%s)%s', uid, act_id, filter.name);
1765     },
1766     /**
1767      * Generates a :js:class:`~instance.web.search.Facet` descriptor from a
1768      * filter descriptor
1769      *
1770      * @param {Object} filter
1771      * @param {String} filter.name
1772      * @param {Object} [filter.context]
1773      * @param {Array} [filter.domain]
1774      * @return {Object}
1775      */
1776     facet_for: function (filter) {
1777         return {
1778             category: _t("Custom Filter"),
1779             icon: 'M',
1780             field: {
1781                 get_context: function () { return filter.context; },
1782                 get_groupby: function () { return [filter.context]; },
1783                 get_domain: function () { return filter.domain; }
1784             },
1785             _id: filter['id'],
1786             is_custom_filter: true,
1787             values: [{label: filter.name, value: null}]
1788         };
1789     },
1790     clear_selection: function () {
1791         this.$('span.badge').removeClass('badge');
1792     },
1793     append_filter: function (filter) {
1794         var self = this;
1795         var key = this.key_for(filter);
1796         var warning = _t("This filter is global and will be removed for everybody if you continue.");
1797
1798         var $filter;
1799         if (key in this.$filters) {
1800             $filter = this.$filters[key];
1801         } else {
1802             var id = filter.id;
1803             this.filters[key] = filter;
1804             $filter = $('<li></li>')
1805                 .appendTo(this.$('.oe_searchview_custom_list'))
1806                 .toggleClass('oe_searchview_custom_default', filter.is_default)
1807                 .append(this.$filters[key] = $('<span>').text(filter.name));
1808
1809             this.$filters[key].addClass(filter.user_id ? 'oe_searchview_custom_private'
1810                                          : 'oe_searchview_custom_public')
1811
1812             $('<a class="oe_searchview_custom_delete">x</a>')
1813                 .click(function (e) {
1814                     e.stopPropagation();
1815                     if (!(filter.user_id || confirm(warning))) {
1816                         return;
1817                     }
1818                     self.model.call('unlink', [id]).done(function () {
1819                         $filter.remove();
1820                         delete self.$filters[key];
1821                         delete self.filters[key];
1822                         if (_.isEmpty(self.filters)) {
1823                             self.hide();
1824                         }
1825                     });
1826                 })
1827                 .appendTo($filter);
1828         }
1829
1830         this.$filters[key].unbind('click').click(function () {
1831             self.toggle_filter(filter);
1832         });
1833         this.show();
1834     },
1835     toggle_filter: function (filter, preventSearch) {
1836         var current = this.view.query.find(function (facet) {
1837             return facet.get('_id') === filter.id;
1838         });
1839         if (current) {
1840             this.view.query.remove(current);
1841             this.$filters[this.key_for(filter)].removeClass('badge');
1842             return;
1843         }
1844         this.view.query.reset([this.facet_for(filter)], {
1845             preventSearch: preventSearch || false});
1846         this.$filters[this.key_for(filter)].addClass('badge');
1847     },
1848     set_filters: function (filters) {
1849         _(filters).map(_.bind(this.append_filter, this));
1850         if (!filters.length) {
1851             this.hide();
1852         }
1853     },
1854     hide: function () {
1855         this.$el.hide();
1856     },
1857     show: function () {
1858         this.$el.show();
1859     },
1860 });
1861
1862 instance.web.search.SaveFilter = instance.web.search.Input.extend({
1863     template: 'SearchView.SaveFilter',
1864     _in_drawer: true,
1865     init: function (parent, custom_filters) {
1866         this._super(parent);
1867         this.custom_filters = custom_filters;
1868     },
1869     start: function () {
1870         var self = this;
1871         this.model = new instance.web.Model('ir.filters');
1872         this.$el.on('submit', 'form', this.proxy('save_current'));
1873         this.$el.on('click', 'input[type=checkbox]', function() {
1874             $(this).siblings('input[type=checkbox]').prop('checked', false);
1875         });
1876         this.$el.on('click', 'h4', function () {
1877             self.$el.toggleClass('oe_opened');
1878         });
1879     },
1880     save_current: function () {
1881         var self = this;
1882         var $name = this.$('input:first');
1883         var private_filter = !this.$('#oe_searchview_custom_public').prop('checked');
1884         var set_as_default = this.$('#oe_searchview_custom_default').prop('checked');
1885         if (_.isEmpty($name.val())){
1886             this.do_warn(_t("Error"), _t("Filter name is required."));
1887             return false;
1888         }
1889         var search = this.view.build_search_data();
1890         instance.web.pyeval.eval_domains_and_contexts({
1891             domains: search.domains,
1892             contexts: search.contexts,
1893             group_by_seq: search.groupbys || []
1894         }).done(function (results) {
1895             if (!_.isEmpty(results.group_by)) {
1896                 results.context.group_by = results.group_by;
1897             }
1898             // Don't save user_context keys in the custom filter, otherwise end
1899             // up with e.g. wrong uid or lang stored *and used in subsequent
1900             // reqs*
1901             var ctx = results.context;
1902             _(_.keys(instance.session.user_context)).each(function (key) {
1903                 delete ctx[key];
1904             });
1905             var filter = {
1906                 name: $name.val(),
1907                 user_id: private_filter ? instance.session.uid : false,
1908                 model_id: self.view.model,
1909                 context: results.context,
1910                 domain: results.domain,
1911                 is_default: set_as_default,
1912                 action_id: self.custom_filters.get_action_id()
1913             };
1914             // FIXME: current context?
1915             return self.model.call('create_or_replace', [filter]).done(function (id) {
1916                 filter.id = id;
1917                 if (self.custom_filters) {
1918                     self.custom_filters.append_filter(filter);
1919                 }
1920                 self.$el
1921                     .removeClass('oe_opened')
1922                     .find('form')[0].reset();
1923             });
1924         });
1925         return false;
1926     },
1927 });
1928
1929 instance.web.search.Filters = instance.web.search.Input.extend({
1930     template: 'SearchView.Filters',
1931     _in_drawer: true,
1932     start: function () {
1933         var self = this;
1934         var is_group = function (i) { return i instanceof instance.web.search.FilterGroup; };
1935         var visible_filters = _(this.drawer.controls).chain().reject(function (group) {
1936             return _(_(group.children).filter(is_group)).isEmpty()
1937                 || group.modifiers.invisible;
1938         });
1939
1940         var groups = visible_filters.map(function (group) {
1941                 var filters = _(group.children).filter(is_group);
1942                 return {
1943                     name: _.str.sprintf("<span class='oe_i'>%s</span> %s",
1944                             group.icon, group.name),
1945                     filters: filters,
1946                     length: _(filters).chain().map(function (i) {
1947                         return i.filters.length; }).sum().value()
1948                 };
1949             }).value();
1950
1951         var $dl = $('<dl class="dl-horizontal">').appendTo(this.$el);
1952
1953         var rendered_lines = _.map(groups, function (group) {
1954             $('<dt>').html(group.name).appendTo($dl);
1955             var $dd = $('<dd>').appendTo($dl);
1956             return $.when.apply(null, _(group.filters).invoke('appendTo', $dd));
1957         });
1958
1959         return $.when.apply(this, rendered_lines);
1960     },
1961 });
1962
1963 instance.web.search.Advanced = instance.web.search.Input.extend({
1964     template: 'SearchView.advanced',
1965     _in_drawer: true,
1966     start: function () {
1967         var self = this;
1968         this.$el
1969             .on('keypress keydown keyup', function (e) { e.stopPropagation(); })
1970             .on('click', 'h4', function () {
1971                 self.$el.toggleClass('oe_opened');
1972             }).on('click', 'button.oe_add_condition', function () {
1973                 self.append_proposition();
1974             }).on('submit', 'form', function (e) {
1975                 e.preventDefault();
1976                 self.commit_search();
1977             });
1978         return $.when(
1979             this._super(),
1980             new instance.web.Model(this.view.model).call('fields_get', {
1981                     context: this.view.dataset.context
1982                 }).done(function(data) {
1983                     self.fields = {
1984                         id: { string: 'ID', type: 'id', searchable: true }
1985                     };
1986                     _.each(data, function(field_def, field_name) {
1987                         if (field_def.selectable !== false && field_name != 'id') {
1988                             self.fields[field_name] = field_def;
1989                         }
1990                     });
1991         })).done(function () {
1992             self.append_proposition();
1993         });
1994     },
1995     append_proposition: function () {
1996         var self = this;
1997         return (new instance.web.search.ExtendedSearchProposition(this, this.fields))
1998             .appendTo(this.$('ul')).done(function () {
1999                 self.$('button.oe_apply').prop('disabled', false);
2000             });
2001     },
2002     remove_proposition: function (prop) {
2003         // removing last proposition, disable apply button
2004         if (this.getChildren().length <= 1) {
2005             this.$('button.oe_apply').prop('disabled', true);
2006         }
2007         prop.destroy();
2008     },
2009     commit_search: function () {
2010         // Get domain sections from all propositions
2011         var children = this.getChildren();
2012         var propositions = _.invoke(children, 'get_proposition');
2013         var domain = _(propositions).pluck('value');
2014         for (var i = domain.length; --i;) {
2015             domain.unshift('|');
2016         }
2017
2018         this.view.query.add({
2019             category: _t("Advanced"),
2020             values: propositions,
2021             field: {
2022                 get_context: function () { },
2023                 get_domain: function () { return domain;},
2024                 get_groupby: function () { }
2025             }
2026         });
2027
2028         // remove all propositions
2029         _.invoke(children, 'destroy');
2030         // add new empty proposition
2031         this.append_proposition();
2032         // TODO: API on searchview
2033         this.view.$el.removeClass('oe_searchview_open_drawer');
2034     }
2035 });
2036
2037 instance.web.search.ExtendedSearchProposition = instance.web.Widget.extend(/** @lends instance.web.search.ExtendedSearchProposition# */{
2038     template: 'SearchView.extended_search.proposition',
2039     events: {
2040         'change .searchview_extended_prop_field': 'changed',
2041         'change .searchview_extended_prop_op': 'operator_changed',
2042         'click .searchview_extended_delete_prop': function (e) {
2043             e.stopPropagation();
2044             this.getParent().remove_proposition(this);
2045         }
2046     },
2047     /**
2048      * @constructs instance.web.search.ExtendedSearchProposition
2049      * @extends instance.web.Widget
2050      *
2051      * @param parent
2052      * @param fields
2053      */
2054     init: function (parent, fields) {
2055         this._super(parent);
2056         this.fields = _(fields).chain()
2057             .map(function(val, key) { return _.extend({}, val, {'name': key}); })
2058             .filter(function (field) { return !field.deprecated && field.searchable; })
2059             .sortBy(function(field) {return field.string;})
2060             .value();
2061         this.attrs = {_: _, fields: this.fields, selected: null};
2062         this.value = null;
2063     },
2064     start: function () {
2065         return this._super().done(this.proxy('changed'));
2066     },
2067     changed: function() {
2068         var nval = this.$(".searchview_extended_prop_field").val();
2069         if(this.attrs.selected === null || this.attrs.selected === undefined || nval != this.attrs.selected.name) {
2070             this.select_field(_.detect(this.fields, function(x) {return x.name == nval;}));
2071         }
2072     },
2073     operator_changed: function (e) {
2074         var $value = this.$('.searchview_extended_prop_value');
2075         switch ($(e.target).val()) {
2076         case '∃':
2077         case '∄':
2078             $value.hide();
2079             break;
2080         default:
2081             $value.show();
2082         }
2083     },
2084     /**
2085      * Selects the provided field object
2086      *
2087      * @param field a field descriptor object (as returned by fields_get, augmented by the field name)
2088      */
2089     select_field: function(field) {
2090         var self = this;
2091         if(this.attrs.selected !== null && this.attrs.selected !== undefined) {
2092             this.value.destroy();
2093             this.value = null;
2094             this.$('.searchview_extended_prop_op').html('');
2095         }
2096         this.attrs.selected = field;
2097         if(field === null || field === undefined) {
2098             return;
2099         }
2100
2101         var type = field.type;
2102         var Field = instance.web.search.custom_filters.get_object(type);
2103         if(!Field) {
2104             Field = instance.web.search.custom_filters.get_object("char");
2105         }
2106         this.value = new Field(this, field);
2107         _.each(this.value.operators, function(operator) {
2108             $('<option>', {value: operator.value})
2109                 .text(String(operator.text))
2110                 .appendTo(self.$('.searchview_extended_prop_op'));
2111         });
2112         var $value_loc = this.$('.searchview_extended_prop_value').show().empty();
2113         this.value.appendTo($value_loc);
2114
2115     },
2116     get_proposition: function() {
2117         if (this.attrs.selected === null || this.attrs.selected === undefined)
2118             return null;
2119         var field = this.attrs.selected;
2120         var op_select = this.$('.searchview_extended_prop_op')[0];
2121         var operator = op_select.options[op_select.selectedIndex];
2122
2123         return {
2124             label: this.value.get_label(field, operator),
2125             value: this.value.get_domain(field, operator),
2126         };
2127     }
2128 });
2129
2130 instance.web.search.ExtendedSearchProposition.Field = instance.web.Widget.extend({
2131     init: function (parent, field) {
2132         this._super(parent);
2133         this.field = field;
2134     },
2135     get_label: function (field, operator) {
2136         var format;
2137         switch (operator.value) {
2138         case '∃': case '∄': format = _t('%(field)s %(operator)s'); break;
2139         default: format = _t('%(field)s %(operator)s "%(value)s"'); break;
2140         }
2141         return this.format_label(format, field, operator);
2142     },
2143     format_label: function (format, field, operator) {
2144         return _.str.sprintf(format, {
2145             field: field.string,
2146             // According to spec, HTMLOptionElement#label should return
2147             // HTMLOptionElement#text when not defined/empty, but it does
2148             // not in older Webkit (between Safari 5.1.5 and Chrome 17) and
2149             // Gecko (pre Firefox 7) browsers, so we need a manual fallback
2150             // for those
2151             operator: operator.label || operator.text,
2152             value: this
2153         });
2154     },
2155     get_domain: function (field, operator) {
2156         switch (operator.value) {
2157         case '∃': return this.make_domain(field.name, '!=', false);
2158         case '∄': return this.make_domain(field.name, '=', false);
2159         default: return this.make_domain(
2160             field.name, operator.value, this.get_value());
2161         }
2162     },
2163     make_domain: function (field, operator, value) {
2164         return [field, operator, value];
2165     },
2166     /**
2167      * Returns a human-readable version of the value, in case the "logical"
2168      * and the "semantic" values of a field differ (as for selection fields,
2169      * for instance).
2170      *
2171      * The default implementation simply returns the value itself.
2172      *
2173      * @return {String} human-readable version of the value
2174      */
2175     toString: function () {
2176         return this.get_value();
2177     }
2178 });
2179 instance.web.search.ExtendedSearchProposition.Char = instance.web.search.ExtendedSearchProposition.Field.extend({
2180     template: 'SearchView.extended_search.proposition.char',
2181     operators: [
2182         {value: "ilike", text: _lt("contains")},
2183         {value: "not ilike", text: _lt("doesn't contain")},
2184         {value: "=", text: _lt("is equal to")},
2185         {value: "!=", text: _lt("is not equal to")},
2186         {value: "∃", text: _lt("is set")},
2187         {value: "∄", text: _lt("is not set")}
2188     ],
2189     get_value: function() {
2190         return this.$el.val();
2191     }
2192 });
2193 instance.web.search.ExtendedSearchProposition.DateTime = instance.web.search.ExtendedSearchProposition.Field.extend({
2194     template: 'SearchView.extended_search.proposition.empty',
2195     operators: [
2196         {value: "=", text: _lt("is equal to")},
2197         {value: "!=", text: _lt("is not equal to")},
2198         {value: ">", text: _lt("greater than")},
2199         {value: "<", text: _lt("less than")},
2200         {value: ">=", text: _lt("greater or equal than")},
2201         {value: "<=", text: _lt("less or equal than")},
2202         {value: "∃", text: _lt("is set")},
2203         {value: "∄", text: _lt("is not set")}
2204     ],
2205     /**
2206      * Date widgets live in view_form which is not yet loaded when this is
2207      * initialized -_-
2208      */
2209     widget: function () { return instance.web.DateTimeWidget; },
2210     get_value: function() {
2211         return this.datewidget.get_value();
2212     },
2213     toString: function () {
2214         return instance.web.format_value(this.get_value(), { type:"datetime" });
2215     },
2216     start: function() {
2217         var ready = this._super();
2218         this.datewidget = new (this.widget())(this);
2219         this.datewidget.appendTo(this.$el);
2220         return ready;
2221     }
2222 });
2223 instance.web.search.ExtendedSearchProposition.Date = instance.web.search.ExtendedSearchProposition.DateTime.extend({
2224     widget: function () { return instance.web.DateWidget; },
2225     toString: function () {
2226         return instance.web.format_value(this.get_value(), { type:"date" });
2227     }
2228 });
2229 instance.web.search.ExtendedSearchProposition.Integer = instance.web.search.ExtendedSearchProposition.Field.extend({
2230     template: 'SearchView.extended_search.proposition.integer',
2231     operators: [
2232         {value: "=", text: _lt("is equal to")},
2233         {value: "!=", text: _lt("is not equal to")},
2234         {value: ">", text: _lt("greater than")},
2235         {value: "<", text: _lt("less than")},
2236         {value: ">=", text: _lt("greater or equal than")},
2237         {value: "<=", text: _lt("less or equal than")},
2238         {value: "∃", text: _lt("is set")},
2239         {value: "∄", text: _lt("is not set")}
2240     ],
2241     toString: function () {
2242         return this.$el.val();
2243     },
2244     get_value: function() {
2245         try {
2246             var val =this.$el.val();
2247             return instance.web.parse_value(val === "" ? 0 : val, {'widget': 'integer'});
2248         } catch (e) {
2249             return "";
2250         }
2251     }
2252 });
2253 instance.web.search.ExtendedSearchProposition.Id = instance.web.search.ExtendedSearchProposition.Integer.extend({
2254     operators: [{value: "=", text: _lt("is")}]
2255 });
2256 instance.web.search.ExtendedSearchProposition.Float = instance.web.search.ExtendedSearchProposition.Field.extend({
2257     template: 'SearchView.extended_search.proposition.float',
2258     operators: [
2259         {value: "=", text: _lt("is equal to")},
2260         {value: "!=", text: _lt("is not equal to")},
2261         {value: ">", text: _lt("greater than")},
2262         {value: "<", text: _lt("less than")},
2263         {value: ">=", text: _lt("greater or equal than")},
2264         {value: "<=", text: _lt("less or equal than")},
2265         {value: "∃", text: _lt("is set")},
2266         {value: "∄", text: _lt("is not set")}
2267     ],
2268     toString: function () {
2269         return this.$el.val();
2270     },
2271     get_value: function() {
2272         try {
2273             var val =this.$el.val();
2274             return instance.web.parse_value(val === "" ? 0.0 : val, {'widget': 'float'});
2275         } catch (e) {
2276             return "";
2277         }
2278     }
2279 });
2280 instance.web.search.ExtendedSearchProposition.Selection = instance.web.search.ExtendedSearchProposition.Field.extend({
2281     template: 'SearchView.extended_search.proposition.selection',
2282     operators: [
2283         {value: "=", text: _lt("is")},
2284         {value: "!=", text: _lt("is not")},
2285         {value: "∃", text: _lt("is set")},
2286         {value: "∄", text: _lt("is not set")}
2287     ],
2288     toString: function () {
2289         var select = this.$el[0];
2290         var option = select.options[select.selectedIndex];
2291         return option.label || option.text;
2292     },
2293     get_value: function() {
2294         return this.$el.val();
2295     }
2296 });
2297 instance.web.search.ExtendedSearchProposition.Boolean = instance.web.search.ExtendedSearchProposition.Field.extend({
2298     template: 'SearchView.extended_search.proposition.empty',
2299     operators: [
2300         {value: "=", text: _lt("is true")},
2301         {value: "!=", text: _lt("is false")}
2302     ],
2303     get_label: function (field, operator) {
2304         return this.format_label(
2305             _t('%(field)s %(operator)s'), field, operator);
2306     },
2307     get_value: function() {
2308         return true;
2309     }
2310 });
2311
2312 instance.web.search.custom_filters = new instance.web.Registry({
2313     'char': 'instance.web.search.ExtendedSearchProposition.Char',
2314     'text': 'instance.web.search.ExtendedSearchProposition.Char',
2315     'one2many': 'instance.web.search.ExtendedSearchProposition.Char',
2316     'many2one': 'instance.web.search.ExtendedSearchProposition.Char',
2317     'many2many': 'instance.web.search.ExtendedSearchProposition.Char',
2318
2319     'datetime': 'instance.web.search.ExtendedSearchProposition.DateTime',
2320     'date': 'instance.web.search.ExtendedSearchProposition.Date',
2321     'integer': 'instance.web.search.ExtendedSearchProposition.Integer',
2322     'float': 'instance.web.search.ExtendedSearchProposition.Float',
2323     'boolean': 'instance.web.search.ExtendedSearchProposition.Boolean',
2324     'selection': 'instance.web.search.ExtendedSearchProposition.Selection',
2325
2326     'id': 'instance.web.search.ExtendedSearchProposition.Id'
2327 });
2328
2329 instance.web.search.AutoComplete = instance.web.Widget.extend({
2330     template: "SearchView.autocomplete",
2331
2332     // Parameters for autocomplete constructor:
2333     //
2334     // parent: this is used to detect keyboard events
2335     //
2336     // options.source: function ({term:query}, callback).  This function will be called to
2337     //      obtain the search results corresponding to the query string.  It is assumed that
2338     //      options.source will call callback with the results.
2339     // options.delay: delay in millisecond before calling source.  Useful if you don't want
2340     //      to make too many rpc calls
2341     // options.select: function (ev, {item: {facet:facet}}).  Autocomplete widget will call
2342     //      that function when a selection is made by the user
2343     // options.get_search_string: function ().  This function will be called by autocomplete
2344     //      to obtain the current search string.
2345     init: function (parent, options) {
2346         this._super(parent);
2347         this.$input = parent.$el;
2348         this.source = options.source;
2349         this.delay = options.delay;
2350         this.select = options.select,
2351         this.get_search_string = options.get_search_string;
2352         this.width = options.width || 400;
2353
2354         this.current_result = null;
2355
2356         this.searching = true;
2357         this.search_string = null;
2358         this.current_search = null;
2359     },
2360     start: function () {
2361         var self = this;
2362         this.$el.width(this.width);
2363         this.$input.on('keyup', function (ev) {
2364             if (ev.which === $.ui.keyCode.RIGHT) {
2365                 self.searching = true;
2366                 ev.preventDefault();
2367                 return;
2368             }
2369             // ENTER is caugth at KeyUp rather than KeyDown to avoid firing
2370             // before all regular keystrokes have been processed
2371             if (ev.which === $.ui.keyCode.ENTER) {
2372                 if (self.current_result && self.get_search_string().length) {
2373                     self.select_item(ev);
2374                 }
2375                 return;
2376             }
2377             if (!self.searching) {
2378                 self.searching = true;
2379                 return;
2380             }
2381             self.search_string = self.get_search_string();
2382             if (self.search_string.length) {
2383                 var search_string = self.search_string;
2384                 setTimeout(function () { self.initiate_search(search_string);}, self.delay);
2385             } else {
2386                 self.close();
2387             }
2388         });
2389         this.$input.on('keydown', function (ev) {
2390             switch (ev.which) {
2391                 // TAB and direction keys are handled at KeyDown because KeyUp
2392                 // is not guaranteed to fire.
2393                 // See e.g. https://github.com/aef-/jquery.masterblaster/issues/13
2394                 case $.ui.keyCode.TAB:
2395                     if (self.current_result && self.get_search_string().length) {
2396                         self.select_item(ev);
2397                     }
2398                     break;
2399                 case $.ui.keyCode.DOWN:
2400                     self.move('down');
2401                     self.searching = false;
2402                     ev.preventDefault();
2403                     break;
2404                 case $.ui.keyCode.UP:
2405                     self.move('up');
2406                     self.searching = false;
2407                     ev.preventDefault();
2408                     break;
2409                 case $.ui.keyCode.RIGHT:
2410                     self.searching = false;
2411                     var current = self.current_result
2412                     if (current && current.expand && !current.expanded) {
2413                         self.expand();
2414                         self.searching = true;
2415                     }
2416                     ev.preventDefault();
2417                     break;
2418                 case $.ui.keyCode.ESCAPE:
2419                     self.close();
2420                     self.searching = false;
2421                     break;
2422             }
2423         });
2424     },
2425     initiate_search: function (query) {
2426         if (query === this.search_string && query !== this.current_search) {
2427             this.search(query);
2428         }
2429     },
2430     search: function (query) {
2431         var self = this;
2432         this.current_search = query;
2433         this.source({term:query}, function (results) {
2434             if (results.length) {
2435                 self.render_search_results(results);
2436                 self.focus_element(self.$('li:first-child'));
2437             } else {
2438                 self.close();
2439             }
2440         });
2441     },
2442     render_search_results: function (results) {
2443         var self = this;
2444         var $list = this.$('ul');
2445         $list.empty();
2446         var render_separator = false;
2447         results.forEach(function (result) {
2448             if (result.is_separator) {
2449                 if (render_separator)
2450                     $list.append($('<li>').addClass('oe-separator'));
2451                 render_separator = false;
2452             } else {
2453                 var $item = self.make_list_item(result).appendTo($list);
2454                 result.$el = $item;
2455                 render_separator = true;
2456             }
2457         });
2458         this.show();
2459     },
2460     make_list_item: function (result) {
2461         var self = this;
2462         var $li = $('<li>')
2463             .hover(function (ev) {self.focus_element($li);})
2464             .mousedown(function (ev) {
2465                 if (ev.button === 0) { // left button
2466                     self.select(ev, {item: {facet: result.facet}});
2467                     self.close();
2468                 } else {
2469                     ev.preventDefault();
2470                 }
2471             })
2472             .data('result', result);
2473         if (result.expand) {
2474             var $expand = $('<span class="oe-expand">').text('▶').appendTo($li);
2475             $expand.mousedown(function (ev) {
2476                 ev.preventDefault();
2477                 ev.stopPropagation();
2478                 if (result.expanded)
2479                     self.fold();
2480                 else
2481                     self.expand();
2482             });
2483             result.expanded = false;
2484         }
2485         if (result.indent) $li.addClass('oe-indent');
2486         $li.append($('<span>').html(result.label));
2487         return $li;
2488     },
2489     expand: function () {
2490         var self = this;
2491         this.current_result.expand(this.get_search_string()).then(function (results) {
2492             (results || [{label: '(no result)'}]).reverse().forEach(function (result) {
2493                 result.indent = true;
2494                 var $li = self.make_list_item(result);
2495                 self.current_result.$el.after($li);
2496             });
2497             self.current_result.expanded = true;
2498             self.current_result.$el.find('span.oe-expand').html('▼');
2499         });
2500     },
2501     fold: function () {
2502         var $next = this.current_result.$el.next();
2503         while ($next.hasClass('oe-indent')) {
2504             $next.remove();
2505             $next = this.current_result.$el.next();
2506         }
2507         this.current_result.expanded = false;
2508         this.current_result.$el.find('span.oe-expand').html('▶');
2509     },
2510     focus_element: function ($li) {
2511         this.$('li').removeClass('oe-selection-focus');
2512         $li.addClass('oe-selection-focus');
2513         this.current_result = $li.data('result');
2514     },
2515     select_item: function (ev) {
2516         if (this.current_result.facet) {
2517             this.select(ev, {item: {facet: this.current_result.facet}});
2518             this.close();
2519         }
2520     },
2521     show: function () {
2522         this.$el.show();
2523     },
2524     close: function () {
2525         this.current_search = null;
2526         this.search_string = null;
2527         this.searching = true;
2528         this.$el.hide();
2529     },
2530     move: function (direction) {
2531         var $next;
2532         if (direction === 'down') {
2533             $next = this.$('li.oe-selection-focus').nextAll(':not(.oe-separator)').first();
2534             if (!$next.length) $next = this.$('li:first-child');
2535         } else {
2536             $next = this.$('li.oe-selection-focus').prevAll(':not(.oe-separator)').first();
2537             if (!$next.length) $next = this.$('li:not(.oe-separator)').last();
2538         }
2539         this.focus_element($next);
2540     },
2541     is_expandable: function () {
2542         return !!this.$('.oe-selection-focus .oe-expand').length;
2543     },
2544 });
2545
2546 })();
2547
2548 // vim:et fdc=0 fdl=0 foldnestmax=3 fdm=syntax: