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