4 var instance = openerp;
5 openerp.web.search = {};
7 var QWeb = instance.web.qweb,
9 _lt = instance.web._lt;
11 sum: function (obj) { return _.reduce(obj, function (a, b) { return a + b; }, 0); }
15 var my = instance.web.search = {};
18 my.FacetValue = B.Model.extend({
21 my.FacetValues = B.Collection.extend({
24 my.Facet = B.Model.extend({
25 initialize: function (attrs) {
26 var values = attrs.values;
29 B.Model.prototype.initialize.apply(this, arguments);
31 this.values = new my.FacetValues(values || []);
32 this.values.on('add remove change reset', function () {
33 this.trigger('change', this);
37 if (key !== 'values') {
38 return B.Model.prototype.get.call(this, key);
40 return this.values.toJSON();
42 set: function (key, value) {
43 if (key !== 'values') {
44 return B.Model.prototype.set.call(this, key, value);
46 this.values.reset(value);
50 var attrs = this.attributes;
51 for(var att in attrs) {
52 if (!attrs.hasOwnProperty(att) || att === 'field') {
55 out[att] = attrs[att];
57 out.values = this.values.toJSON();
61 my.SearchQuery = B.Collection.extend({
63 initialize: function () {
64 B.Collection.prototype.initialize.apply(
66 this.on('change', function (facet) {
67 if(!facet.values.isEmpty()) { return; }
69 this.remove(facet, {silent: true});
72 add: function (values, options) {
73 options = options || {};
77 } else if (!(values instanceof Array)) {
81 _(values).each(function (value) {
82 var model = this._prepareModel(value, options);
83 var previous = this.detect(function (facet) {
84 return facet.get('category') === model.get('category')
85 && facet.get('field') === model.get('field');
88 previous.values.add(model.get('values'), _.omit(options, 'at', 'merge'));
91 B.Collection.prototype.add.call(this, model, options);
93 // warning: in backbone 1.0+ add is supposed to return the added models,
94 // but here toggle may delegate to add and return its value directly.
95 // return value of neither seems actually used but should be tested
96 // before change, probably
99 toggle: function (value, options) {
100 options = options || {};
102 var facet = this.detect(function (facet) {
103 return facet.get('category') === value.category
104 && facet.get('field') === value.field;
107 return this.add(value, options);
111 _(value.values).each(function (val) {
112 var already_value = facet.values.detect(function (v) {
113 return v.get('value') === val.value
114 && v.get('label') === val.label;
118 facet.values.remove(already_value, {silent: true});
120 facet.values.add(val, {silent: true});
124 // "Commit" changes to values array as a single call, so observers of
125 // change event don't get misled by intermediate incomplete toggling
127 facet.trigger('change', facet);
132 function assert(condition, message) {
134 throw new Error(message);
137 my.InputView = instance.web.Widget.extend({
138 template: 'SearchView.InputView',
140 focus: function () { this.trigger('focused', this); },
141 blur: function () { this.$el.text(''); this.trigger('blurred', this); },
142 keydown: 'onKeydown',
145 getSelection: function () {
148 var root = this.el.childNodes[0];
149 if (!root || !root.textContent) {
150 // if input does not have a child node, or the child node is an
151 // empty string, then the selection can only be (0, 0)
152 return {start: 0, end: 0};
154 var range = window.getSelection().getRangeAt(0);
155 // In Firefox, depending on the way text is selected (drag, double- or
156 // triple-click) the range may start or end on the parent of the
157 // selected text node‽ Check for this condition and fixup the range
158 // note: apparently with C-a this can go even higher?
159 if (range.startContainer === this.el && range.startOffset === 0) {
160 range.setStart(root, 0);
162 if (range.endContainer === this.el && range.endOffset === 1) {
163 range.setEnd(root, root.length);
165 assert(range.startContainer === root,
166 "selection should be in the input view");
167 assert(range.endContainer === root,
168 "selection should be in the input view");
170 start: range.startOffset,
174 onKeydown: function (e) {
178 // Do not insert newline, but let it bubble so searchview can use it
179 case $.ui.keyCode.ENTER:
183 // FIXME: may forget content if non-empty but caret at index 0, ok?
184 case $.ui.keyCode.BACKSPACE:
185 sel = this.getSelection();
186 if (sel.start === 0 && sel.start === sel.end) {
188 var preceding = this.getParent().siblingSubview(this, -1);
189 if (preceding && (preceding instanceof my.FacetView)) {
190 preceding.model.destroy();
195 // let left/right events propagate to view if caret is at input border
196 // and not a selection
197 case $.ui.keyCode.LEFT:
198 sel = this.getSelection();
199 if (sel.start !== 0 || sel.start !== sel.end) {
203 case $.ui.keyCode.RIGHT:
204 sel = this.getSelection();
205 var len = this.$el.text().length;
206 if (sel.start !== len || sel.start !== sel.end) {
212 setCursorAtEnd: function () {
214 var sel = window.getSelection();
215 sel.removeAllRanges();
216 var range = document.createRange();
217 // in theory, range.selectNodeContents should work here. In practice,
218 // MSIE9 has issues from time to time, instead of selecting the inner
219 // text node it would select the reference node instead (e.g. in demo
220 // data, company news, copy across the "Company News" link + the title,
221 // from about half the link to half the text, paste in search box then
222 // hit the left arrow key, getSelection would blow up).
224 // Explicitly selecting only the inner text node (only child node
225 // since we've normalized the parent) avoids the issue
226 range.selectNode(this.el.childNodes[0]);
227 range.collapse(false);
230 onPaste: function () {
232 // In MSIE and Webkit, it is possible to get various representations of
233 // the clipboard data at this point e.g.
234 // window.clipboardData.getData('Text') and
235 // event.clipboardData.getData('text/plain') to ensure we have a plain
236 // text representation of the object (and probably ensure the object is
237 // pastable as well, so nobody puts an image in the search view)
238 // (nb: since it's not possible to alter the content of the clipboard
239 // — at least in Webkit — to ensure only textual content is available,
240 // using this would require 1. getting the text data; 2. manually
241 // inserting the text data into the content; and 3. cancelling the
244 // But Firefox doesn't support the clipboard API (as of FF18)
245 // although it correctly triggers the paste event (Opera does not even
246 // do that) => implement lowest-denominator system where onPaste
247 // triggers a followup "cleanup" pass after the data has been pasted
248 setTimeout(function () {
249 // Read text content (ignore pasted HTML)
250 var data = this.$el.text();
253 // paste raw text back in
254 this.$el.empty().text(data);
256 // Set the cursor at the end of the text, so the cursor is not lost
257 // in some kind of error-spawning limbo.
258 this.setCursorAtEnd();
262 my.FacetView = instance.web.Widget.extend({
263 template: 'SearchView.FacetView',
265 'focus': function () { this.trigger('focused', this); },
266 'blur': function () { this.trigger('blurred', this); },
267 'click': function (e) {
268 if ($(e.target).is('.oe_facet_remove')) {
269 this.model.destroy();
275 'keydown': function (e) {
276 var keys = $.ui.keyCode;
280 this.model.destroy();
285 init: function (parent, model) {
288 this.model.on('change', this.model_changed, this);
290 destroy: function () {
291 this.model.off('change', this.model_changed, this);
296 var $e = this.$('> span:last-child');
297 return $.when(this._super()).then(function () {
298 return $.when.apply(null, self.model.values.map(function (value) {
299 return new my.FacetValueView(self, value).appendTo($e);
303 model_changed: function () {
304 this.$el.text(this.$el.text() + '*');
307 my.FacetValueView = instance.web.Widget.extend({
308 template: 'SearchView.FacetView.Value',
309 init: function (parent, model) {
312 this.model.on('change', this.model_changed, this);
314 destroy: function () {
315 this.model.off('change', this.model_changed, this);
318 model_changed: function () {
319 this.$el.text(this.$el.text() + '*');
323 instance.web.SearchView = instance.web.Widget.extend(/** @lends instance.web.SearchView# */{
324 template: "SearchView",
326 // focus last input if view itself is clicked
327 'click': function (e) {
328 if (e.target === this.$('.oe_searchview_facets')[0]) {
329 this.$('.oe_searchview_input:last').focus();
333 'click button.oe_searchview_search': function (e) {
334 e.stopImmediatePropagation();
337 'click .oe_searchview_clear': function (e) {
338 e.stopImmediatePropagation();
341 'click .oe_searchview_unfold_drawer': function (e) {
342 e.stopImmediatePropagation();
343 this.$el.toggleClass('oe_searchview_open_drawer');
345 'keydown .oe_searchview_input, .oe_searchview_facet': function (e) {
347 case $.ui.keyCode.LEFT:
348 this.focusPreceding(this);
351 case $.ui.keyCode.RIGHT:
352 this.focusFollowing(this);
357 'autocompleteopen': function () {
358 this.$el.autocomplete('widget').css('z-index', 9999);
362 * @constructs instance.web.SearchView
363 * @extends instance.web.Widget
369 * @param {Object} [options]
370 * @param {Boolean} [options.hidden=false] hide the search view
371 * @param {Boolean} [options.disable_custom_filters=false] do not load custom filters from ir.filters
373 init: function(parent, dataset, view_id, defaults, options) {
374 this.options = _.defaults(options || {}, {
376 disable_custom_filters: false,
379 this.dataset = dataset;
380 this.model = dataset.model;
381 this.view_id = view_id;
383 this.defaults = defaults || {};
384 this.has_defaults = !_.isEmpty(this.defaults);
389 this.headless = this.options.hidden && !this.has_defaults;
391 this.input_subviews = [];
393 this.ready = $.Deferred();
397 var p = this._super();
399 this.setup_global_completion();
400 this.query = new my.SearchQuery()
401 .on('add change reset remove', this.proxy('do_search'))
402 .on('add change reset remove', this.proxy('renderFacets'));
404 if (this.options.hidden) {
408 this.ready.resolve();
410 var load_view = instance.web.fields_view_get({
411 model: this.dataset._model,
412 view_id: this.view_id,
414 context: this.dataset.get_context(),
417 this.alive($.when(load_view)).then(function (r) {
418 return self.search_view_loaded(r);
419 }).fail(function () {
420 self.ready.reject.apply(null, arguments);
424 instance.web.bus.on('click', this, function(ev) {
425 if ($(ev.target).parents('.oe_searchview').length === 0) {
426 self.$el.removeClass('oe_searchview_open_drawer');
430 return $.when(p, this.ready);
439 subviewForRoot: function (subview_root) {
440 return _(this.input_subviews).detect(function (subview) {
441 return subview.$el[0] === subview_root;
444 siblingSubview: function (subview, direction, wrap_around) {
445 var index = _(this.input_subviews).indexOf(subview) + direction;
446 if (wrap_around && index < 0) {
447 index = this.input_subviews.length - 1;
448 } else if (wrap_around && index >= this.input_subviews.length) {
451 return this.input_subviews[index];
453 focusPreceding: function (subview_root) {
454 return this.siblingSubview(
455 this.subviewForRoot(subview_root), -1, true)
458 focusFollowing: function (subview_root) {
459 return this.siblingSubview(
460 this.subviewForRoot(subview_root), +1, true)
465 * Sets up thingie where all the mess is put?
467 select_for_drawer: function () {
468 return _(this.inputs).filter(function (input) {
469 return input.in_drawer();
473 * Sets up search view's view-wide auto-completion widget
475 setup_global_completion: function () {
476 var autocomplete = this.$el.autocomplete({
477 source: this.proxy('complete_global_search'),
478 select: this.proxy('select_completion'),
479 focus: function (e) { e.preventDefault(); },
484 }).data('autocomplete');
486 this.$el.on('input', function () {
487 this.$el.autocomplete('close');
490 // MonkeyPatch autocomplete instance
491 _.extend(autocomplete, {
492 _renderItem: function (ul, item) {
493 // item of completion list
494 var $item = $( "<li></li>" )
495 .data( "item.autocomplete", item )
498 if (item.facet !== undefined) {
499 // regular completion item
501 $item.css('borderTop', '1px solid #cccccc');
505 ? $('<a>').html(item.label)
506 : $('<a>').text(item.value));
508 return $item.text(item.label)
510 borderTop: '1px solid #cccccc',
520 return self.$('div.oe_searchview_input').text();
525 * Provide auto-completion result for req.term (an array to `resp`)
527 * @param {Object} req request to complete
528 * @param {String} req.term searched term to complete
529 * @param {Function} resp response callback
531 complete_global_search: function (req, resp) {
532 $.when.apply(null, _(this.inputs).chain()
533 .filter(function (input) { return input.visible(); })
534 .invoke('complete', req.term)
535 .value()).then(function () {
536 resp(_(arguments).chain()
538 .map(function (completion) {
539 if (completion.length && completion[0].facet !== undefined) {
540 completion[0].first = true;
550 * Action to perform in case of selection: create a facet (model)
551 * and add it to the search collection
553 * @param {Object} e selection event, preventDefault to avoid setting value on object
554 * @param {Object} ui selection information
555 * @param {Object} ui.item selected completion item
557 select_completion: function (e, ui) {
560 var input_index = _(this.input_subviews).indexOf(
562 this.$('div.oe_searchview_input:focus')[0]));
563 this.query.add(ui.item.facet, {at: input_index / 2});
565 childFocused: function () {
566 this.$el.addClass('oe_focused');
568 childBlurred: function () {
569 var val = this.$el.val();
571 var complete = this.$el.data('autocomplete');
572 if ((val && complete.term === undefined) || complete.previous) {
573 throw new Error("new jquery.ui version altering implementation" +
574 " details relied on");
576 delete complete.term;
577 this.$el.removeClass('oe_focused')
582 * @param {openerp.web.search.SearchQuery | openerp.web.search.Facet} _1
583 * @param {openerp.web.search.Facet} [_2]
584 * @param {Object} [options]
586 renderFacets: function (_1, _2, options) {
587 // _1: model if event=change, otherwise collection
588 // _2: undefined if event=change, otherwise model
591 var $e = this.$('div.oe_searchview_facets');
592 _.invoke(this.input_subviews, 'destroy');
593 this.input_subviews = [];
595 var i = new my.InputView(this);
596 started.push(i.appendTo($e));
597 this.input_subviews.push(i);
598 this.query.each(function (facet) {
599 var f = new my.FacetView(this, facet);
600 started.push(f.appendTo($e));
601 self.input_subviews.push(f);
603 var i = new my.InputView(this);
604 started.push(i.appendTo($e));
605 self.input_subviews.push(i);
607 _.each(this.input_subviews, function (childView) {
608 childView.on('focused', self, self.proxy('childFocused'));
609 childView.on('blurred', self, self.proxy('childBlurred'));
612 $.when.apply(null, started).then(function () {
614 // options.at: facet inserted at given index, focus next input
615 // otherwise just focus last input
616 if (!options || typeof options.at !== 'number') {
617 input_to_focus = _.last(self.input_subviews);
619 input_to_focus = self.input_subviews[(options.at + 1) * 2];
622 input_to_focus.$el.focus();
627 * Builds a list of widget rows (each row is an array of widgets)
629 * @param {Array} items a list of nodes to convert to widgets
630 * @param {Object} fields a mapping of field names to (ORM) field attributes
631 * @param {Object} [group] group to put the new controls in
633 make_widgets: function (items, fields, group) {
635 group = new instance.web.search.Group(
636 this, 'q', {attrs: {string: _t("Filters")}});
640 _.each(items, function (item) {
641 if (filters.length && item.tag !== 'filter') {
642 group.push(new instance.web.search.FilterGroup(filters, group));
647 case 'separator': case 'newline':
650 filters.push(new instance.web.search.Filter(item, group));
653 self.make_widgets(item.children, fields,
654 new instance.web.search.Group(group, 'w', item));
657 var field = this.make_field(
658 item, fields[item['attrs'].name], group);
661 self.make_widgets(item.children, fields, group);
666 if (filters.length) {
667 group.push(new instance.web.search.FilterGroup(filters, this));
671 * Creates a field for the provided field descriptor item (which comes
672 * from fields_view_get)
674 * @param {Object} item fields_view_get node for the field
675 * @param {Object} field fields_get result for the field
676 * @param {Object} [parent]
677 * @returns instance.web.search.Field
679 make_field: function (item, field, parent) {
680 // M2O combined with selection widget is pointless and broken in search views,
681 // but has been used in the past for unsupported hacks -> ignore it
682 if (field.type === "many2one" && item.attrs.widget === "selection"){
683 item.attrs.widget = undefined;
685 var obj = instance.web.search.fields.get_any( [item.attrs.widget, field.type]);
687 return new (obj) (item, field, parent || this);
689 console.group('Unknown field type ' + field.type);
690 console.error('View node', item);
691 console.info('View field', field);
692 console.info('In view', this);
698 add_common_inputs: function() {
699 // add Filters to this.inputs, need view.controls filled
700 (new instance.web.search.Filters(this));
701 // add custom filters to this.inputs
702 this.custom_filters = new instance.web.search.CustomFilters(this);
703 // add Advanced to this.inputs
704 (new instance.web.search.Advanced(this));
707 search_view_loaded: function(data) {
709 this.fields_view = data;
710 if (data.type !== 'search' ||
711 data.arch.tag !== 'search') {
712 throw new Error(_.str.sprintf(
713 "Got non-search view after asking for a search view: type %s, arch root %s",
714 data.type, data.arch.tag));
717 data['arch'].children,
720 this.add_common_inputs();
723 var drawer_started = $.when.apply(
724 null, _(this.select_for_drawer()).invoke(
725 'appendTo', this.$('.oe_searchview_drawer')));
729 var defaults_fetched = $.when.apply(null, _(this.inputs).invoke(
730 'facet_for_defaults', this.defaults))
731 .then(this.proxy('setup_default_query'));
733 return $.when(drawer_started, defaults_fetched)
735 self.trigger("search_view_loaded", data);
736 self.ready.resolve();
739 setup_default_query: function () {
740 // Hacky implementation of CustomFilters#facet_for_defaults ensure
741 // CustomFilters will be ready (and CustomFilters#filters will be
742 // correctly filled) by the time this method executes.
743 var custom_filters = this.custom_filters.filters;
744 if (!this.options.disable_custom_filters && !_(custom_filters).isEmpty()) {
745 // Check for any is_default custom filter
746 var personal_filter = _(custom_filters).find(function (filter) {
747 return filter.user_id && filter.is_default;
749 if (personal_filter) {
750 this.custom_filters.toggle_filter(personal_filter, true);
754 var global_filter = _(custom_filters).find(function (filter) {
755 return !filter.user_id && filter.is_default;
758 this.custom_filters.toggle_filter(global_filter, true);
762 // No custom filter, or no is_default custom filter, apply view defaults
763 this.query.reset(_(arguments).compact(), {preventSearch: true});
766 * Extract search data from the view's facets.
768 * Result is an object with 4 (own) properties:
771 * An array of any error generated during data validation and
772 * extraction, contains the validation error objects
778 * Array of domains, in groupby order rather than view order
782 build_search_data: function () {
783 var domains = [], contexts = [], groupbys = [], errors = [];
785 this.query.each(function (facet) {
786 var field = facet.get('field');
788 var domain = field.get_domain(facet);
790 domains.push(domain);
792 var context = field.get_context(facet);
794 contexts.push(context);
796 var group_by = field.get_groupby(facet);
798 groupbys.push.apply(groupbys, group_by);
801 if (e instanceof instance.web.search.Invalid) {
816 * Performs the search view collection of widget data.
818 * If the collection went well (all fields are valid), then triggers
819 * :js:func:`instance.web.SearchView.on_search`.
821 * If at least one field failed its validation, triggers
822 * :js:func:`instance.web.SearchView.on_invalid` instead.
825 * @param {Object} [options]
827 do_search: function (_query, options) {
828 if (options && options.preventSearch) {
831 var search = this.build_search_data();
832 if (!_.isEmpty(search.errors)) {
833 this.on_invalid(search.errors);
836 this.trigger('search_data', search.domains, search.contexts, search.groupbys);
839 * Triggered after the SearchView has collected all relevant domains and
842 * It is provided with an Array of domains and an Array of contexts, which
843 * may or may not be evaluated (each item can be either a valid domain or
844 * context, or a string to evaluate in order in the sequence)
846 * It is also passed an array of contexts used for group_by (they are in
847 * the correct order for group_by evaluation, which contexts may not be)
850 * @param {Array} domains an array of literal domains or domain references
851 * @param {Array} contexts an array of literal contexts or context refs
852 * @param {Array} groupbys ordered contexts which may or may not have group_by keys
855 * Triggered after a validation error in the SearchView fields.
857 * Error objects have three keys:
858 * * ``field`` is the name of the invalid field
859 * * ``value`` is the invalid value
860 * * ``message`` is the (in)validation message provided by the field
863 * @param {Array} errors a never-empty array of error objects
865 on_invalid: function (errors) {
866 this.do_notify(_t("Invalid Search"), _t("triggered from search view"));
867 this.trigger('invalid_search', errors);
872 * Registry of search fields, called by :js:class:`instance.web.SearchView` to
873 * find and instantiate its field widgets.
875 instance.web.search.fields = new instance.web.Registry({
876 'char': 'instance.web.search.CharField',
877 'text': 'instance.web.search.CharField',
878 'html': 'instance.web.search.CharField',
879 'boolean': 'instance.web.search.BooleanField',
880 'integer': 'instance.web.search.IntegerField',
881 'id': 'instance.web.search.IntegerField',
882 'float': 'instance.web.search.FloatField',
883 'selection': 'instance.web.search.SelectionField',
884 'datetime': 'instance.web.search.DateTimeField',
885 'date': 'instance.web.search.DateField',
886 'many2one': 'instance.web.search.ManyToOneField',
887 'many2many': 'instance.web.search.CharField',
888 'one2many': 'instance.web.search.CharField'
890 instance.web.search.Invalid = instance.web.Class.extend( /** @lends instance.web.search.Invalid# */{
892 * Exception thrown by search widgets when they hold invalid values,
893 * which they can not return when asked.
895 * @constructs instance.web.search.Invalid
896 * @extends instance.web.Class
898 * @param field the name of the field holding an invalid value
899 * @param value the invalid value
900 * @param message validation failure message
902 init: function (field, value, message) {
905 this.message = message;
907 toString: function () {
908 return _.str.sprintf(
909 _t("Incorrect value for field %(fieldname)s: [%(value)s] is %(message)s"),
910 {fieldname: this.field, value: this.value, message: this.message}
914 instance.web.search.Widget = instance.web.Widget.extend( /** @lends instance.web.search.Widget# */{
917 * Root class of all search widgets
919 * @constructs instance.web.search.Widget
920 * @extends instance.web.Widget
922 * @param parent parent of this widget
924 init: function (parent) {
926 var ancestor = parent;
928 this.view = ancestor;
929 } while (!(ancestor instanceof instance.web.SearchView)
930 && (ancestor = (ancestor.getParent && ancestor.getParent())));
934 instance.web.search.add_expand_listener = function($root) {
935 $root.find('a.searchview_group_string').click(function (e) {
936 $root.toggleClass('folded expanded');
941 instance.web.search.Group = instance.web.search.Widget.extend({
942 init: function (parent, icon, node) {
944 var attrs = node.attrs;
945 this.modifiers = attrs.modifiers =
946 attrs.modifiers ? JSON.parse(attrs.modifiers) : {};
949 this.name = attrs.string;
952 this.view.controls.push(this);
954 push: function (input) {
955 this.children.push(input);
957 visible: function () {
958 return !this.modifiers.invisible;
962 instance.web.search.Input = instance.web.search.Widget.extend( /** @lends instance.web.search.Input# */{
965 * @constructs instance.web.search.Input
966 * @extends instance.web.search.Widget
970 init: function (parent) {
973 this.view.inputs.push(this);
976 * Fetch auto-completion values for the widget.
978 * The completion values should be an array of objects with keys category,
979 * label, value prefixed with an object with keys type=section and label
981 * @param {String} value value to complete
982 * @returns {jQuery.Deferred<null|Array>}
984 complete: function (value) {
988 * Returns a Facet instance for the provided defaults if they apply to
989 * this widget, or null if they don't.
991 * This default implementation will try calling
992 * :js:func:`instance.web.search.Input#facet_for` if the widget's name
993 * matches the input key
995 * @param {Object} defaults
996 * @returns {jQuery.Deferred<null|Object>}
998 facet_for_defaults: function (defaults) {
1000 !(this.attrs.name in defaults && defaults[this.attrs.name])) {
1001 return $.when(null);
1003 return this.facet_for(defaults[this.attrs.name]);
1005 in_drawer: function () {
1006 return !!this._in_drawer;
1008 get_context: function () {
1010 "get_context not implemented for widget " + this.attrs.type);
1012 get_groupby: function () {
1014 "get_groupby not implemented for widget " + this.attrs.type);
1016 get_domain: function () {
1018 "get_domain not implemented for widget " + this.attrs.type);
1020 load_attrs: function (attrs) {
1021 attrs.modifiers = attrs.modifiers ? JSON.parse(attrs.modifiers) : {};
1025 * Returns whether the input is "visible". The default behavior is to
1026 * query the ``modifiers.invisible`` flag on the input's description or
1029 * @returns {Boolean}
1031 visible: function () {
1032 if (this.attrs.modifiers.invisible) {
1036 while ((parent = parent.getParent()) &&
1037 ( (parent instanceof instance.web.search.Group)
1038 || (parent instanceof instance.web.search.Input))) {
1039 if (!parent.visible()) {
1046 instance.web.search.FilterGroup = instance.web.search.Input.extend(/** @lends instance.web.search.FilterGroup# */{
1047 template: 'SearchView.filters',
1049 completion_label: _lt("Filter on: %s"),
1051 * Inclusive group of filters, creates a continuous "button" with clickable
1052 * sections (the normal display for filters is to be a self-contained button)
1054 * @constructs instance.web.search.FilterGroup
1055 * @extends instance.web.search.Input
1057 * @param {Array<instance.web.search.Filter>} filters elements of the group
1058 * @param {instance.web.SearchView} parent parent in which the filters are contained
1060 init: function (filters, parent) {
1061 // If all filters are group_by and we're not initializing a GroupbyGroup,
1062 // create a GroupbyGroup instead of the current FilterGroup
1063 if (!(this instanceof instance.web.search.GroupbyGroup) &&
1064 _(filters).all(function (f) {
1065 if (!f.attrs.context) { return false; }
1066 var c = instance.web.pyeval.eval('context', f.attrs.context);
1067 return !_.isEmpty(c.group_by);})) {
1068 return new instance.web.search.GroupbyGroup(filters, parent);
1070 this._super(parent);
1071 this.filters = filters;
1072 this.view.query.on('add remove change reset', this.proxy('search_change'));
1074 start: function () {
1075 this.$el.on('click', 'li', this.proxy('toggle_filter'));
1076 return $.when(null);
1079 * Handles change of the search query: any of the group's filter which is
1080 * in the search query should be visually checked in the drawer
1082 search_change: function () {
1084 var $filters = this.$('> li').removeClass('oe_selected');
1085 var facet = this.view.query.find(_.bind(this.match_facet, this));
1086 if (!facet) { return; }
1087 facet.values.each(function (v) {
1088 var i = _(self.filters).indexOf(v.get('value'));
1089 if (i === -1) { return; }
1090 $filters.filter(function () {
1091 return Number($(this).data('index')) === i;
1092 }).addClass('oe_selected');
1096 * Matches the group to a facet, in order to find if the group is
1097 * represented in the current search query
1099 match_facet: function (facet) {
1100 return facet.get('field') === this;
1102 make_facet: function (values) {
1104 category: _t("Filter"),
1110 make_value: function (filter) {
1112 label: filter.attrs.string || filter.attrs.help || filter.attrs.name,
1116 facet_for_defaults: function (defaults) {
1118 var fs = _(this.filters).chain()
1119 .filter(function (f) {
1120 return f.attrs && f.attrs.name && !!defaults[f.attrs.name];
1121 }).map(function (f) {
1122 return self.make_value(f);
1124 if (_.isEmpty(fs)) { return $.when(null); }
1125 return $.when(this.make_facet(fs));
1128 * Fetches contexts for all enabled filters in the group
1130 * @param {openerp.web.search.Facet} facet
1131 * @return {*} combined contexts of the enabled filters in this group
1133 get_context: function (facet) {
1134 var contexts = facet.values.chain()
1135 .map(function (f) { return f.get('value').attrs.context; })
1140 if (!contexts.length) { return; }
1141 if (contexts.length === 1) { return contexts[0]; }
1142 return _.extend(new instance.web.CompoundContext(), {
1143 __contexts: contexts
1147 * Fetches group_by sequence for all enabled filters in the group
1149 * @param {VS.model.SearchFacet} facet
1150 * @return {Array} enabled filters in this group
1152 get_groupby: function (facet) {
1153 return facet.values.chain()
1154 .map(function (f) { return f.get('value').attrs.context; })
1160 * Handles domains-fetching for all the filters within it: groups them.
1162 * @param {VS.model.SearchFacet} facet
1163 * @return {*} combined domains of the enabled filters in this group
1165 get_domain: function (facet) {
1166 var domains = facet.values.chain()
1167 .map(function (f) { return f.get('value').attrs.domain; })
1172 if (!domains.length) { return; }
1173 if (domains.length === 1) { return domains[0]; }
1174 for (var i=domains.length; --i;) {
1175 domains.unshift(['|']);
1177 return _.extend(new instance.web.CompoundDomain(), {
1181 toggle_filter: function (e) {
1182 this.toggle(this.filters[Number($(e.target).data('index'))]);
1184 toggle: function (filter) {
1185 this.view.query.toggle(this.make_facet([this.make_value(filter)]));
1187 complete: function (item) {
1189 item = item.toLowerCase();
1190 var facet_values = _(this.filters).chain()
1191 .filter(function (filter) { return filter.visible(); })
1192 .filter(function (filter) {
1194 string: filter.attrs.string || '',
1195 help: filter.attrs.help || '',
1196 name: filter.attrs.name || ''
1198 var include = _.str.include;
1199 return include(at.string.toLowerCase(), item)
1200 || include(at.help.toLowerCase(), item)
1201 || include(at.name.toLowerCase(), item);
1203 .map(this.make_value)
1205 if (_(facet_values).isEmpty()) { return $.when(null); }
1206 return $.when(_.map(facet_values, function (facet_value) {
1208 label: _.str.sprintf(self.completion_label.toString(),
1209 _.escape(facet_value.label)),
1210 facet: self.make_facet([facet_value])
1215 instance.web.search.GroupbyGroup = instance.web.search.FilterGroup.extend({
1217 completion_label: _lt("Group by: %s"),
1218 init: function (filters, parent) {
1219 this._super(filters, parent);
1220 // Not flanders: facet unicity is handled through the
1221 // (category, field) pair of facet attributes. This is all well and
1222 // good for regular filter groups where a group matches a facet, but for
1223 // groupby we want a single facet. So cheat: add an attribute on the
1224 // view which proxies to the first GroupbyGroup, so it can be used
1225 // for every GroupbyGroup and still provides the various methods needed
1226 // by the search view. Use weirdo name to avoid risks of conflicts
1227 if (!this.view._s_groupby) {
1228 this.view._s_groupby = {
1229 help: "See GroupbyGroup#init",
1230 get_context: this.proxy('get_context'),
1231 get_domain: this.proxy('get_domain'),
1232 get_groupby: this.proxy('get_groupby')
1236 match_facet: function (facet) {
1237 return facet.get('field') === this.view._s_groupby;
1239 make_facet: function (values) {
1241 category: _t("GroupBy"),
1244 field: this.view._s_groupby
1248 instance.web.search.Filter = instance.web.search.Input.extend(/** @lends instance.web.search.Filter# */{
1249 template: 'SearchView.filter',
1251 * Implementation of the OpenERP filters (button with a context and/or
1252 * a domain sent as-is to the search view)
1254 * Filters are only attributes holder, the actual work (compositing
1255 * domains and contexts, converting between facets and filters) is
1256 * performed by the filter group.
1258 * @constructs instance.web.search.Filter
1259 * @extends instance.web.search.Input
1264 init: function (node, parent) {
1265 this._super(parent);
1266 this.load_attrs(node.attrs);
1268 facet_for: function () { return $.when(null); },
1269 get_context: function () { },
1270 get_domain: function () { },
1272 instance.web.search.Field = instance.web.search.Input.extend( /** @lends instance.web.search.Field# */ {
1273 template: 'SearchView.field',
1274 default_operator: '=',
1276 * @constructs instance.web.search.Field
1277 * @extends instance.web.search.Input
1279 * @param view_section
1283 init: function (view_section, field, parent) {
1284 this._super(parent);
1285 this.load_attrs(_.extend({}, field, view_section.attrs));
1287 facet_for: function (value) {
1290 category: this.attrs.string || this.attrs.name,
1291 values: [{label: String(value), value: value}]
1294 value_from: function (facetValue) {
1295 return facetValue.get('value');
1297 get_context: function (facet) {
1299 // A field needs a context to send when active
1300 var context = this.attrs.context;
1301 if (_.isEmpty(context) || !facet.values.length) {
1304 var contexts = facet.values.map(function (facetValue) {
1305 return new instance.web.CompoundContext(context)
1306 .set_eval_context({self: self.value_from(facetValue)});
1309 if (contexts.length === 1) { return contexts[0]; }
1311 return _.extend(new instance.web.CompoundContext(), {
1312 __contexts: contexts
1315 get_groupby: function () { },
1317 * Function creating the returned domain for the field, override this
1318 * methods in children if you only need to customize the field's domain
1319 * without more complex alterations or tests (and without the need to
1320 * change override the handling of filter_domain)
1322 * @param {String} name the field's name
1323 * @param {String} operator the field's operator (either attribute-specified or default operator for the field
1324 * @param {Number|String} facet parsed value for the field
1325 * @returns {Array<Array>} domain to include in the resulting search
1327 make_domain: function (name, operator, facet) {
1328 return [[name, operator, this.value_from(facet)]];
1330 get_domain: function (facet) {
1331 if (!facet.values.length) { return; }
1333 var value_to_domain;
1335 var domain = this.attrs['filter_domain'];
1337 value_to_domain = function (facetValue) {
1338 return new instance.web.CompoundDomain(domain)
1339 .set_eval_context({self: self.value_from(facetValue)});
1342 value_to_domain = function (facetValue) {
1343 return self.make_domain(
1345 self.attrs.operator || self.default_operator,
1349 var domains = facet.values.map(value_to_domain);
1351 if (domains.length === 1) { return domains[0]; }
1352 for (var i = domains.length; --i;) {
1353 domains.unshift(['|']);
1356 return _.extend(new instance.web.CompoundDomain(), {
1362 * Implementation of the ``char`` OpenERP field type:
1364 * * Default operator is ``ilike`` rather than ``=``
1366 * * The Javascript and the HTML values are identical (strings)
1369 * @extends instance.web.search.Field
1371 instance.web.search.CharField = instance.web.search.Field.extend( /** @lends instance.web.search.CharField# */ {
1372 default_operator: 'ilike',
1373 complete: function (value) {
1374 if (_.isEmpty(value)) { return $.when(null); }
1375 var label = _.str.sprintf(_.str.escapeHTML(
1376 _t("Search %(field)s for: %(value)s")), {
1377 field: '<em>' + _.escape(this.attrs.string) + '</em>',
1378 value: '<strong>' + _.escape(value) + '</strong>'});
1382 category: this.attrs.string,
1384 values: [{label: value, value: value}]
1389 instance.web.search.NumberField = instance.web.search.Field.extend(/** @lends instance.web.search.NumberField# */{
1390 complete: function (value) {
1391 var val = this.parse(value);
1392 if (isNaN(val)) { return $.when(); }
1393 var label = _.str.sprintf(
1394 _t("Search %(field)s for: %(value)s"), {
1395 field: '<em>' + _.escape(this.attrs.string) + '</em>',
1396 value: '<strong>' + _.escape(value) + '</strong>'});
1400 category: this.attrs.string,
1402 values: [{label: value, value: val}]
1409 * @extends instance.web.search.NumberField
1411 instance.web.search.IntegerField = instance.web.search.NumberField.extend(/** @lends instance.web.search.IntegerField# */{
1412 error_message: _t("not a valid integer"),
1413 parse: function (value) {
1415 return instance.web.parse_value(value, {'widget': 'integer'});
1423 * @extends instance.web.search.NumberField
1425 instance.web.search.FloatField = instance.web.search.NumberField.extend(/** @lends instance.web.search.FloatField# */{
1426 error_message: _t("not a valid number"),
1427 parse: function (value) {
1429 return instance.web.parse_value(value, {'widget': 'float'});
1437 * Utility function for m2o & selection fields taking a selection/name_get pair
1438 * (value, name) and converting it to a Facet descriptor
1440 * @param {instance.web.search.Field} field holder field
1441 * @param {Array} pair pair value to convert
1443 function facet_from(field, pair) {
1446 category: field['attrs'].string,
1447 values: [{label: pair[1], value: pair[0]}]
1453 * @extends instance.web.search.Field
1455 instance.web.search.SelectionField = instance.web.search.Field.extend(/** @lends instance.web.search.SelectionField# */{
1456 // This implementation is a basic <select> field, but it may have to be
1457 // altered to be more in line with the GTK client, which uses a combo box
1458 // (~ jquery.autocomplete):
1459 // * If an option was selected in the list, behave as currently
1460 // * If something which is not in the list was entered (via the text input),
1461 // the default domain should become (`ilike` string_value) but **any
1462 // ``context`` or ``filter_domain`` becomes falsy, idem if ``@operator``
1463 // is specified. So at least get_domain needs to be quite a bit
1464 // overridden (if there's no @value and there is no filter_domain and
1465 // there is no @operator, return [[name, 'ilike', str_val]]
1466 template: 'SearchView.field.selection',
1468 this._super.apply(this, arguments);
1469 // prepend empty option if there is no empty option in the selection list
1470 this.prepend_empty = !_(this.attrs.selection).detect(function (item) {
1474 complete: function (needle) {
1476 var results = _(this.attrs.selection).chain()
1477 .filter(function (sel) {
1478 var value = sel[0], label = sel[1];
1479 if (!value) { return false; }
1480 return label.toLowerCase().indexOf(needle.toLowerCase()) !== -1;
1482 .map(function (sel) {
1484 label: _.escape(sel[1]),
1485 facet: facet_from(self, sel)
1488 if (_.isEmpty(results)) { return $.when(null); }
1489 return $.when.call(null, [{
1490 label: _.escape(this.attrs.string)
1491 }].concat(results));
1493 facet_for: function (value) {
1494 var match = _(this.attrs.selection).detect(function (sel) {
1495 return sel[0] === value;
1497 if (!match) { return $.when(null); }
1498 return $.when(facet_from(this, match));
1501 instance.web.search.BooleanField = instance.web.search.SelectionField.extend(/** @lends instance.web.search.BooleanField# */{
1503 * @constructs instance.web.search.BooleanField
1504 * @extends instance.web.search.BooleanField
1507 this._super.apply(this, arguments);
1508 this.attrs.selection = [
1516 * @extends instance.web.search.DateField
1518 instance.web.search.DateField = instance.web.search.Field.extend(/** @lends instance.web.search.DateField# */{
1519 value_from: function (facetValue) {
1520 return instance.web.date_to_str(facetValue.get('value'));
1522 complete: function (needle) {
1523 var d = Date.parse(needle);
1524 if (!d) { return $.when(null); }
1525 var date_string = instance.web.format_value(d, this.attrs);
1526 var label = _.str.sprintf(_.str.escapeHTML(
1527 _t("Search %(field)s at: %(value)s")), {
1528 field: '<em>' + _.escape(this.attrs.string) + '</em>',
1529 value: '<strong>' + date_string + '</strong>'});
1533 category: this.attrs.string,
1535 values: [{label: date_string, value: d}]
1541 * Implementation of the ``datetime`` openerp field type:
1543 * * Uses the same widget as the ``date`` field type (a simple date)
1545 * * Builds a slighly more complex, it's a datetime range (includes time)
1546 * spanning the whole day selected by the date widget
1549 * @extends instance.web.DateField
1551 instance.web.search.DateTimeField = instance.web.search.DateField.extend(/** @lends instance.web.search.DateTimeField# */{
1552 value_from: function (facetValue) {
1553 return instance.web.datetime_to_str(facetValue.get('value'));
1556 instance.web.search.ManyToOneField = instance.web.search.CharField.extend({
1557 default_operator: {},
1558 init: function (view_section, field, parent) {
1559 this._super(view_section, field, parent);
1560 this.model = new instance.web.Model(this.attrs.relation);
1562 complete: function (needle) {
1564 // FIXME: "concurrent" searches (multiple requests, mis-ordered responses)
1565 var context = instance.web.pyeval.eval(
1566 'contexts', [this.view.dataset.get_context()]);
1567 return this.model.call('name_search', [], {
1569 args: instance.web.pyeval.eval(
1570 'domains', this.attrs.domain ? [this.attrs.domain] : [], context),
1573 }).then(function (results) {
1574 if (_.isEmpty(results)) { return null; }
1575 return [{label: self.attrs.string}].concat(
1576 _(results).map(function (result) {
1578 label: _.escape(result[1]),
1579 facet: facet_from(self, result)
1584 facet_for: function (value) {
1586 if (value instanceof Array) {
1587 if (value.length === 2 && _.isString(value[1])) {
1588 return $.when(facet_from(this, value));
1590 assert(value.length <= 1,
1591 _t("M2O search fields do not currently handle multiple default values"));
1592 // there are many cases of {search_default_$m2ofield: [id]}, need
1593 // to handle this as if it were a single value.
1596 return this.model.call('name_get', [value]).then(function (names) {
1597 if (_(names).isEmpty()) { return null; }
1598 return facet_from(self, names[0]);
1601 value_from: function (facetValue) {
1602 return facetValue.get('label');
1604 make_domain: function (name, operator, facetValue) {
1606 case this.default_operator:
1607 return [[name, '=', facetValue.get('value')]];
1609 return [[name, 'child_of', facetValue.get('value')]];
1611 return this._super(name, operator, facetValue);
1613 get_context: function (facet) {
1614 var values = facet.values;
1615 if (_.isEmpty(this.attrs.context) && values.length === 1) {
1617 c['default_' + this.attrs.name] = values.at(0).get('value');
1620 return this._super(facet);
1624 instance.web.search.CustomFilters = instance.web.search.Input.extend({
1625 template: 'SearchView.CustomFilters',
1628 this.is_ready = $.Deferred();
1629 this._super.apply(this, arguments);
1631 start: function () {
1633 this.model = new instance.web.Model('ir.filters');
1637 .on('remove', function (facet) {
1638 if (!facet.get('is_custom_filter')) {
1641 self.clear_selection();
1643 .on('reset', this.proxy('clear_selection'));
1644 this.$el.on('submit', 'form', this.proxy('save_current'));
1645 this.$el.on('click', 'input[type=checkbox]', function() {
1646 $(this).siblings('input[type=checkbox]').prop('checked', false);
1648 this.$el.on('click', 'h4', function () {
1649 self.$el.toggleClass('oe_opened');
1651 return this.model.call('get_filters', [this.view.model])
1652 .then(this.proxy('set_filters'))
1653 .done(function () { self.is_ready.resolve(); })
1654 .fail(function () { self.is_ready.reject.apply(self.is_ready, arguments); });
1657 * Special implementation delaying defaults until CustomFilters is loaded
1659 facet_for_defaults: function () {
1660 return this.is_ready;
1663 * Generates a mapping key (in the filters and $filter mappings) for the
1664 * filter descriptor object provided (as returned by ``get_filters``).
1666 * The mapping key is guaranteed to be unique for a given (user_id, name)
1669 * @param {Object} filter
1670 * @param {String} filter.name
1671 * @param {Number|Pair<Number, String>} [filter.user_id]
1672 * @return {String} mapping key corresponding to the filter
1674 key_for: function (filter) {
1675 var user_id = filter.user_id;
1676 var uid = (user_id instanceof Array) ? user_id[0] : user_id;
1677 return _.str.sprintf('(%s)%s', uid, filter.name);
1680 * Generates a :js:class:`~instance.web.search.Facet` descriptor from a
1683 * @param {Object} filter
1684 * @param {String} filter.name
1685 * @param {Object} [filter.context]
1686 * @param {Array} [filter.domain]
1689 facet_for: function (filter) {
1691 category: _t("Custom Filter"),
1694 get_context: function () { return filter.context; },
1695 get_groupby: function () { return [filter.context]; },
1696 get_domain: function () { return filter.domain; }
1699 is_custom_filter: true,
1700 values: [{label: filter.name, value: null}]
1703 clear_selection: function () {
1704 this.$('li.oe_selected').removeClass('oe_selected');
1706 append_filter: function (filter) {
1708 var key = this.key_for(filter);
1709 var warning = _t("This filter is global and will be removed for everybody if you continue.");
1712 if (key in this.$filters) {
1713 $filter = this.$filters[key];
1716 this.filters[key] = filter;
1717 $filter = this.$filters[key] = $('<li></li>')
1718 .appendTo(this.$('.oe_searchview_custom_list'))
1719 .addClass(filter.user_id ? 'oe_searchview_custom_private'
1720 : 'oe_searchview_custom_public')
1721 .toggleClass('oe_searchview_custom_default', filter.is_default)
1724 $('<a class="oe_searchview_custom_delete">x</a>')
1725 .click(function (e) {
1726 e.stopPropagation();
1727 if (!(filter.user_id || confirm(warning))) {
1730 self.model.call('unlink', [id]).done(function () {
1732 delete self.$filters[key];
1733 delete self.filters[key];
1739 $filter.unbind('click').click(function () {
1740 self.toggle_filter(filter);
1743 toggle_filter: function (filter, preventSearch) {
1744 var current = this.view.query.find(function (facet) {
1745 return facet.get('_id') === filter.id;
1748 this.view.query.remove(current);
1749 this.$filters[this.key_for(filter)].removeClass('oe_selected');
1752 this.view.query.reset([this.facet_for(filter)], {
1753 preventSearch: preventSearch || false});
1754 this.$filters[this.key_for(filter)].addClass('oe_selected');
1756 set_filters: function (filters) {
1757 _(filters).map(_.bind(this.append_filter, this));
1759 save_current: function () {
1761 var $name = this.$('input:first');
1762 var private_filter = !this.$('#oe_searchview_custom_public').prop('checked');
1763 var set_as_default = this.$('#oe_searchview_custom_default').prop('checked');
1764 if (_.isEmpty($name.val())){
1765 this.do_warn(_t("Error"), _t("Filter name is required."));
1768 var search = this.view.build_search_data();
1769 instance.web.pyeval.eval_domains_and_contexts({
1770 domains: search.domains,
1771 contexts: search.contexts,
1772 group_by_seq: search.groupbys || []
1773 }).done(function (results) {
1774 if (!_.isEmpty(results.group_by)) {
1775 results.context.group_by = results.group_by;
1777 // Don't save user_context keys in the custom filter, otherwise end
1778 // up with e.g. wrong uid or lang stored *and used in subsequent
1780 var ctx = results.context;
1781 _(_.keys(instance.session.user_context)).each(function (key) {
1786 user_id: private_filter ? instance.session.uid : false,
1787 model_id: self.view.model,
1788 context: results.context,
1789 domain: results.domain,
1790 is_default: set_as_default
1792 // FIXME: current context?
1793 return self.model.call('create_or_replace', [filter]).done(function (id) {
1795 self.append_filter(filter);
1797 .removeClass('oe_opened')
1798 .find('form')[0].reset();
1805 instance.web.search.Filters = instance.web.search.Input.extend({
1806 template: 'SearchView.Filters',
1808 start: function () {
1810 var running_count = 0;
1811 // get total filters count
1812 var is_group = function (i) { return i instanceof instance.web.search.FilterGroup; };
1813 var visible_filters = _(this.view.controls).chain().reject(function (group) {
1814 return _(_(group.children).filter(is_group)).isEmpty()
1815 || group.modifiers.invisible;
1817 var filters_count = visible_filters
1821 .map(function (i) { return i.filters.length; })
1825 var col1 = [], col2 = visible_filters.map(function (group) {
1826 var filters = _(group.children).filter(is_group);
1828 name: _.str.sprintf("<span class='oe_i'>%s</span> %s",
1829 group.icon, group.name),
1831 length: _(filters).chain().map(function (i) {
1832 return i.filters.length; }).sum().value()
1836 while (col2.length) {
1837 // col1 + group should be smaller than col2 + group
1838 if ((running_count + col2[0].length) <= (filters_count - running_count)) {
1839 running_count += col2[0].length;
1840 col1.push(col2.shift());
1847 this.render_column(col1, $('<div>').appendTo(this.$el)),
1848 this.render_column(col2, $('<div>').appendTo(this.$el)));
1850 render_column: function (column, $el) {
1851 return $.when.apply(null, _(column).map(function (group) {
1852 $('<h3>').html(group.name).appendTo($el);
1853 return $.when.apply(null,
1854 _(group.filters).invoke('appendTo', $el));
1859 instance.web.search.Advanced = instance.web.search.Input.extend({
1860 template: 'SearchView.advanced',
1862 start: function () {
1865 .on('keypress keydown keyup', function (e) { e.stopPropagation(); })
1866 .on('click', 'h4', function () {
1867 self.$el.toggleClass('oe_opened');
1868 }).on('click', 'button.oe_add_condition', function () {
1869 self.append_proposition();
1870 }).on('submit', 'form', function (e) {
1872 self.commit_search();
1876 new instance.web.Model(this.view.model).call('fields_get', {
1877 context: this.view.dataset.context
1878 }).done(function(data) {
1880 id: { string: 'ID', type: 'id' }
1882 _.each(data, function(field_def, field_name) {
1883 if (field_def.selectable !== false && field_name != 'id') {
1884 self.fields[field_name] = field_def;
1887 })).done(function () {
1888 self.append_proposition();
1891 append_proposition: function () {
1893 return (new instance.web.search.ExtendedSearchProposition(this, this.fields))
1894 .appendTo(this.$('ul')).done(function () {
1895 self.$('button.oe_apply').prop('disabled', false);
1898 remove_proposition: function (prop) {
1899 // removing last proposition, disable apply button
1900 if (this.getChildren().length <= 1) {
1901 this.$('button.oe_apply').prop('disabled', true);
1905 commit_search: function () {
1906 // Get domain sections from all propositions
1907 var children = this.getChildren();
1908 var propositions = _.invoke(children, 'get_proposition');
1909 var domain = _(propositions).pluck('value');
1910 for (var i = domain.length; --i;) {
1911 domain.unshift('|');
1914 this.view.query.add({
1915 category: _t("Advanced"),
1916 values: propositions,
1918 get_context: function () { },
1919 get_domain: function () { return domain;},
1920 get_groupby: function () { }
1924 // remove all propositions
1925 _.invoke(children, 'destroy');
1926 // add new empty proposition
1927 this.append_proposition();
1928 // TODO: API on searchview
1929 this.view.$el.removeClass('oe_searchview_open_drawer');
1933 instance.web.search.ExtendedSearchProposition = instance.web.Widget.extend(/** @lends instance.web.search.ExtendedSearchProposition# */{
1934 template: 'SearchView.extended_search.proposition',
1936 'change .searchview_extended_prop_field': 'changed',
1937 'change .searchview_extended_prop_op': 'operator_changed',
1938 'click .searchview_extended_delete_prop': function (e) {
1939 e.stopPropagation();
1940 this.getParent().remove_proposition(this);
1944 * @constructs instance.web.search.ExtendedSearchProposition
1945 * @extends instance.web.Widget
1950 init: function (parent, fields) {
1951 this._super(parent);
1952 this.fields = _(fields).chain()
1953 .map(function(val, key) { return _.extend({}, val, {'name': key}); })
1954 .filter(function (field) { return !field.deprecated && (field.store === void 0 || field.store || field.fnct_search); })
1955 .sortBy(function(field) {return field.string;})
1957 this.attrs = {_: _, fields: this.fields, selected: null};
1960 start: function () {
1961 return this._super().done(this.proxy('changed'));
1963 changed: function() {
1964 var nval = this.$(".searchview_extended_prop_field").val();
1965 if(this.attrs.selected === null || this.attrs.selected === undefined || nval != this.attrs.selected.name) {
1966 this.select_field(_.detect(this.fields, function(x) {return x.name == nval;}));
1969 operator_changed: function (e) {
1970 var $value = this.$('.searchview_extended_prop_value');
1971 switch ($(e.target).val()) {
1981 * Selects the provided field object
1983 * @param field a field descriptor object (as returned by fields_get, augmented by the field name)
1985 select_field: function(field) {
1987 if(this.attrs.selected !== null && this.attrs.selected !== undefined) {
1988 this.value.destroy();
1990 this.$('.searchview_extended_prop_op').html('');
1992 this.attrs.selected = field;
1993 if(field === null || field === undefined) {
1997 var type = field.type;
1998 var Field = instance.web.search.custom_filters.get_object(type);
2000 Field = instance.web.search.custom_filters.get_object("char");
2002 this.value = new Field(this, field);
2003 _.each(this.value.operators, function(operator) {
2004 $('<option>', {value: operator.value})
2005 .text(String(operator.text))
2006 .appendTo(self.$('.searchview_extended_prop_op'));
2008 var $value_loc = this.$('.searchview_extended_prop_value').show().empty();
2009 this.value.appendTo($value_loc);
2012 get_proposition: function() {
2013 if (this.attrs.selected === null || this.attrs.selected === undefined)
2015 var field = this.attrs.selected;
2016 var op_select = this.$('.searchview_extended_prop_op')[0];
2017 var operator = op_select.options[op_select.selectedIndex];
2020 label: this.value.get_label(field, operator),
2021 value: this.value.get_domain(field, operator),
2026 instance.web.search.ExtendedSearchProposition.Field = instance.web.Widget.extend({
2027 init: function (parent, field) {
2028 this._super(parent);
2031 get_label: function (field, operator) {
2033 switch (operator.value) {
2034 case '∃': case '∄': format = _t('%(field)s %(operator)s'); break;
2035 default: format = _t('%(field)s %(operator)s "%(value)s"'); break;
2037 return this.format_label(format, field, operator);
2039 format_label: function (format, field, operator) {
2040 return _.str.sprintf(format, {
2041 field: field.string,
2042 // According to spec, HTMLOptionElement#label should return
2043 // HTMLOptionElement#text when not defined/empty, but it does
2044 // not in older Webkit (between Safari 5.1.5 and Chrome 17) and
2045 // Gecko (pre Firefox 7) browsers, so we need a manual fallback
2047 operator: operator.label || operator.text,
2051 get_domain: function (field, operator) {
2052 switch (operator.value) {
2053 case '∃': return this.make_domain(field.name, '!=', false);
2054 case '∄': return this.make_domain(field.name, '=', false);
2055 default: return this.make_domain(
2056 field.name, operator.value, this.get_value());
2059 make_domain: function (field, operator, value) {
2060 return [field, operator, value];
2063 * Returns a human-readable version of the value, in case the "logical"
2064 * and the "semantic" values of a field differ (as for selection fields,
2067 * The default implementation simply returns the value itself.
2069 * @return {String} human-readable version of the value
2071 toString: function () {
2072 return this.get_value();
2075 instance.web.search.ExtendedSearchProposition.Char = instance.web.search.ExtendedSearchProposition.Field.extend({
2076 template: 'SearchView.extended_search.proposition.char',
2078 {value: "ilike", text: _lt("contains")},
2079 {value: "not ilike", text: _lt("doesn't contain")},
2080 {value: "=", text: _lt("is equal to")},
2081 {value: "!=", text: _lt("is not equal to")},
2082 {value: "∃", text: _lt("is set")},
2083 {value: "∄", text: _lt("is not set")}
2085 get_value: function() {
2086 return this.$el.val();
2089 instance.web.search.ExtendedSearchProposition.DateTime = instance.web.search.ExtendedSearchProposition.Field.extend({
2090 template: 'SearchView.extended_search.proposition.empty',
2092 {value: "=", text: _lt("is equal to")},
2093 {value: "!=", text: _lt("is not equal to")},
2094 {value: ">", text: _lt("greater than")},
2095 {value: "<", text: _lt("less than")},
2096 {value: ">=", text: _lt("greater or equal than")},
2097 {value: "<=", text: _lt("less or equal than")},
2098 {value: "∃", text: _lt("is set")},
2099 {value: "∄", text: _lt("is not set")}
2102 * Date widgets live in view_form which is not yet loaded when this is
2105 widget: function () { return instance.web.DateTimeWidget; },
2106 get_value: function() {
2107 return this.datewidget.get_value();
2109 toString: function () {
2110 return instance.web.format_value(this.get_value(), { type:"datetime" });
2113 var ready = this._super();
2114 this.datewidget = new (this.widget())(this);
2115 this.datewidget.appendTo(this.$el);
2119 instance.web.search.ExtendedSearchProposition.Date = instance.web.search.ExtendedSearchProposition.DateTime.extend({
2120 widget: function () { return instance.web.DateWidget; },
2121 toString: function () {
2122 return instance.web.format_value(this.get_value(), { type:"date" });
2125 instance.web.search.ExtendedSearchProposition.Integer = instance.web.search.ExtendedSearchProposition.Field.extend({
2126 template: 'SearchView.extended_search.proposition.integer',
2128 {value: "=", text: _lt("is equal to")},
2129 {value: "!=", text: _lt("is not equal to")},
2130 {value: ">", text: _lt("greater than")},
2131 {value: "<", text: _lt("less than")},
2132 {value: ">=", text: _lt("greater or equal than")},
2133 {value: "<=", text: _lt("less or equal than")},
2134 {value: "∃", text: _lt("is set")},
2135 {value: "∄", text: _lt("is not set")}
2137 toString: function () {
2138 return this.$el.val();
2140 get_value: function() {
2142 var val =this.$el.val();
2143 return instance.web.parse_value(val === "" ? 0 : val, {'widget': 'integer'});
2149 instance.web.search.ExtendedSearchProposition.Id = instance.web.search.ExtendedSearchProposition.Integer.extend({
2150 operators: [{value: "=", text: _lt("is")}]
2152 instance.web.search.ExtendedSearchProposition.Float = instance.web.search.ExtendedSearchProposition.Field.extend({
2153 template: 'SearchView.extended_search.proposition.float',
2155 {value: "=", text: _lt("is equal to")},
2156 {value: "!=", text: _lt("is not equal to")},
2157 {value: ">", text: _lt("greater than")},
2158 {value: "<", text: _lt("less than")},
2159 {value: ">=", text: _lt("greater or equal than")},
2160 {value: "<=", text: _lt("less or equal than")},
2161 {value: "∃", text: _lt("is set")},
2162 {value: "∄", text: _lt("is not set")}
2164 toString: function () {
2165 return this.$el.val();
2167 get_value: function() {
2169 var val =this.$el.val();
2170 return instance.web.parse_value(val === "" ? 0.0 : val, {'widget': 'float'});
2176 instance.web.search.ExtendedSearchProposition.Selection = instance.web.search.ExtendedSearchProposition.Field.extend({
2177 template: 'SearchView.extended_search.proposition.selection',
2179 {value: "=", text: _lt("is")},
2180 {value: "!=", text: _lt("is not")},
2181 {value: "∃", text: _lt("is set")},
2182 {value: "∄", text: _lt("is not set")}
2184 toString: function () {
2185 var select = this.$el[0];
2186 var option = select.options[select.selectedIndex];
2187 return option.label || option.text;
2189 get_value: function() {
2190 return this.$el.val();
2193 instance.web.search.ExtendedSearchProposition.Boolean = instance.web.search.ExtendedSearchProposition.Field.extend({
2194 template: 'SearchView.extended_search.proposition.empty',
2196 {value: "=", text: _lt("is true")},
2197 {value: "!=", text: _lt("is false")}
2199 get_label: function (field, operator) {
2200 return this.format_label(
2201 _t('%(field)s %(operator)s'), field, operator);
2203 get_value: function() {
2208 instance.web.search.custom_filters = new instance.web.Registry({
2209 'char': 'instance.web.search.ExtendedSearchProposition.Char',
2210 'text': 'instance.web.search.ExtendedSearchProposition.Char',
2211 'one2many': 'instance.web.search.ExtendedSearchProposition.Char',
2212 'many2one': 'instance.web.search.ExtendedSearchProposition.Char',
2213 'many2many': 'instance.web.search.ExtendedSearchProposition.Char',
2215 'datetime': 'instance.web.search.ExtendedSearchProposition.DateTime',
2216 'date': 'instance.web.search.ExtendedSearchProposition.Date',
2217 'integer': 'instance.web.search.ExtendedSearchProposition.Integer',
2218 'float': 'instance.web.search.ExtendedSearchProposition.Float',
2219 'boolean': 'instance.web.search.ExtendedSearchProposition.Boolean',
2220 'selection': 'instance.web.search.ExtendedSearchProposition.Selection',
2222 'id': 'instance.web.search.ExtendedSearchProposition.Id'
2227 // vim:et fdc=0 fdl=0 foldnestmax=3 fdm=syntax: