[FIX] hack around double-search: prevent loading of defaults in search view from...
[odoo/odoo.git] / addons / web / static / src / js / search.js
1 openerp.web.search = function(openerp) {
2 var QWeb = openerp.web.qweb,
3       _t =  openerp.web._t,
4      _lt = openerp.web._lt;
5 _.mixin({
6     sum: function (obj) { return _.reduce(obj, function (a, b) { return a + b; }, 0); }
7 });
8
9 // Have SearchBox optionally use callback function to produce inputs and facets
10 // (views) set on callbacks.make_facet and callbacks.make_input keys when
11 // initializing VisualSearch
12 var SearchBox_renderFacet = function (facet, position) {
13     var view = new (this.app.options.callbacks['make_facet'] || VS.ui.SearchFacet)({
14       app   : this.app,
15       model : facet,
16       order : position
17     });
18
19     // Input first, facet second.
20     this.renderSearchInput();
21     this.facetViews.push(view);
22     this.$('.VS-search-inner').children().eq(position*2).after(view.render().el);
23
24     view.calculateSize();
25     _.defer(_.bind(view.calculateSize, view));
26
27     return view;
28   }; // warning: will not match
29 // Ensure we're replacing the function we think
30 if (SearchBox_renderFacet.toString() !== VS.ui.SearchBox.prototype.renderFacet.toString().replace(/(VS\.ui\.SearchFacet)/, "(this.app.options.callbacks['make_facet'] || $1)")) {
31     throw new Error(
32         "Trying to replace wrong version of VS.ui.SearchBox#renderFacet. "
33         + "Please fix replacement.");
34 }
35 var SearchBox_renderSearchInput = function () {
36     var input = new (this.app.options.callbacks['make_input'] || VS.ui.SearchInput)({position: this.inputViews.length, app: this.app});
37     this.$('.VS-search-inner').append(input.render().el);
38     this.inputViews.push(input);
39   };
40 // Ensure we're replacing the function we think
41 if (SearchBox_renderSearchInput.toString() !== VS.ui.SearchBox.prototype.renderSearchInput.toString().replace(/(VS\.ui\.SearchInput)/, "(this.app.options.callbacks['make_input'] || $1)")) {
42     throw new Error(
43         "Trying to replace wrong version of VS.ui.SearchBox#renderSearchInput. "
44         + "Please fix replacement.");
45 }
46 var SearchBox_searchEvent = function (e) {
47     var query = null;
48     this.renderFacets();
49     this.focusSearch(e);
50     this.app.options.callbacks.search(query, this.app.searchQuery);
51   };
52 if (SearchBox_searchEvent.toString() !== VS.ui.SearchBox.prototype.searchEvent.toString().replace(
53         /this\.value\(\);\n[ ]{4}this\.focusSearch\(e\);\n[ ]{4}this\.value\(query\)/,
54         'null;\n    this.renderFacets();\n    this.focusSearch(e)')) {
55     throw new Error(
56         "Trying to replace wrong version of VS.ui.SearchBox#searchEvent. "
57         + "Please fix replacement.");
58 }
59 _.extend(VS.ui.SearchBox.prototype, {
60     renderFacet: SearchBox_renderFacet,
61     renderSearchInput: SearchBox_renderSearchInput,
62     searchEvent: SearchBox_searchEvent
63 });
64 _.extend(VS.model.SearchFacet.prototype, {
65     value: function () {
66         if (this.has('json')) {
67             return this.get('json');
68         }
69         return this.get('value');
70     }
71 });
72
73 openerp.web.SearchView = openerp.web.Widget.extend(/** @lends openerp.web.SearchView# */{
74     template: "SearchView",
75     /**
76      * @constructs openerp.web.SearchView
77      * @extends openerp.web.OldWidget
78      *
79      * @param parent
80      * @param element_id
81      * @param dataset
82      * @param view_id
83      * @param defaults
84      */
85     init: function(parent, dataset, view_id, defaults, hidden) {
86         this._super(parent);
87         this.dataset = dataset;
88         this.model = dataset.model;
89         this.view_id = view_id;
90
91         this.defaults = defaults || {};
92         this.has_defaults = !_.isEmpty(this.defaults);
93
94         this.inputs = [];
95         this.controls = {};
96
97         this.hidden = !!hidden;
98         this.headless = this.hidden && !this.has_defaults;
99
100         this.filter_data = {};
101
102         this.ready = $.Deferred();
103     },
104     start: function() {
105         var self = this;
106         var p = this._super();
107
108         this.setup_global_completion();
109         this.vs = VS.init({
110             container: this.$element,
111             query: '',
112             callbacks: {
113                 make_facet: this.proxy('make_visualsearch_facet'),
114                 make_input: this.proxy('make_visualsearch_input'),
115                 search: function (query, searchCollection) {
116                     self.do_search();
117                 },
118                 facetMatches: function (callback) {
119                 },
120                 valueMatches : function(facet, searchTerm, callback) {
121                 }
122             }
123         });
124
125         var search = function () { self.vs.searchBox.searchEvent({}); };
126         // searchQuery operations
127         this.vs.searchQuery
128             .off('add').on('add', search)
129             .off('change').on('change', search)
130             .off('reset').on('reset', search)
131             .off('remove').on('remove', function (record, collection, options) {
132                 if (options['trigger_search']) {
133                     search();
134                 }
135             });
136
137         if (this.hidden) {
138             this.$element.hide();
139         }
140         if (this.headless) {
141             this.ready.resolve();
142         } else {
143             var load_view = this.rpc("/web/searchview/load", {
144                 model: this.model,
145                 view_id: this.view_id,
146                 context: this.dataset.get_context() });
147             // FIXME: local eval of domain and context to get rid of special endpoint
148             var filters = this.rpc('/web/searchview/get_filters', {
149                 model: this.model
150             }).then(function (filters) { self.custom_filters = filters; });
151
152             $.when(load_view, filters)
153                 .pipe(function (load) { return load[0]; })
154                 .then(this.on_loaded);
155         }
156
157         this.$element.on('click', '.oe_vs_unfold_drawer', function () {
158             self.$element.toggleClass('oe_searchview_open_drawer');
159         });
160
161         return $.when(p, this.ready);
162     },
163     show: function () {
164         this.$element.show();
165     },
166     hide: function () {
167         this.$element.hide();
168     },
169
170     /**
171      * Sets up thingie where all the mess is put?
172      */
173     setup_stuff_drawer: function () {
174         var self = this;
175         $('<div class="oe_vs_unfold_drawer">').appendTo(this.$element.find('.VS-search-box'));
176         var $drawer = $('<div class="oe_searchview_drawer">').appendTo(this.$element);
177         var $filters = $('<div class="oe_searchview_filters">').appendTo($drawer);
178
179         var running_count = 0;
180         // get total filters count
181         var is_group = function (i) { return i instanceof openerp.web.search.FilterGroup; };
182         var filters_count = _(this.controls).chain()
183             .flatten()
184             .filter(is_group)
185             .map(function (i) { return i.filters.length; })
186             .sum()
187             .value();
188
189         var col1 = [], col2 = _(this.controls).map(function (inputs, group) {
190             var filters = _(inputs).filter(is_group);
191             return {
192                 name: group === 'null' ? _t("Filters") : group,
193                 filters: filters,
194                 length: _(filters).chain().map(function (i) {
195                     return i.filters.length; }).sum().value()
196             };
197         });
198
199         while (col2.length) {
200             // col1 + group should be smaller than col2 + group
201             if ((running_count + col2[0].length) <= (filters_count - running_count)) {
202                 running_count += col2[0].length;
203                 col1.push(col2.shift());
204             } else {
205                 break;
206             }
207         }
208
209         // Create a Custom Filter FilterGroup for each custom filter read from
210         // the db, add all of this as a group in the smallest column
211         [].push.call(col1.length <= col2.length ? col1 : col2, {
212             name: _t("Custom Filters"),
213             filters: _.map(this.custom_filters, function (filter) {
214                 // FIXME: handling of ``disabled`` being set
215                 var f = new openerp.web.search.Filter({attrs: {
216                     string: filter.name,
217                     context: filter.context,
218                     domain: filter.domain
219                 }}, self);
220                 return new openerp.web.search.FilterGroup([f], self);
221             }),
222             length: 3
223         });
224
225         return $.when(
226             this.render_column(col1, $('<div>').appendTo($filters)),
227             this.render_column(col2, $('<div>').appendTo($filters)),
228             (new openerp.web.search.Advanced(this).appendTo($drawer)));
229     },
230     render_column: function (column, $el) {
231         return $.when.apply(null, _(column).map(function (group) {
232             $('<h3>').text(group.name).appendTo($el);
233             return $.when.apply(null,
234                 _(group.filters).invoke('appendTo', $el));
235         }));
236     },
237     /**
238      * Sets up search view's view-wide auto-completion widget
239      */
240     setup_global_completion: function () {
241         // Prevent keydown from within a facet's input from reaching the
242         // auto-completion widget and opening the completion list
243         this.$element.on('keydown', '.search_facet input', function (e) {
244             e.stopImmediatePropagation();
245         });
246
247         this.$element.autocomplete({
248             source: this.proxy('complete_global_search'),
249             select: this.proxy('select_completion'),
250             focus: function (e) { e.preventDefault(); },
251             html: true,
252             minLength: 0,
253             delay: 0
254         }).data('autocomplete')._renderItem = function (ul, item) {
255             // item of completion list
256             var $item = $( "<li></li>" )
257                 .data( "item.autocomplete", item )
258                 .appendTo( ul );
259
260             if (item.value !== undefined) {
261                 // regular completion item
262                 return $item.append(
263                     (item.label)
264                         ? $('<a>').html(item.label)
265                         : $('<a>').text(item.value));
266             }
267             return $item.text(item.category)
268                 .css({
269                     borderTop: '1px solid #cccccc',
270                     margin: 0,
271                     padding: 0,
272                     zoom: 1,
273                     'float': 'left',
274                     clear: 'left',
275                     width: '100%'
276                 });
277         }
278     },
279     /**
280      * Provide auto-completion result for req.term (an array to `resp`)
281      *
282      * @param {Object} req request to complete
283      * @param {String} req.term searched term to complete
284      * @param {Function} resp response callback
285      */
286     complete_global_search:  function (req, resp) {
287         $.when.apply(null, _(this.inputs).chain()
288             .invoke('complete', req.term)
289             .value()).then(function () {
290                 resp(_(_(arguments).compact()).flatten(true));
291         });
292     },
293
294     /**
295      * Action to perform in case of selection: create a facet (model)
296      * and add it to the search collection
297      *
298      * @param {Object} e selection event, preventDefault to avoid setting value on object
299      * @param {Object} ui selection information
300      * @param {Object} ui.item selected completion item
301      */
302     select_completion: function (e, ui) {
303         e.preventDefault();
304         this.vs.searchQuery.add(new VS.model.SearchFacet(_.extend(
305             {app: this.vs}, ui.item)));
306         this.vs.searchBox.searchEvent({});
307     },
308
309     /**
310      * Builds the right SearchFacet view based on the facet object to render
311      * (e.g. readonly facets for filters)
312      *
313      * @param {Object} options
314      * @param {VS.model.SearchFacet} options.model facet object to render
315      */
316     make_visualsearch_facet: function (options) {
317         return new openerp.web.search.FilterGroupFacet(options);
318
319 //        if (options.model.get('field') instanceof openerp.web.search.FilterGroup) {
320 //            return new openerp.web.search.FilterGroupFacet(options);
321 //        }
322 //        return new VS.ui.SearchFacet(options);
323     },
324     /**
325      * Proxies searches on a SearchInput to the search view's global completion
326      *
327      * Also disables SearchInput.autocomplete#_move so search view's
328      * autocomplete can get the corresponding events, or something.
329      *
330      * @param options
331      */
332     make_visualsearch_input: function (options) {
333         var self = this, input = new VS.ui.SearchInput(options);
334         input.setupAutocomplete = function () {
335             _.extend(this.box.autocomplete({
336                 minLength: 1,
337                 delay: 0,
338                 search: function () {
339                     self.$element.autocomplete('search', input.box.val());
340                     return false;
341                 }
342             }).data('autocomplete'), {
343                 _move: function () {},
344                 close: function () { self.$element.autocomplete('close'); }
345             });
346         };
347         return input;
348     },
349
350     /**
351      * Builds a list of widget rows (each row is an array of widgets)
352      *
353      * @param {Array} items a list of nodes to convert to widgets
354      * @param {Object} fields a mapping of field names to (ORM) field attributes
355      * @param {String} [group_name] name of the group to put the new controls in
356      */
357     make_widgets: function (items, fields, group_name) {
358         group_name = group_name || null;
359         if (!(group_name in this.controls)) {
360             this.controls[group_name] = [];
361         }
362         var self = this, group = this.controls[group_name];
363         var filters = [];
364         _.each(items, function (item) {
365             if (filters.length && item.tag !== 'filter') {
366                 group.push(new openerp.web.search.FilterGroup(filters, this));
367                 filters = [];
368             }
369
370             switch (item.tag) {
371             case 'separator': case 'newline':
372                 break;
373             case 'filter':
374                 filters.push(new openerp.web.search.Filter(item, this));
375                 break;
376             case 'group':
377                 self.make_widgets(item.children, fields, item.attrs.string);
378                 break;
379             case 'field':
380                 group.push(this.make_field(item, fields[item['attrs'].name]));
381                 // filters
382                 self.make_widgets(item.children, fields, group_name);
383                 break;
384             }
385         }, this);
386
387         if (filters.length) {
388             group.push(new openerp.web.search.FilterGroup(filters, this));
389         }
390     },
391     /**
392      * Creates a field for the provided field descriptor item (which comes
393      * from fields_view_get)
394      *
395      * @param {Object} item fields_view_get node for the field
396      * @param {Object} field fields_get result for the field
397      * @returns openerp.web.search.Field
398      */
399     make_field: function (item, field) {
400         var obj = openerp.web.search.fields.get_any( [item.attrs.widget, field.type]);
401         if(obj) {
402             return new (obj) (item, field, this);
403         } else {
404             console.group('Unknown field type ' + field.type);
405             console.error('View node', item);
406             console.info('View field', field);
407             console.info('In view', this);
408             console.groupEnd();
409             return null;
410         }
411     },
412     on_loaded: function(data) {
413         var self = this;
414         this.fields_view = data.fields_view;
415         if (data.fields_view.type !== 'search' ||
416             data.fields_view.arch.tag !== 'search') {
417                 throw new Error(_.str.sprintf(
418                     "Got non-search view after asking for a search view: type %s, arch root %s",
419                     data.fields_view.type, data.fields_view.arch.tag));
420         }
421
422         this.make_widgets(
423             data.fields_view['arch'].children,
424             data.fields_view.fields);
425
426         // load defaults
427         return $.when(
428                 this.setup_stuff_drawer(),
429                 $.when.apply(null, _(this.inputs).invoke('facet_for_defaults', this.defaults))
430                     .then(function () {
431                         self.vs.searchQuery.reset(_(arguments).compact(), {silent: true});
432                         self.vs.searchBox.renderFacets();
433                     }))
434             .then(function () { self.ready.resolve(); })
435     },
436     /**
437      * Handle event when the user make a selection in the filters management select box.
438      */
439     on_filters_management: function(e) {
440         var self = this;
441         var select = this.$element.find(".oe_search-view-filters-management");
442         var val = select.val();
443         switch(val) {
444         case 'advanced_filter':
445             this.extended_search.on_activate();
446             break;
447         case 'add_to_dashboard':
448             this.on_add_to_dashboard();
449             break;
450         case 'manage_filters':
451             this.do_action({
452                 res_model: 'ir.filters',
453                 views: [[false, 'list'], [false, 'form']],
454                 type: 'ir.actions.act_window',
455                 context: {"search_default_user_id": this.session.uid,
456                 "search_default_model_id": this.dataset.model},
457                 target: "current",
458                 limit : 80
459             });
460             break;
461         case 'save_filter':
462             var data = this.build_search_data();
463             var context = new openerp.web.CompoundContext();
464             _.each(data.contexts, function(x) {
465                 context.add(x);
466             });
467             var domain = new openerp.web.CompoundDomain();
468             _.each(data.domains, function(x) {
469                 domain.add(x);
470             });
471             var groupbys = _.pluck(data.groupbys, "group_by").join();
472             context.add({"group_by": groupbys});
473             var dial_html = QWeb.render("SearchView.managed-filters.add");
474             var $dial = $(dial_html);
475             openerp.web.dialog($dial, {
476                 modal: true,
477                 title: _t("Filter Entry"),
478                 buttons: [
479                     {text: _t("Cancel"), click: function() {
480                         $(this).dialog("close");
481                     }},
482                     {text: _t("OK"), click: function() {
483                         $(this).dialog("close");
484                         var name = $(this).find("input").val();
485                         self.rpc('/web/searchview/save_filter', {
486                             model: self.dataset.model,
487                             context_to_save: context,
488                             domain: domain,
489                             name: name
490                         }).then(function() {
491                             self.reload_managed_filters();
492                         });
493                     }}
494                 ]
495             });
496             break;
497         case '':
498             this.do_clear();
499         }
500         if (val.slice(0, 4) == "get:") {
501             val = val.slice(4);
502             val = parseInt(val, 10);
503             var filter = this.managed_filters[val];
504             this.do_clear(false).then(_.bind(function() {
505                 select.val('get:' + val);
506
507                 var groupbys = [];
508                 var group_by = filter.context.group_by;
509                 if (group_by) {
510                     groupbys = _.map(
511                         group_by instanceof Array ? group_by : group_by.split(','),
512                         function (el) { return { group_by: el }; });
513                 }
514                 this.filter_data = {
515                     domains: [filter.domain],
516                     contexts: [filter.context],
517                     groupbys: groupbys
518                 };
519                 this.do_search();
520             }, this));
521         } else {
522             select.val('');
523         }
524     },
525     on_add_to_dashboard: function() {
526         this.$element.find(".oe_search-view-filters-management")[0].selectedIndex = 0;
527         var self = this,
528             menu = openerp.webclient.menu,
529             $dialog = $(QWeb.render("SearchView.add_to_dashboard", {
530                 dashboards : menu.data.data.children,
531                 selected_menu_id : menu.$element.find('a.active').data('menu')
532             }));
533         $dialog.find('input').val(this.fields_view.name);
534         openerp.web.dialog($dialog, {
535             modal: true,
536             title: _t("Add to Dashboard"),
537             buttons: [
538                 {text: _t("Cancel"), click: function() {
539                     $(this).dialog("close");
540                 }},
541                 {text: _t("OK"), click: function() {
542                     $(this).dialog("close");
543                     var menu_id = $(this).find("select").val(),
544                         title = $(this).find("input").val(),
545                         data = self.build_search_data(),
546                         context = new openerp.web.CompoundContext(),
547                         domain = new openerp.web.CompoundDomain();
548                     _.each(data.contexts, function(x) {
549                         context.add(x);
550                     });
551                     _.each(data.domains, function(x) {
552                            domain.add(x);
553                     });
554                     self.rpc('/web/searchview/add_to_dashboard', {
555                         menu_id: menu_id,
556                         action_id: self.getParent().action.id,
557                         context_to_save: context,
558                         domain: domain,
559                         view_mode: self.getParent().active_view,
560                         name: title
561                     }, function(r) {
562                         if (r === false) {
563                             self.do_warn("Could not add filter to dashboard");
564                         } else {
565                             self.do_notify("Filter added to dashboard", '');
566                         }
567                     });
568                 }}
569             ]
570         });
571     },
572     /**
573      * Performs the search view collection of widget data.
574      *
575      * If the collection went well (all fields are valid), then triggers
576      * :js:func:`openerp.web.SearchView.on_search`.
577      *
578      * If at least one field failed its validation, triggers
579      * :js:func:`openerp.web.SearchView.on_invalid` instead.
580      *
581      * @param e jQuery event object coming from the "Search" button
582      */
583     do_search: function () {
584         var domains = [], contexts = [], groupbys = [], errors = [];
585
586         this.vs.searchQuery.each(function (facet) {
587             var field = facet.get('field');
588             try {
589                 var domain = field.get_domain(facet);
590                 if (domain) {
591                     domains.push(domain);
592                 }
593                 var context = field.get_context(facet);
594                 if (context) {
595                     contexts.push(context);
596                 }
597                 var group_by = field.get_groupby(facet);
598                 if (group_by) {
599                     groupbys.push.apply(groupbys, group_by);
600                 }
601             } catch (e) {
602                 if (e instanceof openerp.web.search.Invalid) {
603                     errors.push(e);
604                 } else {
605                     throw e;
606                 }
607             }
608         });
609
610         if (!_.isEmpty(errors)) {
611             this.on_invalid(errors);
612             return;
613         }
614         return this.on_search(domains, contexts, groupbys);
615     },
616     /**
617      * Triggered after the SearchView has collected all relevant domains and
618      * contexts.
619      *
620      * It is provided with an Array of domains and an Array of contexts, which
621      * may or may not be evaluated (each item can be either a valid domain or
622      * context, or a string to evaluate in order in the sequence)
623      *
624      * It is also passed an array of contexts used for group_by (they are in
625      * the correct order for group_by evaluation, which contexts may not be)
626      *
627      * @event
628      * @param {Array} domains an array of literal domains or domain references
629      * @param {Array} contexts an array of literal contexts or context refs
630      * @param {Array} groupbys ordered contexts which may or may not have group_by keys
631      */
632     on_search: function (domains, contexts, groupbys) {
633     },
634     /**
635      * Triggered after a validation error in the SearchView fields.
636      *
637      * Error objects have three keys:
638      * * ``field`` is the name of the invalid field
639      * * ``value`` is the invalid value
640      * * ``message`` is the (in)validation message provided by the field
641      *
642      * @event
643      * @param {Array} errors a never-empty array of error objects
644      */
645     on_invalid: function (errors) {
646         this.do_notify(_t("Invalid Search"), _t("triggered from search view"));
647     }
648 });
649
650 /** @namespace */
651 openerp.web.search = {};
652
653 openerp.web.search.FilterGroupFacet = VS.ui.SearchFacet.extend({
654     events: _.extend({
655         'click': 'selectFacet'
656     }, VS.ui.SearchFacet.prototype.events),
657
658     render: function () {
659         this.setMode('not', 'editing');
660         this.setMode('not', 'selected');
661
662         var value = this.model.get('value');
663         this.$el.html(QWeb.render('SearchView.filters.facet', {
664             facet: this.model
665         }));
666         // virtual input so SearchFacet code has something to play with
667         this.box = $('<input>').val(value);
668
669         return this;
670     },
671     enableEdit: function () {
672         this.selectFacet()
673     },
674     keydown: function (e) {
675         var key = VS.app.hotkeys.key(e);
676         if (key !== 'right') {
677             return VS.ui.SearchFacet.prototype.keydown.call(this, e);
678         }
679         e.preventDefault();
680         this.deselectFacet();
681         this.options.app.searchBox.focusNextFacet(this, 1);
682     }
683 });
684 /**
685  * Registry of search fields, called by :js:class:`openerp.web.SearchView` to
686  * find and instantiate its field widgets.
687  */
688 openerp.web.search.fields = new openerp.web.Registry({
689     'char': 'openerp.web.search.CharField',
690     'text': 'openerp.web.search.CharField',
691     'boolean': 'openerp.web.search.BooleanField',
692     'integer': 'openerp.web.search.IntegerField',
693     'id': 'openerp.web.search.IntegerField',
694     'float': 'openerp.web.search.FloatField',
695     'selection': 'openerp.web.search.SelectionField',
696     'datetime': 'openerp.web.search.DateTimeField',
697     'date': 'openerp.web.search.DateField',
698     'many2one': 'openerp.web.search.ManyToOneField',
699     'many2many': 'openerp.web.search.CharField',
700     'one2many': 'openerp.web.search.CharField'
701 });
702 openerp.web.search.Invalid = openerp.web.Class.extend( /** @lends openerp.web.search.Invalid# */{
703     /**
704      * Exception thrown by search widgets when they hold invalid values,
705      * which they can not return when asked.
706      *
707      * @constructs openerp.web.search.Invalid
708      * @extends openerp.web.Class
709      *
710      * @param field the name of the field holding an invalid value
711      * @param value the invalid value
712      * @param message validation failure message
713      */
714     init: function (field, value, message) {
715         this.field = field;
716         this.value = value;
717         this.message = message;
718     },
719     toString: function () {
720         return _.str.sprintf(
721             _t("Incorrect value for field %(fieldname)s: [%(value)s] is %(message)s"),
722             {fieldname: this.field, value: this.value, message: this.message}
723         );
724     }
725 });
726 openerp.web.search.Widget = openerp.web.OldWidget.extend( /** @lends openerp.web.search.Widget# */{
727     template: null,
728     /**
729      * Root class of all search widgets
730      *
731      * @constructs openerp.web.search.Widget
732      * @extends openerp.web.OldWidget
733      *
734      * @param view the ancestor view of this widget
735      */
736     init: function (view) {
737         this._super(view);
738         this.view = view;
739     }
740 });
741 openerp.web.search.add_expand_listener = function($root) {
742     $root.find('a.searchview_group_string').click(function (e) {
743         $root.toggleClass('folded expanded');
744         e.stopPropagation();
745         e.preventDefault();
746     });
747 };
748 openerp.web.search.Group = openerp.web.search.Widget.extend({
749     template: 'SearchView.group',
750     init: function (view_section, view, fields) {
751         this._super(view);
752         this.attrs = view_section.attrs;
753         this.lines = view.make_widgets(
754             view_section.children, fields);
755     }
756 });
757
758 openerp.web.search.Input = openerp.web.search.Widget.extend( /** @lends openerp.web.search.Input# */{
759     /**
760      * @constructs openerp.web.search.Input
761      * @extends openerp.web.search.Widget
762      *
763      * @param view
764      */
765     init: function (view) {
766         this._super(view);
767         this.view.inputs.push(this);
768         this.style = undefined;
769     },
770     /**
771      * Fetch auto-completion values for the widget.
772      *
773      * The completion values should be an array of objects with keys category,
774      * label, value prefixed with an object with keys type=section and label
775      *
776      * @param {String} value value to complete
777      * @returns {jQuery.Deferred<null|Array>}
778      */
779     complete: function (value) {
780         return $.when(null)
781     },
782     /**
783      * Returns a VS.model.SearchFacet instance for the provided defaults if
784      * they apply to this widget, or null if they don't.
785      *
786      * This default implementation will try calling
787      * :js:func:`openerp.web.search.Input#facet_for` if the widget's name
788      * matches the input key
789      *
790      * @param {Object} defaults
791      * @returns {jQuery.Deferred<null|Object>}
792      */
793     facet_for_defaults: function (defaults) {
794         if (!this.attrs ||
795             !(this.attrs.name in defaults && defaults[this.attrs.name])) {
796             return $.when(null);
797         }
798         return this.facet_for(defaults[this.attrs.name]);
799     },
800     get_context: function () {
801         throw new Error(
802             "get_context not implemented for widget " + this.attrs.type);
803     },
804     get_groupby: function () {
805         throw new Error(
806             "get_groupby not implemented for widget " + this.attrs.type);
807     },
808     get_domain: function () {
809         throw new Error(
810             "get_domain not implemented for widget " + this.attrs.type);
811     },
812     load_attrs: function (attrs) {
813         if (attrs.modifiers) {
814             attrs.modifiers = JSON.parse(attrs.modifiers);
815             attrs.invisible = attrs.modifiers.invisible || false;
816             if (attrs.invisible) {
817                 this.style = 'display: none;'
818             }
819         }
820         this.attrs = attrs;
821     }
822 });
823 openerp.web.search.FilterGroup = openerp.web.search.Input.extend(/** @lends openerp.web.search.FilterGroup# */{
824     template: 'SearchView.filters',
825     /**
826      * Inclusive group of filters, creates a continuous "button" with clickable
827      * sections (the normal display for filters is to be a self-contained button)
828      *
829      * @constructs openerp.web.search.FilterGroup
830      * @extends openerp.web.search.Input
831      *
832      * @param {Array<openerp.web.search.Filter>} filters elements of the group
833      * @param {openerp.web.SearchView} view view in which the filters are contained
834      */
835     init: function (filters, view) {
836         this._super(view);
837         this.filters = filters;
838     },
839     start: function () {
840         this.$element.on('click', 'li', this.proxy('toggle_filter'));
841         return $.when(null);
842     },
843     facet_for_defaults: function (defaults) {
844         var fs = _(this.filters).filter(function (f) {
845             return f.attrs && f.attrs.name && !!defaults[f.attrs.name];
846         });
847         if (_.isEmpty(fs)) { return $.when(null); }
848         return $.when(new VS.model.SearchFacet({
849             category: _t("Filter"),
850             value: _(fs).map(function (f) {
851                 return f.attrs.string || f.attrs.name }).join(' | '),
852             json: fs,
853             field: this,
854             app: this.view.vs
855         }));
856     },
857     /**
858      * Fetches contexts for all enabled filters in the group
859      *
860      * @param {VS.model.SearchFacet} facet
861      * @return {*} combined contexts of the enabled filters in this group
862      */
863     get_context: function (facet) {
864         var contexts = _(facet.get('json')).chain()
865             .map(function (filter) { return filter.attrs.context; })
866             .reject(_.isEmpty)
867             .value();
868
869         if (!contexts.length) { return; }
870         if (contexts.length === 1) { return contexts[0]; }
871         return _.extend(new openerp.web.CompoundContext, {
872             __contexts: contexts
873         });
874     },
875     /**
876      * Fetches group_by sequence for all enabled filters in the group
877      *
878      * @param {VS.model.SearchFacet} facet
879      * @return {Array} enabled filters in this group
880      */
881     get_groupby: function (facet) {
882         return  _(facet.get('json')).chain()
883             .map(function (filter) { return filter.attrs.context; })
884             .reject(_.isEmpty)
885             .value();
886     },
887     /**
888      * Handles domains-fetching for all the filters within it: groups them.
889      *
890      * @param {VS.model.SearchFacet} facet
891      * @return {*} combined domains of the enabled filters in this group
892      */
893     get_domain: function (facet) {
894         var domains = _(facet.get('json')).chain()
895             .map(function (filter) { return filter.attrs.domain; })
896             .reject(_.isEmpty)
897             .value();
898
899         if (!domains.length) { return; }
900         if (domains.length === 1) { return domains[0]; }
901         for (var i=domains.length; --i;) {
902             domains.unshift(['|']);
903         }
904         return _.extend(new openerp.web.CompoundDomain(), {
905             __domains: domains
906         });
907     },
908     toggle_filter: function (e) {
909         this.toggle(this.filters[$(e.target).index()]);
910     },
911     toggle: function (filter) {
912         // FIXME: oh god, my eyes, they hurt
913         var self = this, fs;
914         var facet = this.view.vs.searchQuery.detect(function (f) {
915             return f.get('field') === self; });
916         if (facet) {
917             fs = facet.get('json');
918
919             if (_.include(fs, filter)) {
920                 fs = _.without(fs, filter);
921             } else {
922                 fs.push(filter);
923             }
924             if (_(fs).isEmpty()) {
925                 this.view.vs.searchQuery.remove(facet, {trigger_search: true});
926             } else {
927                 facet.set({
928                     json: fs,
929                     value: _(fs).map(function (f) {
930                         return f.attrs.string || f.attrs.name }).join(' | ')
931                 });
932             }
933             return;
934         } else {
935             fs = [filter];
936         }
937
938         this.view.vs.searchQuery.add({
939             category: _t("Filter"),
940             value: _(fs).map(function (f) {
941                 return f.attrs.string || f.attrs.name }).join(' | '),
942             json: fs,
943             field: this,
944             app: this.view.vs
945         });
946     }
947 });
948 openerp.web.search.Filter = openerp.web.search.Input.extend(/** @lends openerp.web.search.Filter# */{
949     template: 'SearchView.filter',
950     /**
951      * Implementation of the OpenERP filters (button with a context and/or
952      * a domain sent as-is to the search view)
953      *
954      * Filters are only attributes holder, the actual work (compositing
955      * domains and contexts, converting between facets and filters) is
956      * performed by the filter group.
957      *
958      * @constructs openerp.web.search.Filter
959      * @extends openerp.web.search.Input
960      *
961      * @param node
962      * @param view
963      */
964     init: function (node, view) {
965         this._super(view);
966         this.load_attrs(node.attrs);
967     },
968     facet_for: function () { return $.when(null); },
969     get_context: function () { },
970     get_domain: function () { },
971 });
972 openerp.web.search.Field = openerp.web.search.Input.extend( /** @lends openerp.web.search.Field# */ {
973     template: 'SearchView.field',
974     default_operator: '=',
975     /**
976      * @constructs openerp.web.search.Field
977      * @extends openerp.web.search.Input
978      *
979      * @param view_section
980      * @param field
981      * @param view
982      */
983     init: function (view_section, field, view) {
984         this._super(view);
985         this.load_attrs(_.extend({}, field, view_section.attrs));
986     },
987     facet_for: function (value) {
988         return $.when(new VS.model.SearchFacet({
989             category: this.attrs.string || this.attrs.name,
990             value: String(value),
991             json: value,
992             field: this,
993             app: this.view.vs
994         }));
995     },
996     get_value: function (facet) {
997         return facet.value();
998     },
999     get_context: function (facet) {
1000         var val = this.get_value(facet);
1001         // A field needs a value to be "active", and a context to send when
1002         // active
1003         var has_value = (val !== null && val !== '');
1004         var context = this.attrs.context;
1005         if (!(has_value && context)) {
1006             return;
1007         }
1008         return new openerp.web.CompoundContext(context)
1009                 .set_eval_context({self: val});
1010     },
1011     get_groupby: function () { },
1012     /**
1013      * Function creating the returned domain for the field, override this
1014      * methods in children if you only need to customize the field's domain
1015      * without more complex alterations or tests (and without the need to
1016      * change override the handling of filter_domain)
1017      *
1018      * @param {String} name the field's name
1019      * @param {String} operator the field's operator (either attribute-specified or default operator for the field
1020      * @param {Number|String} value parsed value for the field
1021      * @returns {Array<Array>} domain to include in the resulting search
1022      */
1023     make_domain: function (name, operator, facet) {
1024         return [[name, operator, this.get_value(facet)]];
1025     },
1026     get_domain: function (facet) {
1027         var val = this.get_value(facet);
1028         if (val === null || val === '') {
1029             return;
1030         }
1031
1032         var domain = this.attrs['filter_domain'];
1033         if (!domain) {
1034             return this.make_domain(
1035                 this.attrs.name,
1036                 this.attrs.operator || this.default_operator,
1037                 facet);
1038         }
1039         return new openerp.web.CompoundDomain(domain)
1040                 .set_eval_context({self: val});
1041     }
1042 });
1043 /**
1044  * Implementation of the ``char`` OpenERP field type:
1045  *
1046  * * Default operator is ``ilike`` rather than ``=``
1047  *
1048  * * The Javascript and the HTML values are identical (strings)
1049  *
1050  * @class
1051  * @extends openerp.web.search.Field
1052  */
1053 openerp.web.search.CharField = openerp.web.search.Field.extend( /** @lends openerp.web.search.CharField# */ {
1054     default_operator: 'ilike',
1055     complete: function (value) {
1056         if (_.isEmpty(value)) { return $.when(null); }
1057         var label = _.str.sprintf(_.str.escapeHTML(
1058             _t("Search %(field)s for: %(value)s")), {
1059                 field: '<em>' + this.attrs.string + '</em>',
1060                 value: '<strong>' + _.str.escapeHTML(value) + '</strong>'});
1061         return $.when([{
1062             category: this.attrs.string,
1063             label: label,
1064             value: value,
1065             field: this
1066         }]);
1067     }
1068 });
1069 openerp.web.search.NumberField = openerp.web.search.Field.extend(/** @lends openerp.web.search.NumberField# */{
1070     get_value: function () {
1071         if (!this.$element.val()) {
1072             return null;
1073         }
1074         var val = this.parse(this.$element.val()),
1075           check = Number(this.$element.val());
1076         if (isNaN(val) || val !== check) {
1077             this.$element.addClass('error');
1078             throw new openerp.web.search.Invalid(
1079                 this.attrs.name, this.$element.val(), this.error_message);
1080         }
1081         this.$element.removeClass('error');
1082         return val;
1083     }
1084 });
1085 /**
1086  * @class
1087  * @extends openerp.web.search.NumberField
1088  */
1089 openerp.web.search.IntegerField = openerp.web.search.NumberField.extend(/** @lends openerp.web.search.IntegerField# */{
1090     error_message: _t("not a valid integer"),
1091     parse: function (value) {
1092         try {
1093             return openerp.web.parse_value(value, {'widget': 'integer'});
1094         } catch (e) {
1095             return NaN;
1096         }
1097     }
1098 });
1099 /**
1100  * @class
1101  * @extends openerp.web.search.NumberField
1102  */
1103 openerp.web.search.FloatField = openerp.web.search.NumberField.extend(/** @lends openerp.web.search.FloatField# */{
1104     error_message: _t("not a valid number"),
1105     parse: function (value) {
1106         try {
1107             return openerp.web.parse_value(value, {'widget': 'float'});
1108         } catch (e) {
1109             return NaN;
1110         }
1111     }
1112 });
1113 /**
1114  * @class
1115  * @extends openerp.web.search.Field
1116  */
1117 openerp.web.search.SelectionField = openerp.web.search.Field.extend(/** @lends openerp.web.search.SelectionField# */{
1118     // This implementation is a basic <select> field, but it may have to be
1119     // altered to be more in line with the GTK client, which uses a combo box
1120     // (~ jquery.autocomplete):
1121     // * If an option was selected in the list, behave as currently
1122     // * If something which is not in the list was entered (via the text input),
1123     //   the default domain should become (`ilike` string_value) but **any
1124     //   ``context`` or ``filter_domain`` becomes falsy, idem if ``@operator``
1125     //   is specified. So at least get_domain needs to be quite a bit
1126     //   overridden (if there's no @value and there is no filter_domain and
1127     //   there is no @operator, return [[name, 'ilike', str_val]]
1128     template: 'SearchView.field.selection',
1129     init: function () {
1130         this._super.apply(this, arguments);
1131         // prepend empty option if there is no empty option in the selection list
1132         this.prepend_empty = !_(this.attrs.selection).detect(function (item) {
1133             return !item[1];
1134         });
1135     },
1136     complete: function (needle) {
1137         var self = this;
1138         var results = _(this.attrs.selection).chain()
1139             .filter(function (sel) {
1140                 var value = sel[0], label = sel[1];
1141                 if (!value) { return false; }
1142                 return label.toLowerCase().indexOf(needle.toLowerCase()) !== -1;
1143             })
1144             .map(function (sel) {
1145                 return {
1146                     category: self.attrs.string,
1147                     field: self,
1148                     value: sel[1],
1149                     json: sel[0]
1150                 };
1151             }).value();
1152         if (_.isEmpty(results)) { return $.when(null); }
1153         return $.when.apply(null, [{
1154             category: this.attrs.string
1155         }].concat(results));
1156     },
1157     facet_for: function (value) {
1158         var match = _(this.attrs.selection).detect(function (sel) {
1159             return sel[0] === value;
1160         });
1161         if (!match) { return $.when(null); }
1162         return $.when(new VS.model.SearchFacet({
1163             category: this.attrs.string,
1164             value: match[1],
1165             json: match[0],
1166             field: this,
1167             app: this.view.app
1168         }));
1169     },
1170     get_value: function (facet) {
1171         return facet.get('json');
1172     }
1173 });
1174 openerp.web.search.BooleanField = openerp.web.search.SelectionField.extend(/** @lends openerp.web.search.BooleanField# */{
1175     /**
1176      * @constructs openerp.web.search.BooleanField
1177      * @extends openerp.web.search.BooleanField
1178      */
1179     init: function () {
1180         this._super.apply(this, arguments);
1181         this.attrs.selection = [
1182             ['true', _t("Yes")],
1183             ['false', _t("No")]
1184         ];
1185     },
1186     get_value: function (facet) {
1187         switch (this._super(facet)) {
1188             case 'false': return false;
1189             case 'true': return true;
1190             default: return null;
1191         }
1192     }
1193 });
1194 /**
1195  * @class
1196  * @extends openerp.web.search.DateField
1197  */
1198 openerp.web.search.DateField = openerp.web.search.Field.extend(/** @lends openerp.web.search.DateField# */{
1199     get_value: function (facet) {
1200         return openerp.web.date_to_str(facet.get('json'));
1201     },
1202     complete: function (needle) {
1203         var d = Date.parse(needle);
1204         if (!d) { return $.when(null); }
1205         var value = openerp.web.format_value(d, this.attrs);
1206         var label = _.str.sprintf(_.str.escapeHTML(
1207             _t("Search %(field)s at: %(value)s")), {
1208                 field: '<em>' + this.attrs.string + '</em>',
1209                 value: '<strong>' + value + '</strong>'});
1210         return $.when([{
1211             category: this.attrs.string,
1212             label: label,
1213             value: value,
1214             json: d,
1215             field: this
1216         }]);
1217     }
1218 });
1219 /**
1220  * Implementation of the ``datetime`` openerp field type:
1221  *
1222  * * Uses the same widget as the ``date`` field type (a simple date)
1223  *
1224  * * Builds a slighly more complex, it's a datetime range (includes time)
1225  *   spanning the whole day selected by the date widget
1226  *
1227  * @class
1228  * @extends openerp.web.DateField
1229  */
1230 openerp.web.search.DateTimeField = openerp.web.search.DateField.extend(/** @lends openerp.web.search.DateTimeField# */{
1231     get_value: function (facet) {
1232         return openerp.web.datetime_to_str(facet.get('json'));
1233     }
1234 });
1235 openerp.web.search.ManyToOneField = openerp.web.search.CharField.extend({
1236     init: function (view_section, field, view) {
1237         this._super(view_section, field, view);
1238         this.model = new openerp.web.Model(this.attrs.relation);
1239     },
1240     complete: function (needle) {
1241         var self = this;
1242         // TODO: context
1243         // FIXME: "concurrent" searches (multiple requests, mis-ordered responses)
1244         return this.model.call('name_search', [], {
1245             name: needle,
1246             limit: 8,
1247             context: {}
1248         }).pipe(function (results) {
1249             if (_.isEmpty(results)) { return null; }
1250             return [{category: self.attrs.string}].concat(
1251                 _(results).map(function (result) {
1252                     return {
1253                         category: self.attrs.string,
1254                         value: result[1],
1255                         json: result[0],
1256                         field: self
1257                     };
1258                 }));
1259         });
1260     },
1261     facet_for: function (value) {
1262         var self = this;
1263         if (value instanceof Array) {
1264             return $.when(new VS.model.SearchFacet({
1265                 category: this.attrs.string,
1266                 value: value[1],
1267                 json: value[0],
1268                 field: this,
1269                 app: this.view.vs
1270             }));
1271         }
1272         return this.model.call('name_get', [value], {}).pipe(function (names) {
1273                 return new VS.model.SearchFacet({
1274                 category: self.attrs.string,
1275                 value: names[0][1],
1276                 json: names[0][0],
1277                 field: self,
1278                 app: self.view.vs
1279             });
1280         })
1281     },
1282     make_domain: function (name, operator, facet) {
1283         // ``json`` -> actual auto-completed id
1284         if (facet.get('json')) {
1285             return [[name, '=', facet.get('json')]];
1286         }
1287
1288         return this._super(name, operator, facet);
1289     }
1290 });
1291
1292 openerp.web.search.Advanced = openerp.web.search.Input.extend({
1293     template: 'SearchView.advanced',
1294     start: function () {
1295         var self = this;
1296         this.$element
1297             .on('keypress keydown keyup', function (e) { e.stopPropagation(); })
1298             .on('click', 'h4', function () {
1299                 self.$element.toggleClass('oe_opened');
1300             }).on('click', 'button.oe_add_condition', function () {
1301                 self.append_proposition();
1302             }).on('submit', 'form', function (e) {
1303                 e.preventDefault();
1304                 self.commit_search();
1305             });
1306         return $.when(
1307             this._super(),
1308             this.rpc("/web/searchview/fields_get", {model: this.view.model}, function(data) {
1309                 self.fields = _.extend({
1310                     id: { string: 'ID', type: 'id' }
1311                 }, data.fields);
1312         })).then(function () {
1313             self.append_proposition();
1314         });
1315     },
1316     append_proposition: function () {
1317         return (new openerp.web.search.ExtendedSearchProposition(this, this.fields))
1318             .appendTo(this.$element.find('ul'));
1319     },
1320     commit_search: function () {
1321         var self = this;
1322         // Get domain sections from all propositions
1323         var children = this.getChildren(),
1324             domain = _.invoke(children, 'get_proposition');
1325         var filters = _(domain).map(function (section) {
1326             return new openerp.web.search.Filter({attrs: {
1327                 string: _.str.sprintf('%s(%s)%s',
1328                     section[0], section[1], section[2]),
1329                 domain: [section]
1330             }}, self.view);
1331         });
1332         // Create Filter (& FilterGroup around it) with that domain
1333         var f = new openerp.web.search.FilterGroup(filters, this.view);
1334         // add group to query
1335         this.view.vs.searchQuery.add({
1336             category: _t("Advanced"),
1337             value: _(filters).map(function (f) {
1338                 return f.attrs.string || f.attrs.name }).join(' | '),
1339             json: filters,
1340             field: f,
1341             app: this.view.vs
1342         });
1343         // remove all propositions
1344         _.invoke(children, 'destroy');
1345         // add new empty proposition
1346         this.append_proposition();
1347         // TODO: API on searchview
1348         this.view.$element.removeClass('oe_searchview_open_drawer');
1349     }
1350 });
1351
1352 openerp.web.search.ExtendedSearchProposition = openerp.web.OldWidget.extend(/** @lends openerp.web.search.ExtendedSearchProposition# */{
1353     template: 'SearchView.extended_search.proposition',
1354     /**
1355      * @constructs openerp.web.search.ExtendedSearchProposition
1356      * @extends openerp.web.OldWidget
1357      *
1358      * @param parent
1359      * @param fields
1360      */
1361     init: function (parent, fields) {
1362         this._super(parent);
1363         this.fields = _(fields).chain()
1364             .map(function(val, key) { return _.extend({}, val, {'name': key}); })
1365             .sortBy(function(field) {return field.string;})
1366             .value();
1367         this.attrs = {_: _, fields: this.fields, selected: null};
1368         this.value = null;
1369     },
1370     start: function () {
1371         var _this = this;
1372         this.$element.find(".searchview_extended_prop_field").change(function() {
1373             _this.changed();
1374         });
1375         this.$element.find('.searchview_extended_delete_prop').click(function () {
1376             _this.destroy();
1377         });
1378         this.changed();
1379     },
1380     changed: function() {
1381         var nval = this.$element.find(".searchview_extended_prop_field").val();
1382         if(this.attrs.selected == null || nval != this.attrs.selected.name) {
1383             this.select_field(_.detect(this.fields, function(x) {return x.name == nval;}));
1384         }
1385     },
1386     /**
1387      * Selects the provided field object
1388      *
1389      * @param field a field descriptor object (as returned by fields_get, augmented by the field name)
1390      */
1391     select_field: function(field) {
1392         var self = this;
1393         if(this.attrs.selected != null) {
1394             this.value.destroy();
1395             this.value = null;
1396             this.$element.find('.searchview_extended_prop_op').html('');
1397         }
1398         this.attrs.selected = field;
1399         if(field == null) {
1400             return;
1401         }
1402
1403         var type = field.type;
1404         var obj = openerp.web.search.custom_filters.get_object(type);
1405         if(obj === null) {
1406             console.log('Unknow field type ' + e.key);
1407             obj = openerp.web.search.custom_filters.get_object("char");
1408         }
1409         this.value = new (obj) (this);
1410         if(this.value.set_field) {
1411             this.value.set_field(field);
1412         }
1413         _.each(this.value.operators, function(operator) {
1414             $('<option>', {value: operator.value})
1415                 .text(String(operator.text))
1416                 .appendTo(self.$element.find('.searchview_extended_prop_op'));
1417         });
1418         this.$element.find('.searchview_extended_prop_value').html(
1419             this.value.render({}));
1420         this.value.start();
1421
1422     },
1423     get_proposition: function() {
1424         if ( this.attrs.selected == null)
1425             return null;
1426         var field = this.attrs.selected.name;
1427         var op =  this.$element.find('.searchview_extended_prop_op').val();
1428         var value = this.value.get_value();
1429         return [field, op, value];
1430     }
1431 });
1432
1433 openerp.web.search.ExtendedSearchProposition.Field = openerp.web.OldWidget.extend({
1434     start: function () {
1435         this.$element = $("#" + this.element_id);
1436     }
1437 });
1438 openerp.web.search.ExtendedSearchProposition.Char = openerp.web.search.ExtendedSearchProposition.Field.extend({
1439     template: 'SearchView.extended_search.proposition.char',
1440     operators: [
1441         {value: "ilike", text: _lt("contains")},
1442         {value: "not ilike", text: _lt("doesn't contain")},
1443         {value: "=", text: _lt("is equal to")},
1444         {value: "!=", text: _lt("is not equal to")}
1445     ],
1446     get_value: function() {
1447         return this.$element.val();
1448     }
1449 });
1450 openerp.web.search.ExtendedSearchProposition.DateTime = openerp.web.search.ExtendedSearchProposition.Field.extend({
1451     template: 'SearchView.extended_search.proposition.empty',
1452     operators: [
1453         {value: "=", text: _lt("is equal to")},
1454         {value: "!=", text: _lt("is not equal to")},
1455         {value: ">", text: _lt("greater than")},
1456         {value: "<", text: _lt("less than")},
1457         {value: ">=", text: _lt("greater or equal than")},
1458         {value: "<=", text: _lt("less or equal than")}
1459     ],
1460     get_value: function() {
1461         return this.datewidget.get_value();
1462     },
1463     start: function() {
1464         this._super();
1465         this.datewidget = new openerp.web.DateTimeWidget(this);
1466         this.datewidget.prependTo(this.$element);
1467     }
1468 });
1469 openerp.web.search.ExtendedSearchProposition.Date = openerp.web.search.ExtendedSearchProposition.Field.extend({
1470     template: 'SearchView.extended_search.proposition.empty',
1471     operators: [
1472         {value: "=", text: _lt("is equal to")},
1473         {value: "!=", text: _lt("is not equal to")},
1474         {value: ">", text: _lt("greater than")},
1475         {value: "<", text: _lt("less than")},
1476         {value: ">=", text: _lt("greater or equal than")},
1477         {value: "<=", text: _lt("less or equal than")}
1478     ],
1479     get_value: function() {
1480         return this.datewidget.get_value();
1481     },
1482     start: function() {
1483         this._super();
1484         this.datewidget = new openerp.web.DateWidget(this);
1485         this.datewidget.prependTo(this.$element);
1486     }
1487 });
1488 openerp.web.search.ExtendedSearchProposition.Integer = openerp.web.search.ExtendedSearchProposition.Field.extend({
1489     template: 'SearchView.extended_search.proposition.integer',
1490     operators: [
1491         {value: "=", text: _lt("is equal to")},
1492         {value: "!=", text: _lt("is not equal to")},
1493         {value: ">", text: _lt("greater than")},
1494         {value: "<", text: _lt("less than")},
1495         {value: ">=", text: _lt("greater or equal than")},
1496         {value: "<=", text: _lt("less or equal than")}
1497     ],
1498     get_value: function() {
1499         try {
1500             return openerp.web.parse_value(this.$element.val(), {'widget': 'integer'});
1501         } catch (e) {
1502             return "";
1503         }
1504     }
1505 });
1506 openerp.web.search.ExtendedSearchProposition.Id = openerp.web.search.ExtendedSearchProposition.Integer.extend({
1507     operators: [{value: "=", text: _lt("is")}]
1508 });
1509 openerp.web.search.ExtendedSearchProposition.Float = openerp.web.search.ExtendedSearchProposition.Field.extend({
1510     template: 'SearchView.extended_search.proposition.float',
1511     operators: [
1512         {value: "=", text: _lt("is equal to")},
1513         {value: "!=", text: _lt("is not equal to")},
1514         {value: ">", text: _lt("greater than")},
1515         {value: "<", text: _lt("less than")},
1516         {value: ">=", text: _lt("greater or equal than")},
1517         {value: "<=", text: _lt("less or equal than")}
1518     ],
1519     get_value: function() {
1520         try {
1521             return openerp.web.parse_value(this.$element.val(), {'widget': 'float'});
1522         } catch (e) {
1523             return "";
1524         }
1525     }
1526 });
1527 openerp.web.search.ExtendedSearchProposition.Selection = openerp.web.search.ExtendedSearchProposition.Field.extend({
1528     template: 'SearchView.extended_search.proposition.selection',
1529     operators: [
1530         {value: "=", text: _lt("is")},
1531         {value: "!=", text: _lt("is not")}
1532     ],
1533     set_field: function(field) {
1534         this.field = field;
1535     },
1536     get_value: function() {
1537         return this.$element.val();
1538     }
1539 });
1540 openerp.web.search.ExtendedSearchProposition.Boolean = openerp.web.search.ExtendedSearchProposition.Field.extend({
1541     template: 'SearchView.extended_search.proposition.boolean',
1542     operators: [
1543         {value: "=", text: _lt("is true")},
1544         {value: "!=", text: _lt("is false")}
1545     ],
1546     get_value: function() {
1547         return true;
1548     }
1549 });
1550
1551 openerp.web.search.custom_filters = new openerp.web.Registry({
1552     'char': 'openerp.web.search.ExtendedSearchProposition.Char',
1553     'text': 'openerp.web.search.ExtendedSearchProposition.Char',
1554     'one2many': 'openerp.web.search.ExtendedSearchProposition.Char',
1555     'many2one': 'openerp.web.search.ExtendedSearchProposition.Char',
1556     'many2many': 'openerp.web.search.ExtendedSearchProposition.Char',
1557
1558     'datetime': 'openerp.web.search.ExtendedSearchProposition.DateTime',
1559     'date': 'openerp.web.search.ExtendedSearchProposition.Date',
1560     'integer': 'openerp.web.search.ExtendedSearchProposition.Integer',
1561     'float': 'openerp.web.search.ExtendedSearchProposition.Float',
1562     'boolean': 'openerp.web.search.ExtendedSearchProposition.Boolean',
1563     'selection': 'openerp.web.search.ExtendedSearchProposition.Selection',
1564
1565     'id': 'openerp.web.search.ExtendedSearchProposition.Id'
1566 });
1567
1568 };
1569
1570 // vim:et fdc=0 fdl=0 foldnestmax=3 fdm=syntax: