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