Skip to content

Commit e586cd8

Browse files
committed
[REF] Refactore the update_mru_list method to always put the selected item in the top of the list and add some important comments
1 parent a1cc499 commit e586cd8

File tree

1 file changed

+18
-4
lines changed
  • web_m2x_options/static/src/js

1 file changed

+18
-4
lines changed

web_m2x_options/static/src/js/form.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -311,28 +311,42 @@ odoo.define('web_m2x_options.web_m2x_options', function (require) {
311311
var self = this,
312312
mru_option = 'web_m2x_options_mru';
313313
var key = self.compute_mru_key();
314-
315314
// check if the localstorage has some items for the current model
316315
if (localStorage.getItem(mru_option)) {
317316
var restore_mru_list = JSON.parse(localStorage.getItem(mru_option));
318317
if (restore_mru_list[key]) {
319318
var queue = restore_mru_list[key];
319+
// if the element doesn't exist in the stack
320320
if (queue.indexOf(self.get_value(true)) < 0 && self.get_value(true)){
321321
if (queue.length < 5) {
322-
queue.push(self.get_value(true));
322+
// add the new element at the beginning
323+
queue.unshift(self.get_value(true));
323324
}else {
324-
queue.shift();
325-
queue.push(self.get_value(true));
325+
// remove the last element
326+
queue.pop();
327+
// add the new element at the beginning
328+
queue.unshift(self.get_value(true));
326329
}
327330
restore_mru_list[key] = queue;
331+
}else{
332+
// if the element already exist in the stack
333+
if (queue.indexOf(self.get_value(true)) >= 0 && self.get_value(true)){
334+
var index = queue.indexOf(self.get_value(true));
335+
// remove the element from the list
336+
queue.splice(index, 1);
337+
// and put it back at the beginning
338+
queue.unshift(self.get_value(true));
339+
}
328340
}
329341
}else{
342+
// if the element is the first one
330343
if (self.get_value(true)){
331344
restore_mru_list[key] = [self.get_value(true)];
332345
}
333346
}
334347
localStorage.setItem(mru_option, JSON.stringify(restore_mru_list));
335348
}else {
349+
// first time to create an entry in the localstorage
336350
if (self.get_value(true)){
337351
var values = {}
338352
values[key] = [self.get_value(true)]

0 commit comments

Comments
 (0)