-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ADD] web_m2x_options: display last selected records at first when se…
…lecting a value in a many2one before the user types something
- Loading branch information
Showing
7 changed files
with
211 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/** @odoo-module **/ | ||
import {session} from "@web/session"; | ||
|
||
const LOCAL_STORAGE_NAME = "web_m2x_options_mru"; | ||
|
||
export function getMruMaxLength() { | ||
return ( | ||
parseInt(session.web_m2x_options["web_m2x_options.search_mru_max_length"]) || 5 | ||
); | ||
} | ||
|
||
export function isMruGlobalOptionEnabled() { | ||
return session.web_m2x_options["web_m2x_options.search_mru"] === "True"; | ||
} | ||
|
||
export function getMruKey(modelName, fieldName) { | ||
return session.db + "/" + modelName + "/" + fieldName; | ||
} | ||
|
||
export function getMruStorage() { | ||
let data = localStorage.getItem(LOCAL_STORAGE_NAME); | ||
if (!data) { | ||
return {}; | ||
} | ||
data = JSON.parse(data); | ||
return data; | ||
} | ||
|
||
export function getMruValue(mruKey) { | ||
const data = getMruStorage(); | ||
return mruKey in data ? data[mruKey] : []; | ||
} | ||
|
||
export function setMruValue(mruKey, value) { | ||
const data = getMruStorage(); | ||
data[mruKey] = value; | ||
localStorage.setItem(LOCAL_STORAGE_NAME, JSON.stringify(data)); | ||
} | ||
|
||
export function updateMruIds(mruKey, recordId) { | ||
if (!recordId) { | ||
return; | ||
} | ||
const cachedIds = getMruValue(mruKey); | ||
const currentIndex = cachedIds.indexOf(recordId); | ||
if (currentIndex !== -1) { | ||
cachedIds.splice(currentIndex, 1); | ||
} | ||
cachedIds.unshift(recordId); | ||
const maxLength = getMruMaxLength(); | ||
if (cachedIds.length > maxLength) { | ||
cachedIds.splice(maxLength, cachedIds.length); | ||
} | ||
setMruValue(mruKey, cachedIds); | ||
} | ||
|
||
export function updateMruLocalStorageValues(modelName, values) { | ||
Object.keys(values).forEach(function (key) { | ||
const mruKey = getMruKey(modelName, key); | ||
updateMruIds(mruKey, values[key]); | ||
}); | ||
} |