[FIX] web: correctly init searchview when no action_id
[odoo/odoo.git] / addons / web / static / src / js / search.js
index 8641290..312432d 100644 (file)
@@ -387,7 +387,8 @@ instance.web.SearchView = instance.web.Widget.extend(/** @lends instance.web.Sea
 
         this.filter_menu = undefined;
         this.groupby_menu = undefined;
-        this.favorite_menu = undefined
+        this.favorite_menu = undefined;
+        this.action_id = this.options && this.options.action && this.options.action.id;
     },    
     start: function() {
         if (this.headless) {
@@ -414,6 +415,7 @@ instance.web.SearchView = instance.web.Widget.extend(/** @lends instance.web.Sea
     view_loaded: function (r) {
         var self = this;
         this.fields_view_get = r;
+        this.view_id = this.view_id || r.view_id;
         this.prepare_search_inputs();
         if (this.$buttons) {
 
@@ -423,7 +425,7 @@ instance.web.SearchView = instance.web.Widget.extend(/** @lends instance.web.Sea
 
             this.groupby_menu = new my.GroupByMenu(this, this.groupbys, fields_def);
             this.filter_menu = new my.FilterMenu(this, this.filters, fields_def);
-            this.favorite_menu = new my.FavoriteMenu(this, this.query, this.dataset.model);
+            this.favorite_menu = new my.FavoriteMenu(this, this.query, this.dataset.model, this.action_id);
 
             this.filter_menu.appendTo(this.$buttons);
             this.groupby_menu.appendTo(this.$buttons);
@@ -1272,8 +1274,9 @@ instance.web.search.DateField = instance.web.search.Field.extend(/** @lends inst
         return instance.web.date_to_str(facetValue.get('value'));
     },
     complete: function (needle) {
-        var d = Date.parse(needle);
-        if (!d) { return $.when(null); }
+        var m = moment(needle);
+        if (!m.isValid()) { return $.when(null); }
+        var d = m.toDate();
         var date_string = instance.web.format_value(d, this.attrs);
         var label = _.str.sprintf(_.str.escapeHTML(
             _t("Search %(field)s at: %(value)s")), {
@@ -1387,7 +1390,10 @@ instance.web.search.ManyToOneField = instance.web.search.CharField.extend({
         var values = facet.values;
         if (_.isEmpty(this.attrs.context) && values.length === 1) {
             var c = {};
-            c['default_' + this.attrs.name] = values.at(0).get('value');
+            var v = values.at(0);
+            if (v.get('operator') !== 'ilike') {
+                c['default_' + this.attrs.name] = v.get('value');
+            }
             return c;
         }
         return this._super(facet);
@@ -1441,6 +1447,10 @@ instance.web.search.FilterMenu = instance.web.Widget.extend({
             prop.$el.hide();
         });
     },
+    update_max_height: function () {
+        var max_height = $(window).height() - this.$menu[0].getBoundingClientRect().top - 10;
+        this.$menu.css('max-height', max_height);
+    },
     toggle_custom_filter_menu: function (is_open) {
         this.$add_filter
             .toggleClass('closed-menu', !is_open)
@@ -1450,6 +1460,7 @@ instance.web.search.FilterMenu = instance.web.Widget.extend({
             this.append_proposition();
         }
         this.$('.oe-filter-condition').toggle(is_open);
+        this.update_max_height();
     },
     append_proposition: function () {
         var self = this;
@@ -1458,6 +1469,7 @@ instance.web.search.FilterMenu = instance.web.Widget.extend({
             self.propositions.push(prop);
             prop.insertBefore(self.$add_filter_menu);
             self.$apply_filter.prop('disabled', false);
+            self.update_max_height();
             return prop;
         });
     },
@@ -1574,7 +1586,7 @@ instance.web.search.FavoriteMenu = instance.web.Widget.extend({
             this.close_menus();
         },
     },
-    init: function (parent, query, target_model) {
+    init: function (parent, query, target_model, action_id) {
         this._super.apply(this,arguments);
         this.searchview = parent;
         this.query = query;
@@ -1582,8 +1594,7 @@ instance.web.search.FavoriteMenu = instance.web.Widget.extend({
         this.model = new instance.web.Model('ir.filters');
         this.filters = {};
         this.$filters = {};
-        var action = instance.client.action_manager.inner_action;
-        this.action_id = action && action.id;
+        this.action_id = action_id;
     },
     start: function () {
         var self = this;
@@ -1604,6 +1615,10 @@ instance.web.search.FavoriteMenu = instance.web.Widget.extend({
                 }
             })
             .on('reset', this.proxy('clear_selection'));
+        if (!this.action_id) {
+            this.prepare_dropdown_menu([]);
+            return $.when();
+        }
         return this.model.call('get_filters', [this.target_model, this.action_id])
             .done(this.proxy('prepare_dropdown_menu'));
     },
@@ -2117,6 +2132,14 @@ instance.web.search.AutoComplete = instance.web.Widget.extend({
                 ev.preventDefault();
                 return;
             }
+            // ENTER is caugth at KeyUp rather than KeyDown to avoid firing
+            // before all regular keystrokes have been processed
+            if (ev.which === $.ui.keyCode.ENTER) {
+                if (self.current_result && self.get_search_string().length) {
+                    self.select_item(ev);
+                }
+                return;
+            }
             if (!self.searching) {
                 self.searching = true;
                 return;
@@ -2131,8 +2154,10 @@ instance.web.search.AutoComplete = instance.web.Widget.extend({
         });
         this.$input.on('keydown', function (ev) {
             switch (ev.which) {
+                // TAB and direction keys are handled at KeyDown because KeyUp
+                // is not guaranteed to fire.
+                // See e.g. https://github.com/aef-/jquery.masterblaster/issues/13
                 case $.ui.keyCode.TAB:
-                case $.ui.keyCode.ENTER:
                     if (self.current_result && self.get_search_string().length) {
                         self.select_item(ev);
                     }