| 1 |
import { |
| 2 |
deserializeDateTime, |
| 3 |
serializeDateTime, |
| 4 |
deserializeDate, |
| 5 |
serializeDate, |
| 6 |
} from "@web/core/l10n/dates"; |
| 7 |
export const RELATION_TYPES = new Set(["many2many", "many2one", "one2many"]); |
| 8 |
export const DATE_TIME_TYPE = new Set(["date", "datetime"]); |
| 9 |
export const X2MANY_TYPES = new Set(["many2many", "one2many"]); |
| 10 |
export const PARENT_X2MANY_TYPES = new Set(["many2one", "many2many"]); |
| 11 |
export const RAW_SYMBOL = Symbol("raw"); |
| 12 |
export const STORE_SYMBOL = Symbol("store"); |
| 13 |
export const SERIALIZED_UI_STATE_PROP = "JSONuiState"; |
| 14 |
export const BACKREF_PREFIX = "<-"; |
| 15 |
|
| 16 |
export function getBackRef(model, fieldName) { |
| 17 |
return `${BACKREF_PREFIX}${model}.${fieldName}`; |
| 18 |
} |
| 19 |
export function clone(obj) { |
| 20 |
return JSON.parse( |
| 21 |
JSON.stringify(obj, (_, value) => (value instanceof Set ? [...value] : value)) |
| 22 |
); |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Returns a new object with the same keys, but with values transformed by the provided function. |
| 27 |
* |
| 28 |
* @param {Object} obj - The input object whose values will be transformed. |
| 29 |
* @param {Function} fn - A function that takes (key, value, index) and returns a new value. |
| 30 |
* @returns {Object} A new object with the same keys but transformed values. |
| 31 |
* |
| 32 |
* @example |
| 33 |
* const obj = { a: 1, b: 2, c: 3 }; |
| 34 |
* const doubled = mapObj(obj, (key, value) => value * 2); |
| 35 |
* console.log(doubled); // { a: 2, b: 4, c: 6 } |
| 36 |
*/ |
| 37 |
export function mapObj(obj, fn) { |
| 38 |
return Object.fromEntries(Object.entries(obj).map(([k, v], i) => [k, fn(k, v, i)])); |
| 39 |
} |
| 40 |
|
| 41 |
export function convertRawToDateTime(model, value, prop) { |
| 42 |
if (!value) { |
| 43 |
return undefined; |
| 44 |
} |
| 45 |
const datetime = deserializeDateTime(value); |
| 46 |
if (!datetime.isValid) { |
| 47 |
throw new Error(`Invalid date: ${value} for model ${model.model} in field ${prop}`); |
| 48 |
} |
| 49 |
return datetime; |
| 50 |
} |
| 51 |
|
| 52 |
export function convertDateTimeToRaw(value) { |
| 53 |
if (!value) { |
| 54 |
return undefined; |
| 55 |
} |
| 56 |
// Verify if is already a valid date object |
| 57 |
if (typeof value !== "string") { |
| 58 |
return serializeDateTime(value); |
| 59 |
} |
| 60 |
return value; |
| 61 |
} |
| 62 |
|
| 63 |
export function convertRawToDate(model, value, prop) { |
| 64 |
if (!value) { |
| 65 |
return undefined; |
| 66 |
} |
| 67 |
const date = deserializeDate(value); |
| 68 |
if (!date.isValid) { |
| 69 |
throw new Error(`Invalid date: ${value} for model ${model.model} in field ${prop}`); |
| 70 |
} |
| 71 |
return date; |
| 72 |
} |
| 73 |
|
| 74 |
export function convertDateToRaw(value) { |
| 75 |
if (!value) { |
| 76 |
return undefined; |
| 77 |
} |
| 78 |
// Verify if is already a valid date object |
| 79 |
if (typeof value !== "string") { |
| 80 |
return serializeDate(value); |
| 81 |
} |
| 82 |
return value; |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Creates a deep immutable proxy for the given object or array. |
| 87 |
* Any attempts to modify, delete, or redefine properties will throw an error. |
| 88 |
* |
| 89 |
* Note: This function only supports plain objects and arrays. |
| 90 |
* It does NOT support Map, Set, or other complex data structures. |
| 91 |
* |
| 92 |
* @param {Object|Array} obj - The object or array to make immutable. |
| 93 |
* @param {string} errorMsg - The error message to throw on modification attempts. |
| 94 |
* @returns {Proxy} A Proxy that enforces deep immutability. |
| 95 |
*/ |
| 96 |
export function deepImmutable(obj, errorMsg) { |
| 97 |
return new Proxy(obj, { |
| 98 |
get(target, prop, receiver) { |
| 99 |
if ("__deepImmutable" === prop) { |
| 100 |
return true; |
| 101 |
} |
| 102 |
const value = Reflect.get(target, prop, receiver); |
| 103 |
return value && typeof value === "object" ? deepImmutable(value, errorMsg) : value; |
| 104 |
}, |
| 105 |
set() { |
| 106 |
throw new Error(errorMsg); |
| 107 |
}, |
| 108 |
deleteProperty() { |
| 109 |
throw new Error(errorMsg); |
| 110 |
}, |
| 111 |
defineProperty() { |
| 112 |
throw new Error(errorMsg); |
| 113 |
}, |
| 114 |
}); |
| 115 |
} |
| 116 |
|
| 117 |
export class AggregatedUpdates { |
| 118 |
constructor() { |
| 119 |
this.updates = new Map(); |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Adds a field update for the given record. |
| 124 |
* @param {Object} record - The record |
| 125 |
* @param {string} fieldName - The name of the field that was updated. |
| 126 |
*/ |
| 127 |
add(record, fieldName) { |
| 128 |
if (!this.updates.has(record)) { |
| 129 |
this.updates.set(record, new Set()); |
| 130 |
} |
| 131 |
this.updates.get(record).add(fieldName); |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Iterates over all updated records, fires the update event (unless silenced), and marks the records as dirty. |
| 136 |
* |
| 137 |
* @param {Object} opts - Options for controlling the update behavior. |
| 138 |
* @param {string[]} [opts.silentModels=[]] - List of model names to exclude from triggering update events. |
| 139 |
*/ |
| 140 |
fireEventAndDirty(opts = {}) { |
| 141 |
const { silentModels = [] } = opts; |
| 142 |
for (const [record, fields] of this.updates) { |
| 143 |
if (!silentModels.includes(record.model.name)) { |
| 144 |
record.model.triggerEvents("update", { id: record.id, fields: [...fields] }); |
| 145 |
} |
| 146 |
record._markDirty(); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Remove record updated |
| 152 |
*/ |
| 153 |
remove(record) { |
| 154 |
this.updates.delete(record); |
| 155 |
} |
| 156 |
} |