@azrico/webobject 1.5.58 → 1.5.61
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -9
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/src/WebObjects/ParentWebObject.d.ts +24 -0
- package/dist/src/WebObjects/ParentWebObject.js +77 -0
- package/dist/src/WebObjects/WebObject.d.ts +153 -0
- package/dist/src/WebObjects/WebObject.js +654 -0
- package/dist/src/WebObjects/WebObjectHelper.d.ts +9 -0
- package/dist/src/WebObjects/WebObjectHelper.js +18 -0
- package/dist/src/WebObjects/WebObjectHelper.test.d.ts +1 -0
- package/dist/src/WebObjects/WebObjectHelper.test.js +45 -0
- package/dist/src/WebObjects/WebObjectManager.d.ts +68 -0
- package/dist/src/WebObjects/WebObjectManager.js +290 -0
- package/dist/src/WebObjects/WebObjectPathHelper.d.ts +36 -0
- package/dist/src/WebObjects/WebObjectPathHelper.js +84 -0
- package/dist/src/WebObjects/WebObjectPathHelper.test.d.ts +1 -0
- package/dist/src/WebObjects/WebObjectPathHelper.test.js +31 -0
- package/dist/src/index.d.ts +10 -0
- package/dist/src/index.js +10 -0
- package/dist/src/structs/DummyObject.d.ts +5 -0
- package/dist/src/structs/DummyObject.js +8 -0
- package/dist/src/structs/MapableObject.d.ts +43 -0
- package/dist/src/structs/MapableObject.js +77 -0
- package/dist/src/structs/MetaObject.d.ts +15 -0
- package/dist/src/structs/MetaObject.js +35 -0
- package/dist/src/structs/ObjectArray.d.ts +10 -0
- package/dist/src/structs/ObjectArray.js +43 -0
- package/dist/src/structs/ObjectManager.d.ts +136 -0
- package/dist/src/structs/ObjectManager.js +332 -0
- package/dist/src/structs/ObjectWithTemp.d.ts +16 -0
- package/dist/src/structs/ObjectWithTemp.js +35 -0
- package/dist/src/structs/SimpleObject.js +1138 -0
- package/package.json +1 -1
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { WebObject } from "../index";
|
|
2
|
+
import WebObjectHelper from "./WebObjectHelper";
|
|
3
|
+
class TestWebObject extends WebObject {
|
|
4
|
+
name = "";
|
|
5
|
+
}
|
|
6
|
+
describe("WebObjectHelper", () => {
|
|
7
|
+
let wobj;
|
|
8
|
+
let wobj2;
|
|
9
|
+
let wobjArray;
|
|
10
|
+
beforeEach(() => {
|
|
11
|
+
wobj = new TestWebObject();
|
|
12
|
+
wobj2 = new TestWebObject();
|
|
13
|
+
wobjArray = [
|
|
14
|
+
wobj,
|
|
15
|
+
new TestWebObject(),
|
|
16
|
+
new TestWebObject(),
|
|
17
|
+
new TestWebObject(),
|
|
18
|
+
wobj2,
|
|
19
|
+
];
|
|
20
|
+
});
|
|
21
|
+
describe("isChangedWebObjectField", () => {
|
|
22
|
+
it("returns false for non webobjects", () => {
|
|
23
|
+
expect(WebObjectHelper.isChangedWebObjectField(1)).toBeFalsy();
|
|
24
|
+
expect(WebObjectHelper.isChangedWebObjectField("test")).toBeFalsy();
|
|
25
|
+
expect(WebObjectHelper.isChangedWebObjectField(null)).toBeFalsy();
|
|
26
|
+
expect(WebObjectHelper.isChangedWebObjectField(undefined)).toBeFalsy();
|
|
27
|
+
expect(WebObjectHelper.isChangedWebObjectField([])).toBeFalsy();
|
|
28
|
+
expect(WebObjectHelper.isChangedWebObjectField({})).toBeFalsy();
|
|
29
|
+
});
|
|
30
|
+
it("works on single object", () => {
|
|
31
|
+
expect(WebObjectHelper.isChangedWebObjectField(wobj)).toBeFalsy();
|
|
32
|
+
wobj.set("name", "test");
|
|
33
|
+
expect(WebObjectHelper.isChangedWebObjectField(wobj)).toBeTruthy();
|
|
34
|
+
wobj.clear_changes();
|
|
35
|
+
expect(WebObjectHelper.isChangedWebObjectField(wobj)).toBeFalsy();
|
|
36
|
+
});
|
|
37
|
+
it("works on array", () => {
|
|
38
|
+
expect(WebObjectHelper.isChangedWebObjectField(wobjArray)).toBeFalsy();
|
|
39
|
+
wobj2.set("name", "test");
|
|
40
|
+
expect(WebObjectHelper.isChangedWebObjectField(wobjArray)).toBeTruthy();
|
|
41
|
+
wobj2.clear_changes();
|
|
42
|
+
expect(WebObjectHelper.isChangedWebObjectField(wobjArray)).toBeFalsy();
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
});
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { ClassType } from "@azrico/types";
|
|
2
|
+
import { WebObject, sendChangesType } from "../index";
|
|
3
|
+
import ObjectManager from "../structs/ObjectManager";
|
|
4
|
+
/**
|
|
5
|
+
* This class manages the events of an Simple Object
|
|
6
|
+
*/
|
|
7
|
+
export declare class WebObjectManager<ObjType extends WebObject> extends ObjectManager<ObjType> {
|
|
8
|
+
protected static readonly __object_type: () => typeof WebObject | ClassType<WebObject>;
|
|
9
|
+
static readonly __save_message: boolean;
|
|
10
|
+
constructor(defaultObject?: ObjType);
|
|
11
|
+
setObject(toObj: ObjType | undefined): boolean;
|
|
12
|
+
/**
|
|
13
|
+
* Update meta value of this object , meta data is stored in meta_data variable
|
|
14
|
+
* @param key
|
|
15
|
+
* @param value
|
|
16
|
+
* @param createIfNotExists should we create a new meta if key does not exist
|
|
17
|
+
* @param sendChange
|
|
18
|
+
* @returns
|
|
19
|
+
*/
|
|
20
|
+
setMetaValue(key: string, value: any, sendChange?: sendChangesType): Promise<boolean | undefined>;
|
|
21
|
+
getMetaValue(key: string): any;
|
|
22
|
+
get_deltaObject(): Promise<any>;
|
|
23
|
+
Save(): Promise<boolean>;
|
|
24
|
+
New(): Promise<void>;
|
|
25
|
+
Delete(): Promise<any>;
|
|
26
|
+
Lock(): Promise<any>;
|
|
27
|
+
UnLock(): Promise<any>;
|
|
28
|
+
Archive(): Promise<any>;
|
|
29
|
+
UnArchive(): Promise<any>;
|
|
30
|
+
Load(id: any, ...p: any[]): Promise<WebObject | undefined>;
|
|
31
|
+
get __web_path(): string;
|
|
32
|
+
get __db_name(): string;
|
|
33
|
+
get __object_name(): string;
|
|
34
|
+
get_webpath(): string;
|
|
35
|
+
private __getObjectData;
|
|
36
|
+
/**
|
|
37
|
+
* Use this when you delete Object
|
|
38
|
+
* @param p
|
|
39
|
+
* @returns
|
|
40
|
+
*/
|
|
41
|
+
onDelete(...p: any): Promise<any>;
|
|
42
|
+
onArchive(...p: any): Promise<any>;
|
|
43
|
+
onUnArchive(...p: any): Promise<any>;
|
|
44
|
+
onLock(...p: any): Promise<any>;
|
|
45
|
+
onUnLock(...p: any): Promise<any>;
|
|
46
|
+
/**
|
|
47
|
+
* Use this when you load new Object
|
|
48
|
+
* @param p
|
|
49
|
+
* @returns
|
|
50
|
+
*/
|
|
51
|
+
onNew(...p: any): Promise<any>;
|
|
52
|
+
/**
|
|
53
|
+
* Use this when you load data to the object
|
|
54
|
+
* @param p
|
|
55
|
+
* @returns
|
|
56
|
+
*/
|
|
57
|
+
onLoad(...p: any): Promise<any>;
|
|
58
|
+
/**
|
|
59
|
+
* Use this after succesful save to Database
|
|
60
|
+
* @param p
|
|
61
|
+
* @returns
|
|
62
|
+
*/
|
|
63
|
+
onSave(...p: any): Promise<any>;
|
|
64
|
+
onAfterSave(...p: any): Promise<any>;
|
|
65
|
+
getObjectType(): typeof WebObject;
|
|
66
|
+
static getObjectType(): typeof WebObject | ClassType<WebObject>;
|
|
67
|
+
}
|
|
68
|
+
export default WebObjectManager;
|
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
import { SimpleObject, WebObject } from "../index";
|
|
2
|
+
import ObjectManager from "../structs/ObjectManager";
|
|
3
|
+
import { gfunc } from "@azrico/global";
|
|
4
|
+
import { fn_eval, } from "@azrico/object";
|
|
5
|
+
/**
|
|
6
|
+
* This class manages the events of an Simple Object
|
|
7
|
+
*/
|
|
8
|
+
export class WebObjectManager extends ObjectManager {
|
|
9
|
+
static __object_type;
|
|
10
|
+
static __save_message = true;
|
|
11
|
+
/* -------------------------------------------------------------------------- */
|
|
12
|
+
constructor(defaultObject) {
|
|
13
|
+
super(defaultObject);
|
|
14
|
+
if (this.constructor["__save_message"]) {
|
|
15
|
+
/* ---------------- should we display a message after saving ? -------------- */
|
|
16
|
+
this.event_add("on_save", "on_save-sendmsg", async (srt) => {
|
|
17
|
+
switch (srt) {
|
|
18
|
+
case "nothing":
|
|
19
|
+
return await gfunc.run_action("show_message", {
|
|
20
|
+
message: "Nothing to save",
|
|
21
|
+
type: "info",
|
|
22
|
+
});
|
|
23
|
+
case "normal":
|
|
24
|
+
return await gfunc.run_action("show_message", {
|
|
25
|
+
message: "Saved",
|
|
26
|
+
type: "success",
|
|
27
|
+
});
|
|
28
|
+
default:
|
|
29
|
+
return await gfunc.run_action("show_message", {
|
|
30
|
+
message: "Not saved",
|
|
31
|
+
type: "error",
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
this.event_add("on_archive", "on_archive-sendmsg", async (srType) => {
|
|
36
|
+
switch (srType) {
|
|
37
|
+
case "normal":
|
|
38
|
+
return await gfunc.run_action("show_message", {
|
|
39
|
+
message: "Archived",
|
|
40
|
+
type: "success",
|
|
41
|
+
});
|
|
42
|
+
default:
|
|
43
|
+
return await gfunc.run_action("show_message", {
|
|
44
|
+
message: `Not archived (${srType})`,
|
|
45
|
+
type: "error",
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
this.event_add("on_unarchive", "on_unarchive-sendmsg", async (srType) => {
|
|
50
|
+
switch (srType) {
|
|
51
|
+
case "normal":
|
|
52
|
+
return await gfunc.run_action("show_message", {
|
|
53
|
+
message: "Restored",
|
|
54
|
+
type: "success",
|
|
55
|
+
});
|
|
56
|
+
default:
|
|
57
|
+
return await gfunc.run_action("show_message", {
|
|
58
|
+
message: `Not restored (${srType})`,
|
|
59
|
+
type: "error",
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
this.event_add("on_delete", "on_delete-sendmsg", async (srType) => {
|
|
64
|
+
switch (srType) {
|
|
65
|
+
case "normal":
|
|
66
|
+
return await gfunc.run_action("show_message", {
|
|
67
|
+
message: "Deleted",
|
|
68
|
+
type: "success",
|
|
69
|
+
});
|
|
70
|
+
default:
|
|
71
|
+
return await gfunc.run_action("show_message", {
|
|
72
|
+
message: `Not deleted (${srType})`,
|
|
73
|
+
type: "error",
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
/* -------------------------------------------------------------------------- */
|
|
80
|
+
setObject(toObj) {
|
|
81
|
+
const res = super.setObject(toObj);
|
|
82
|
+
if (this.__currentObject &&
|
|
83
|
+
this.__currentObject instanceof SimpleObject) {
|
|
84
|
+
const checkKeys = ["__web_path", "__db_name", "__object_name"];
|
|
85
|
+
for (let index = 0; index < checkKeys.length; index++) {
|
|
86
|
+
const k = checkKeys[index];
|
|
87
|
+
if (!this.__last_object_data)
|
|
88
|
+
this.__last_object_data = {};
|
|
89
|
+
if (this.__currentObject[k] != null)
|
|
90
|
+
this.__last_object_data[k] = this.__currentObject[k];
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return res;
|
|
94
|
+
}
|
|
95
|
+
/* -------------------------------------------------------------------------- */
|
|
96
|
+
/* Meta data */
|
|
97
|
+
/* -------------------------------------------------------------------------- */
|
|
98
|
+
/**
|
|
99
|
+
* Update meta value of this object , meta data is stored in meta_data variable
|
|
100
|
+
* @param key
|
|
101
|
+
* @param value
|
|
102
|
+
* @param createIfNotExists should we create a new meta if key does not exist
|
|
103
|
+
* @param sendChange
|
|
104
|
+
* @returns
|
|
105
|
+
*/
|
|
106
|
+
async setMetaValue(key, value, sendChange = "auto") {
|
|
107
|
+
return await this.obj?.setMetaValue(key, value, sendChange);
|
|
108
|
+
}
|
|
109
|
+
getMetaValue(key) {
|
|
110
|
+
return this.obj?.getMetaValue(key);
|
|
111
|
+
}
|
|
112
|
+
/* -------------------------------------------------------------------------- */
|
|
113
|
+
async get_deltaObject() {
|
|
114
|
+
return (await this.obj?.get_deltaObject()) || {};
|
|
115
|
+
}
|
|
116
|
+
/* ------------------------------ save and load ----------------------------- */
|
|
117
|
+
async Save() {
|
|
118
|
+
return (await this.obj?.Save()) || false;
|
|
119
|
+
}
|
|
120
|
+
async New() {
|
|
121
|
+
const cls = this.getObjectType();
|
|
122
|
+
this.setObject(new cls());
|
|
123
|
+
await this.onNew();
|
|
124
|
+
}
|
|
125
|
+
async Delete() {
|
|
126
|
+
return (await this.obj?.Delete()) || false;
|
|
127
|
+
}
|
|
128
|
+
async Lock() {
|
|
129
|
+
return (await this.obj?.Lock()) || false;
|
|
130
|
+
}
|
|
131
|
+
async UnLock() {
|
|
132
|
+
return (await this.obj?.UnLock()) || false;
|
|
133
|
+
}
|
|
134
|
+
async Archive() {
|
|
135
|
+
return (await this.obj?.Archive()) || false;
|
|
136
|
+
}
|
|
137
|
+
async UnArchive() {
|
|
138
|
+
return (await this.obj?.UnArchive()) || false;
|
|
139
|
+
}
|
|
140
|
+
/* -------------------------------------------------------------------------- */
|
|
141
|
+
async Load(id, ...p) {
|
|
142
|
+
if (!id)
|
|
143
|
+
return;
|
|
144
|
+
try {
|
|
145
|
+
this.event_run("on_process_start", true);
|
|
146
|
+
const classType = this.getObjectType();
|
|
147
|
+
if (!classType) {
|
|
148
|
+
console.error("[webobjectmanager]", "getObjectType failed to get the object_type.", "did you set the __object_type correctly ?");
|
|
149
|
+
}
|
|
150
|
+
const result_product = await classType.get_single(id, false);
|
|
151
|
+
if (result_product &&
|
|
152
|
+
typeof result_product === "object" &&
|
|
153
|
+
WebObject.getID(result_product)) {
|
|
154
|
+
this.setObject(result_product);
|
|
155
|
+
await result_product.onLoad();
|
|
156
|
+
}
|
|
157
|
+
else {
|
|
158
|
+
console.error("badobject:", id, result_product);
|
|
159
|
+
WebObject.addError(this.obj, `Invalid ${this.__object_name}`, `The loaded ${this.__object_name} from website is invalid`, true);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
finally {
|
|
163
|
+
this.event_run("on_process_end", true);
|
|
164
|
+
}
|
|
165
|
+
return this.getObject();
|
|
166
|
+
}
|
|
167
|
+
/* -------------------------------------------------------------------------- */
|
|
168
|
+
/* Child Functions */
|
|
169
|
+
/* -------------------------------------------------------------------------- */
|
|
170
|
+
get __web_path() {
|
|
171
|
+
return this.__getObjectData(["__web_path", "__db_name"]);
|
|
172
|
+
}
|
|
173
|
+
get __db_name() {
|
|
174
|
+
return this.__getObjectData(["__db_name", "__web_path"]);
|
|
175
|
+
}
|
|
176
|
+
get __object_name() {
|
|
177
|
+
return this.__getObjectData(["__object_name", "__db_name"]);
|
|
178
|
+
}
|
|
179
|
+
get_webpath() {
|
|
180
|
+
return this.__getObjectData(["__web_path", "__db_name"]);
|
|
181
|
+
}
|
|
182
|
+
__getObjectData(checkKeys) {
|
|
183
|
+
const objtype = this.getObjectType();
|
|
184
|
+
for (let index = 0; index < checkKeys.length; index++) {
|
|
185
|
+
const k = checkKeys[index];
|
|
186
|
+
const v = this.obj?.[k] || this.__last_object_data[k] || objtype?.[k];
|
|
187
|
+
if (v)
|
|
188
|
+
return v;
|
|
189
|
+
}
|
|
190
|
+
return "";
|
|
191
|
+
}
|
|
192
|
+
/* -------------------------------------------------------------------------- */
|
|
193
|
+
/* Default Events */
|
|
194
|
+
/* -------------------------------------------------------------------------- */
|
|
195
|
+
/**
|
|
196
|
+
* Use this when you delete Object
|
|
197
|
+
* @param p
|
|
198
|
+
* @returns
|
|
199
|
+
*/
|
|
200
|
+
async onDelete(...p) {
|
|
201
|
+
const srt = this.getSaveResultType();
|
|
202
|
+
await this.event_run("on_delete", srt);
|
|
203
|
+
await this.event_run("on_after_delete", srt);
|
|
204
|
+
return true;
|
|
205
|
+
}
|
|
206
|
+
async onArchive(...p) {
|
|
207
|
+
const srt = this.getSaveResultType();
|
|
208
|
+
await this.event_run("on_archive", srt);
|
|
209
|
+
await this.event_run("on_after_archive", srt);
|
|
210
|
+
return true;
|
|
211
|
+
}
|
|
212
|
+
async onUnArchive(...p) {
|
|
213
|
+
const srt = this.getSaveResultType();
|
|
214
|
+
await this.event_run("on_unarchive", srt);
|
|
215
|
+
await this.event_run("on_after_unarchive", srt);
|
|
216
|
+
return true;
|
|
217
|
+
}
|
|
218
|
+
async onLock(...p) {
|
|
219
|
+
const srt = this.getSaveResultType();
|
|
220
|
+
await this.event_run("on_lock", srt);
|
|
221
|
+
await this.event_run("on_after_lock", srt);
|
|
222
|
+
return true;
|
|
223
|
+
}
|
|
224
|
+
async onUnLock(...p) {
|
|
225
|
+
const srt = this.getSaveResultType();
|
|
226
|
+
await this.event_run("on_unlock", srt);
|
|
227
|
+
await this.event_run("on_after_unlock", srt);
|
|
228
|
+
return true;
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* Use this when you load new Object
|
|
232
|
+
* @param p
|
|
233
|
+
* @returns
|
|
234
|
+
*/
|
|
235
|
+
async onNew(...p) {
|
|
236
|
+
if (this.canModify) {
|
|
237
|
+
this.obj?.setOriginal();
|
|
238
|
+
}
|
|
239
|
+
await this.event_run("on_new");
|
|
240
|
+
await this.event_run("on_change", true);
|
|
241
|
+
await this.event_run("on_after_new");
|
|
242
|
+
await this.checkIsChanged();
|
|
243
|
+
return await Promise.resolve(true);
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* Use this when you load data to the object
|
|
247
|
+
* @param p
|
|
248
|
+
* @returns
|
|
249
|
+
*/
|
|
250
|
+
async onLoad(...p) {
|
|
251
|
+
if (this.canModify && this.obj) {
|
|
252
|
+
this.obj.isDeleted = false;
|
|
253
|
+
this.obj.setOriginal();
|
|
254
|
+
}
|
|
255
|
+
await this.event_run("on_load");
|
|
256
|
+
await this.event_run("on_change", true);
|
|
257
|
+
await this.event_run("on_after_load");
|
|
258
|
+
await this.event_run("on_load_finished");
|
|
259
|
+
await this.checkIsChanged();
|
|
260
|
+
return true;
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Use this after succesful save to Database
|
|
264
|
+
* @param p
|
|
265
|
+
* @returns
|
|
266
|
+
*/
|
|
267
|
+
async onSave(...p) {
|
|
268
|
+
const srt = this.getSaveResultType();
|
|
269
|
+
await this.event_run("on_save", srt);
|
|
270
|
+
await this.event_run("on_change", true);
|
|
271
|
+
await this.checkIsChanged();
|
|
272
|
+
return await Promise.resolve(true);
|
|
273
|
+
}
|
|
274
|
+
async onAfterSave(...p) {
|
|
275
|
+
const srt = this.getSaveResultType();
|
|
276
|
+
await this.event_run("on_after_save", srt);
|
|
277
|
+
await this.event_run("on_save_finished", srt);
|
|
278
|
+
return await Promise.resolve(true);
|
|
279
|
+
}
|
|
280
|
+
/* -------------------------------------------------------------------------- */
|
|
281
|
+
/* get __object_type */
|
|
282
|
+
/* -------------------------------------------------------------------------- */
|
|
283
|
+
getObjectType() {
|
|
284
|
+
return this.constructor["getObjectType"]();
|
|
285
|
+
}
|
|
286
|
+
static getObjectType() {
|
|
287
|
+
return fn_eval(this.__object_type);
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
export default WebObjectManager;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { AnyClassType } from "@azrico/types";
|
|
2
|
+
import WebObject from "./WebObject";
|
|
3
|
+
type PathFunction<T extends AnyClassType> = string | ((w: InstanceType<T>) => string);
|
|
4
|
+
export declare class WebObjectPathHelper {
|
|
5
|
+
static readonly defaultPaths: {
|
|
6
|
+
lock: string;
|
|
7
|
+
unlock: string;
|
|
8
|
+
archive: string;
|
|
9
|
+
unarchive: string;
|
|
10
|
+
delete: string;
|
|
11
|
+
new: string;
|
|
12
|
+
save: string;
|
|
13
|
+
get: string;
|
|
14
|
+
};
|
|
15
|
+
static readonly pathOverrides: {};
|
|
16
|
+
/**
|
|
17
|
+
* get path that we use for `action` for the `obj`
|
|
18
|
+
* @param obj
|
|
19
|
+
* @param action
|
|
20
|
+
* @returns
|
|
21
|
+
*/
|
|
22
|
+
static get_path<T extends WebObject | typeof WebObject>(obj: T, action: string): Promise<string>;
|
|
23
|
+
/**
|
|
24
|
+
* clear all paths that are override by `override_path`
|
|
25
|
+
*/
|
|
26
|
+
static clear_overrides(): Promise<void>;
|
|
27
|
+
/**
|
|
28
|
+
* override a save path for the gievn webobject
|
|
29
|
+
* @param obj
|
|
30
|
+
* @param pn
|
|
31
|
+
* @param override
|
|
32
|
+
* @returns
|
|
33
|
+
*/
|
|
34
|
+
static override_path<T extends AnyClassType>(obj: T | InstanceType<T> | string, pn: string, override: PathFunction<T>): Promise<false | PathFunction<T>>;
|
|
35
|
+
}
|
|
36
|
+
export default WebObjectPathHelper;
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import WebObject from "./WebObject";
|
|
2
|
+
export class WebObjectPathHelper {
|
|
3
|
+
static defaultPaths = {
|
|
4
|
+
lock: "{wn}/{id}/lock",
|
|
5
|
+
unlock: "{wn}/{id}/unlock",
|
|
6
|
+
archive: "{wn}/{id}/archive",
|
|
7
|
+
unarchive: "{wn}/{id}/unarchive",
|
|
8
|
+
delete: "{wn}/{id}",
|
|
9
|
+
new: "{wn}",
|
|
10
|
+
save: "{wn}/{id}",
|
|
11
|
+
get: "{wn}",
|
|
12
|
+
};
|
|
13
|
+
static pathOverrides = {};
|
|
14
|
+
/**
|
|
15
|
+
* get path that we use for `action` for the `obj`
|
|
16
|
+
* @param obj
|
|
17
|
+
* @param action
|
|
18
|
+
* @returns
|
|
19
|
+
*/
|
|
20
|
+
static async get_path(obj, action) {
|
|
21
|
+
action = String(action).toLowerCase();
|
|
22
|
+
const wn = obj.get_webpath();
|
|
23
|
+
const wp = obj.__web_path;
|
|
24
|
+
let usepath = "";
|
|
25
|
+
if (WebObjectPathHelper.pathOverrides[wn] &&
|
|
26
|
+
WebObjectPathHelper.pathOverrides[wn][action]) {
|
|
27
|
+
usepath = WebObjectPathHelper.pathOverrides[wn][action];
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
usepath = WebObjectPathHelper.defaultPaths[action];
|
|
31
|
+
}
|
|
32
|
+
if (!usepath)
|
|
33
|
+
usepath = WebObjectPathHelper.defaultPaths["save"];
|
|
34
|
+
if (typeof usepath === "function")
|
|
35
|
+
usepath = String(usepath(obj));
|
|
36
|
+
/* ----------------------------- place variables ---------------------------- */
|
|
37
|
+
usepath = String(usepath).replaceAll("{wn}", wn).replaceAll("{wp}", wp);
|
|
38
|
+
if (obj instanceof WebObject) {
|
|
39
|
+
usepath = usepath.replaceAll("{id}", obj.getID());
|
|
40
|
+
}
|
|
41
|
+
/* -------------------------------------------------------------------------- */
|
|
42
|
+
return usepath;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* clear all paths that are override by `override_path`
|
|
46
|
+
*/
|
|
47
|
+
static async clear_overrides() {
|
|
48
|
+
for (const key in this.pathOverrides) {
|
|
49
|
+
this.pathOverrides[key] = {};
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* override a save path for the gievn webobject
|
|
54
|
+
* @param obj
|
|
55
|
+
* @param pn
|
|
56
|
+
* @param override
|
|
57
|
+
* @returns
|
|
58
|
+
*/
|
|
59
|
+
static async override_path(obj, pn, override) {
|
|
60
|
+
pn = String(pn).toLowerCase();
|
|
61
|
+
let wn = "";
|
|
62
|
+
/* ------------------------------ find webname ------------------------------ */
|
|
63
|
+
if (typeof obj === "string") {
|
|
64
|
+
wn = obj;
|
|
65
|
+
}
|
|
66
|
+
else if (typeof obj.get_webpath === "function") {
|
|
67
|
+
wn = obj.get_webpath();
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
console.error("[override_path]", "given object has no get_webpath");
|
|
71
|
+
wn = obj.__web_path || obj.__web_type;
|
|
72
|
+
}
|
|
73
|
+
if (!wn) {
|
|
74
|
+
console.error("[override_path]", "bad webname");
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
/* --------------------------- apply the override --------------------------- */
|
|
78
|
+
if (!WebObjectPathHelper.pathOverrides[wn])
|
|
79
|
+
WebObjectPathHelper.pathOverrides[wn] = {};
|
|
80
|
+
WebObjectPathHelper.pathOverrides[wn][pn] = override;
|
|
81
|
+
return override;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
export default WebObjectPathHelper;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { WebObject } from "../index";
|
|
2
|
+
import WebObjectPathHelper from "./WebObjectPathHelper";
|
|
3
|
+
class TestWebObject extends WebObject {
|
|
4
|
+
static __web_path = "test-wp";
|
|
5
|
+
}
|
|
6
|
+
describe("WebObjectPathHelper", () => {
|
|
7
|
+
let wobj;
|
|
8
|
+
beforeEach(() => {
|
|
9
|
+
WebObjectPathHelper.clear_overrides();
|
|
10
|
+
wobj = new TestWebObject();
|
|
11
|
+
wobj._id = "test";
|
|
12
|
+
});
|
|
13
|
+
it("get_path", async () => {
|
|
14
|
+
expect(await WebObjectPathHelper.get_path(wobj, "save")).toBe(wobj.get_webpath() + "/" + wobj.getID());
|
|
15
|
+
expect(await WebObjectPathHelper.get_path(wobj, "new")).toBe(wobj.get_webpath());
|
|
16
|
+
expect(await WebObjectPathHelper.get_path(wobj, "lock")).toBe(`${wobj.get_webpath()}/${wobj.getID()}/lock`);
|
|
17
|
+
});
|
|
18
|
+
it("override_path", async () => {
|
|
19
|
+
await WebObjectPathHelper.override_path(wobj.get_webpath(), "save", "blah");
|
|
20
|
+
expect(await WebObjectPathHelper.get_path(wobj, "save")).toBe("blah");
|
|
21
|
+
/* -------------------------------------------------------------------------- */
|
|
22
|
+
await WebObjectPathHelper.override_path(wobj.get_webpath(), "save", "{id}");
|
|
23
|
+
expect(await WebObjectPathHelper.get_path(wobj, "save")).toBe(wobj.getID());
|
|
24
|
+
/* ------------------------ old data is not affected ------------------------ */
|
|
25
|
+
expect(await WebObjectPathHelper.get_path(wobj, "new")).toBe(wobj.get_webpath());
|
|
26
|
+
});
|
|
27
|
+
it("override_path with function", async () => {
|
|
28
|
+
await WebObjectPathHelper.override_path(wobj, "save", (s) => s._id);
|
|
29
|
+
expect(await WebObjectPathHelper.get_path(wobj, "save")).toBe(wobj.getID());
|
|
30
|
+
});
|
|
31
|
+
});
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export * from "./structs/SimpleObject";
|
|
2
|
+
export * from "./structs/ObjectArray";
|
|
3
|
+
export * from "./structs/MapableObject";
|
|
4
|
+
export * from "./structs/MetaObject";
|
|
5
|
+
export * from "./structs/DummyObject";
|
|
6
|
+
export * from "./structs/ObjectManager";
|
|
7
|
+
export * from "./WebObjects/WebObjectManager";
|
|
8
|
+
export * from "./WebObjects/WebObject";
|
|
9
|
+
export * from "./WebObjects/WebObjectPathHelper";
|
|
10
|
+
export * from "./WebObjects/ParentWebObject";
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export * from "./structs/SimpleObject";
|
|
2
|
+
export * from "./structs/ObjectArray";
|
|
3
|
+
export * from "./structs/MapableObject";
|
|
4
|
+
export * from "./structs/MetaObject";
|
|
5
|
+
export * from "./structs/DummyObject";
|
|
6
|
+
export * from "./structs/ObjectManager";
|
|
7
|
+
export * from "./WebObjects/WebObjectManager";
|
|
8
|
+
export * from "./WebObjects/WebObject";
|
|
9
|
+
export * from "./WebObjects/WebObjectPathHelper";
|
|
10
|
+
export * from "./WebObjects/ParentWebObject";
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import ObjectWithTemp from "./ObjectWithTemp";
|
|
2
|
+
declare type FirstInTuple<T extends any[]> = ((...args: T) => any) extends (arg: infer R, ...rest: infer U) => any ? R : T;
|
|
3
|
+
/**
|
|
4
|
+
* This object can be mapped and converted
|
|
5
|
+
*/
|
|
6
|
+
export declare class MapableObject extends ObjectWithTemp {
|
|
7
|
+
readonly __uid: string;
|
|
8
|
+
__table_id?: string;
|
|
9
|
+
__parent__table_id?: string;
|
|
10
|
+
__row_number?: number;
|
|
11
|
+
constructor();
|
|
12
|
+
getUID(): string;
|
|
13
|
+
static getUID(o: MapableObject): string;
|
|
14
|
+
static mapto<T>(maptoType: {
|
|
15
|
+
new (...p: any): T;
|
|
16
|
+
}, objectlist: any[] | null | undefined, is_readonly?: boolean, ...p: any[]): T[];
|
|
17
|
+
static mapto<T extends typeof MapableObject>(objectlist: any[] | null | undefined, is_readonly?: boolean, ...p: any[]): InstanceType<T>[];
|
|
18
|
+
/**
|
|
19
|
+
* construct a new class from the given first parameter
|
|
20
|
+
* @param maptoType class
|
|
21
|
+
* @param first_param first parameter
|
|
22
|
+
* @returns
|
|
23
|
+
*/
|
|
24
|
+
static ensure<ClassType extends new (...args: any) => any>(maptoType: ClassType, first_param: FirstInTuple<ConstructorParameters<ClassType>>): InstanceType<ClassType>;
|
|
25
|
+
/**
|
|
26
|
+
* construct a new class from the given parameters
|
|
27
|
+
* @param maptoType class
|
|
28
|
+
* @param first_param first parameter
|
|
29
|
+
* @returns
|
|
30
|
+
*/
|
|
31
|
+
static ensure<ClassType extends new (...args: any) => any>(maptoType: ClassType, ...rest_params: ConstructorParameters<ClassType>): InstanceType<ClassType>;
|
|
32
|
+
/**
|
|
33
|
+
* make sure the object is instance of class
|
|
34
|
+
* @param maptoType class
|
|
35
|
+
* @param obj
|
|
36
|
+
* @returns
|
|
37
|
+
*/
|
|
38
|
+
static ensure<ClassType extends new (...args: any) => any>(maptoType: ClassType, obj: InstanceType<ClassType>): InstanceType<ClassType>;
|
|
39
|
+
static map_rowindex<T extends MapableObject>(objectlist: T[] | null | undefined): T[];
|
|
40
|
+
static map_tableid<T extends MapableObject>(objectlist: T[] | null | undefined): T[];
|
|
41
|
+
static equals(a?: MapableObject | any, b?: MapableObject | any): boolean;
|
|
42
|
+
}
|
|
43
|
+
export default MapableObject;
|