-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
354 lines (306 loc) · 12.3 KB
/
utils.js
File metadata and controls
354 lines (306 loc) · 12.3 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/**
* This class holds some static utility methods.
*/
class Utils {
constructor() {
}
/**
* Returns GMT date and time in the format: "Y-m-d H:i:s". Example: "2023-10-25 11:03:42"
*/
static get_GMT_datetime() {
var result = "";
result = new Date().toISOString();
result = result.substring(0, result.length-5);
result = result.replaceAll("T", " ");
result = result.replaceAll("Z", "");
return result;
}
/**
* Searches for a plain or a wildcard string (rule) into some text.
* If the rule contains asterisks then it executes a wildcard search. If not, then it executes a simple search.
* Searching is case-insensitive.
* if rule is an empty string then the function returns true.
* @param str (String): some text
* @param rule (String): a rule with wildcard asterisks like soft* or *soft* or *so*ft*
* @return true if the string contains characters that match the rule.
*/
static WildcardSearch(str, rule) {
var result = false;
try {
if( str == null ) {
result = false;
} else if( rule.length == 0 ) {
result = true;
} else if( rule.includes("*") ) {
var escapeRegex = (str) => str.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
rule = rule.split("*").map(escapeRegex).join(".*");
rule = "^" + rule + "$";
var regex = new RegExp(rule, "im"); // i=case-insensitive m=multiline
result = regex.test(str);
} else {
result = str.toLowerCase().includes( rule.toLowerCase() ); // result = str.trim().toLowerCase().equals( rule.trim().toLowerCase() );
}
} catch(ex) {}
return result;
}
/**
* @param str1 (String): some text
* @param str2 (String): some text
* @return true if the arguments are the same. Case is ignored.
*/
static ExactSearch(str1, str2) {
var result = false;
try {
if( str1 == null || str2 == null) {
result = false;
} else {
result = str1.toLowerCase().localeCompare( str2.toLowerCase() ) == 0;
}
} catch(ex) {}
return result;
}
/**
* Adds a row to an html table. Used for the Reference-Links table.
* @param (String) TableName: the css id of the table
*/
static AddRowToTable( TableName ) {
var table = document.getElementById( TableName );
var row = table.insertRow(-1);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1); //cell2.innerHTML = "new";
}
/**
* Gets a color representation as an rgb string and returns the same color with transparency as an rgba string
* For example: "rgb(122, 243, 98)" --> "rgba(122, 243, 98, 0.3)"
* @param rgb_string: an rgb color representation. Example "rgb(122, 243, 98)"
* @param alpha_value: the transparency value. Zero is fully transparent, One is opaque.
*/
static rgb_to_rgba( rgb_string, alpha_value ) {
return rgb_string.replace(")", ", "+alpha_value+")").replace("rgb","rgba");
}
/**
* Gets a name and returns the corresponding alias.
* The name can be an item field, a category title etc, which we would like to be displayed to the user with a different name than it is stored in the system.
* @param (String) Name: the name to be translated to an alias.
* @return the alias of the given name.
*/
static NameToAlias( Name ) {
var result = Name;
if( Name.localeCompare("BoneSpecies") == 0 ) {
result = "Preliminary Bone Species";
} else if( Name.localeCompare("RelationBelongsToUUID") == 0 ) {
result = "Locus";
} else if( Name.localeCompare("RelationIncludesUUID") == 0 ) {
result = "Includes";
} else if( Name.localeCompare("RelationIsAboveUUID") == 0 ) {
result = "Is Above of";
} else if( Name.localeCompare("RelationIsBelowUUID") == 0 ) {
result = "Is Below of";
} else if( Name.localeCompare("CoverageArea") == 0 ) {
result = "Area (m<sup>2</sup>)";
} else if( Name.localeCompare("Square") == 0 ) {
result = "Square/Space";
} else if( Name.localeCompare("Diameter") == 0 ) {
result = "Diameter (mm)";
} else if( Name.localeCompare("CoinWeight") == 0 ) {
result = "Weight (g.)";
} else if( Name.localeCompare("Axis") == 0 ) {
result = "Axis (hours)";
} else if( Name.localeCompare("IssueAuthority") == 0 ) {
result = "Issuing Authority";
} else if( Name.localeCompare("CoinInscription") == 0 ) {
result = "Inscription obv/rev";
}
return result;
}
/**
* Gets an alias and returns the corresponding original name.
* The name can be an item field, a category title etc, which we would like to be displayed to the user with a different name than it is stored in the system.
* @param (String) Alias: the alias to be translated to a name.
* @return the name which corresponds to the given alias.
*/
static AliasToName( Alias ) {
var result = Alias;
return result;
}
/**
* Gets a field name and returns a description of it. The description can be displayed as hover text over the field name
* @param (String) Name: a field name.
* @return the description of the field name.
*/
static getFieldDescription( FieldName ) {
var result = FieldName;
if( FieldName.localeCompare("Identifier") == 0 ) {
result = "The unique inventory number for an object. During item creation, the application proposes the next number once the letter that corresponds to the type of find is entered: A=architecture, B=bronze, C=coin, G=glass, I=inscription, IL=iron and lead, J=jewelry, L=lamp, O=organic, P=pottery, ST=stone, T=terracotta. There is no space between the letter and the number.";
} else if( FieldName.localeCompare("Title") == 0 ) {
result = "The brief name or description of the object, e.g., Thasian amphora stamp.";
} else if( FieldName.localeCompare("RelationBelongsToUUID") == 0 ) {
result = "The locus in which the object was found. During item creation, the locus can be selected from the pull-down menu.";
} else if( FieldName.localeCompare("Category") == 0 ) {
result = "This field describes an item more specifically than its Type.";
} else if( FieldName.localeCompare("Subcategory") == 0 ) {
result = "Subcategory can aid you in organizing your information. For example, you might add \"Attic\" or \"PRS\" for pottery.";
} else if( FieldName.localeCompare("ArtifactDate") == 0 ) {
result = "The date you give the object, as it would appear in a catalogue entry.";
} else if( FieldName.localeCompare("Dimensions") == 0 ) {
result = "in meters";
} else if( FieldName.localeCompare("Description") == 0 ) {
result = "Your description of the object. Include the state of preservation.";
} else if( FieldName.localeCompare("Comparanda") == 0 ) {
result = "Cite comparanda as needed.";
} else if( FieldName.localeCompare("Additional bibliography") == 0 ) {
result = "Provide any additional bibliography as needed. Reference Links menu may help.";
} else if( FieldName.localeCompare("Notes") == 0 ) {
result = "This section will not be public but is just for your use.";
} else if( FieldName.localeCompare("CoinInscription") == 0 ) {
result = "Enter the inscription on the the coin's obverse and reverse sides separated by a slash.";
} else if( FieldName.localeCompare("CoverageEarliest") == 0 ) {
result = "insert one number, adding \"-\" for BC. -300 = 300 BC. 300 = AD 300";
} else if( FieldName.localeCompare("CoverageLatest") == 0 ) {
result = "insert one number, adding \"-\" for BC. -300 = 300 BC. 300 = AD 300";
}
return result;
}
/**
* @return true if the user's computer is a mobile device. It may not work successfully for all cases.
*/
static Am_I_running_on_mobile_device() {
var result = false;
if(running_on_mobile_device == null) {
try {
var ua = navigator.userAgent.toLowerCase();
if ( ua.includes("android") || ua.includes("iphone") ) {
result = true;
}
} catch( ex ) {}
} else {
result = running_on_mobile_device;
}
return result;
}
/**
* @param s (String): a string
* @return true if the argument contains an integer, positive or negative
*/
static ContainsInteger( s ) {
if( s == null ) return false;
var result = true;
if( s.length == 0 ) result = false;
for( let i=0; i<s.length; i++ ) {
var c = s.charAt(i);
if( c!='0' && c!='1' && c!='2' && c!='3' && c!='4' && c!='5' && c!='6' && c!='7' && c!='8' && c!='9' && c!='-') {
result = false;
break;
}
}
return result;
}
/**
* @param s (String): a string
* @return true if the argument contains a float, positive or negative
*/
static ContainsFloat( s ) {
if( s == null ) return false;
var result = true;
if( s.length == 0 ) result = false;
for( let i=0; i<s.length; i++ ) {
var c = s.charAt(i);
if( c!='0' && c!='1' && c!='2' && c!='3' && c!='4' && c!='5' && c!='6' && c!='7' && c!='8' && c!='9' && c!='-' && c!='.') {
result = false;
break;
}
}
return result;
}
/**
*
* Source: https://www.w3schools.com/howto/howto_js_sort_table.asp
* @arg sorting_direction (String) can take two possible values: "ascending" or "descending"
*/
static SortTable( table_html_id, column_number_to_sort_by, sorting_direction) {
var table, rows, switching, i, x, y, shouldSwitch, switchcount = 0;
table = document.getElementById( table_html_id );
switching = true;
//Set the sorting direction to ascending:
sorting_direction = "asc";
/*Make a loop that will continue until no switching has been done:*/
while (switching) {
//start by saying: no switching is done:
switching = false;
rows = table.rows;
/*Loop through all table rows (except the
first, which contains table headers):*/
for (i = 1; i < (rows.length - 1); i++) {
//start by saying there should be no switching:
shouldSwitch = false;
/*Get the two elements you want to compare,
one from current row and one from the next:*/
x = rows[i].getElementsByTagName("TD")[column_number_to_sort_by];
y = rows[i + 1].getElementsByTagName("TD")[column_number_to_sort_by];
/*check if the two rows should switch place,
based on the direction, asc or desc:*/
if (sorting_direction == "asc") {
if (x.innerHTML.toLowerCase() > y.innerHTML.toLowerCase()) {
//if so, mark as a switch and break the loop:
shouldSwitch= true;
break;
}
} else if (sorting_direction == "desc") {
if (x.innerHTML.toLowerCase() < y.innerHTML.toLowerCase()) {
//if so, mark as a switch and break the loop:
shouldSwitch = true;
break;
}
}
}
if (shouldSwitch) {
/*If a switch has been marked, make the switch
and mark that a switch has been done:*/
rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
switching = true;
//Each time a switch is done, increase this count by 1:
switchcount ++;
} else {
/*If no switching has been done AND the direction is "asc",
set the direction to "desc" and run the while loop again.*/
if (switchcount == 0 && sorting_direction == "asc") {
sorting_direction = "desc";
switching = true;
}
}
}
}
/**
* Removes all elements having a certain value from an Array
* @arg theArray the array which will be processed
* @arg theValue the element's value to be removed. All occurences inside the array will be removed
* @return the same array as given in the argument without the value to be removed
*/
static removeElementsFromArrayByValue( theArray, theValue ) {
var result = theArray.concat();
var index = result.indexOf( theValue );
while( index >= 0 ) {
theArray.splice(index, 1);
index = result.indexOf( theValue );
}
return result;
}
/**
* Removes all duplicate values from the array
* @arg theArray the array which will be processed
* @return the same array as given in the argument without any duplicate values
*/
static RemoveDuplicatesFromArray( theArray ) {
var result = theArray.concat();
for(var i=0; i<result.length; ++i) {
for(var j=i+1; j<result.length; ++j) {
if(result[i] === result[j])
result.splice(j--, 1);
}
}
return result;
}
static convert_JSON_keys_to_lowercase( json_object ) {
return Object.fromEntries( Object.entries(json_object).map(([key, value]) => [key.toLowerCase(), value]) );
}
}