@crowdin/app-project-module 0.50.0 → 0.51.0

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.
@@ -0,0 +1,306 @@
1
+ document.addEventListener('DOMContentLoaded', () => {
2
+ const fields = document.querySelectorAll('[data-dependency]');
3
+
4
+ if (fields.length > 0) {
5
+ fields.forEach((field) => {
6
+ const conditions = JSON.parse( field.dataset['dependency']);
7
+ action(field, conditions);
8
+
9
+ const success = check(conditions);
10
+ showHide(field, success);
11
+ });
12
+ }
13
+ });
14
+
15
+ function action(field, conditions) {
16
+ conditions.forEach((rules) => {
17
+ for (const [selector, rule] of Object.entries(rules)) {
18
+ ['input', 'change'].forEach((event) => {
19
+ const element = document.querySelector(selector);
20
+ if (element) {
21
+ element.addEventListener(event, () => {
22
+ const success = check(conditions);
23
+ showHide(field, success);
24
+ });
25
+ }
26
+ });
27
+ }
28
+ });
29
+ }
30
+
31
+ function showHide(element, success) {
32
+ if (success) {
33
+ element.classList.remove('dependency-show');
34
+ element.classList.add('dependency-show');
35
+ return true;
36
+ } else {
37
+ element.classList.remove('dependency-show');
38
+ return false;
39
+ }
40
+ }
41
+
42
+ function check(conditions) {
43
+ return conditions.every((conditionObj) => {
44
+ const selectors = Object.keys(conditionObj);
45
+ return selectors.every((selector) => {
46
+ const condition = conditionObj[selector];
47
+ return decision(selector, condition);
48
+ });
49
+ });
50
+ }
51
+
52
+ function decision(selector, condition) {
53
+ const type = condition['type'];
54
+ const currentValue = getValue(selector);
55
+
56
+ let checkValue = typeof condition['value'] === 'undefined' ? false : condition['value'];
57
+
58
+ let minValue = typeof condition['min'] === 'undefined' ? false : parseInt(condition['min']);
59
+ let maxValue = typeof condition['max'] === 'undefined' ? false : parseInt(condition['max']);
60
+
61
+ const allowEmpty = typeof condition['empty'] === 'undefined' ? false : condition['empty'];
62
+ const isEmpty = !allowEmpty && currentValue.length < 1;
63
+
64
+ const likeSelector = typeof condition['like'] === 'undefined' ? false : condition['like'];
65
+ const likeSelectorValue = getValue(likeSelector);
66
+
67
+ const regExpPattern = typeof condition['pattern'] === 'undefined' ? false : condition['pattern'];
68
+ const regExpModifier = typeof condition['modifier'] === 'undefined' ? 'gi' : condition['modifier'];
69
+ const sign = typeof condition['sign'] === 'undefined' ? false : condition['sign'];
70
+ const strict = typeof condition['strict'] === 'undefined' ? false : condition['strict'];
71
+
72
+ const emptyTypes = ['empty', 'blank'];
73
+ const notEmptyTypes = ['!empty', 'notEmpty', 'not-empty', 'notempty'];
74
+
75
+ const equalTypes = ['equal', '=', '==', '==='];
76
+ const notEqualTypes = ['!equal', '!=', '!==', '!===', 'notEqual', 'not-equal', 'notequal'];
77
+
78
+ const regularExpressionTypes = ['regexp', 'exp', 'expression', 'match'];
79
+
80
+ // if empty return true
81
+ if (emptyTypes.includes(type)) {
82
+ return currentValue.length < 1;
83
+ }
84
+
85
+ // if not empty return true
86
+ if (notEmptyTypes.includes(type)) {
87
+ return currentValue.length > 0;
88
+ }
89
+
90
+ // if equal return true
91
+ if (equalTypes.includes(type)) {
92
+ if (isEmpty) {
93
+ return false;
94
+ }
95
+
96
+ // Match two selector value/s
97
+ if (likeSelector) {
98
+ if (strict) {
99
+ return likeSelectorValue.every((value) => {
100
+ return currentValue.includes(value);
101
+ });
102
+ } else {
103
+ return likeSelectorValue.some((value) => {
104
+ return currentValue.includes(value);
105
+ });
106
+ }
107
+ }
108
+
109
+ // Match pre-defined value/s
110
+ if (strict) {
111
+ if (checkValue && Array.isArray(checkValue)) {
112
+ return checkValue.every((value) => {
113
+ return currentValue.includes(value);
114
+ });
115
+ }
116
+
117
+ if (checkValue && !Array.isArray(checkValue)) {
118
+ return currentValue.includes(checkValue);
119
+ }
120
+ } else {
121
+ if (checkValue && Array.isArray(checkValue)) {
122
+ return checkValue.some((value) => {
123
+ return currentValue.includes(value);
124
+ });
125
+ }
126
+
127
+ if (checkValue && !Array.isArray(checkValue)) {
128
+ return currentValue.includes(checkValue);
129
+ }
130
+ }
131
+ }
132
+
133
+ // if not equal return true
134
+ if (notEqualTypes.includes(type)) {
135
+ if (isEmpty) {
136
+ return false;
137
+ }
138
+
139
+ // Match two selector value/s
140
+ if (likeSelector) {
141
+ if (strict) {
142
+ return likeSelectorValue.every((value) => {
143
+ return !currentValue.includes(value);
144
+ });
145
+ } else {
146
+ return likeSelectorValue.some((value) => {
147
+ return !currentValue.includes(value);
148
+ });
149
+ }
150
+ }
151
+
152
+ // Match pre-defined value/s
153
+ if (strict) {
154
+ if (checkValue && Array.isArray(checkValue)) {
155
+ return checkValue.every((value) => {
156
+ return !currentValue.includes(value);
157
+ });
158
+ }
159
+
160
+ if (checkValue && !Array.isArray(checkValue)) {
161
+ return !currentValue.includes(checkValue);
162
+ }
163
+ } else {
164
+ if (checkValue && Array.isArray(checkValue)) {
165
+ return checkValue.some((value) => {
166
+ return !currentValue.includes(value);
167
+ });
168
+ }
169
+
170
+ if (checkValue && !Array.isArray(checkValue)) {
171
+ return !currentValue.includes(checkValue);
172
+ }
173
+ }
174
+ }
175
+
176
+ // if regexp match
177
+ if (regularExpressionTypes.includes(type) && regExpPattern) {
178
+ if (isEmpty) {
179
+ return false;
180
+ }
181
+
182
+ const exp = new RegExp(regExpPattern, regExpModifier);
183
+ return currentValue.every((value) => {
184
+ return exp.test(value);
185
+ });
186
+ }
187
+
188
+ // if length
189
+ if ('length' === type) {
190
+ if (isEmpty) {
191
+ return false;
192
+ }
193
+
194
+ if (checkValue && Array.isArray(checkValue)) {
195
+ minValue = parseInt(checkValue[0]);
196
+ maxValue = typeof checkValue[1] === 'undefined' ? false : parseInt(checkValue[1]);
197
+ }
198
+
199
+ if (checkValue && !Array.isArray(checkValue)) {
200
+ minValue = parseInt(checkValue);
201
+ maxValue = false;
202
+ }
203
+
204
+ return currentValue.every((value) => {
205
+ if (!maxValue) {
206
+ return value.length >= minValue;
207
+ }
208
+
209
+ if (!minValue) {
210
+ return value.length <= maxValue;
211
+ }
212
+
213
+ return value.length >= minValue && value.length <= maxValue;
214
+ });
215
+ }
216
+
217
+ // if range
218
+ if ('range' === type) {
219
+ if (isEmpty) {
220
+ return false;
221
+ }
222
+
223
+ if (checkValue && Array.isArray(checkValue)) {
224
+ minValue = parseInt(checkValue[0]);
225
+ maxValue = typeof checkValue[1] === 'undefined' ? false : parseInt(checkValue[1]);
226
+ }
227
+
228
+ return currentValue.every((value) => {
229
+ if (!maxValue) {
230
+ return parseInt(value) > minValue;
231
+ }
232
+
233
+ if (!minValue) {
234
+ return parseInt(value) < maxValue;
235
+ }
236
+
237
+ return parseInt(value) > minValue && parseInt(value) < maxValue;
238
+ });
239
+ }
240
+
241
+ // if compare
242
+ if ('compare' === type && sign && checkValue) {
243
+ if (isEmpty) {
244
+ return false;
245
+ }
246
+
247
+ checkValue = parseInt(checkValue);
248
+
249
+ switch (sign) {
250
+ case '<':
251
+ return currentValue.every((value) => {
252
+ return parseInt(value) < checkValue;
253
+ });
254
+ break;
255
+
256
+ case '<=':
257
+ return currentValue.every((value) => {
258
+ return parseInt(value) <= checkValue;
259
+ });
260
+ break;
261
+
262
+ case '>':
263
+ return currentValue.every((value) => {
264
+ return parseInt(value) > checkValue;
265
+ });
266
+ break;
267
+
268
+ case '>=':
269
+ return currentValue.every((value) => {
270
+ return parseInt(value) >= checkValue;
271
+ });
272
+ break;
273
+
274
+ case '=':
275
+ case '==':
276
+ return currentValue.every((value) => {
277
+ return parseInt(value) === checkValue;
278
+ });
279
+ break;
280
+ }
281
+ }
282
+ }
283
+
284
+ function getValue(selector) {
285
+ const values = [];
286
+ if (selector && document.querySelector(selector)) {
287
+ const inputType = document.querySelector(selector).tagName.toLowerCase();``
288
+
289
+ let currentSelector = selector;
290
+
291
+ if ('crowdin-select' === inputType) {
292
+ currentSelector = `${selector} option:checked`;
293
+ }
294
+
295
+ if ('crowdin-checkbox' === inputType) {
296
+ currentSelector = `${selector}:not(input)`;
297
+ }
298
+
299
+ document.querySelectorAll(`${currentSelector}`).forEach((element) => {
300
+ const value = element.value || element.checked;
301
+ values.push(value);
302
+ });
303
+ }
304
+
305
+ return values.filter((value) => value !== '');
306
+ }