|
1 |
| -openerp.web_search_with_and = function (instance) { |
| 1 | +odoo.define('web_search_with_and', function (require) { |
| 2 | + "use strict"; |
2 | 3 |
|
3 |
| - instance.web.SearchView = instance.web.SearchView.extend({ |
| 4 | + var SearchView = require('web.SearchView'); |
| 5 | + var Backbone = window.Backbone; |
| 6 | + |
| 7 | + SearchView.include({ |
| 8 | + // Override the base method to detect a 'shift' event |
4 | 9 | select_completion: function (e, ui) {
|
5 |
| - var self = this; |
6 |
| - if (e.shiftKey) { |
| 10 | + if (e.shiftKey |
| 11 | + && ui.item.facet.values |
| 12 | + && ui.item.facet.values.length |
| 13 | + && String(ui.item.facet.values[0].value).trim() !== "") { |
| 14 | + // In case of an 'AND' search a new facet is added regarding of the previous facets |
7 | 15 | e.preventDefault();
|
8 | 16 |
|
9 |
| - var input_index = _(this.input_subviews).indexOf( |
10 |
| - this.subviewForRoot( |
11 |
| - this.$('div.oe_searchview_input:focus')[0])); |
12 |
| - this.query.add(ui.item.facet, {at: input_index / 2, shiftKey: true}); |
| 17 | + this.query.add(ui.item.facet, {shiftKey: true}); |
13 | 18 | } else {
|
14 |
| - this._super(e, ui); |
| 19 | + |
| 20 | + return this._super.apply(this, arguments); |
15 | 21 | }
|
16 |
| - }, |
| 22 | + } |
17 | 23 | });
|
18 | 24 |
|
19 |
| - instance.web.search.SearchQuery = instance.web.search.SearchQuery.extend({ |
| 25 | + SearchView.SearchQuery.prototype = SearchView.SearchQuery.extend({ |
| 26 | + // Override the odoo method to (conditionally) add a search facet even if a existing |
| 27 | + // facet for the same field/category already exists. |
| 28 | + // The prototype is used to override the 'add' function in order to execute the |
| 29 | + // following code before the Odoo native override (trick) |
20 | 30 | add: function (values, options) {
|
21 |
| - |
22 | 31 | options = options || {};
|
| 32 | + if (options.shiftKey) { |
23 | 33 |
|
24 |
| - if (!values) { |
25 |
| - values = []; |
26 |
| - } else if (!(values instanceof Array)) { |
27 |
| - values = [values]; |
28 |
| - } |
| 34 | + if (!values) { |
| 35 | + values = []; |
| 36 | + } |
| 37 | + else if (!(values instanceof Array)) { |
| 38 | + values = [values]; |
| 39 | + } |
29 | 40 |
|
30 |
| - if (options.shiftKey) { |
31 | 41 | delete options.shiftKey;
|
32 | 42 | _(values).each(function (value) {
|
33 | 43 | var model = this._prepareModel(value, options);
|
34 | 44 | Backbone.Collection.prototype.add.call(this, model, options);
|
35 | 45 | }, this);
|
| 46 | + |
36 | 47 | return this;
|
37 | 48 | }
|
38 | 49 | else {
|
39 |
| - return this.constructor.__super__.add.apply(this, arguments); |
| 50 | + return this.constructor.__super__.add.call(this, values, options); |
40 | 51 | }
|
41 |
| - }, |
42 |
| - }); |
43 |
| -}; |
| 52 | + } |
| 53 | + }).prototype; |
| 54 | + |
| 55 | +}); |
0 commit comments