1 openerp.web.search = function(instance) {
2 var QWeb = instance.web.qweb,
4 _lt = instance.web._lt;
6 sum: function (obj) { return _.reduce(obj, function (a, b) { return a + b; }, 0); }
10 var my = instance.web.search = {};
13 my.FacetValue = B.Model.extend({
16 my.FacetValues = B.Collection.extend({
19 my.Facet = B.Model.extend({
20 initialize: function (attrs) {
21 var values = attrs.values;
24 B.Model.prototype.initialize.apply(this, arguments);
26 this.values = new my.FacetValues(values || []);
27 this.values.on('add remove change reset', function () {
28 this.trigger('change', this);
32 if (key !== 'values') {
33 return B.Model.prototype.get.call(this, key);
35 return this.values.toJSON();
37 set: function (key, value) {
38 if (key !== 'values') {
39 return B.Model.prototype.set.call(this, key, value);
41 this.values.reset(value);
45 var attrs = this.attributes;
46 for(var att in attrs) {
47 if (!attrs.hasOwnProperty(att) || att === 'field') {
50 out[att] = attrs[att];
52 out.values = this.values.toJSON();
56 my.SearchQuery = B.Collection.extend({
58 initialize: function () {
59 B.Collection.prototype.initialize.apply(
61 this.on('change', function (facet) {
62 if(!facet.values.isEmpty()) { return; }
64 this.remove(facet, {silent: true});
67 add: function (values, options) {
68 options || (options = {});
69 if (!(values instanceof Array)) {
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');
80 previous.values.add(model.get('values'));
83 B.Collection.prototype.add.call(this, model, options);
87 toggle: function (value, options) {
88 options || (options = {});
90 var facet = this.detect(function (facet) {
91 return facet.get('category') === value.category
92 && facet.get('field') === value.field;
95 return this.add(value, options);
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;
106 facet.values.remove(already_value, {silent: true});
108 facet.values.add(val, {silent: true});
112 // "Commit" changes to values array as a single call, so observers of
113 // change event don't get misled by intermediate incomplete toggling
115 facet.trigger('change', facet);
120 function assert(condition, message) {
122 throw new Error(message);
125 my.InputView = instance.web.Widget.extend({
126 template: 'SearchView.InputView',
128 focus: function () { this.trigger('focused', this); },
129 blur: function () { this.$el.text(''); this.trigger('blurred', this); },
130 keydown: 'onKeydown',
133 getSelection: function () {
136 var root = this.el.childNodes[0];
137 if (!root || !root.textContent) {
138 // if input does not have a child node, or the child node is an
139 // empty string, then the selection can only be (0, 0)
140 return {start: 0, end: 0};
142 var range = window.getSelection().getRangeAt(0);
143 // In Firefox, depending on the way text is selected (drag, double- or
144 // triple-click) the range may start or end on the parent of the
145 // selected text node‽ Check for this condition and fixup the range
146 // note: apparently with C-a this can go even higher?
147 if (range.startContainer === this.el && range.startOffset === 0) {
148 range.setStart(root, 0);
150 if (range.endContainer === this.el && range.endOffset === 1) {
151 range.setEnd(root, root.length)
153 assert(range.startContainer === root,
154 "selection should be in the input view");
155 assert(range.endContainer === root,
156 "selection should be in the input view");
158 start: range.startOffset,
162 onKeydown: function (e) {
166 // Do not insert newline, but let it bubble so searchview can use it
167 case $.ui.keyCode.ENTER:
171 // FIXME: may forget content if non-empty but caret at index 0, ok?
172 case $.ui.keyCode.BACKSPACE:
173 sel = this.getSelection();
174 if (sel.start === 0 && sel.start === sel.end) {
176 var preceding = this.getParent().siblingSubview(this, -1);
177 if (preceding && (preceding instanceof my.FacetView)) {
178 preceding.model.destroy();
183 // let left/right events propagate to view if caret is at input border
184 // and not a selection
185 case $.ui.keyCode.LEFT:
186 sel = this.getSelection();
187 if (sel.start !== 0 || sel.start !== sel.end) {
191 case $.ui.keyCode.RIGHT:
192 sel = this.getSelection();
193 var len = this.$el.text().length;
194 if (sel.start !== len || sel.start !== sel.end) {
200 setCursorAtEnd: function () {
202 var sel = window.getSelection();
203 sel.removeAllRanges();
204 var range = document.createRange();
205 // in theory, range.selectNodeContents should work here. In practice,
206 // MSIE9 has issues from time to time, instead of selecting the inner
207 // text node it would select the reference node instead (e.g. in demo
208 // data, company news, copy across the "Company News" link + the title,
209 // from about half the link to half the text, paste in search box then
210 // hit the left arrow key, getSelection would blow up).
212 // Explicitly selecting only the inner text node (only child node
213 // since we've normalized the parent) avoids the issue
214 range.selectNode(this.el.childNodes[0]);
215 range.collapse(false);
218 onPaste: function () {
220 // In MSIE and Webkit, it is possible to get various representations of
221 // the clipboard data at this point e.g.
222 // window.clipboardData.getData('Text') and
223 // event.clipboardData.getData('text/plain') to ensure we have a plain
224 // text representation of the object (and probably ensure the object is
225 // pastable as well, so nobody puts an image in the search view)
226 // (nb: since it's not possible to alter the content of the clipboard
227 // — at least in Webkit — to ensure only textual content is available,
228 // using this would require 1. getting the text data; 2. manually
229 // inserting the text data into the content; and 3. cancelling the
232 // But Firefox doesn't support the clipboard API (as of FF18)
233 // although it correctly triggers the paste event (Opera does not even
234 // do that) => implement lowest-denominator system where onPaste
235 // triggers a followup "cleanup" pass after the data has been pasted
236 setTimeout(function () {
237 // Read text content (ignore pasted HTML)
238 var data = this.$el.text();
239 // paste raw text back in
240 this.$el.empty().text(data);
242 // Set the cursor at the end of the text, so the cursor is not lost
243 // in some kind of error-spawning limbo.
244 this.setCursorAtEnd();
248 my.FacetView = instance.web.Widget.extend({
249 template: 'SearchView.FacetView',
251 'focus': function () { this.trigger('focused', this); },
252 'blur': function () { this.trigger('blurred', this); },
253 'click': function (e) {
254 if ($(e.target).is('.oe_facet_remove')) {
255 this.model.destroy();
261 'keydown': function (e) {
262 var keys = $.ui.keyCode;
266 this.model.destroy();
271 init: function (parent, model) {
274 this.model.on('change', this.model_changed, this);
276 destroy: function () {
277 this.model.off('change', this.model_changed, this);
282 var $e = this.$('> span:last-child');
283 return $.when(this._super()).then(function () {
284 return $.when.apply(null, self.model.values.map(function (value) {
285 return new my.FacetValueView(self, value).appendTo($e);
289 model_changed: function () {
290 this.$el.text(this.$el.text() + '*');
293 my.FacetValueView = instance.web.Widget.extend({
294 template: 'SearchView.FacetView.Value',
295 init: function (parent, model) {
298 this.model.on('change', this.model_changed, this);
300 destroy: function () {
301 this.model.off('change', this.model_changed, this);
304 model_changed: function () {
305 this.$el.text(this.$el.text() + '*');
309 instance.web.SearchView = instance.web.Widget.extend(/** @lends instance.web.SearchView# */{
310 template: "SearchView",
312 // focus last input if view itself is clicked
313 'click': function (e) {
314 if (e.target === this.$('.oe_searchview_facets')[0]) {
315 this.$('.oe_searchview_input:last').focus();
319 'click button.oe_searchview_search': function (e) {
320 e.stopImmediatePropagation();
323 'click .oe_searchview_clear': function (e) {
324 e.stopImmediatePropagation();
327 'click .oe_searchview_unfold_drawer': function (e) {
328 e.stopImmediatePropagation();
329 this.$el.toggleClass('oe_searchview_open_drawer');
331 'keydown .oe_searchview_input, .oe_searchview_facet': function (e) {
333 case $.ui.keyCode.LEFT:
334 this.focusPreceding(e.target);
337 case $.ui.keyCode.RIGHT:
338 this.focusFollowing(e.target);
343 'autocompleteopen': function () {
344 this.$el.autocomplete('widget').css('z-index', 9999);
348 * @constructs instance.web.SearchView
349 * @extends instance.web.Widget
355 * @param {Object} [options]
356 * @param {Boolean} [options.hidden=false] hide the search view
357 * @param {Boolean} [options.disable_custom_filters=false] do not load custom filters from ir.filters
359 init: function(parent, dataset, view_id, defaults, options) {
360 // Backward compatibility - Can be removed when forward porting
361 if (Object(options) !== options) {
366 // End of Backward compatibility
367 this.options = _.defaults(options || {}, {
369 disable_custom_filters: false,
372 this.dataset = dataset;
373 this.model = dataset.model;
374 this.view_id = view_id;
376 this.defaults = defaults || {};
377 this.has_defaults = !_.isEmpty(this.defaults);
382 this.headless = this.options.hidden && !this.has_defaults;
384 this.input_subviews = [];
386 this.ready = $.Deferred();
390 var p = this._super();
392 this.setup_global_completion();
393 this.query = new my.SearchQuery()
394 .on('add change reset remove', this.proxy('do_search'))
395 .on('add change reset remove', this.proxy('renderFacets'));
397 if (this.options.hidden) {
401 this.ready.resolve();
403 var load_view = instance.web.fields_view_get({
404 model: this.dataset._model,
405 view_id: this.view_id,
407 context: this.dataset.get_context(),
410 this.alive($.when(load_view)).then(function (r) {
411 return self.search_view_loaded(r)
412 }).fail(function () {
413 self.ready.reject.apply(null, arguments);
417 instance.web.bus.on('click', this, function(ev) {
418 if ($(ev.target).parents('.oe_searchview').length === 0) {
419 self.$el.removeClass('oe_searchview_open_drawer');
423 return $.when(p, this.ready);
432 subviewForRoot: function (subview_root) {
433 return _(this.input_subviews).detect(function (subview) {
434 return subview.$el[0] === subview_root;
437 siblingSubview: function (subview, direction, wrap_around) {
438 var index = _(this.input_subviews).indexOf(subview) + direction;
439 if (wrap_around && index < 0) {
440 index = this.input_subviews.length - 1;
441 } else if (wrap_around && index >= this.input_subviews.length) {
444 return this.input_subviews[index];
446 focusPreceding: function (subview_root) {
447 return this.siblingSubview(
448 this.subviewForRoot(subview_root), -1, true)
451 focusFollowing: function (subview_root) {
452 return this.siblingSubview(
453 this.subviewForRoot(subview_root), +1, true)
458 * Sets up thingie where all the mess is put?
460 select_for_drawer: function () {
461 return _(this.inputs).filter(function (input) {
462 return input.in_drawer();
466 * Sets up search view's view-wide auto-completion widget
468 setup_global_completion: function () {
469 var autocomplete = this.$el.autocomplete({
470 source: this.proxy('complete_global_search'),
471 select: this.proxy('select_completion'),
472 focus: function (e) { e.preventDefault(); },
477 }).data('autocomplete');
479 this.$el.on('input', function () {
480 this.$el.autocomplete('close');
483 // MonkeyPatch autocomplete instance
484 _.extend(autocomplete, {
485 _renderItem: function (ul, item) {
486 // item of completion list
487 var $item = $( "<li></li>" )
488 .data( "item.autocomplete", item )
491 if (item.facet !== undefined) {
492 // regular completion item
495 ? $('<a>').html(item.label)
496 : $('<a>').text(item.value));
498 return $item.text(item.label)
500 borderTop: '1px solid #cccccc',
510 return self.$('div.oe_searchview_input').text();
515 * Provide auto-completion result for req.term (an array to `resp`)
517 * @param {Object} req request to complete
518 * @param {String} req.term searched term to complete
519 * @param {Function} resp response callback
521 complete_global_search: function (req, resp) {
522 $.when.apply(null, _(this.inputs).chain()
523 .filter(function (input) { return input.visible(); })
524 .invoke('complete', req.term)
525 .value()).then(function () {
526 resp(_(_(arguments).compact()).flatten(true));
531 * Action to perform in case of selection: create a facet (model)
532 * and add it to the search collection
534 * @param {Object} e selection event, preventDefault to avoid setting value on object
535 * @param {Object} ui selection information
536 * @param {Object} ui.item selected completion item
538 select_completion: function (e, ui) {
541 var input_index = _(this.input_subviews).indexOf(
543 this.$('div.oe_searchview_input:focus')[0]));
544 this.query.add(ui.item.facet, {at: input_index / 2});
546 childFocused: function () {
547 this.$el.addClass('oe_focused');
549 childBlurred: function () {
550 var val = this.$el.val();
552 var complete = this.$el.data('autocomplete');
553 if ((val && complete.term === undefined) || complete.previous) {
554 throw new Error("new jquery.ui version altering implementation" +
555 " details relied on");
557 delete complete.term;
558 this.$el.removeClass('oe_focused')
563 * @param {openerp.web.search.SearchQuery | openerp.web.search.Facet} _1
564 * @param {openerp.web.search.Facet} [_2]
565 * @param {Object} [options]
567 renderFacets: function (_1, _2, options) {
568 // _1: model if event=change, otherwise collection
569 // _2: undefined if event=change, otherwise model
572 var $e = this.$('div.oe_searchview_facets');
573 _.invoke(this.input_subviews, 'destroy');
574 this.input_subviews = [];
576 var i = new my.InputView(this);
577 started.push(i.appendTo($e));
578 this.input_subviews.push(i);
579 this.query.each(function (facet) {
580 var f = new my.FacetView(this, facet);
581 started.push(f.appendTo($e));
582 self.input_subviews.push(f);
584 var i = new my.InputView(this);
585 started.push(i.appendTo($e));
586 self.input_subviews.push(i);
588 _.each(this.input_subviews, function (childView) {
589 childView.on('focused', self, self.proxy('childFocused'));
590 childView.on('blurred', self, self.proxy('childBlurred'));
593 $.when.apply(null, started).then(function () {
595 // options.at: facet inserted at given index, focus next input
596 // otherwise just focus last input
597 if (!options || typeof options.at !== 'number') {
598 input_to_focus = _.last(self.input_subviews);
600 input_to_focus = self.input_subviews[(options.at + 1) * 2];
603 input_to_focus.$el.focus();
608 * Builds a list of widget rows (each row is an array of widgets)
610 * @param {Array} items a list of nodes to convert to widgets
611 * @param {Object} fields a mapping of field names to (ORM) field attributes
612 * @param {Object} [group] group to put the new controls in
614 make_widgets: function (items, fields, group) {
616 group = new instance.web.search.Group(
617 this, 'q', {attrs: {string: _t("Filters")}});
621 _.each(items, function (item) {
622 if (filters.length && item.tag !== 'filter') {
623 group.push(new instance.web.search.FilterGroup(filters, group));
628 case 'separator': case 'newline':
631 filters.push(new instance.web.search.Filter(item, group));
634 self.make_widgets(item.children, fields,
635 new instance.web.search.Group(group, 'w', item));
638 var field = this.make_field(
639 item, fields[item['attrs'].name], group);
642 self.make_widgets(item.children, fields, group);
647 if (filters.length) {
648 group.push(new instance.web.search.FilterGroup(filters, this));
652 * Creates a field for the provided field descriptor item (which comes
653 * from fields_view_get)
655 * @param {Object} item fields_view_get node for the field
656 * @param {Object} field fields_get result for the field
657 * @param {Object} [parent]
658 * @returns instance.web.search.Field
660 make_field: function (item, field, parent) {
661 var obj = instance.web.search.fields.get_any( [item.attrs.widget, field.type]);
663 return new (obj) (item, field, parent || this);
665 console.group('Unknown field type ' + field.type);
666 console.error('View node', item);
667 console.info('View field', field);
668 console.info('In view', this);
674 add_common_inputs: function() {
675 // add Filters to this.inputs, need view.controls filled
676 (new instance.web.search.Filters(this));
677 // add custom filters to this.inputs
678 this.custom_filters = new instance.web.search.CustomFilters(this);
679 // add Advanced to this.inputs
680 (new instance.web.search.Advanced(this));
683 search_view_loaded: function(data) {
685 this.fields_view = data;
686 if (data.type !== 'search' ||
687 data.arch.tag !== 'search') {
688 throw new Error(_.str.sprintf(
689 "Got non-search view after asking for a search view: type %s, arch root %s",
690 data.type, data.arch.tag));
693 data['arch'].children,
696 this.add_common_inputs();
699 var drawer_started = $.when.apply(
700 null, _(this.select_for_drawer()).invoke(
701 'appendTo', this.$('.oe_searchview_drawer')));
705 var defaults_fetched = $.when.apply(null, _(this.inputs).invoke(
706 'facet_for_defaults', this.defaults))
707 .then(this.proxy('setup_default_query'));
709 return $.when(drawer_started, defaults_fetched)
711 self.trigger("search_view_loaded", data);
712 self.ready.resolve();
715 setup_default_query: function () {
716 // Hacky implementation of CustomFilters#facet_for_defaults ensure
717 // CustomFilters will be ready (and CustomFilters#filters will be
718 // correctly filled) by the time this method executes.
719 var custom_filters = this.custom_filters.filters;
720 if (!this.options.disable_custom_filters && !_(custom_filters).isEmpty()) {
721 // Check for any is_default custom filter
722 var personal_filter = _(custom_filters).find(function (filter) {
723 return filter.user_id && filter.is_default;
725 if (personal_filter) {
726 this.custom_filters.toggle_filter(personal_filter, true);
730 var global_filter = _(custom_filters).find(function (filter) {
731 return !filter.user_id && filter.is_default;
734 this.custom_filters.toggle_filter(global_filter, true);
738 // No custom filter, or no is_default custom filter, apply view defaults
739 this.query.reset(_(arguments).compact(), {preventSearch: true});
742 * Extract search data from the view's facets.
744 * Result is an object with 4 (own) properties:
747 * An array of any error generated during data validation and
748 * extraction, contains the validation error objects
754 * Array of domains, in groupby order rather than view order
758 build_search_data: function () {
759 var domains = [], contexts = [], groupbys = [], errors = [];
761 this.query.each(function (facet) {
762 var field = facet.get('field');
764 var domain = field.get_domain(facet);
766 domains.push(domain);
768 var context = field.get_context(facet);
770 contexts.push(context);
772 var group_by = field.get_groupby(facet);
774 groupbys.push.apply(groupbys, group_by);
777 if (e instanceof instance.web.search.Invalid) {
792 * Performs the search view collection of widget data.
794 * If the collection went well (all fields are valid), then triggers
795 * :js:func:`instance.web.SearchView.on_search`.
797 * If at least one field failed its validation, triggers
798 * :js:func:`instance.web.SearchView.on_invalid` instead.
801 * @param {Object} [options]
803 do_search: function (_query, options) {
804 if (options && options.preventSearch) {
807 var search = this.build_search_data();
808 if (!_.isEmpty(search.errors)) {
809 this.on_invalid(search.errors);
812 this.trigger('search_data', search.domains, search.contexts, search.groupbys);
815 * Triggered after the SearchView has collected all relevant domains and
818 * It is provided with an Array of domains and an Array of contexts, which
819 * may or may not be evaluated (each item can be either a valid domain or
820 * context, or a string to evaluate in order in the sequence)
822 * It is also passed an array of contexts used for group_by (they are in
823 * the correct order for group_by evaluation, which contexts may not be)
826 * @param {Array} domains an array of literal domains or domain references
827 * @param {Array} contexts an array of literal contexts or context refs
828 * @param {Array} groupbys ordered contexts which may or may not have group_by keys
831 * Triggered after a validation error in the SearchView fields.
833 * Error objects have three keys:
834 * * ``field`` is the name of the invalid field
835 * * ``value`` is the invalid value
836 * * ``message`` is the (in)validation message provided by the field
839 * @param {Array} errors a never-empty array of error objects
841 on_invalid: function (errors) {
842 this.do_notify(_t("Invalid Search"), _t("triggered from search view"));
843 this.trigger('invalid_search', errors);
848 * Registry of search fields, called by :js:class:`instance.web.SearchView` to
849 * find and instantiate its field widgets.
851 instance.web.search.fields = new instance.web.Registry({
852 'char': 'instance.web.search.CharField',
853 'text': 'instance.web.search.CharField',
854 'html': 'instance.web.search.CharField',
855 'boolean': 'instance.web.search.BooleanField',
856 'integer': 'instance.web.search.IntegerField',
857 'id': 'instance.web.search.IntegerField',
858 'float': 'instance.web.search.FloatField',
859 'selection': 'instance.web.search.SelectionField',
860 'datetime': 'instance.web.search.DateTimeField',
861 'date': 'instance.web.search.DateField',
862 'many2one': 'instance.web.search.ManyToOneField',
863 'many2many': 'instance.web.search.CharField',
864 'one2many': 'instance.web.search.CharField'
866 instance.web.search.Invalid = instance.web.Class.extend( /** @lends instance.web.search.Invalid# */{
868 * Exception thrown by search widgets when they hold invalid values,
869 * which they can not return when asked.
871 * @constructs instance.web.search.Invalid
872 * @extends instance.web.Class
874 * @param field the name of the field holding an invalid value
875 * @param value the invalid value
876 * @param message validation failure message
878 init: function (field, value, message) {
881 this.message = message;
883 toString: function () {
884 return _.str.sprintf(
885 _t("Incorrect value for field %(fieldname)s: [%(value)s] is %(message)s"),
886 {fieldname: this.field, value: this.value, message: this.message}
890 instance.web.search.Widget = instance.web.Widget.extend( /** @lends instance.web.search.Widget# */{
893 * Root class of all search widgets
895 * @constructs instance.web.search.Widget
896 * @extends instance.web.Widget
898 * @param parent parent of this widget
900 init: function (parent) {
902 var ancestor = parent;
904 this.view = ancestor;
905 } while (!(ancestor instanceof instance.web.SearchView)
906 && (ancestor = (ancestor.getParent && ancestor.getParent())));
910 instance.web.search.add_expand_listener = function($root) {
911 $root.find('a.searchview_group_string').click(function (e) {
912 $root.toggleClass('folded expanded');
917 instance.web.search.Group = instance.web.search.Widget.extend({
918 init: function (parent, icon, node) {
920 var attrs = node.attrs;
921 this.modifiers = attrs.modifiers =
922 attrs.modifiers ? JSON.parse(attrs.modifiers) : {};
925 this.name = attrs.string;
928 this.view.controls.push(this);
930 push: function (input) {
931 this.children.push(input);
933 visible: function () {
934 return !this.modifiers.invisible;
938 instance.web.search.Input = instance.web.search.Widget.extend( /** @lends instance.web.search.Input# */{
941 * @constructs instance.web.search.Input
942 * @extends instance.web.search.Widget
946 init: function (parent) {
949 this.view.inputs.push(this);
952 * Fetch auto-completion values for the widget.
954 * The completion values should be an array of objects with keys category,
955 * label, value prefixed with an object with keys type=section and label
957 * @param {String} value value to complete
958 * @returns {jQuery.Deferred<null|Array>}
960 complete: function (value) {
964 * Returns a Facet instance for the provided defaults if they apply to
965 * this widget, or null if they don't.
967 * This default implementation will try calling
968 * :js:func:`instance.web.search.Input#facet_for` if the widget's name
969 * matches the input key
971 * @param {Object} defaults
972 * @returns {jQuery.Deferred<null|Object>}
974 facet_for_defaults: function (defaults) {
976 !(this.attrs.name in defaults && defaults[this.attrs.name])) {
979 return this.facet_for(defaults[this.attrs.name]);
981 in_drawer: function () {
982 return !!this._in_drawer;
984 get_context: function () {
986 "get_context not implemented for widget " + this.attrs.type);
988 get_groupby: function () {
990 "get_groupby not implemented for widget " + this.attrs.type);
992 get_domain: function () {
994 "get_domain not implemented for widget " + this.attrs.type);
996 load_attrs: function (attrs) {
997 attrs.modifiers = attrs.modifiers ? JSON.parse(attrs.modifiers) : {};
1001 * Returns whether the input is "visible". The default behavior is to
1002 * query the ``modifiers.invisible`` flag on the input's description or
1005 * @returns {Boolean}
1007 visible: function () {
1008 if (this.attrs.modifiers.invisible) {
1012 while ((parent = parent.getParent()) &&
1013 ( (parent instanceof instance.web.search.Group)
1014 || (parent instanceof instance.web.search.Input))) {
1015 if (!parent.visible()) {
1022 instance.web.search.FilterGroup = instance.web.search.Input.extend(/** @lends instance.web.search.FilterGroup# */{
1023 template: 'SearchView.filters',
1025 completion_label: _lt("Filter on: %s"),
1027 * Inclusive group of filters, creates a continuous "button" with clickable
1028 * sections (the normal display for filters is to be a self-contained button)
1030 * @constructs instance.web.search.FilterGroup
1031 * @extends instance.web.search.Input
1033 * @param {Array<instance.web.search.Filter>} filters elements of the group
1034 * @param {instance.web.SearchView} parent parent in which the filters are contained
1036 init: function (filters, parent) {
1037 // If all filters are group_by and we're not initializing a GroupbyGroup,
1038 // create a GroupbyGroup instead of the current FilterGroup
1039 if (!(this instanceof instance.web.search.GroupbyGroup) &&
1040 _(filters).all(function (f) {
1041 if (!f.attrs.context) { return false; }
1042 var c = instance.web.pyeval.eval('context', f.attrs.context);
1043 return !_.isEmpty(c.group_by);})) {
1044 return new instance.web.search.GroupbyGroup(filters, parent);
1046 this._super(parent);
1047 this.filters = filters;
1048 this.view.query.on('add remove change reset', this.proxy('search_change'));
1050 start: function () {
1051 this.$el.on('click', 'li', this.proxy('toggle_filter'));
1052 return $.when(null);
1055 * Handles change of the search query: any of the group's filter which is
1056 * in the search query should be visually checked in the drawer
1058 search_change: function () {
1060 var $filters = this.$('> li').removeClass('oe_selected');
1061 var facet = this.view.query.find(_.bind(this.match_facet, this));
1062 if (!facet) { return; }
1063 facet.values.each(function (v) {
1064 var i = _(self.filters).indexOf(v.get('value'));
1065 if (i === -1) { return; }
1066 $filters.filter(function () {
1067 return Number($(this).data('index')) === i;
1068 }).addClass('oe_selected');
1072 * Matches the group to a facet, in order to find if the group is
1073 * represented in the current search query
1075 match_facet: function (facet) {
1076 return facet.get('field') === this;
1078 make_facet: function (values) {
1080 category: _t("Filter"),
1086 make_value: function (filter) {
1088 label: filter.attrs.string || filter.attrs.help || filter.attrs.name,
1092 facet_for_defaults: function (defaults) {
1094 var fs = _(this.filters).chain()
1095 .filter(function (f) {
1096 return f.attrs && f.attrs.name && !!defaults[f.attrs.name];
1097 }).map(function (f) {
1098 return self.make_value(f);
1100 if (_.isEmpty(fs)) { return $.when(null); }
1101 return $.when(this.make_facet(fs));
1104 * Fetches contexts for all enabled filters in the group
1106 * @param {openerp.web.search.Facet} facet
1107 * @return {*} combined contexts of the enabled filters in this group
1109 get_context: function (facet) {
1110 var contexts = facet.values.chain()
1111 .map(function (f) { return f.get('value').attrs.context; })
1116 if (!contexts.length) { return; }
1117 if (contexts.length === 1) { return contexts[0]; }
1118 return _.extend(new instance.web.CompoundContext, {
1119 __contexts: contexts
1123 * Fetches group_by sequence for all enabled filters in the group
1125 * @param {VS.model.SearchFacet} facet
1126 * @return {Array} enabled filters in this group
1128 get_groupby: function (facet) {
1129 return facet.values.chain()
1130 .map(function (f) { return f.get('value').attrs.context; })
1136 * Handles domains-fetching for all the filters within it: groups them.
1138 * @param {VS.model.SearchFacet} facet
1139 * @return {*} combined domains of the enabled filters in this group
1141 get_domain: function (facet) {
1142 var domains = facet.values.chain()
1143 .map(function (f) { return f.get('value').attrs.domain; })
1148 if (!domains.length) { return; }
1149 if (domains.length === 1) { return domains[0]; }
1150 for (var i=domains.length; --i;) {
1151 domains.unshift(['|']);
1153 return _.extend(new instance.web.CompoundDomain(), {
1157 toggle_filter: function (e) {
1158 this.toggle(this.filters[Number($(e.target).data('index'))]);
1160 toggle: function (filter) {
1161 this.view.query.toggle(this.make_facet([this.make_value(filter)]));
1163 complete: function (item) {
1165 item = item.toLowerCase();
1166 var facet_values = _(this.filters).chain()
1167 .filter(function (filter) { return filter.visible(); })
1168 .filter(function (filter) {
1170 string: filter.attrs.string || '',
1171 help: filter.attrs.help || '',
1172 name: filter.attrs.name || ''
1174 var include = _.str.include;
1175 return include(at.string.toLowerCase(), item)
1176 || include(at.help.toLowerCase(), item)
1177 || include(at.name.toLowerCase(), item);
1179 .map(this.make_value)
1181 if (_(facet_values).isEmpty()) { return $.when(null); }
1182 return $.when(_.map(facet_values, function (facet_value) {
1184 label: _.str.sprintf(self.completion_label.toString(),
1185 _.escape(facet_value.label)),
1186 facet: self.make_facet([facet_value])
1191 instance.web.search.GroupbyGroup = instance.web.search.FilterGroup.extend({
1193 completion_label: _lt("Group by: %s"),
1194 init: function (filters, parent) {
1195 this._super(filters, parent);
1196 // Not flanders: facet unicity is handled through the
1197 // (category, field) pair of facet attributes. This is all well and
1198 // good for regular filter groups where a group matches a facet, but for
1199 // groupby we want a single facet. So cheat: add an attribute on the
1200 // view which proxies to the first GroupbyGroup, so it can be used
1201 // for every GroupbyGroup and still provides the various methods needed
1202 // by the search view. Use weirdo name to avoid risks of conflicts
1203 if (!this.view._s_groupby) {
1204 this.view._s_groupby = {
1205 help: "See GroupbyGroup#init",
1206 get_context: this.proxy('get_context'),
1207 get_domain: this.proxy('get_domain'),
1208 get_groupby: this.proxy('get_groupby')
1212 match_facet: function (facet) {
1213 return facet.get('field') === this.view._s_groupby;
1215 make_facet: function (values) {
1217 category: _t("GroupBy"),
1220 field: this.view._s_groupby
1224 instance.web.search.Filter = instance.web.search.Input.extend(/** @lends instance.web.search.Filter# */{
1225 template: 'SearchView.filter',
1227 * Implementation of the OpenERP filters (button with a context and/or
1228 * a domain sent as-is to the search view)
1230 * Filters are only attributes holder, the actual work (compositing
1231 * domains and contexts, converting between facets and filters) is
1232 * performed by the filter group.
1234 * @constructs instance.web.search.Filter
1235 * @extends instance.web.search.Input
1240 init: function (node, parent) {
1241 this._super(parent);
1242 this.load_attrs(node.attrs);
1244 facet_for: function () { return $.when(null); },
1245 get_context: function () { },
1246 get_domain: function () { },
1248 instance.web.search.Field = instance.web.search.Input.extend( /** @lends instance.web.search.Field# */ {
1249 template: 'SearchView.field',
1250 default_operator: '=',
1252 * @constructs instance.web.search.Field
1253 * @extends instance.web.search.Input
1255 * @param view_section
1259 init: function (view_section, field, parent) {
1260 this._super(parent);
1261 this.load_attrs(_.extend({}, field, view_section.attrs));
1263 facet_for: function (value) {
1266 category: this.attrs.string || this.attrs.name,
1267 values: [{label: String(value), value: value}]
1270 value_from: function (facetValue) {
1271 return facetValue.get('value');
1273 get_context: function (facet) {
1275 // A field needs a context to send when active
1276 var context = this.attrs.context;
1277 if (_.isEmpty(context) || !facet.values.length) {
1280 var contexts = facet.values.map(function (facetValue) {
1281 return new instance.web.CompoundContext(context)
1282 .set_eval_context({self: self.value_from(facetValue)});
1285 if (contexts.length === 1) { return contexts[0]; }
1287 return _.extend(new instance.web.CompoundContext, {
1288 __contexts: contexts
1291 get_groupby: function () { },
1293 * Function creating the returned domain for the field, override this
1294 * methods in children if you only need to customize the field's domain
1295 * without more complex alterations or tests (and without the need to
1296 * change override the handling of filter_domain)
1298 * @param {String} name the field's name
1299 * @param {String} operator the field's operator (either attribute-specified or default operator for the field
1300 * @param {Number|String} facet parsed value for the field
1301 * @returns {Array<Array>} domain to include in the resulting search
1303 make_domain: function (name, operator, facet) {
1304 return [[name, operator, this.value_from(facet)]];
1306 get_domain: function (facet) {
1307 if (!facet.values.length) { return; }
1309 var value_to_domain;
1311 var domain = this.attrs['filter_domain'];
1313 value_to_domain = function (facetValue) {
1314 return new instance.web.CompoundDomain(domain)
1315 .set_eval_context({self: self.value_from(facetValue)});
1318 value_to_domain = function (facetValue) {
1319 return self.make_domain(
1321 self.attrs.operator || self.default_operator,
1325 var domains = facet.values.map(value_to_domain);
1327 if (domains.length === 1) { return domains[0]; }
1328 for (var i = domains.length; --i;) {
1329 domains.unshift(['|']);
1332 return _.extend(new instance.web.CompoundDomain, {
1338 * Implementation of the ``char`` OpenERP field type:
1340 * * Default operator is ``ilike`` rather than ``=``
1342 * * The Javascript and the HTML values are identical (strings)
1345 * @extends instance.web.search.Field
1347 instance.web.search.CharField = instance.web.search.Field.extend( /** @lends instance.web.search.CharField# */ {
1348 default_operator: 'ilike',
1349 complete: function (value) {
1350 if (_.isEmpty(value)) { return $.when(null); }
1351 var label = _.str.sprintf(_.str.escapeHTML(
1352 _t("Search %(field)s for: %(value)s")), {
1353 field: '<em>' + _.escape(this.attrs.string) + '</em>',
1354 value: '<strong>' + _.escape(value) + '</strong>'});
1358 category: this.attrs.string,
1360 values: [{label: value, value: value}]
1365 instance.web.search.NumberField = instance.web.search.Field.extend(/** @lends instance.web.search.NumberField# */{
1366 complete: function (value) {
1367 var val = this.parse(value);
1368 if (isNaN(val)) { return $.when(); }
1369 var label = _.str.sprintf(
1370 _t("Search %(field)s for: %(value)s"), {
1371 field: '<em>' + _.escape(this.attrs.string) + '</em>',
1372 value: '<strong>' + _.escape(value) + '</strong>'});
1376 category: this.attrs.string,
1378 values: [{label: value, value: val}]
1385 * @extends instance.web.search.NumberField
1387 instance.web.search.IntegerField = instance.web.search.NumberField.extend(/** @lends instance.web.search.IntegerField# */{
1388 error_message: _t("not a valid integer"),
1389 parse: function (value) {
1391 return instance.web.parse_value(value, {'widget': 'integer'});
1399 * @extends instance.web.search.NumberField
1401 instance.web.search.FloatField = instance.web.search.NumberField.extend(/** @lends instance.web.search.FloatField# */{
1402 error_message: _t("not a valid number"),
1403 parse: function (value) {
1405 return instance.web.parse_value(value, {'widget': 'float'});
1413 * Utility function for m2o & selection fields taking a selection/name_get pair
1414 * (value, name) and converting it to a Facet descriptor
1416 * @param {instance.web.search.Field} field holder field
1417 * @param {Array} pair pair value to convert
1419 function facet_from(field, pair) {
1422 category: field['attrs'].string,
1423 values: [{label: pair[1], value: pair[0]}]
1429 * @extends instance.web.search.Field
1431 instance.web.search.SelectionField = instance.web.search.Field.extend(/** @lends instance.web.search.SelectionField# */{
1432 // This implementation is a basic <select> field, but it may have to be
1433 // altered to be more in line with the GTK client, which uses a combo box
1434 // (~ jquery.autocomplete):
1435 // * If an option was selected in the list, behave as currently
1436 // * If something which is not in the list was entered (via the text input),
1437 // the default domain should become (`ilike` string_value) but **any
1438 // ``context`` or ``filter_domain`` becomes falsy, idem if ``@operator``
1439 // is specified. So at least get_domain needs to be quite a bit
1440 // overridden (if there's no @value and there is no filter_domain and
1441 // there is no @operator, return [[name, 'ilike', str_val]]
1442 template: 'SearchView.field.selection',
1444 this._super.apply(this, arguments);
1445 // prepend empty option if there is no empty option in the selection list
1446 this.prepend_empty = !_(this.attrs.selection).detect(function (item) {
1450 complete: function (needle) {
1452 var results = _(this.attrs.selection).chain()
1453 .filter(function (sel) {
1454 var value = sel[0], label = sel[1];
1455 if (!value) { return false; }
1456 return label.toLowerCase().indexOf(needle.toLowerCase()) !== -1;
1458 .map(function (sel) {
1460 label: _.escape(sel[1]),
1461 facet: facet_from(self, sel)
1464 if (_.isEmpty(results)) { return $.when(null); }
1465 return $.when.call(null, [{
1466 label: _.escape(this.attrs.string)
1467 }].concat(results));
1469 facet_for: function (value) {
1470 var match = _(this.attrs.selection).detect(function (sel) {
1471 return sel[0] === value;
1473 if (!match) { return $.when(null); }
1474 return $.when(facet_from(this, match));
1477 instance.web.search.BooleanField = instance.web.search.SelectionField.extend(/** @lends instance.web.search.BooleanField# */{
1479 * @constructs instance.web.search.BooleanField
1480 * @extends instance.web.search.BooleanField
1483 this._super.apply(this, arguments);
1484 this.attrs.selection = [
1492 * @extends instance.web.search.DateField
1494 instance.web.search.DateField = instance.web.search.Field.extend(/** @lends instance.web.search.DateField# */{
1495 value_from: function (facetValue) {
1496 return instance.web.date_to_str(facetValue.get('value'));
1498 complete: function (needle) {
1499 var d = Date.parse(needle);
1500 if (!d) { return $.when(null); }
1501 var date_string = instance.web.format_value(d, this.attrs);
1502 var label = _.str.sprintf(_.str.escapeHTML(
1503 _t("Search %(field)s at: %(value)s")), {
1504 field: '<em>' + _.escape(this.attrs.string) + '</em>',
1505 value: '<strong>' + date_string + '</strong>'});
1509 category: this.attrs.string,
1511 values: [{label: date_string, value: d}]
1517 * Implementation of the ``datetime`` openerp field type:
1519 * * Uses the same widget as the ``date`` field type (a simple date)
1521 * * Builds a slighly more complex, it's a datetime range (includes time)
1522 * spanning the whole day selected by the date widget
1525 * @extends instance.web.DateField
1527 instance.web.search.DateTimeField = instance.web.search.DateField.extend(/** @lends instance.web.search.DateTimeField# */{
1528 value_from: function (facetValue) {
1529 return instance.web.datetime_to_str(facetValue.get('value'));
1532 instance.web.search.ManyToOneField = instance.web.search.CharField.extend({
1533 default_operator: {},
1534 init: function (view_section, field, parent) {
1535 this._super(view_section, field, parent);
1536 this.model = new instance.web.Model(this.attrs.relation);
1538 complete: function (needle) {
1540 // FIXME: "concurrent" searches (multiple requests, mis-ordered responses)
1541 var context = instance.web.pyeval.eval(
1542 'contexts', [this.view.dataset.get_context()]);
1543 return this.model.call('name_search', [], {
1545 args: instance.web.pyeval.eval(
1546 'domains', this.attrs.domain ? [this.attrs.domain] : [], context),
1549 }).then(function (results) {
1550 if (_.isEmpty(results)) { return null; }
1551 return [{label: self.attrs.string}].concat(
1552 _(results).map(function (result) {
1554 label: _.escape(result[1]),
1555 facet: facet_from(self, result)
1560 facet_for: function (value) {
1562 if (value instanceof Array) {
1563 if (value.length === 2 && _.isString(value[1])) {
1564 return $.when(facet_from(this, value));
1566 assert(value.length <= 1,
1567 _t("M2O search fields do not currently handle multiple default values"));
1568 // there are many cases of {search_default_$m2ofield: [id]}, need
1569 // to handle this as if it were a single value.
1572 return this.model.call('name_get', [value]).then(function (names) {
1573 if (_(names).isEmpty()) { return null; }
1574 return facet_from(self, names[0]);
1577 value_from: function (facetValue) {
1578 return facetValue.get('label');
1580 make_domain: function (name, operator, facetValue) {
1582 case this.default_operator:
1583 return [[name, '=', facetValue.get('value')]];
1585 return [[name, 'child_of', facetValue.get('value')]];
1587 return this._super(name, operator, facetValue);
1589 get_context: function (facet) {
1590 var values = facet.values;
1591 if (_.isEmpty(this.attrs.context) && values.length === 1) {
1593 c['default_' + this.attrs.name] = values.at(0).get('value');
1596 return this._super(facet);
1600 instance.web.search.CustomFilters = instance.web.search.Input.extend({
1601 template: 'SearchView.CustomFilters',
1604 this.is_ready = $.Deferred();
1605 this._super.apply(this, arguments);
1607 start: function () {
1609 this.model = new instance.web.Model('ir.filters');
1613 .on('remove', function (facet) {
1614 if (!facet.get('is_custom_filter')) {
1617 self.clear_selection();
1619 .on('reset', this.proxy('clear_selection'));
1620 this.$el.on('submit', 'form', this.proxy('save_current'));
1621 this.$el.on('click', 'input[type=checkbox]', function() {
1622 $(this).siblings('input[type=checkbox]').prop('checked', false);
1624 this.$el.on('click', 'h4', function () {
1625 self.$el.toggleClass('oe_opened');
1627 return this.model.call('get_filters', [this.view.model])
1628 .then(this.proxy('set_filters'))
1629 .done(function () { self.is_ready.resolve(); })
1630 .fail(function () { self.is_ready.reject.apply(self.is_ready, arguments); });
1633 * Special implementation delaying defaults until CustomFilters is loaded
1635 facet_for_defaults: function () {
1636 return this.is_ready;
1639 * Generates a mapping key (in the filters and $filter mappings) for the
1640 * filter descriptor object provided (as returned by ``get_filters``).
1642 * The mapping key is guaranteed to be unique for a given (user_id, name)
1645 * @param {Object} filter
1646 * @param {String} filter.name
1647 * @param {Number|Pair<Number, String>} [filter.user_id]
1648 * @return {String} mapping key corresponding to the filter
1650 key_for: function (filter) {
1651 var user_id = filter.user_id;
1652 var uid = (user_id instanceof Array) ? user_id[0] : user_id;
1653 return _.str.sprintf('(%s)%s', uid, filter.name);
1656 * Generates a :js:class:`~instance.web.search.Facet` descriptor from a
1659 * @param {Object} filter
1660 * @param {String} filter.name
1661 * @param {Object} [filter.context]
1662 * @param {Array} [filter.domain]
1665 facet_for: function (filter) {
1667 category: _t("Custom Filter"),
1670 get_context: function () { return filter.context; },
1671 get_groupby: function () { return [filter.context]; },
1672 get_domain: function () { return filter.domain; }
1675 is_custom_filter: true,
1676 values: [{label: filter.name, value: null}]
1679 clear_selection: function () {
1680 this.$('li.oe_selected').removeClass('oe_selected');
1682 append_filter: function (filter) {
1684 var key = this.key_for(filter);
1685 var warning = _t("This filter is global and will be removed for everybody if you continue.");
1688 if (key in this.$filters) {
1689 $filter = this.$filters[key];
1692 this.filters[key] = filter;
1693 $filter = this.$filters[key] = $('<li></li>')
1694 .appendTo(this.$('.oe_searchview_custom_list'))
1695 .addClass(filter.user_id ? 'oe_searchview_custom_private'
1696 : 'oe_searchview_custom_public')
1697 .toggleClass('oe_searchview_custom_default', filter.is_default)
1700 $('<a class="oe_searchview_custom_delete">x</a>')
1701 .click(function (e) {
1702 e.stopPropagation();
1703 if (!(filter.user_id || confirm(warning))) {
1706 self.model.call('unlink', [id]).done(function () {
1708 delete self.$filters[key];
1709 delete self.filters[key];
1715 $filter.unbind('click').click(function () {
1716 self.toggle_filter(filter);
1719 toggle_filter: function (filter, preventSearch) {
1720 var current = this.view.query.find(function (facet) {
1721 return facet.get('_id') === filter.id;
1724 this.view.query.remove(current);
1725 this.$filters[this.key_for(filter)].removeClass('oe_selected');
1728 this.view.query.reset([this.facet_for(filter)], {
1729 preventSearch: preventSearch || false});
1730 this.$filters[this.key_for(filter)].addClass('oe_selected');
1732 set_filters: function (filters) {
1733 _(filters).map(_.bind(this.append_filter, this));
1735 save_current: function () {
1737 var $name = this.$('input:first');
1738 var private_filter = !this.$('#oe_searchview_custom_public').prop('checked');
1739 var set_as_default = this.$('#oe_searchview_custom_default').prop('checked');
1740 if (_.isEmpty($name.val())){
1741 this.do_warn(_t("Error"), _t("Filter name is required."));
1744 var search = this.view.build_search_data();
1745 instance.web.pyeval.eval_domains_and_contexts({
1746 domains: search.domains,
1747 contexts: search.contexts,
1748 group_by_seq: search.groupbys || []
1749 }).done(function (results) {
1750 if (!_.isEmpty(results.group_by)) {
1751 results.context.group_by = results.group_by;
1753 // Don't save user_context keys in the custom filter, otherwise end
1754 // up with e.g. wrong uid or lang stored *and used in subsequent
1756 var ctx = results.context;
1757 _(_.keys(instance.session.user_context)).each(function (key) {
1762 user_id: private_filter ? instance.session.uid : false,
1763 model_id: self.view.model,
1764 context: results.context,
1765 domain: results.domain,
1766 is_default: set_as_default
1768 // FIXME: current context?
1769 return self.model.call('create_or_replace', [filter]).done(function (id) {
1771 self.append_filter(filter);
1773 .removeClass('oe_opened')
1774 .find('form')[0].reset();
1781 instance.web.search.Filters = instance.web.search.Input.extend({
1782 template: 'SearchView.Filters',
1784 start: function () {
1786 var running_count = 0;
1787 // get total filters count
1788 var is_group = function (i) { return i instanceof instance.web.search.FilterGroup; };
1789 var visible_filters = _(this.view.controls).chain().reject(function (group) {
1790 return _(_(group.children).filter(is_group)).isEmpty()
1791 || group.modifiers.invisible;
1793 var filters_count = visible_filters
1797 .map(function (i) { return i.filters.length; })
1801 var col1 = [], col2 = visible_filters.map(function (group) {
1802 var filters = _(group.children).filter(is_group);
1804 name: _.str.sprintf("<span class='oe_i'>%s</span> %s",
1805 group.icon, group.name),
1807 length: _(filters).chain().map(function (i) {
1808 return i.filters.length; }).sum().value()
1812 while (col2.length) {
1813 // col1 + group should be smaller than col2 + group
1814 if ((running_count + col2[0].length) <= (filters_count - running_count)) {
1815 running_count += col2[0].length;
1816 col1.push(col2.shift());
1823 this.render_column(col1, $('<div>').appendTo(this.$el)),
1824 this.render_column(col2, $('<div>').appendTo(this.$el)));
1826 render_column: function (column, $el) {
1827 return $.when.apply(null, _(column).map(function (group) {
1828 $('<h3>').html(group.name).appendTo($el);
1829 return $.when.apply(null,
1830 _(group.filters).invoke('appendTo', $el));
1835 instance.web.search.Advanced = instance.web.search.Input.extend({
1836 template: 'SearchView.advanced',
1838 start: function () {
1841 .on('keypress keydown keyup', function (e) { e.stopPropagation(); })
1842 .on('click', 'h4', function () {
1843 self.$el.toggleClass('oe_opened');
1844 }).on('click', 'button.oe_add_condition', function () {
1845 self.append_proposition();
1846 }).on('submit', 'form', function (e) {
1848 self.commit_search();
1852 new instance.web.Model(this.view.model).call('fields_get', {
1853 context: this.view.dataset.context
1854 }).done(function(data) {
1856 id: { string: 'ID', type: 'id' }
1858 _.each(data, function(field_def, field_name) {
1859 if (field_def.selectable !== false && field_name != 'id') {
1860 self.fields[field_name] = field_def;
1863 })).done(function () {
1864 self.append_proposition();
1867 append_proposition: function () {
1869 return (new instance.web.search.ExtendedSearchProposition(this, this.fields))
1870 .appendTo(this.$('ul')).done(function () {
1871 self.$('button.oe_apply').prop('disabled', false);
1874 remove_proposition: function (prop) {
1875 // removing last proposition, disable apply button
1876 if (this.getChildren().length <= 1) {
1877 this.$('button.oe_apply').prop('disabled', true);
1881 commit_search: function () {
1882 // Get domain sections from all propositions
1883 var children = this.getChildren();
1884 var propositions = _.invoke(children, 'get_proposition');
1885 var domain = _(propositions).pluck('value');
1886 for (var i = domain.length; --i;) {
1887 domain.unshift('|');
1890 this.view.query.add({
1891 category: _t("Advanced"),
1892 values: propositions,
1894 get_context: function () { },
1895 get_domain: function () { return domain;},
1896 get_groupby: function () { }
1900 // remove all propositions
1901 _.invoke(children, 'destroy');
1902 // add new empty proposition
1903 this.append_proposition();
1904 // TODO: API on searchview
1905 this.view.$el.removeClass('oe_searchview_open_drawer');
1909 instance.web.search.ExtendedSearchProposition = instance.web.Widget.extend(/** @lends instance.web.search.ExtendedSearchProposition# */{
1910 template: 'SearchView.extended_search.proposition',
1912 'change .searchview_extended_prop_field': 'changed',
1913 'change .searchview_extended_prop_op': 'operator_changed',
1914 'click .searchview_extended_delete_prop': function (e) {
1915 e.stopPropagation();
1916 this.getParent().remove_proposition(this);
1920 * @constructs instance.web.search.ExtendedSearchProposition
1921 * @extends instance.web.Widget
1926 init: function (parent, fields) {
1927 this._super(parent);
1928 this.fields = _(fields).chain()
1929 .map(function(val, key) { return _.extend({}, val, {'name': key}); })
1930 .filter(function (field) { return !field.deprecated && (field.store === void 0 || field.store || field.fnct_search); })
1931 .sortBy(function(field) {return field.string;})
1933 this.attrs = {_: _, fields: this.fields, selected: null};
1936 start: function () {
1937 return this._super().done(this.proxy('changed'));
1939 changed: function() {
1940 var nval = this.$(".searchview_extended_prop_field").val();
1941 if(this.attrs.selected == null || nval != this.attrs.selected.name) {
1942 this.select_field(_.detect(this.fields, function(x) {return x.name == nval;}));
1945 operator_changed: function (e) {
1946 var $value = this.$('.searchview_extended_prop_value');
1947 switch ($(e.target).val()) {
1957 * Selects the provided field object
1959 * @param field a field descriptor object (as returned by fields_get, augmented by the field name)
1961 select_field: function(field) {
1963 if(this.attrs.selected != null) {
1964 this.value.destroy();
1966 this.$('.searchview_extended_prop_op').html('');
1968 this.attrs.selected = field;
1973 var type = field.type;
1974 var Field = instance.web.search.custom_filters.get_object(type);
1976 Field = instance.web.search.custom_filters.get_object("char");
1978 this.value = new Field(this, field);
1979 _.each(this.value.operators, function(operator) {
1980 $('<option>', {value: operator.value})
1981 .text(String(operator.text))
1982 .appendTo(self.$('.searchview_extended_prop_op'));
1984 var $value_loc = this.$('.searchview_extended_prop_value').show().empty();
1985 this.value.appendTo($value_loc);
1988 get_proposition: function() {
1989 if ( this.attrs.selected == null)
1991 var field = this.attrs.selected;
1992 var op_select = this.$('.searchview_extended_prop_op')[0];
1993 var operator = op_select.options[op_select.selectedIndex];
1996 label: this.value.get_label(field, operator),
1997 value: this.value.get_domain(field, operator),
2002 instance.web.search.ExtendedSearchProposition.Field = instance.web.Widget.extend({
2003 init: function (parent, field) {
2004 this._super(parent);
2007 get_label: function (field, operator) {
2009 switch (operator.value) {
2010 case '∃': case '∄': format = _t('%(field)s %(operator)s'); break;
2011 default: format = _t('%(field)s %(operator)s "%(value)s"'); break;
2013 return this.format_label(format, field, operator);
2015 format_label: function (format, field, operator) {
2016 return _.str.sprintf(format, {
2017 field: field.string,
2018 // According to spec, HTMLOptionElement#label should return
2019 // HTMLOptionElement#text when not defined/empty, but it does
2020 // not in older Webkit (between Safari 5.1.5 and Chrome 17) and
2021 // Gecko (pre Firefox 7) browsers, so we need a manual fallback
2023 operator: operator.label || operator.text,
2027 get_domain: function (field, operator) {
2028 switch (operator.value) {
2029 case '∃': return this.make_domain(field.name, '!=', false);
2030 case '∄': return this.make_domain(field.name, '=', false);
2031 default: return this.make_domain(
2032 field.name, operator.value, this.get_value());
2035 make_domain: function (field, operator, value) {
2036 return [field, operator, value];
2039 * Returns a human-readable version of the value, in case the "logical"
2040 * and the "semantic" values of a field differ (as for selection fields,
2043 * The default implementation simply returns the value itself.
2045 * @return {String} human-readable version of the value
2047 toString: function () {
2048 return this.get_value();
2051 instance.web.search.ExtendedSearchProposition.Char = instance.web.search.ExtendedSearchProposition.Field.extend({
2052 template: 'SearchView.extended_search.proposition.char',
2054 {value: "ilike", text: _lt("contains")},
2055 {value: "not ilike", text: _lt("doesn't contain")},
2056 {value: "=", text: _lt("is equal to")},
2057 {value: "!=", text: _lt("is not equal to")},
2058 {value: "∃", text: _lt("is set")},
2059 {value: "∄", text: _lt("is not set")}
2061 get_value: function() {
2062 return this.$el.val();
2065 instance.web.search.ExtendedSearchProposition.DateTime = instance.web.search.ExtendedSearchProposition.Field.extend({
2066 template: 'SearchView.extended_search.proposition.empty',
2068 {value: "=", text: _lt("is equal to")},
2069 {value: "!=", text: _lt("is not equal to")},
2070 {value: ">", text: _lt("greater than")},
2071 {value: "<", text: _lt("less than")},
2072 {value: ">=", text: _lt("greater or equal than")},
2073 {value: "<=", text: _lt("less or equal than")},
2074 {value: "∃", text: _lt("is set")},
2075 {value: "∄", text: _lt("is not set")}
2078 * Date widgets live in view_form which is not yet loaded when this is
2081 widget: function () { return instance.web.DateTimeWidget; },
2082 get_value: function() {
2083 return this.datewidget.get_value();
2085 toString: function () {
2086 return instance.web.format_value(this.get_value(), { type:"datetime" });
2089 var ready = this._super();
2090 this.datewidget = new (this.widget())(this);
2091 this.datewidget.appendTo(this.$el);
2095 instance.web.search.ExtendedSearchProposition.Date = instance.web.search.ExtendedSearchProposition.DateTime.extend({
2096 widget: function () { return instance.web.DateWidget; },
2097 toString: function () {
2098 return instance.web.format_value(this.get_value(), { type:"date" });
2101 instance.web.search.ExtendedSearchProposition.Integer = instance.web.search.ExtendedSearchProposition.Field.extend({
2102 template: 'SearchView.extended_search.proposition.integer',
2104 {value: "=", text: _lt("is equal to")},
2105 {value: "!=", text: _lt("is not equal to")},
2106 {value: ">", text: _lt("greater than")},
2107 {value: "<", text: _lt("less than")},
2108 {value: ">=", text: _lt("greater or equal than")},
2109 {value: "<=", text: _lt("less or equal than")},
2110 {value: "∃", text: _lt("is set")},
2111 {value: "∄", text: _lt("is not set")}
2113 toString: function () {
2114 return this.$el.val();
2116 get_value: function() {
2118 var val =this.$el.val();
2119 return instance.web.parse_value(val == "" ? 0 : val, {'widget': 'integer'});
2125 instance.web.search.ExtendedSearchProposition.Id = instance.web.search.ExtendedSearchProposition.Integer.extend({
2126 operators: [{value: "=", text: _lt("is")}]
2128 instance.web.search.ExtendedSearchProposition.Float = instance.web.search.ExtendedSearchProposition.Field.extend({
2129 template: 'SearchView.extended_search.proposition.float',
2131 {value: "=", text: _lt("is equal to")},
2132 {value: "!=", text: _lt("is not equal to")},
2133 {value: ">", text: _lt("greater than")},
2134 {value: "<", text: _lt("less than")},
2135 {value: ">=", text: _lt("greater or equal than")},
2136 {value: "<=", text: _lt("less or equal than")},
2137 {value: "∃", text: _lt("is set")},
2138 {value: "∄", text: _lt("is not set")}
2140 toString: function () {
2141 return this.$el.val();
2143 get_value: function() {
2145 var val =this.$el.val();
2146 return instance.web.parse_value(val == "" ? 0.0 : val, {'widget': 'float'});
2152 instance.web.search.ExtendedSearchProposition.Selection = instance.web.search.ExtendedSearchProposition.Field.extend({
2153 template: 'SearchView.extended_search.proposition.selection',
2155 {value: "=", text: _lt("is")},
2156 {value: "!=", text: _lt("is not")},
2157 {value: "∃", text: _lt("is set")},
2158 {value: "∄", text: _lt("is not set")}
2160 toString: function () {
2161 var select = this.$el[0];
2162 var option = select.options[select.selectedIndex];
2163 return option.label || option.text;
2165 get_value: function() {
2166 return this.$el.val();
2169 instance.web.search.ExtendedSearchProposition.Boolean = instance.web.search.ExtendedSearchProposition.Field.extend({
2170 template: 'SearchView.extended_search.proposition.empty',
2172 {value: "=", text: _lt("is true")},
2173 {value: "!=", text: _lt("is false")}
2175 get_label: function (field, operator) {
2176 return this.format_label(
2177 _t('%(field)s %(operator)s'), field, operator);
2179 get_value: function() {
2184 instance.web.search.custom_filters = new instance.web.Registry({
2185 'char': 'instance.web.search.ExtendedSearchProposition.Char',
2186 'text': 'instance.web.search.ExtendedSearchProposition.Char',
2187 'one2many': 'instance.web.search.ExtendedSearchProposition.Char',
2188 'many2one': 'instance.web.search.ExtendedSearchProposition.Char',
2189 'many2many': 'instance.web.search.ExtendedSearchProposition.Char',
2191 'datetime': 'instance.web.search.ExtendedSearchProposition.DateTime',
2192 'date': 'instance.web.search.ExtendedSearchProposition.Date',
2193 'integer': 'instance.web.search.ExtendedSearchProposition.Integer',
2194 'float': 'instance.web.search.ExtendedSearchProposition.Float',
2195 'boolean': 'instance.web.search.ExtendedSearchProposition.Boolean',
2196 'selection': 'instance.web.search.ExtendedSearchProposition.Selection',
2198 'id': 'instance.web.search.ExtendedSearchProposition.Id'
2203 // vim:et fdc=0 fdl=0 foldnestmax=3 fdm=syntax: