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 (_, options) {
33 this.trigger('change', this, options);
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();
344 this.drawer.toggle();
346 'keydown .oe_searchview_input, .oe_searchview_facet': function (e) {
348 case $.ui.keyCode.LEFT:
349 this.focusPreceding(e.target);
352 case $.ui.keyCode.RIGHT:
353 if (!this.autocomplete.is_expandable()) {
354 this.focusFollowing(e.target);
360 'autocompleteopen': function () {
361 this.$el.autocomplete('widget').css('z-index', 9999);
365 * @constructs instance.web.SearchView
366 * @extends instance.web.Widget
372 * @param {Object} [options]
373 * @param {Boolean} [options.hidden=false] hide the search view
374 * @param {Boolean} [options.disable_custom_filters=false] do not load custom filters from ir.filters
376 init: function(parent, dataset, view_id, defaults, options) {
377 this.options = _.defaults(options || {}, {
379 disable_custom_filters: false,
382 this.dataset = dataset;
383 this.model = dataset.model;
384 this.view_id = view_id;
386 this.defaults = defaults || {};
387 this.has_defaults = !_.isEmpty(this.defaults);
389 this.headless = this.options.hidden && !this.has_defaults;
391 this.input_subviews = [];
392 this.view_manager = null;
393 this.$view_manager_header = null;
395 this.ready = $.Deferred();
396 this.drawer_ready = $.Deferred();
397 this.fields_view_get = $.Deferred();
398 this.drawer = new instance.web.SearchViewDrawer(parent, this);
403 var p = this._super();
405 this.$view_manager_header = this.$el.parents(".oe_view_manager_header").first();
407 this.setup_global_completion();
408 this.query = new my.SearchQuery()
409 .on('add change reset remove', this.proxy('do_search'))
410 .on('change', this.proxy('renderChangedFacets'))
411 .on('add reset remove', this.proxy('renderFacets'));
413 if (this.options.hidden) {
417 this.ready.resolve();
419 var load_view = instance.web.fields_view_get({
420 model: this.dataset._model,
421 view_id: this.view_id,
423 context: this.dataset.get_context(),
426 this.alive($.when(load_view)).then(function (r) {
427 self.fields_view_get.resolve(r);
428 return self.search_view_loaded(r);
429 }).fail(function () {
430 self.ready.reject.apply(null, arguments);
434 var view_manager = this.getParent();
435 while (!(view_manager instanceof instance.web.ViewManager) &&
436 view_manager && view_manager.getParent) {
437 view_manager = view_manager.getParent();
441 this.view_manager = view_manager;
442 view_manager.on('switch_mode', this, function (e) {
443 self.drawer.toggle(e === 'graph');
446 return $.when(p, this.ready);
449 set_drawer: function (drawer) {
450 this.drawer = drawer;
460 subviewForRoot: function (subview_root) {
461 return _(this.input_subviews).detect(function (subview) {
462 return subview.$el[0] === subview_root;
465 siblingSubview: function (subview, direction, wrap_around) {
466 var index = _(this.input_subviews).indexOf(subview) + direction;
467 if (wrap_around && index < 0) {
468 index = this.input_subviews.length - 1;
469 } else if (wrap_around && index >= this.input_subviews.length) {
472 return this.input_subviews[index];
474 focusPreceding: function (subview_root) {
475 return this.siblingSubview(
476 this.subviewForRoot(subview_root), -1, true)
479 focusFollowing: function (subview_root) {
480 return this.siblingSubview(
481 this.subviewForRoot(subview_root), +1, true)
486 * Sets up search view's view-wide auto-completion widget
488 setup_global_completion: function () {
490 this.autocomplete = new instance.web.search.AutoComplete(this, {
491 source: this.proxy('complete_global_search'),
492 select: this.proxy('select_completion'),
494 get_search_string: function () {
495 return self.$('div.oe_searchview_input').text();
497 width: this.$el.width(),
499 this.autocomplete.appendTo(this.$el);
502 * Provide auto-completion result for req.term (an array to `resp`)
504 * @param {Object} req request to complete
505 * @param {String} req.term searched term to complete
506 * @param {Function} resp response callback
508 complete_global_search: function (req, resp) {
509 $.when.apply(null, _(this.drawer.inputs).chain()
510 .filter(function (input) { return input.visible(); })
511 .invoke('complete', req.term)
512 .value()).then(function () {
513 resp(_(arguments).chain()
521 * Action to perform in case of selection: create a facet (model)
522 * and add it to the search collection
524 * @param {Object} e selection event, preventDefault to avoid setting value on object
525 * @param {Object} ui selection information
526 * @param {Object} ui.item selected completion item
528 select_completion: function (e, ui) {
531 var input_index = _(this.input_subviews).indexOf(
533 this.$('div.oe_searchview_input:focus')[0]));
534 this.query.add(ui.item.facet, {at: input_index / 2});
536 childFocused: function () {
537 this.$el.addClass('oe_focused');
539 childBlurred: function () {
540 var val = this.$el.val();
542 this.$el.removeClass('oe_focused')
544 this.autocomplete.close();
547 * Call the renderFacets method with the correct arguments.
548 * This is due to the fact that change events are called with two arguments
549 * (model, options) while add, reset and remove events are called with
550 * (collection, model, options) as arguments
552 renderChangedFacets: function (model, options) {
553 this.renderFacets(undefined, model, options);
556 * @param {openerp.web.search.SearchQuery | undefined} Undefined if event is change
557 * @param {openerp.web.search.Facet}
558 * @param {Object} [options]
560 renderFacets: function (collection, model, options) {
563 var $e = this.$('div.oe_searchview_facets');
564 _.invoke(this.input_subviews, 'destroy');
565 this.input_subviews = [];
567 var i = new my.InputView(this);
568 started.push(i.appendTo($e));
569 this.input_subviews.push(i);
570 this.query.each(function (facet) {
571 var f = new my.FacetView(this, facet);
572 started.push(f.appendTo($e));
573 self.input_subviews.push(f);
575 var i = new my.InputView(this);
576 started.push(i.appendTo($e));
577 self.input_subviews.push(i);
579 _.each(this.input_subviews, function (childView) {
580 childView.on('focused', self, self.proxy('childFocused'));
581 childView.on('blurred', self, self.proxy('childBlurred'));
584 $.when.apply(null, started).then(function () {
585 if (options && options.focus_input === false) return;
587 // options.at: facet inserted at given index, focus next input
588 // otherwise just focus last input
589 if (!options || typeof options.at !== 'number') {
590 input_to_focus = _.last(self.input_subviews);
592 input_to_focus = self.input_subviews[(options.at + 1) * 2];
594 input_to_focus.$el.focus();
598 search_view_loaded: function(data) {
600 this.fields_view = data;
601 this.view_id = this.view_id || data.view_id;
602 if (data.type !== 'search' ||
603 data.arch.tag !== 'search') {
604 throw new Error(_.str.sprintf(
605 "Got non-search view after asking for a search view: type %s, arch root %s",
606 data.type, data.arch.tag));
609 return this.drawer_ready
610 .then(this.proxy('setup_default_query'))
612 self.trigger("search_view_loaded", data);
613 self.ready.resolve();
616 setup_default_query: function () {
617 // Hacky implementation of CustomFilters#facet_for_defaults ensure
618 // CustomFilters will be ready (and CustomFilters#filters will be
619 // correctly filled) by the time this method executes.
620 var custom_filters = this.drawer.custom_filters.filters;
621 if (!this.options.disable_custom_filters && !_(custom_filters).isEmpty()) {
622 // Check for any is_default custom filter
623 var personal_filter = _(custom_filters).find(function (filter) {
624 return filter.user_id && filter.is_default;
626 if (personal_filter) {
627 this.drawer.custom_filters.toggle_filter(personal_filter, true);
631 var global_filter = _(custom_filters).find(function (filter) {
632 return !filter.user_id && filter.is_default;
635 this.drawer.custom_filters.toggle_filter(global_filter, true);
639 // No custom filter, or no is_default custom filter, apply view defaults
640 this.query.reset(_(arguments).compact(), {preventSearch: true});
643 * Extract search data from the view's facets.
645 * Result is an object with 4 (own) properties:
648 * An array of any error generated during data validation and
649 * extraction, contains the validation error objects
655 * Array of domains, in groupby order rather than view order
659 build_search_data: function () {
660 var domains = [], contexts = [], groupbys = [], errors = [];
662 this.query.each(function (facet) {
663 var field = facet.get('field');
665 var domain = field.get_domain(facet);
667 domains.push(domain);
669 var context = field.get_context(facet);
671 contexts.push(context);
673 var group_by = field.get_groupby(facet);
675 groupbys.push.apply(groupbys, group_by);
678 if (e instanceof instance.web.search.Invalid) {
693 * Performs the search view collection of widget data.
695 * If the collection went well (all fields are valid), then triggers
696 * :js:func:`instance.web.SearchView.on_search`.
698 * If at least one field failed its validation, triggers
699 * :js:func:`instance.web.SearchView.on_invalid` instead.
702 * @param {Object} [options]
704 do_search: function (_query, options) {
705 if (options && options.preventSearch) {
708 var search = this.build_search_data();
709 if (!_.isEmpty(search.errors)) {
710 this.on_invalid(search.errors);
713 this.trigger('search_data', search.domains, search.contexts, search.groupbys);
716 * Triggered after the SearchView has collected all relevant domains and
719 * It is provided with an Array of domains and an Array of contexts, which
720 * may or may not be evaluated (each item can be either a valid domain or
721 * context, or a string to evaluate in order in the sequence)
723 * It is also passed an array of contexts used for group_by (they are in
724 * the correct order for group_by evaluation, which contexts may not be)
727 * @param {Array} domains an array of literal domains or domain references
728 * @param {Array} contexts an array of literal contexts or context refs
729 * @param {Array} groupbys ordered contexts which may or may not have group_by keys
732 * Triggered after a validation error in the SearchView fields.
734 * Error objects have three keys:
735 * * ``field`` is the name of the invalid field
736 * * ``value`` is the invalid value
737 * * ``message`` is the (in)validation message provided by the field
740 * @param {Array} errors a never-empty array of error objects
742 on_invalid: function (errors) {
743 this.do_notify(_t("Invalid Search"), _t("triggered from search view"));
744 this.trigger('invalid_search', errors);
747 // The method appendTo is overwrited to be able to insert the drawer anywhere
748 appendTo: function ($searchview_parent, $searchview_drawer_node) {
749 var $searchview_drawer_node = $searchview_drawer_node || $searchview_parent;
752 this._super($searchview_parent),
753 this.drawer.appendTo($searchview_drawer_node)
757 destroy: function () {
758 this.drawer.destroy();
759 this.getParent().destroy.call(this);
763 instance.web.SearchViewDrawer = instance.web.Widget.extend({
764 template: "SearchViewDrawer",
766 init: function(parent, searchview) {
768 this.searchview = searchview;
769 this.searchview.set_drawer(this);
770 this.ready = searchview.drawer_ready;
775 toggle: function (visibility) {
776 this.$el.toggle(visibility);
777 var $view_manager_body = this.$el.closest('.oe_view_manager_body');
778 if ($view_manager_body.length) {
779 $view_manager_body.scrollTop(0);
785 if (this.searchview.headless) return $.when(this._super(), this.searchview.ready);
786 var filters_ready = this.searchview.fields_view_get
787 .then(this.proxy('prepare_filters'));
788 return $.when(this._super(), filters_ready).then(function () {
789 var defaults = arguments[1][0];
790 self.ready.resolve.apply(null, defaults);
793 prepare_filters: function (data) {
795 data['arch'].children,
798 this.add_common_inputs();
801 var in_drawer = this.select_for_drawer();
803 var $first_col = this.$(".col-md-7"),
804 $snd_col = this.$(".col-md-5");
806 var add_custom_filters = in_drawer[0].appendTo($first_col),
807 add_filters = in_drawer[1].appendTo($first_col),
808 add_rest = $.when.apply(null, _(in_drawer.slice(2)).invoke('appendTo', $snd_col)),
809 defaults_fetched = $.when.apply(null, _(this.inputs).invoke(
810 'facet_for_defaults', this.searchview.defaults));
812 return $.when(defaults_fetched, add_custom_filters, add_filters, add_rest);
815 * Sets up thingie where all the mess is put?
817 select_for_drawer: function () {
818 return _(this.inputs).filter(function (input) {
819 return input.in_drawer();
824 * Builds a list of widget rows (each row is an array of widgets)
826 * @param {Array} items a list of nodes to convert to widgets
827 * @param {Object} fields a mapping of field names to (ORM) field attributes
828 * @param {Object} [group] group to put the new controls in
830 make_widgets: function (items, fields, group) {
832 group = new instance.web.search.Group(
833 this, 'q', {attrs: {string: _t("Filters")}});
837 _.each(items, function (item) {
838 if (filters.length && item.tag !== 'filter') {
839 group.push(new instance.web.search.FilterGroup(filters, group));
844 case 'separator': case 'newline':
847 filters.push(new instance.web.search.Filter(item, group));
850 self.add_separator();
851 self.make_widgets(item.children, fields,
852 new instance.web.search.Group(group, 'w', item));
853 self.add_separator();
856 var field = this.make_field(
857 item, fields[item['attrs'].name], group);
860 self.make_widgets(item.children, fields, group);
865 if (filters.length) {
866 group.push(new instance.web.search.FilterGroup(filters, this));
870 add_separator: function () {
871 if (!(_.last(this.inputs) instanceof instance.web.search.Separator))
872 new instance.web.search.Separator(this);
875 * Creates a field for the provided field descriptor item (which comes
876 * from fields_view_get)
878 * @param {Object} item fields_view_get node for the field
879 * @param {Object} field fields_get result for the field
880 * @param {Object} [parent]
881 * @returns instance.web.search.Field
883 make_field: function (item, field, parent) {
884 // M2O combined with selection widget is pointless and broken in search views,
885 // but has been used in the past for unsupported hacks -> ignore it
886 if (field.type === "many2one" && item.attrs.widget === "selection"){
887 item.attrs.widget = undefined;
889 var obj = instance.web.search.fields.get_any( [item.attrs.widget, field.type]);
891 return new (obj) (item, field, parent || this);
893 console.group('Unknown field type ' + field.type);
894 console.error('View node', item);
895 console.info('View field', field);
896 console.info('In view', this);
902 add_common_inputs: function() {
903 // add custom filters to this.inputs
904 this.custom_filters = new instance.web.search.CustomFilters(this);
905 // add Filters to this.inputs, need view.controls filled
906 (new instance.web.search.Filters(this));
907 (new instance.web.search.SaveFilter(this, this.custom_filters));
908 // add Advanced to this.inputs
909 (new instance.web.search.Advanced(this));
915 * Registry of search fields, called by :js:class:`instance.web.SearchView` to
916 * find and instantiate its field widgets.
918 instance.web.search.fields = new instance.web.Registry({
919 'char': 'instance.web.search.CharField',
920 'text': 'instance.web.search.CharField',
921 'html': 'instance.web.search.CharField',
922 'boolean': 'instance.web.search.BooleanField',
923 'integer': 'instance.web.search.IntegerField',
924 'id': 'instance.web.search.IntegerField',
925 'float': 'instance.web.search.FloatField',
926 'selection': 'instance.web.search.SelectionField',
927 'datetime': 'instance.web.search.DateTimeField',
928 'date': 'instance.web.search.DateField',
929 'many2one': 'instance.web.search.ManyToOneField',
930 'many2many': 'instance.web.search.CharField',
931 'one2many': 'instance.web.search.CharField'
933 instance.web.search.Invalid = instance.web.Class.extend( /** @lends instance.web.search.Invalid# */{
935 * Exception thrown by search widgets when they hold invalid values,
936 * which they can not return when asked.
938 * @constructs instance.web.search.Invalid
939 * @extends instance.web.Class
941 * @param field the name of the field holding an invalid value
942 * @param value the invalid value
943 * @param message validation failure message
945 init: function (field, value, message) {
948 this.message = message;
950 toString: function () {
951 return _.str.sprintf(
952 _t("Incorrect value for field %(fieldname)s: [%(value)s] is %(message)s"),
953 {fieldname: this.field, value: this.value, message: this.message}
957 instance.web.search.Widget = instance.web.Widget.extend( /** @lends instance.web.search.Widget# */{
960 * Root class of all search widgets
962 * @constructs instance.web.search.Widget
963 * @extends instance.web.Widget
965 * @param parent parent of this widget
967 init: function (parent) {
969 var ancestor = parent;
971 this.drawer = ancestor;
972 } while (!(ancestor instanceof instance.web.SearchViewDrawer)
973 && (ancestor = (ancestor.getParent && ancestor.getParent())));
974 this.view = this.drawer.searchview || this.drawer;
978 instance.web.search.add_expand_listener = function($root) {
979 $root.find('a.searchview_group_string').click(function (e) {
980 $root.toggleClass('folded expanded');
985 instance.web.search.Group = instance.web.search.Widget.extend({
986 init: function (parent, icon, node) {
988 var attrs = node.attrs;
989 this.modifiers = attrs.modifiers =
990 attrs.modifiers ? JSON.parse(attrs.modifiers) : {};
993 this.name = attrs.string;
996 this.drawer.controls.push(this);
998 push: function (input) {
999 this.children.push(input);
1001 visible: function () {
1002 return !this.modifiers.invisible;
1006 instance.web.search.Input = instance.web.search.Widget.extend( /** @lends instance.web.search.Input# */{
1009 * @constructs instance.web.search.Input
1010 * @extends instance.web.search.Widget
1014 init: function (parent) {
1015 this._super(parent);
1016 this.load_attrs({});
1017 this.drawer.inputs.push(this);
1020 * Fetch auto-completion values for the widget.
1022 * The completion values should be an array of objects with keys category,
1023 * label, value prefixed with an object with keys type=section and label
1025 * @param {String} value value to complete
1026 * @returns {jQuery.Deferred<null|Array>}
1028 complete: function (value) {
1029 return $.when(null);
1032 * Returns a Facet instance for the provided defaults if they apply to
1033 * this widget, or null if they don't.
1035 * This default implementation will try calling
1036 * :js:func:`instance.web.search.Input#facet_for` if the widget's name
1037 * matches the input key
1039 * @param {Object} defaults
1040 * @returns {jQuery.Deferred<null|Object>}
1042 facet_for_defaults: function (defaults) {
1044 !(this.attrs.name in defaults && defaults[this.attrs.name])) {
1045 return $.when(null);
1047 return this.facet_for(defaults[this.attrs.name]);
1049 in_drawer: function () {
1050 return !!this._in_drawer;
1052 get_context: function () {
1054 "get_context not implemented for widget " + this.attrs.type);
1056 get_groupby: function () {
1058 "get_groupby not implemented for widget " + this.attrs.type);
1060 get_domain: function () {
1062 "get_domain not implemented for widget " + this.attrs.type);
1064 load_attrs: function (attrs) {
1065 attrs.modifiers = attrs.modifiers ? JSON.parse(attrs.modifiers) : {};
1069 * Returns whether the input is "visible". The default behavior is to
1070 * query the ``modifiers.invisible`` flag on the input's description or
1073 * @returns {Boolean}
1075 visible: function () {
1076 if (this.attrs.modifiers.invisible) {
1080 while ((parent = parent.getParent()) &&
1081 ( (parent instanceof instance.web.search.Group)
1082 || (parent instanceof instance.web.search.Input))) {
1083 if (!parent.visible()) {
1090 instance.web.search.FilterGroup = instance.web.search.Input.extend(/** @lends instance.web.search.FilterGroup# */{
1091 template: 'SearchView.filters',
1093 completion_label: _lt("Filter on: %s"),
1095 * Inclusive group of filters, creates a continuous "button" with clickable
1096 * sections (the normal display for filters is to be a self-contained button)
1098 * @constructs instance.web.search.FilterGroup
1099 * @extends instance.web.search.Input
1101 * @param {Array<instance.web.search.Filter>} filters elements of the group
1102 * @param {instance.web.SearchView} parent parent in which the filters are contained
1104 init: function (filters, parent) {
1105 // If all filters are group_by and we're not initializing a GroupbyGroup,
1106 // create a GroupbyGroup instead of the current FilterGroup
1107 if (!(this instanceof instance.web.search.GroupbyGroup) &&
1108 _(filters).all(function (f) {
1109 if (!f.attrs.context) { return false; }
1110 var c = instance.web.pyeval.eval('context', f.attrs.context);
1111 return !_.isEmpty(c.group_by);})) {
1112 return new instance.web.search.GroupbyGroup(filters, parent);
1114 this._super(parent);
1115 this.filters = filters;
1116 this.view.query.on('add remove change reset', this.proxy('search_change'));
1118 start: function () {
1119 this.$el.on('click', 'li', this.proxy('toggle_filter'));
1120 return $.when(null);
1123 * Handles change of the search query: any of the group's filter which is
1124 * in the search query should be visually checked in the drawer
1126 search_change: function () {
1128 var $filters = this.$('> li').removeClass('badge');
1129 var facet = this.view.query.find(_.bind(this.match_facet, this));
1130 if (!facet) { return; }
1131 facet.values.each(function (v) {
1132 var i = _(self.filters).indexOf(v.get('value'));
1133 if (i === -1) { return; }
1134 $filters.filter(function () {
1135 return Number($(this).data('index')) === i;
1136 }).addClass('badge');
1140 * Matches the group to a facet, in order to find if the group is
1141 * represented in the current search query
1143 match_facet: function (facet) {
1144 return facet.get('field') === this;
1146 make_facet: function (values) {
1148 category: _t("Filter"),
1154 make_value: function (filter) {
1156 label: filter.attrs.string || filter.attrs.help || filter.attrs.name,
1160 facet_for_defaults: function (defaults) {
1162 var fs = _(this.filters).chain()
1163 .filter(function (f) {
1164 return f.attrs && f.attrs.name && !!defaults[f.attrs.name];
1165 }).map(function (f) {
1166 return self.make_value(f);
1168 if (_.isEmpty(fs)) { return $.when(null); }
1169 return $.when(this.make_facet(fs));
1172 * Fetches contexts for all enabled filters in the group
1174 * @param {openerp.web.search.Facet} facet
1175 * @return {*} combined contexts of the enabled filters in this group
1177 get_context: function (facet) {
1178 var contexts = facet.values.chain()
1179 .map(function (f) { return f.get('value').attrs.context; })
1184 if (!contexts.length) { return; }
1185 if (contexts.length === 1) { return contexts[0]; }
1186 return _.extend(new instance.web.CompoundContext(), {
1187 __contexts: contexts
1191 * Fetches group_by sequence for all enabled filters in the group
1193 * @param {VS.model.SearchFacet} facet
1194 * @return {Array} enabled filters in this group
1196 get_groupby: function (facet) {
1197 return facet.values.chain()
1198 .map(function (f) { return f.get('value').attrs.context; })
1204 * Handles domains-fetching for all the filters within it: groups them.
1206 * @param {VS.model.SearchFacet} facet
1207 * @return {*} combined domains of the enabled filters in this group
1209 get_domain: function (facet) {
1210 var domains = facet.values.chain()
1211 .map(function (f) { return f.get('value').attrs.domain; })
1216 if (!domains.length) { return; }
1217 if (domains.length === 1) { return domains[0]; }
1218 for (var i=domains.length; --i;) {
1219 domains.unshift(['|']);
1221 return _.extend(new instance.web.CompoundDomain(), {
1225 toggle_filter: function (e) {
1226 this.toggle(this.filters[Number($(e.target).data('index'))]);
1228 toggle: function (filter) {
1229 this.view.query.toggle(this.make_facet([this.make_value(filter)]));
1231 complete: function (item) {
1233 item = item.toLowerCase();
1234 var facet_values = _(this.filters).chain()
1235 .filter(function (filter) { return filter.visible(); })
1236 .filter(function (filter) {
1238 string: filter.attrs.string || '',
1239 help: filter.attrs.help || '',
1240 name: filter.attrs.name || ''
1242 var include = _.str.include;
1243 return include(at.string.toLowerCase(), item)
1244 || include(at.help.toLowerCase(), item)
1245 || include(at.name.toLowerCase(), item);
1247 .map(this.make_value)
1249 if (_(facet_values).isEmpty()) { return $.when(null); }
1250 return $.when(_.map(facet_values, function (facet_value) {
1252 label: _.str.sprintf(self.completion_label.toString(),
1253 _.escape(facet_value.label)),
1254 facet: self.make_facet([facet_value])
1259 instance.web.search.GroupbyGroup = instance.web.search.FilterGroup.extend({
1261 completion_label: _lt("Group by: %s"),
1262 init: function (filters, parent) {
1263 this._super(filters, parent);
1264 // Not flanders: facet unicity is handled through the
1265 // (category, field) pair of facet attributes. This is all well and
1266 // good for regular filter groups where a group matches a facet, but for
1267 // groupby we want a single facet. So cheat: add an attribute on the
1268 // view which proxies to the first GroupbyGroup, so it can be used
1269 // for every GroupbyGroup and still provides the various methods needed
1270 // by the search view. Use weirdo name to avoid risks of conflicts
1271 if (!this.view._s_groupby) {
1272 this.view._s_groupby = {
1273 help: "See GroupbyGroup#init",
1274 get_context: this.proxy('get_context'),
1275 get_domain: this.proxy('get_domain'),
1276 get_groupby: this.proxy('get_groupby')
1280 match_facet: function (facet) {
1281 return facet.get('field') === this.view._s_groupby;
1283 make_facet: function (values) {
1285 category: _t("GroupBy"),
1288 field: this.view._s_groupby
1292 instance.web.search.Filter = instance.web.search.Input.extend(/** @lends instance.web.search.Filter# */{
1293 template: 'SearchView.filter',
1295 * Implementation of the OpenERP filters (button with a context and/or
1296 * a domain sent as-is to the search view)
1298 * Filters are only attributes holder, the actual work (compositing
1299 * domains and contexts, converting between facets and filters) is
1300 * performed by the filter group.
1302 * @constructs instance.web.search.Filter
1303 * @extends instance.web.search.Input
1308 init: function (node, parent) {
1309 this._super(parent);
1310 this.load_attrs(node.attrs);
1312 facet_for: function () { return $.when(null); },
1313 get_context: function () { },
1314 get_domain: function () { },
1317 instance.web.search.Separator = instance.web.search.Input.extend({
1320 complete: function () {
1321 return {is_separator: true};
1325 instance.web.search.Field = instance.web.search.Input.extend( /** @lends instance.web.search.Field# */ {
1326 template: 'SearchView.field',
1327 default_operator: '=',
1329 * @constructs instance.web.search.Field
1330 * @extends instance.web.search.Input
1332 * @param view_section
1336 init: function (view_section, field, parent) {
1337 this._super(parent);
1338 this.load_attrs(_.extend({}, field, view_section.attrs));
1340 facet_for: function (value) {
1343 category: this.attrs.string || this.attrs.name,
1344 values: [{label: String(value), value: value}]
1347 value_from: function (facetValue) {
1348 return facetValue.get('value');
1350 get_context: function (facet) {
1352 // A field needs a context to send when active
1353 var context = this.attrs.context;
1354 if (_.isEmpty(context) || !facet.values.length) {
1357 var contexts = facet.values.map(function (facetValue) {
1358 return new instance.web.CompoundContext(context)
1359 .set_eval_context({self: self.value_from(facetValue)});
1362 if (contexts.length === 1) { return contexts[0]; }
1364 return _.extend(new instance.web.CompoundContext(), {
1365 __contexts: contexts
1368 get_groupby: function () { },
1370 * Function creating the returned domain for the field, override this
1371 * methods in children if you only need to customize the field's domain
1372 * without more complex alterations or tests (and without the need to
1373 * change override the handling of filter_domain)
1375 * @param {String} name the field's name
1376 * @param {String} operator the field's operator (either attribute-specified or default operator for the field
1377 * @param {Number|String} facet parsed value for the field
1378 * @returns {Array<Array>} domain to include in the resulting search
1380 make_domain: function (name, operator, facet) {
1381 return [[name, operator, this.value_from(facet)]];
1383 get_domain: function (facet) {
1384 if (!facet.values.length) { return; }
1386 var value_to_domain;
1388 var domain = this.attrs['filter_domain'];
1390 value_to_domain = function (facetValue) {
1391 return new instance.web.CompoundDomain(domain)
1392 .set_eval_context({self: self.value_from(facetValue)});
1395 value_to_domain = function (facetValue) {
1396 return self.make_domain(
1398 self.attrs.operator || self.default_operator,
1402 var domains = facet.values.map(value_to_domain);
1404 if (domains.length === 1) { return domains[0]; }
1405 for (var i = domains.length; --i;) {
1406 domains.unshift(['|']);
1409 return _.extend(new instance.web.CompoundDomain(), {
1415 * Implementation of the ``char`` OpenERP field type:
1417 * * Default operator is ``ilike`` rather than ``=``
1419 * * The Javascript and the HTML values are identical (strings)
1422 * @extends instance.web.search.Field
1424 instance.web.search.CharField = instance.web.search.Field.extend( /** @lends instance.web.search.CharField# */ {
1425 default_operator: 'ilike',
1426 complete: function (value) {
1427 if (_.isEmpty(value)) { return $.when(null); }
1428 var label = _.str.sprintf(_.str.escapeHTML(
1429 _t("Search %(field)s for: %(value)s")), {
1430 field: '<em>' + _.escape(this.attrs.string) + '</em>',
1431 value: '<strong>' + _.escape(value) + '</strong>'});
1435 category: this.attrs.string,
1437 values: [{label: value, value: value}]
1442 instance.web.search.NumberField = instance.web.search.Field.extend(/** @lends instance.web.search.NumberField# */{
1443 complete: function (value) {
1444 var val = this.parse(value);
1445 if (isNaN(val)) { return $.when(); }
1446 var label = _.str.sprintf(
1447 _t("Search %(field)s for: %(value)s"), {
1448 field: '<em>' + _.escape(this.attrs.string) + '</em>',
1449 value: '<strong>' + _.escape(value) + '</strong>'});
1453 category: this.attrs.string,
1455 values: [{label: value, value: val}]
1462 * @extends instance.web.search.NumberField
1464 instance.web.search.IntegerField = instance.web.search.NumberField.extend(/** @lends instance.web.search.IntegerField# */{
1465 error_message: _t("not a valid integer"),
1466 parse: function (value) {
1468 return instance.web.parse_value(value, {'widget': 'integer'});
1476 * @extends instance.web.search.NumberField
1478 instance.web.search.FloatField = instance.web.search.NumberField.extend(/** @lends instance.web.search.FloatField# */{
1479 error_message: _t("not a valid number"),
1480 parse: function (value) {
1482 return instance.web.parse_value(value, {'widget': 'float'});
1490 * Utility function for m2o & selection fields taking a selection/name_get pair
1491 * (value, name) and converting it to a Facet descriptor
1493 * @param {instance.web.search.Field} field holder field
1494 * @param {Array} pair pair value to convert
1496 function facet_from(field, pair) {
1499 category: field['attrs'].string,
1500 values: [{label: pair[1], value: pair[0]}]
1506 * @extends instance.web.search.Field
1508 instance.web.search.SelectionField = instance.web.search.Field.extend(/** @lends instance.web.search.SelectionField# */{
1509 // This implementation is a basic <select> field, but it may have to be
1510 // altered to be more in line with the GTK client, which uses a combo box
1511 // (~ jquery.autocomplete):
1512 // * If an option was selected in the list, behave as currently
1513 // * If something which is not in the list was entered (via the text input),
1514 // the default domain should become (`ilike` string_value) but **any
1515 // ``context`` or ``filter_domain`` becomes falsy, idem if ``@operator``
1516 // is specified. So at least get_domain needs to be quite a bit
1517 // overridden (if there's no @value and there is no filter_domain and
1518 // there is no @operator, return [[name, 'ilike', str_val]]
1519 template: 'SearchView.field.selection',
1521 this._super.apply(this, arguments);
1522 // prepend empty option if there is no empty option in the selection list
1523 this.prepend_empty = !_(this.attrs.selection).detect(function (item) {
1527 complete: function (needle) {
1529 var results = _(this.attrs.selection).chain()
1530 .filter(function (sel) {
1531 var value = sel[0], label = sel[1];
1532 if (!value) { return false; }
1533 return label.toLowerCase().indexOf(needle.toLowerCase()) !== -1;
1535 .map(function (sel) {
1537 label: _.escape(sel[1]),
1538 facet: facet_from(self, sel)
1541 if (_.isEmpty(results)) { return $.when(null); }
1542 return $.when.call(null, [{
1543 label: _.escape(this.attrs.string)
1544 }].concat(results));
1546 facet_for: function (value) {
1547 var match = _(this.attrs.selection).detect(function (sel) {
1548 return sel[0] === value;
1550 if (!match) { return $.when(null); }
1551 return $.when(facet_from(this, match));
1554 instance.web.search.BooleanField = instance.web.search.SelectionField.extend(/** @lends instance.web.search.BooleanField# */{
1556 * @constructs instance.web.search.BooleanField
1557 * @extends instance.web.search.BooleanField
1560 this._super.apply(this, arguments);
1561 this.attrs.selection = [
1569 * @extends instance.web.search.DateField
1571 instance.web.search.DateField = instance.web.search.Field.extend(/** @lends instance.web.search.DateField# */{
1572 value_from: function (facetValue) {
1573 return instance.web.date_to_str(facetValue.get('value'));
1575 complete: function (needle) {
1578 var t = (this.attrs && this.attrs.type === 'datetime') ? 'datetime' : 'date';
1579 var v = instance.web.parse_value(needle, {'widget': t});
1580 if (t === 'datetime'){
1581 d = instance.web.str_to_datetime(v);
1584 d = instance.web.str_to_date(v);
1589 if (!d) { return $.when(null); }
1590 var date_string = instance.web.format_value(d, this.attrs);
1591 var label = _.str.sprintf(_.str.escapeHTML(
1592 _t("Search %(field)s at: %(value)s")), {
1593 field: '<em>' + _.escape(this.attrs.string) + '</em>',
1594 value: '<strong>' + date_string + '</strong>'});
1598 category: this.attrs.string,
1600 values: [{label: date_string, value: d}]
1606 * Implementation of the ``datetime`` openerp field type:
1608 * * Uses the same widget as the ``date`` field type (a simple date)
1610 * * Builds a slighly more complex, it's a datetime range (includes time)
1611 * spanning the whole day selected by the date widget
1614 * @extends instance.web.DateField
1616 instance.web.search.DateTimeField = instance.web.search.DateField.extend(/** @lends instance.web.search.DateTimeField# */{
1617 value_from: function (facetValue) {
1618 return instance.web.datetime_to_str(facetValue.get('value'));
1621 instance.web.search.ManyToOneField = instance.web.search.CharField.extend({
1622 default_operator: {},
1623 init: function (view_section, field, parent) {
1624 this._super(view_section, field, parent);
1625 this.model = new instance.web.Model(this.attrs.relation);
1628 complete: function (value) {
1629 if (_.isEmpty(value)) { return $.when(null); }
1630 var label = _.str.sprintf(_.str.escapeHTML(
1631 _t("Search %(field)s for: %(value)s")), {
1632 field: '<em>' + _.escape(this.attrs.string) + '</em>',
1633 value: '<strong>' + _.escape(value) + '</strong>'});
1637 category: this.attrs.string,
1639 values: [{label: value, value: value, operator: 'ilike'}]
1641 expand: this.expand.bind(this),
1645 expand: function (needle) {
1647 // FIXME: "concurrent" searches (multiple requests, mis-ordered responses)
1648 var context = instance.web.pyeval.eval(
1649 'contexts', [this.view.dataset.get_context()]);
1650 return this.model.call('name_search', [], {
1652 args: (typeof this.attrs.domain === 'string') ? [] : this.attrs.domain,
1655 }).then(function (results) {
1656 if (_.isEmpty(results)) { return null; }
1657 return _(results).map(function (result) {
1659 label: _.escape(result[1]),
1660 facet: facet_from(self, result)
1665 facet_for: function (value) {
1667 if (value instanceof Array) {
1668 if (value.length === 2 && _.isString(value[1])) {
1669 return $.when(facet_from(this, value));
1671 assert(value.length <= 1,
1672 _t("M2O search fields do not currently handle multiple default values"));
1673 // there are many cases of {search_default_$m2ofield: [id]}, need
1674 // to handle this as if it were a single value.
1677 return this.model.call('name_get', [value]).then(function (names) {
1678 if (_(names).isEmpty()) { return null; }
1679 return facet_from(self, names[0]);
1682 value_from: function (facetValue) {
1683 return facetValue.get('label');
1685 make_domain: function (name, operator, facetValue) {
1686 operator = facetValue.get('operator') || operator;
1689 case this.default_operator:
1690 return [[name, '=', facetValue.get('value')]];
1692 return [[name, 'ilike', facetValue.get('value')]];
1694 return [[name, 'child_of', facetValue.get('value')]];
1696 return this._super(name, operator, facetValue);
1698 get_context: function (facet) {
1699 var values = facet.values;
1700 if (_.isEmpty(this.attrs.context) && values.length === 1) {
1702 var v = values.at(0);
1703 if (v.get('operator') !== 'ilike') {
1704 c['default_' + this.attrs.name] = v.get('value');
1708 return this._super(facet);
1712 instance.web.search.CustomFilters = instance.web.search.Input.extend({
1713 template: 'SearchView.Custom',
1716 this.is_ready = $.Deferred();
1717 this._super.apply(this,arguments);
1719 start: function () {
1721 this.model = new instance.web.Model('ir.filters');
1725 .on('remove', function (facet) {
1726 if (!facet.get('is_custom_filter')) {
1729 self.clear_selection();
1731 .on('reset', this.proxy('clear_selection'));
1732 return this.model.call('get_filters', [this.view.model, this.get_action_id()])
1733 .then(this.proxy('set_filters'))
1734 .done(function () { self.is_ready.resolve(); })
1735 .fail(function () { self.is_ready.reject.apply(self.is_ready, arguments); });
1737 get_action_id: function(){
1738 var action = instance.client.action_manager.inner_action;
1739 if (action) return action.id;
1742 * Special implementation delaying defaults until CustomFilters is loaded
1744 facet_for_defaults: function () {
1745 return this.is_ready;
1748 * Generates a mapping key (in the filters and $filter mappings) for the
1749 * filter descriptor object provided (as returned by ``get_filters``).
1751 * The mapping key is guaranteed to be unique for a given (user_id, name)
1754 * @param {Object} filter
1755 * @param {String} filter.name
1756 * @param {Number|Pair<Number, String>} [filter.user_id]
1757 * @return {String} mapping key corresponding to the filter
1759 key_for: function (filter) {
1760 var user_id = filter.user_id,
1761 action_id = filter.action_id;
1762 var uid = (user_id instanceof Array) ? user_id[0] : user_id;
1763 var act_id = (action_id instanceof Array) ? action_id[0] : action_id;
1764 return _.str.sprintf('(%s)(%s)%s', uid, act_id, filter.name);
1767 * Generates a :js:class:`~instance.web.search.Facet` descriptor from a
1770 * @param {Object} filter
1771 * @param {String} filter.name
1772 * @param {Object} [filter.context]
1773 * @param {Array} [filter.domain]
1776 facet_for: function (filter) {
1778 category: _t("Custom Filter"),
1781 get_context: function () { return filter.context; },
1782 get_groupby: function () { return [filter.context]; },
1783 get_domain: function () { return filter.domain; }
1786 is_custom_filter: true,
1787 values: [{label: filter.name, value: null}]
1790 clear_selection: function () {
1791 this.$('span.badge').removeClass('badge');
1793 append_filter: function (filter) {
1795 var key = this.key_for(filter);
1796 var warning = _t("This filter is global and will be removed for everybody if you continue.");
1799 if (key in this.$filters) {
1800 $filter = this.$filters[key];
1803 this.filters[key] = filter;
1804 $filter = $('<li></li>')
1805 .appendTo(this.$('.oe_searchview_custom_list'))
1806 .toggleClass('oe_searchview_custom_default', filter.is_default)
1807 .append(this.$filters[key] = $('<span>').text(filter.name));
1809 this.$filters[key].addClass(filter.user_id ? 'oe_searchview_custom_private'
1810 : 'oe_searchview_custom_public')
1812 $('<a class="oe_searchview_custom_delete">x</a>')
1813 .click(function (e) {
1814 e.stopPropagation();
1815 if (!(filter.user_id || confirm(warning))) {
1818 self.model.call('unlink', [id]).done(function () {
1820 delete self.$filters[key];
1821 delete self.filters[key];
1822 if (_.isEmpty(self.filters)) {
1830 this.$filters[key].unbind('click').click(function () {
1831 self.toggle_filter(filter);
1835 toggle_filter: function (filter, preventSearch) {
1836 var current = this.view.query.find(function (facet) {
1837 return facet.get('_id') === filter.id;
1840 this.view.query.remove(current);
1841 this.$filters[this.key_for(filter)].removeClass('badge');
1844 this.view.query.reset([this.facet_for(filter)], {
1845 preventSearch: preventSearch || false});
1846 this.$filters[this.key_for(filter)].addClass('badge');
1848 set_filters: function (filters) {
1849 _(filters).map(_.bind(this.append_filter, this));
1850 if (!filters.length) {
1862 instance.web.search.SaveFilter = instance.web.search.Input.extend({
1863 template: 'SearchView.SaveFilter',
1865 init: function (parent, custom_filters) {
1866 this._super(parent);
1867 this.custom_filters = custom_filters;
1869 start: function () {
1871 this.model = new instance.web.Model('ir.filters');
1872 this.$el.on('submit', 'form', this.proxy('save_current'));
1873 this.$el.on('click', 'input[type=checkbox]', function() {
1874 $(this).siblings('input[type=checkbox]').prop('checked', false);
1876 this.$el.on('click', 'h4', function () {
1877 self.$el.toggleClass('oe_opened');
1880 save_current: function () {
1882 var $name = this.$('input:first');
1883 var private_filter = !this.$('#oe_searchview_custom_public').prop('checked');
1884 var set_as_default = this.$('#oe_searchview_custom_default').prop('checked');
1885 if (_.isEmpty($name.val())){
1886 this.do_warn(_t("Error"), _t("Filter name is required."));
1889 var search = this.view.build_search_data();
1890 instance.web.pyeval.eval_domains_and_contexts({
1891 domains: search.domains,
1892 contexts: search.contexts,
1893 group_by_seq: search.groupbys || []
1894 }).done(function (results) {
1895 if (!_.isEmpty(results.group_by)) {
1896 results.context.group_by = results.group_by;
1898 // Don't save user_context keys in the custom filter, otherwise end
1899 // up with e.g. wrong uid or lang stored *and used in subsequent
1901 var ctx = results.context;
1902 _(_.keys(instance.session.user_context)).each(function (key) {
1907 user_id: private_filter ? instance.session.uid : false,
1908 model_id: self.view.model,
1909 context: results.context,
1910 domain: results.domain,
1911 is_default: set_as_default,
1912 action_id: self.custom_filters.get_action_id()
1914 // FIXME: current context?
1915 return self.model.call('create_or_replace', [filter]).done(function (id) {
1917 if (self.custom_filters) {
1918 self.custom_filters.append_filter(filter);
1921 .removeClass('oe_opened')
1922 .find('form')[0].reset();
1929 instance.web.search.Filters = instance.web.search.Input.extend({
1930 template: 'SearchView.Filters',
1932 start: function () {
1934 var is_group = function (i) { return i instanceof instance.web.search.FilterGroup; };
1935 var visible_filters = _(this.drawer.controls).chain().reject(function (group) {
1936 return _(_(group.children).filter(is_group)).isEmpty()
1937 || group.modifiers.invisible;
1940 var groups = visible_filters.map(function (group) {
1941 var filters = _(group.children).filter(is_group);
1943 name: _.str.sprintf("<span class='oe_i'>%s</span> %s",
1944 group.icon, group.name),
1946 length: _(filters).chain().map(function (i) {
1947 return i.filters.length; }).sum().value()
1951 var $dl = $('<dl class="dl-horizontal">').appendTo(this.$el);
1953 var rendered_lines = _.map(groups, function (group) {
1954 $('<dt>').html(group.name).appendTo($dl);
1955 var $dd = $('<dd>').appendTo($dl);
1956 return $.when.apply(null, _(group.filters).invoke('appendTo', $dd));
1959 return $.when.apply(this, rendered_lines);
1963 instance.web.search.Advanced = instance.web.search.Input.extend({
1964 template: 'SearchView.advanced',
1966 start: function () {
1969 .on('keypress keydown keyup', function (e) { e.stopPropagation(); })
1970 .on('click', 'h4', function () {
1971 self.$el.toggleClass('oe_opened');
1972 }).on('click', 'button.oe_add_condition', function () {
1973 self.append_proposition();
1974 }).on('submit', 'form', function (e) {
1976 self.commit_search();
1980 new instance.web.Model(this.view.model).call('fields_get', {
1981 context: this.view.dataset.context
1982 }).done(function(data) {
1984 id: { string: 'ID', type: 'id', searchable: true }
1986 _.each(data, function(field_def, field_name) {
1987 if (field_def.selectable !== false && field_name != 'id') {
1988 self.fields[field_name] = field_def;
1991 })).done(function () {
1992 self.append_proposition();
1995 append_proposition: function () {
1997 return (new instance.web.search.ExtendedSearchProposition(this, this.fields))
1998 .appendTo(this.$('ul')).done(function () {
1999 self.$('button.oe_apply').prop('disabled', false);
2002 remove_proposition: function (prop) {
2003 // removing last proposition, disable apply button
2004 if (this.getChildren().length <= 1) {
2005 this.$('button.oe_apply').prop('disabled', true);
2009 commit_search: function () {
2010 // Get domain sections from all propositions
2011 var children = this.getChildren();
2012 var propositions = _.invoke(children, 'get_proposition');
2013 var domain = _(propositions).pluck('value');
2014 for (var i = domain.length; --i;) {
2015 domain.unshift('|');
2018 this.view.query.add({
2019 category: _t("Advanced"),
2020 values: propositions,
2022 get_context: function () { },
2023 get_domain: function () { return domain;},
2024 get_groupby: function () { }
2028 // remove all propositions
2029 _.invoke(children, 'destroy');
2030 // add new empty proposition
2031 this.append_proposition();
2032 // TODO: API on searchview
2033 this.view.$el.removeClass('oe_searchview_open_drawer');
2037 instance.web.search.ExtendedSearchProposition = instance.web.Widget.extend(/** @lends instance.web.search.ExtendedSearchProposition# */{
2038 template: 'SearchView.extended_search.proposition',
2040 'change .searchview_extended_prop_field': 'changed',
2041 'change .searchview_extended_prop_op': 'operator_changed',
2042 'click .searchview_extended_delete_prop': function (e) {
2043 e.stopPropagation();
2044 this.getParent().remove_proposition(this);
2048 * @constructs instance.web.search.ExtendedSearchProposition
2049 * @extends instance.web.Widget
2054 init: function (parent, fields) {
2055 this._super(parent);
2056 this.fields = _(fields).chain()
2057 .map(function(val, key) { return _.extend({}, val, {'name': key}); })
2058 .filter(function (field) { return !field.deprecated && field.searchable; })
2059 .sortBy(function(field) {return field.string;})
2061 this.attrs = {_: _, fields: this.fields, selected: null};
2064 start: function () {
2065 return this._super().done(this.proxy('changed'));
2067 changed: function() {
2068 var nval = this.$(".searchview_extended_prop_field").val();
2069 if(this.attrs.selected === null || this.attrs.selected === undefined || nval != this.attrs.selected.name) {
2070 this.select_field(_.detect(this.fields, function(x) {return x.name == nval;}));
2073 operator_changed: function (e) {
2074 var $value = this.$('.searchview_extended_prop_value');
2075 switch ($(e.target).val()) {
2085 * Selects the provided field object
2087 * @param field a field descriptor object (as returned by fields_get, augmented by the field name)
2089 select_field: function(field) {
2091 if(this.attrs.selected !== null && this.attrs.selected !== undefined) {
2092 this.value.destroy();
2094 this.$('.searchview_extended_prop_op').html('');
2096 this.attrs.selected = field;
2097 if(field === null || field === undefined) {
2101 var type = field.type;
2102 var Field = instance.web.search.custom_filters.get_object(type);
2104 Field = instance.web.search.custom_filters.get_object("char");
2106 this.value = new Field(this, field);
2107 _.each(this.value.operators, function(operator) {
2108 $('<option>', {value: operator.value})
2109 .text(String(operator.text))
2110 .appendTo(self.$('.searchview_extended_prop_op'));
2112 var $value_loc = this.$('.searchview_extended_prop_value').show().empty();
2113 this.value.appendTo($value_loc);
2116 get_proposition: function() {
2117 if (this.attrs.selected === null || this.attrs.selected === undefined)
2119 var field = this.attrs.selected;
2120 var op_select = this.$('.searchview_extended_prop_op')[0];
2121 var operator = op_select.options[op_select.selectedIndex];
2124 label: this.value.get_label(field, operator),
2125 value: this.value.get_domain(field, operator),
2130 instance.web.search.ExtendedSearchProposition.Field = instance.web.Widget.extend({
2131 init: function (parent, field) {
2132 this._super(parent);
2135 get_label: function (field, operator) {
2137 switch (operator.value) {
2138 case '∃': case '∄': format = _t('%(field)s %(operator)s'); break;
2139 default: format = _t('%(field)s %(operator)s "%(value)s"'); break;
2141 return this.format_label(format, field, operator);
2143 format_label: function (format, field, operator) {
2144 return _.str.sprintf(format, {
2145 field: field.string,
2146 // According to spec, HTMLOptionElement#label should return
2147 // HTMLOptionElement#text when not defined/empty, but it does
2148 // not in older Webkit (between Safari 5.1.5 and Chrome 17) and
2149 // Gecko (pre Firefox 7) browsers, so we need a manual fallback
2151 operator: operator.label || operator.text,
2155 get_domain: function (field, operator) {
2156 switch (operator.value) {
2157 case '∃': return this.make_domain(field.name, '!=', false);
2158 case '∄': return this.make_domain(field.name, '=', false);
2159 default: return this.make_domain(
2160 field.name, operator.value, this.get_value());
2163 make_domain: function (field, operator, value) {
2164 return [field, operator, value];
2167 * Returns a human-readable version of the value, in case the "logical"
2168 * and the "semantic" values of a field differ (as for selection fields,
2171 * The default implementation simply returns the value itself.
2173 * @return {String} human-readable version of the value
2175 toString: function () {
2176 return this.get_value();
2179 instance.web.search.ExtendedSearchProposition.Char = instance.web.search.ExtendedSearchProposition.Field.extend({
2180 template: 'SearchView.extended_search.proposition.char',
2182 {value: "ilike", text: _lt("contains")},
2183 {value: "not ilike", text: _lt("doesn't contain")},
2184 {value: "=", text: _lt("is equal to")},
2185 {value: "!=", text: _lt("is not equal to")},
2186 {value: "∃", text: _lt("is set")},
2187 {value: "∄", text: _lt("is not set")}
2189 get_value: function() {
2190 return this.$el.val();
2193 instance.web.search.ExtendedSearchProposition.DateTime = instance.web.search.ExtendedSearchProposition.Field.extend({
2194 template: 'SearchView.extended_search.proposition.empty',
2196 {value: "=", text: _lt("is equal to")},
2197 {value: "!=", text: _lt("is not equal to")},
2198 {value: ">", text: _lt("greater than")},
2199 {value: "<", text: _lt("less than")},
2200 {value: ">=", text: _lt("greater or equal than")},
2201 {value: "<=", text: _lt("less or equal than")},
2202 {value: "∃", text: _lt("is set")},
2203 {value: "∄", text: _lt("is not set")}
2206 * Date widgets live in view_form which is not yet loaded when this is
2209 widget: function () { return instance.web.DateTimeWidget; },
2210 get_value: function() {
2211 return this.datewidget.get_value();
2213 toString: function () {
2214 return instance.web.format_value(this.get_value(), { type:"datetime" });
2217 var ready = this._super();
2218 this.datewidget = new (this.widget())(this);
2219 this.datewidget.appendTo(this.$el);
2223 instance.web.search.ExtendedSearchProposition.Date = instance.web.search.ExtendedSearchProposition.DateTime.extend({
2224 widget: function () { return instance.web.DateWidget; },
2225 toString: function () {
2226 return instance.web.format_value(this.get_value(), { type:"date" });
2229 instance.web.search.ExtendedSearchProposition.Integer = instance.web.search.ExtendedSearchProposition.Field.extend({
2230 template: 'SearchView.extended_search.proposition.integer',
2232 {value: "=", text: _lt("is equal to")},
2233 {value: "!=", text: _lt("is not equal to")},
2234 {value: ">", text: _lt("greater than")},
2235 {value: "<", text: _lt("less than")},
2236 {value: ">=", text: _lt("greater or equal than")},
2237 {value: "<=", text: _lt("less or equal than")},
2238 {value: "∃", text: _lt("is set")},
2239 {value: "∄", text: _lt("is not set")}
2241 toString: function () {
2242 return this.$el.val();
2244 get_value: function() {
2246 var val =this.$el.val();
2247 return instance.web.parse_value(val === "" ? 0 : val, {'widget': 'integer'});
2253 instance.web.search.ExtendedSearchProposition.Id = instance.web.search.ExtendedSearchProposition.Integer.extend({
2254 operators: [{value: "=", text: _lt("is")}]
2256 instance.web.search.ExtendedSearchProposition.Float = instance.web.search.ExtendedSearchProposition.Field.extend({
2257 template: 'SearchView.extended_search.proposition.float',
2259 {value: "=", text: _lt("is equal to")},
2260 {value: "!=", text: _lt("is not equal to")},
2261 {value: ">", text: _lt("greater than")},
2262 {value: "<", text: _lt("less than")},
2263 {value: ">=", text: _lt("greater or equal than")},
2264 {value: "<=", text: _lt("less or equal than")},
2265 {value: "∃", text: _lt("is set")},
2266 {value: "∄", text: _lt("is not set")}
2268 toString: function () {
2269 return this.$el.val();
2271 get_value: function() {
2273 var val =this.$el.val();
2274 return instance.web.parse_value(val === "" ? 0.0 : val, {'widget': 'float'});
2280 instance.web.search.ExtendedSearchProposition.Selection = instance.web.search.ExtendedSearchProposition.Field.extend({
2281 template: 'SearchView.extended_search.proposition.selection',
2283 {value: "=", text: _lt("is")},
2284 {value: "!=", text: _lt("is not")},
2285 {value: "∃", text: _lt("is set")},
2286 {value: "∄", text: _lt("is not set")}
2288 toString: function () {
2289 var select = this.$el[0];
2290 var option = select.options[select.selectedIndex];
2291 return option.label || option.text;
2293 get_value: function() {
2294 return this.$el.val();
2297 instance.web.search.ExtendedSearchProposition.Boolean = instance.web.search.ExtendedSearchProposition.Field.extend({
2298 template: 'SearchView.extended_search.proposition.empty',
2300 {value: "=", text: _lt("is true")},
2301 {value: "!=", text: _lt("is false")}
2303 get_label: function (field, operator) {
2304 return this.format_label(
2305 _t('%(field)s %(operator)s'), field, operator);
2307 get_value: function() {
2312 instance.web.search.custom_filters = new instance.web.Registry({
2313 'char': 'instance.web.search.ExtendedSearchProposition.Char',
2314 'text': 'instance.web.search.ExtendedSearchProposition.Char',
2315 'one2many': 'instance.web.search.ExtendedSearchProposition.Char',
2316 'many2one': 'instance.web.search.ExtendedSearchProposition.Char',
2317 'many2many': 'instance.web.search.ExtendedSearchProposition.Char',
2319 'datetime': 'instance.web.search.ExtendedSearchProposition.DateTime',
2320 'date': 'instance.web.search.ExtendedSearchProposition.Date',
2321 'integer': 'instance.web.search.ExtendedSearchProposition.Integer',
2322 'float': 'instance.web.search.ExtendedSearchProposition.Float',
2323 'boolean': 'instance.web.search.ExtendedSearchProposition.Boolean',
2324 'selection': 'instance.web.search.ExtendedSearchProposition.Selection',
2326 'id': 'instance.web.search.ExtendedSearchProposition.Id'
2329 instance.web.search.AutoComplete = instance.web.Widget.extend({
2330 template: "SearchView.autocomplete",
2332 // Parameters for autocomplete constructor:
2334 // parent: this is used to detect keyboard events
2336 // options.source: function ({term:query}, callback). This function will be called to
2337 // obtain the search results corresponding to the query string. It is assumed that
2338 // options.source will call callback with the results.
2339 // options.delay: delay in millisecond before calling source. Useful if you don't want
2340 // to make too many rpc calls
2341 // options.select: function (ev, {item: {facet:facet}}). Autocomplete widget will call
2342 // that function when a selection is made by the user
2343 // options.get_search_string: function (). This function will be called by autocomplete
2344 // to obtain the current search string.
2345 init: function (parent, options) {
2346 this._super(parent);
2347 this.$input = parent.$el;
2348 this.source = options.source;
2349 this.delay = options.delay;
2350 this.select = options.select,
2351 this.get_search_string = options.get_search_string;
2352 this.width = options.width || 400;
2354 this.current_result = null;
2356 this.searching = true;
2357 this.search_string = null;
2358 this.current_search = null;
2360 start: function () {
2362 this.$el.width(this.width);
2363 this.$input.on('keyup', function (ev) {
2364 if (ev.which === $.ui.keyCode.RIGHT) {
2365 self.searching = true;
2366 ev.preventDefault();
2369 // ENTER is caugth at KeyUp rather than KeyDown to avoid firing
2370 // before all regular keystrokes have been processed
2371 if (ev.which === $.ui.keyCode.ENTER) {
2372 if (self.current_result && self.get_search_string().length) {
2373 self.select_item(ev);
2377 if (!self.searching) {
2378 self.searching = true;
2381 self.search_string = self.get_search_string();
2382 if (self.search_string.length) {
2383 var search_string = self.search_string;
2384 setTimeout(function () { self.initiate_search(search_string);}, self.delay);
2389 this.$input.on('keydown', function (ev) {
2391 // TAB and direction keys are handled at KeyDown because KeyUp
2392 // is not guaranteed to fire.
2393 // See e.g. https://github.com/aef-/jquery.masterblaster/issues/13
2394 case $.ui.keyCode.TAB:
2395 if (self.current_result && self.get_search_string().length) {
2396 self.select_item(ev);
2399 case $.ui.keyCode.DOWN:
2401 self.searching = false;
2402 ev.preventDefault();
2404 case $.ui.keyCode.UP:
2406 self.searching = false;
2407 ev.preventDefault();
2409 case $.ui.keyCode.RIGHT:
2410 self.searching = false;
2411 var current = self.current_result
2412 if (current && current.expand && !current.expanded) {
2414 self.searching = true;
2416 ev.preventDefault();
2418 case $.ui.keyCode.ESCAPE:
2420 self.searching = false;
2425 initiate_search: function (query) {
2426 if (query === this.search_string && query !== this.current_search) {
2430 search: function (query) {
2432 this.current_search = query;
2433 this.source({term:query}, function (results) {
2434 if (results.length) {
2435 self.render_search_results(results);
2436 self.focus_element(self.$('li:first-child'));
2442 render_search_results: function (results) {
2444 var $list = this.$('ul');
2446 var render_separator = false;
2447 results.forEach(function (result) {
2448 if (result.is_separator) {
2449 if (render_separator)
2450 $list.append($('<li>').addClass('oe-separator'));
2451 render_separator = false;
2453 var $item = self.make_list_item(result).appendTo($list);
2455 render_separator = true;
2460 make_list_item: function (result) {
2463 .hover(function (ev) {self.focus_element($li);})
2464 .mousedown(function (ev) {
2465 if (ev.button === 0) { // left button
2466 self.select(ev, {item: {facet: result.facet}});
2469 ev.preventDefault();
2472 .data('result', result);
2473 if (result.expand) {
2474 var $expand = $('<span class="oe-expand">').text('▶').appendTo($li);
2475 $expand.mousedown(function (ev) {
2476 ev.preventDefault();
2477 ev.stopPropagation();
2478 if (result.expanded)
2483 result.expanded = false;
2485 if (result.indent) $li.addClass('oe-indent');
2486 $li.append($('<span>').html(result.label));
2489 expand: function () {
2491 this.current_result.expand(this.get_search_string()).then(function (results) {
2492 (results || [{label: '(no result)'}]).reverse().forEach(function (result) {
2493 result.indent = true;
2494 var $li = self.make_list_item(result);
2495 self.current_result.$el.after($li);
2497 self.current_result.expanded = true;
2498 self.current_result.$el.find('span.oe-expand').html('▼');
2502 var $next = this.current_result.$el.next();
2503 while ($next.hasClass('oe-indent')) {
2505 $next = this.current_result.$el.next();
2507 this.current_result.expanded = false;
2508 this.current_result.$el.find('span.oe-expand').html('▶');
2510 focus_element: function ($li) {
2511 this.$('li').removeClass('oe-selection-focus');
2512 $li.addClass('oe-selection-focus');
2513 this.current_result = $li.data('result');
2515 select_item: function (ev) {
2516 if (this.current_result.facet) {
2517 this.select(ev, {item: {facet: this.current_result.facet}});
2524 close: function () {
2525 this.current_search = null;
2526 this.search_string = null;
2527 this.searching = true;
2530 move: function (direction) {
2532 if (direction === 'down') {
2533 $next = this.$('li.oe-selection-focus').nextAll(':not(.oe-separator)').first();
2534 if (!$next.length) $next = this.$('li:first-child');
2536 $next = this.$('li.oe-selection-focus').prevAll(':not(.oe-separator)').first();
2537 if (!$next.length) $next = this.$('li:not(.oe-separator)').last();
2539 this.focus_element($next);
2541 is_expandable: function () {
2542 return !!this.$('.oe-selection-focus .oe-expand').length;
2548 // vim:et fdc=0 fdl=0 foldnestmax=3 fdm=syntax: