Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[13.0][MIG] web_m2x_options: mru #14

Open
wants to merge 6 commits into
base: 13.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions web_m2x_options/models/ir_config_parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ def get_web_m2x_options(self):
"web_m2x_options.limit",
"web_m2x_options.search_more",
"web_m2x_options.m2o_dialog",
"web_m2x_options.search_mru",
]
return self.sudo().search_read([["key", "in", opts]], ["key", "value"])
9 changes: 9 additions & 0 deletions web_m2x_options/readme/USAGE.rst
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ in the field's options dict

Deactivates the color picker on many2many_tags buttons to do nothing (ignored if open is set)

``search_mru`` *boolean* (Default: ``False``)

Display the MRU list stored in the localstorage before the user start typing.

ir.config_parameter options
~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand All @@ -69,13 +73,18 @@ If you disable one option, you can enable it for particular field by setting "cr

Whether the field should always show "Search more..." entry or not.

``web_m2x_options.search_mru`` *boolean* (Default: default value is ``False``)

Display the MRU list stored in the localstorage before the user start typing.

To add these parameters go to Configuration -> Technical -> Parameters -> System Parameters and add new parameters like:

- web_m2x_options.create: False
- web_m2x_options.create_edit: False
- web_m2x_options.m2o_dialog: False
- web_m2x_options.limit: 10
- web_m2x_options.search_more: True
- web_m2x_options.search_mru: False


Example
Expand Down
115 changes: 113 additions & 2 deletions web_m2x_options/static/src/js/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,22 @@ odoo.define("web_m2x_options.web_m2x_options", function(require) {
}
},

compute_mru_key: function() {
return odoo.session_info.db + "/" + this.model + "/" + this.name;
},

get_mru_ids: function() {
const mru_option = "web_m2x_options_mru",
restore_mru_ids = JSON.parse(localStorage.getItem(mru_option)),
key = this.compute_mru_key();
if (restore_mru_ids) {
if (!_.isUndefined(restore_mru_ids[key])) {
return restore_mru_ids[key];
}
}
return [];
},

_search: function(search_val) {
var self = this;
if (search_val === undefined) {
Expand Down Expand Up @@ -163,6 +179,27 @@ odoo.define("web_m2x_options.web_m2x_options", function(require) {
domain.push(["id", "not in", blacklisted_ids]);
}

var can_search_mru =
self.nodeOptions &&
self.is_option_set(self.nodeOptions.search_mru),
search_mru_undef = _.isUndefined(self.nodeOptions.search_mru),
search_mru = self.is_option_set(
self.ir_options["web_m2x_options.search_mru"]
);

var mru_ids = [];
var in_search_mru = false;
if (
search_val == "" &&
(can_search_mru || (search_mru_undef && search_mru))
) {
mru_ids = self.get_mru_ids();
if (mru_ids.length > 0) {
domain.push(["id", "in", mru_ids]);
in_search_mru = true;
}
}

self._rpc({
model: self.field.relation,
method: "name_search",
Expand All @@ -177,14 +214,26 @@ odoo.define("web_m2x_options.web_m2x_options", function(require) {
// Possible selections for the m2o
var values = _.map(result, x => {
x[1] = self._getDisplayName(x[1]);
return {
const val = {
label:
_.str.escapeHTML(x[1].trim()) || data.noDisplayContent,
value: x[1],
name: x[1],
id: x[0],
};
if (in_search_mru) {
val.classname = "web_m2x_dropdown_option_mru";
}
return val;
});
// If we are in a mru search, reorder the result list in the
// same order as the one stored to keep the saved preference
// order (The most recent ones first)
if (in_search_mru) {
values = _(values).sortBy(function(item) {
return mru_ids.indexOf(item.id);
});
}

// Search result value colors
if (self.colors && self.field_color) {
Expand Down Expand Up @@ -235,7 +284,7 @@ odoo.define("web_m2x_options.web_m2x_options", function(require) {
self.ir_options["web_m2x_options.search_more"]
);

if (values.length > self.limit) {
if (values.length > self.limit || in_search_mru) {
values = values.slice(0, self.limit);
if (can_search_more || search_more_undef || search_more) {
values.push({
Expand Down Expand Up @@ -379,6 +428,68 @@ odoo.define("web_m2x_options.web_m2x_options", function(require) {
this.orderer.add(def);
return def;
},

update_mru_ids: function() {
var mru_option = "web_m2x_options_mru";
var key = this.compute_mru_key();
const field_val = _.isUndefined(this.value.data) ? false : this.value.data.id;
// Check if the localstorage has some items for the current model
if (localStorage.getItem(mru_option)) {
var restore_mru_ids = JSON.parse(localStorage.getItem(mru_option));
if (restore_mru_ids[key]) {
var queue = restore_mru_ids[key];
// If the element doesn't exist in the stack
if (queue.indexOf(field_val) < 0 && field_val) {
if (queue.length < 5) {
// Add the new element at the beginning
queue.unshift(field_val);
} else {
// Remove the last element
queue.pop();
// Add the new element at the beginning
queue.unshift(field_val);
}
restore_mru_ids[key] = queue;
} else if (queue.indexOf(field_val) >= 0 && field_val) {
// If the element already exist in the stack
var index = queue.indexOf(field_val);
// Remove the element from the list
queue.splice(index, 1);
// And put it back at the beginning
queue.unshift(field_val);
}
} else if (field_val && key) {
// If the element is the first one and the key is well computed
restore_mru_ids[key] = [field_val];
}
localStorage.setItem(mru_option, JSON.stringify(restore_mru_ids));
} else if (field_val && key) {
// First time to create an entry in the localstorage if the key is well computed
const values = {};
values[key] = [field_val];
localStorage.setItem(mru_option, JSON.stringify(values));
}
},

commitChanges: function() {
// If the field value has changed and has favorites option
const has_changed =
!_.isUndefined(this.lastChangeEvent) &&
this.lastChangeEvent.name === "field_changed";
if (this.isDirty || has_changed) {
const can_search_mru =
this.nodeOptions &&
this.is_option_set(this.nodeOptions.search_mru),
search_mru_undef = _.isUndefined(this.nodeOptions.search_mru),
search_mru = this.is_option_set(
this.ir_options["web_m2x_options.search_mru"]
);

if (can_search_mru || (search_mru_undef && search_mru)) {
this.update_mru_ids();
}
}
},
});

FieldMany2ManyTags.include({
Expand Down
3 changes: 3 additions & 0 deletions web_m2x_options/static/src/scss/web_m2x_options.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.web_m2x_dropdown_option_mru {
font-style: italic;
}
7 changes: 7 additions & 0 deletions web_m2x_options/static/src/xml/base.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,11 @@
</attribute>
</t>
</t>
<t t-extend="FieldMany2ManyTag">
<t t-jquery="span.badge" t-operation="attributes">
<attribute name="t-add-data-id">
el[0]
</attribute>
</t>
</t>
</templates>