-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathowc.resources.js
176 lines (170 loc) · 4.03 KB
/
owc.resources.js
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
"use strict";
/*
This file is part of the ONLINE WARBAND CREATOR (https://github.com/suppenhuhn79/sbhowc)
Copyright 2021 Christoph Zager
Licensed unter the GNU Affero General Public License, Version 3
See the full license text at https://www.gnu.org/licenses/agpl-3.0.en.html
*/
owc.resources =
{
DEFAULT_LANGUAGE: "en",
data: {},
loadedUrls: []
};
owc.resources.import = function (urls, callback)
{
function _allSettled(promises)
{
for (let promise of promises)
{
if (promise.status === "fulfilled")
{
_append(promise.value);
}
else
{
console.error(promise.reason);
};
};
_manageCrossreferences();
};
function _append(json)
{
let currentLang = json.lang || owc.resources.DEFAULT_LANGUAGE;
let currentScope = json.scope;
for (let key in json.data)
{
if (owc.resources.data.hasOwnProperty(key) === false)
{
owc.resources.data[key] = {};
};
if (owc.resources.data[key].hasOwnProperty(currentLang) === false)
{
if (typeof json.data[key] === "object")
{
for (let prop in json.data[key])
{
if (prop === "text")
{
owc.resources.data[key][currentLang] = json.data[key]["text"];
}
else
{
owc.resources.data[key][prop] = json.data[key][prop];
};
};
}
else
{
owc.resources.data[key][currentLang] = json.data[key];
};
if (!!currentScope)
{
owc.resources.data[key]["scope"] = currentScope;
};
}
else
{
console.warn("Duplicate resource identifier \"" + key + "\".",
{
"Existing resource": owc.resources.data[key],
"Data to import": json
}
);
};
};
};
function _manageCrossreferences()
{
function __copyAllReferences(originResourceKey, referredResourceKey, attribute)
{
let originResource = owc.resources.data[originResourceKey];
let referredResource = owc.resources.data[referredResourceKey];
if (referredResource[attribute] === undefined)
{
referredResource[attribute] = [];
};
if (referredResource[attribute].includes(originResourceKey) === false)
{
referredResource[attribute].push(originResourceKey);
};
for (let referredValue of originResource[attribute])
{
if ((referredValue !== referredResourceKey) && (referredResource[attribute].includes(referredValue) === false))
{
referredResource[attribute].push(referredValue);
};
};
};
const attributes = ["replaces", "exclusive"];
for (let key in owc.resources.data)
{
let originResource = owc.resources.data[key];
for (let attribute of attributes)
{
if (originResource[attribute] !== undefined)
{
for (let originAttribute of originResource[attribute])
{
__copyAllReferences(key, originAttribute, attribute);
};
};
};
};
};
return new Promise((resolve, reject) =>
{
let executors = [];
for (let url of urls)
{
if (owc.resources.loadedUrls.includes(url) === false)
{
executors.push(fileIo.fetchServerFile(url));
owc.resources.loadedUrls.push(url);
};
};
Promise.allSettled(executors).then((promises) =>
{
_allSettled(promises);
resolve();
}
);
}
);
};
owc.resources.defaultText = function (resourceId, placeholders = {}
)
{
let result = owc.resources.data[resourceId][owc.resources.DEFAULT_LANGUAGE];
for (let key in placeholders)
{
result = result.replace("{" + key + "}", placeholders[key]);
};
return result;
};
owc.resources.translate = function (resourceId, toLanguage, placeholders = {}
)
{
let result = "";
let resource = owc.resources.data[resourceId];
if (!!resource)
{
result = resource[toLanguage];
if ((typeof result === "undefined") || (result === ""))
{
console.warn("Language \"" + toLanguage + "\" not defined for resource \"" + resourceId + "\":", resource);
result = owc.resources.defaultText(resourceId);
};
for (let key in placeholders)
{
result = result.replace("{" + key + "}", placeholders[key]);
};
}
else
{
console.warn("Undefined resource \"" + resourceId + "\"");
console.trace();
result = resourceId;
};
return result;
};