blob: 0fea33fb94dedb47056e6a919dd6ae592950533e [file] [log] [blame]
Akrone51eaa32020-11-10 09:35:53 +01001"use strict";
2
Akronc1457bf2015-06-11 19:24:00 +02003window.KorAP = window.KorAP || {};
4
Nils Diewald0e6992a2015-04-14 20:13:52 +00005// Don't let events bubble up
6if (Event.halt === undefined) {
7 // Don't let events bubble up
8 Event.prototype.halt = function () {
9 this.stopPropagation();
10 this.preventDefault();
11 };
12};
13
Akron0c4cd222019-07-19 16:33:34 +020014const _quoteRE = new RegExp("([\"\\\\])", 'g');
Nils Diewald7c8ced22015-04-15 19:21:00 +000015String.prototype.quote = function () {
Akron0c4cd222019-07-19 16:33:34 +020016 return '"' + this.replace(_quoteRE, '\\$1') + '"';
Nils Diewald7c8ced22015-04-15 19:21:00 +000017};
18
Akron0c4cd222019-07-19 16:33:34 +020019const _escapeRE = new RegExp("([\/\\\\])", 'g');
Akron8778f5d2017-06-30 21:25:55 +020020String.prototype.escapeRegex = function () {
21 return this.replace(_escapeRE, '\\$1');
22};
23
Akron0c4cd222019-07-19 16:33:34 +020024const _slug1RE = new RegExp("[^-a-zA-Z0-9_\\s]+", 'g');
25const _slug2RE = new RegExp("[-\\s]+", 'g');
26String.prototype.slugify = function () {
27 return this.toLowerCase().replace(_slug1RE, '').replace(_slug2RE, '-');
28};
29
Nils Diewald0e6992a2015-04-14 20:13:52 +000030// Add toggleClass method similar to jquery
31HTMLElement.prototype.toggleClass = function (c1, c2) {
Akrondf90c592020-10-20 08:42:50 +020032 const cl = this.classList;
Nils Diewald0e6992a2015-04-14 20:13:52 +000033 if (cl.contains(c1)) {
34 cl.add(c2);
35 cl.remove(c1);
36 }
37 else {
38 cl.remove(c2);
39 cl.add(c1);
40 };
41};
42
Akron0b489ad2018-02-02 16:49:32 +010043// Append element by tag name
Akron151bc872018-02-02 14:04:15 +010044HTMLElement.prototype.addE = function (tag) {
45 return this.appendChild(document.createElement(tag));
46};
47
Akron0b489ad2018-02-02 16:49:32 +010048// Append text node
Akron151bc872018-02-02 14:04:15 +010049HTMLElement.prototype.addT = function (text) {
50 return this.appendChild(document.createTextNode(text));
51};
52
53
Nils Diewald0e6992a2015-04-14 20:13:52 +000054// Utility for removing all children of a node
55function _removeChildren (node) {
56 // Remove everything underneath
57 while (node.firstChild)
58 node.removeChild(node.firstChild);
59};
60
Akron151bc872018-02-02 14:04:15 +010061
Akron6a535d42015-08-26 20:16:58 +020062// Utility to get either the charCode
63// or the keyCode of an event
64function _codeFromEvent (e) {
65 if ((e.charCode) && (e.keyCode==0))
66 return e.charCode
67 return e.keyCode;
68};
69
Akrona6c32b92018-07-02 18:39:42 +020070function _dec2hex (dec) {
71 return ('0' + dec.toString(16)).substr(-2)
72};
73
74
75/**
76 * Create random identifiers
77 */
78/*
79 * code based on
80 * https://stackoverflow.com/questions/1349404/generate-random-string-characters-in-javascript#8084248
81 */
82function randomID (len) {
Akrondf90c592020-10-20 08:42:50 +020083 const arr = new Uint8Array((len || 40) / 2)
Akrona6c32b92018-07-02 18:39:42 +020084 window.crypto.getRandomValues(arr)
85 return Array.from(arr, _dec2hex).join('')
86};
87
Nils Diewald0e6992a2015-04-14 20:13:52 +000088
89define(function () {
Akronc1457bf2015-06-11 19:24:00 +020090 // Todo: That's double now!
Nils Diewald0e6992a2015-04-14 20:13:52 +000091 KorAP.API = KorAP.API || {};
Akronc1457bf2015-06-11 19:24:00 +020092 KorAP.Locale = KorAP.Locale || {};
Nils Diewald0e6992a2015-04-14 20:13:52 +000093
Akron0b489ad2018-02-02 16:49:32 +010094 const loc = KorAP.Locale;
Nils Diewald359a72c2015-04-20 17:40:29 +000095 loc.OR = loc.OR || 'or';
96 loc.AND = loc.AND || 'and';
97
98 // Add new stylesheet object lazily to document
99 KorAP.newStyleSheet = function () {
100 if (KorAP._sheet === undefined) {
Akrondf90c592020-10-20 08:42:50 +0200101 const sElem = document.createElement('style');
Nils Diewald359a72c2015-04-20 17:40:29 +0000102 document.head.appendChild(sElem);
103 KorAP._sheet = sElem.sheet;
104 };
105 return KorAP._sheet;
106 };
107
108
Nils Diewald0e6992a2015-04-14 20:13:52 +0000109 // Default log message
Akronc0a2da82018-07-04 15:27:37 +0200110 KorAP.log = KorAP.log || function (type, msg, src) {
Akrondf90c592020-10-20 08:42:50 +0200111 if (src)
112 msg += ' from ' + src;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000113 console.log(type + ": " + msg);
114 };
115
116 return KorAP;
117});