Debian Codegraph @

addons/point_of_sale/static/src/app/models/related_models/base.js

raw
1 import { WithLazyGetterTrap } from "@point_of_sale/lazy_getter";
2 import { deepImmutable, clone, RAW_SYMBOL } from "./utils";
3 import { toRaw } from "@odoo/owl";
4 const { DateTime } = luxon;
5
6 export class Base extends WithLazyGetterTrap {
7 static excludedLazyGetters = ["id", "models"];
8
9 constructor({ model, raw }) {
10 super({});
11 this.model = model;
12 this[RAW_SYMBOL] = raw;
13 }
14
15 get models() {
16 return this.model.models;
17 }
18
19 get id() {
20 return this[RAW_SYMBOL].id;
21 }
22
23 get isSynced() {
24 return typeof this.id === "number";
25 }
26
27 get raw() {
28 return deepImmutable(clone(this[RAW_SYMBOL]), "Raw data cannot be modified");
29 }
30
31 /**
32 * Called during instantiation when the instance is fully-populated with field values.
33 * This method is called when the instance is created or updated
34 * @param {*} _vals
35 */
36 setup(_vals) {
37 this._dirty = !this.isSynced;
38 }
39
40 /**
41 * This method is invoked only during instance creation to preserve the state across updates.
42 */
43 initState() {}
44
45 /**
46 * Restore state serialized from indexedDB
47 */
48 restoreState(uiState) {
49 this.uiState = uiState;
50 }
51
52 isDirty() {
53 return this._dirty;
54 }
55
56 formatDateOrTime(field, type = "datetime") {
57 if (type === "date") {
58 return this[field].toLocaleString(DateTime.DATE_SHORT);
59 }
60 return this[field].toLocaleString(DateTime.DATETIME_SHORT);
61 }
62
63 isEqual(other) {
64 return toRaw(this) === toRaw(other);
65 }
66
67 update(vals, opts = {}) {
68 return this.model.update(this, vals, opts);
69 }
70
71 delete(opts = {}) {
72 return this.model.delete(this, opts);
73 }
74
75 serializeForORM(opts = {}) {
76 return this.model.serializeForORM(this, opts);
77 }
78
79 serializeForIndexedDB() {
80 return this.model.serializeForIndexedDB(this);
81 }
82
83 serializeState() {
84 if (!this.uiState) {
85 return;
86 }
87 return { ...this.uiState };
88 }
89
90 backLink(link) {
91 return this.model.backLink(this, link);
92 }
93
94 _markDirty() {
95 if (this.models._loadingData || this._dirty) {
96 return;
97 }
98
99 this._dirty = true;
100 this.model.getParentFields().forEach((field) => {
101 this[field.name]?._markDirty?.();
102 });
103 }
104 }