blob: 8e40c12795896169635bf56f94b78a937ca52cf6 [file] [log] [blame]
Akronc1457bf2015-06-11 19:24:00 +02001window.KorAP = window.KorAP || {};
2
Nils Diewald0e6992a2015-04-14 20:13:52 +00003// Don't let events bubble up
4if (Event.halt === undefined) {
5 // Don't let events bubble up
6 Event.prototype.halt = function () {
7 this.stopPropagation();
8 this.preventDefault();
9 };
10};
11
Akron0c4cd222019-07-19 16:33:34 +020012const _quoteRE = new RegExp("([\"\\\\])", 'g');
Nils Diewald7c8ced22015-04-15 19:21:00 +000013String.prototype.quote = function () {
Akron0c4cd222019-07-19 16:33:34 +020014 return '"' + this.replace(_quoteRE, '\\$1') + '"';
Nils Diewald7c8ced22015-04-15 19:21:00 +000015};
16
Akron0c4cd222019-07-19 16:33:34 +020017const _escapeRE = new RegExp("([\/\\\\])", 'g');
Akron8778f5d2017-06-30 21:25:55 +020018String.prototype.escapeRegex = function () {
19 return this.replace(_escapeRE, '\\$1');
20};
21
Akron0c4cd222019-07-19 16:33:34 +020022const _slug1RE = new RegExp("[^-a-zA-Z0-9_\\s]+", 'g');
23const _slug2RE = new RegExp("[-\\s]+", 'g');
24String.prototype.slugify = function () {
25 return this.toLowerCase().replace(_slug1RE, '').replace(_slug2RE, '-');
26};
27
Nils Diewald0e6992a2015-04-14 20:13:52 +000028// Add toggleClass method similar to jquery
29HTMLElement.prototype.toggleClass = function (c1, c2) {
Akrondf90c592020-10-20 08:42:50 +020030 const cl = this.classList;
Nils Diewald0e6992a2015-04-14 20:13:52 +000031 if (cl.contains(c1)) {
32 cl.add(c2);
33 cl.remove(c1);
34 }
35 else {
36 cl.remove(c2);
37 cl.add(c1);
38 };
39};
40
Akron0b489ad2018-02-02 16:49:32 +010041// Append element by tag name
Akron151bc872018-02-02 14:04:15 +010042HTMLElement.prototype.addE = function (tag) {
43 return this.appendChild(document.createElement(tag));
44};
45
Akron0b489ad2018-02-02 16:49:32 +010046// Append text node
Akron151bc872018-02-02 14:04:15 +010047HTMLElement.prototype.addT = function (text) {
48 return this.appendChild(document.createTextNode(text));
49};
50
51
Nils Diewald0e6992a2015-04-14 20:13:52 +000052// Utility for removing all children of a node
53function _removeChildren (node) {
54 // Remove everything underneath
55 while (node.firstChild)
56 node.removeChild(node.firstChild);
57};
58
Akron151bc872018-02-02 14:04:15 +010059
Akron6a535d42015-08-26 20:16:58 +020060// Utility to get either the charCode
61// or the keyCode of an event
62function _codeFromEvent (e) {
63 if ((e.charCode) && (e.keyCode==0))
64 return e.charCode
65 return e.keyCode;
66};
67
Akrona6c32b92018-07-02 18:39:42 +020068function _dec2hex (dec) {
69 return ('0' + dec.toString(16)).substr(-2)
70};
71
72
73/**
74 * Create random identifiers
75 */
76/*
77 * code based on
78 * https://stackoverflow.com/questions/1349404/generate-random-string-characters-in-javascript#8084248
79 */
80function randomID (len) {
Akrondf90c592020-10-20 08:42:50 +020081 const arr = new Uint8Array((len || 40) / 2)
Akrona6c32b92018-07-02 18:39:42 +020082 window.crypto.getRandomValues(arr)
83 return Array.from(arr, _dec2hex).join('')
84};
85
Nils Diewald0e6992a2015-04-14 20:13:52 +000086
87define(function () {
Akronc1457bf2015-06-11 19:24:00 +020088 // Todo: That's double now!
Nils Diewald0e6992a2015-04-14 20:13:52 +000089 KorAP.API = KorAP.API || {};
Akronc1457bf2015-06-11 19:24:00 +020090 KorAP.Locale = KorAP.Locale || {};
Nils Diewald0e6992a2015-04-14 20:13:52 +000091
Akron0b489ad2018-02-02 16:49:32 +010092 const loc = KorAP.Locale;
Nils Diewald359a72c2015-04-20 17:40:29 +000093 loc.OR = loc.OR || 'or';
94 loc.AND = loc.AND || 'and';
95
96 // Add new stylesheet object lazily to document
97 KorAP.newStyleSheet = function () {
98 if (KorAP._sheet === undefined) {
Akrondf90c592020-10-20 08:42:50 +020099 const sElem = document.createElement('style');
Nils Diewald359a72c2015-04-20 17:40:29 +0000100 document.head.appendChild(sElem);
101 KorAP._sheet = sElem.sheet;
102 };
103 return KorAP._sheet;
104 };
105
106
Nils Diewald0e6992a2015-04-14 20:13:52 +0000107 // Default log message
Akronc0a2da82018-07-04 15:27:37 +0200108 KorAP.log = KorAP.log || function (type, msg, src) {
Akrondf90c592020-10-20 08:42:50 +0200109 if (src)
110 msg += ' from ' + src;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000111 console.log(type + ": " + msg);
112 };
113
114 return KorAP;
115});