/
scriptdata-compat.ts
141 lines (116 loc) · 4.43 KB
/
scriptdata-compat.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import {
Lookup, getColumnsScript, getFormScript, getGlobalObject, getLookupAsync, getRemoteDataAsync, getScriptData, getScriptDataHash, handleScriptDataError, peekScriptData, reloadLookupAsync,
requestFinished, requestStarting,
resolveUrl, setScriptData, type PropertyItem, type PropertyItemsData
} from "../base";
export namespace ScriptData {
export function bindToChange(name: string, onChange: () => void): void | (() => void) {
if (typeof document !== "undefined" && document.addEventListener) {
var unbind = function () {
onChange && typeof document !== "undefined" && document.removeEventListener?.('scriptdatachange.' + name, onChange);
onChange = null;
}
document.addEventListener('scriptdatachange.' + name, onChange);
return unbind;
}
}
export const canLoad = canLoadScriptData;
export function ensure<TData = any>(name: string, dynJS?: boolean): TData {
var data = peekScriptData(name);
if (data != null)
return data;
var url = resolveUrl(dynJS ? '~/DynJS.axd/' : '~/DynamicData/') + name + (dynJS ? '.js' : '') + '?v=' + (getScriptDataHash(name) ?? new Date().getTime());
var xhr = new XMLHttpRequest();
xhr.open("GET", url, false);
requestStarting();
try {
xhr.send(null);
if (xhr.status !== 200)
handleScriptDataError(name, xhr.status, xhr.statusText);
if (dynJS) {
var script = document.createElement("script");
script.text = xhr.responseText;
document.head.appendChild(script).parentNode.removeChild(script);
data = peekScriptData(name);
if (data == null)
handleScriptDataError(name);
}
data = JSON.parse(xhr.responseText);
if (data == null)
handleScriptDataError(name);
if (name.startsWith("Lookup."))
data = new Lookup(data.Params, data.Items);
set(name, data);
return data;
}
finally {
requestFinished();
}
}
export function reload<TData = any>(name: string, dynJS?: boolean): TData {
getScriptDataHash(name, true);
setScriptData(name, null);
return ensure(name, dynJS);
}
export async function reloadAsync<TData = any>(name: string): Promise<TData> {
return await getScriptData(name, true);
}
export const set = setScriptData;
}
/**
* Check if a dynamic script with provided name is available in the cache
* or it is a registered script name
* @param name Dynamic script name
* @returns True if already available or registered
*/
export function canLoadScriptData(name: string) {
return peekScriptData(name) != null || getScriptDataHash(name) != null;
}
export function getRemoteData<TData = any>(key: string): TData {
return ScriptData.ensure('RemoteData.' + key);
}
export function getLookup<TItem>(key: string): Lookup<TItem> {
return ScriptData.ensure('Lookup.' + key);
}
export function reloadLookup<TItem = any>(key: string): Lookup<TItem> {
return ScriptData.reload('Lookup.' + key);
}
export function getColumns(key: string): PropertyItem[] {
return getColumnsData(key).items;
}
export async function getColumnsAsync(key: string): Promise<PropertyItem[]> {
return (await getColumnsScript(key)).items;
}
export function getColumnsData(key: string): PropertyItemsData {
return ScriptData.ensure('Columns.' + key);
}
export const getColumnsDataAsync = getColumnsScript;
export function getForm(key: string): PropertyItem[] {
return getFormData(key).items;
}
export async function getFormAsync(key: string): Promise<PropertyItem[]> {
return (await getFormScript(key)).items;
}
export function getFormData(key: string): PropertyItemsData {
return ScriptData.ensure('Form.' + key);
}
export const getFormDataAsync = getFormScript;
var compatExports = {
canLoadScriptData,
getColumns,
getColumnsAsync,
getForm,
getFormAsync,
getLookup,
getLookupAsync,
getRemoteData,
getRemoteDataAsync,
reloadLookup,
reloadLookupAsync,
ScriptData
}
let global = getGlobalObject();
let serenity = (global.Serenity || (global.Serenity = Object.create(null)));
for (var i in compatExports)
if (serenity[i] == null)
serenity[i] = (compatExports as any)[i];