blob: cb60c2a02a17131026e422b5a07aff792851cd98 [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
Nils Diewald7c8ced22015-04-15 19:21:00 +000012var _quoteRE = new RegExp("([\"\\\\])", 'g');
13String.prototype.quote = function () {
14 return this.replace(_quoteRE, '\\$1');
15};
16
Akron8778f5d2017-06-30 21:25:55 +020017var _escapeRE = new RegExp("([\/\\\\])", 'g');
18String.prototype.escapeRegex = function () {
19 return this.replace(_escapeRE, '\\$1');
20};
21
Nils Diewald0e6992a2015-04-14 20:13:52 +000022// Add toggleClass method similar to jquery
23HTMLElement.prototype.toggleClass = function (c1, c2) {
24 var cl = this.classList;
25 if (cl.contains(c1)) {
26 cl.add(c2);
27 cl.remove(c1);
28 }
29 else {
30 cl.remove(c2);
31 cl.add(c1);
32 };
33};
34
Akron0b489ad2018-02-02 16:49:32 +010035// Append element by tag name
Akron151bc872018-02-02 14:04:15 +010036HTMLElement.prototype.addE = function (tag) {
37 return this.appendChild(document.createElement(tag));
38};
39
Akron0b489ad2018-02-02 16:49:32 +010040// Append text node
Akron151bc872018-02-02 14:04:15 +010041HTMLElement.prototype.addT = function (text) {
42 return this.appendChild(document.createTextNode(text));
43};
44
45
Nils Diewald0e6992a2015-04-14 20:13:52 +000046// Utility for removing all children of a node
47function _removeChildren (node) {
48 // Remove everything underneath
49 while (node.firstChild)
50 node.removeChild(node.firstChild);
51};
52
Akron151bc872018-02-02 14:04:15 +010053
Akron6a535d42015-08-26 20:16:58 +020054// Utility to get either the charCode
55// or the keyCode of an event
56function _codeFromEvent (e) {
57 if ((e.charCode) && (e.keyCode==0))
58 return e.charCode
59 return e.keyCode;
60};
61
Akrona6c32b92018-07-02 18:39:42 +020062function _dec2hex (dec) {
63 return ('0' + dec.toString(16)).substr(-2)
64};
65
66
67/**
68 * Create random identifiers
69 */
70/*
71 * code based on
72 * https://stackoverflow.com/questions/1349404/generate-random-string-characters-in-javascript#8084248
73 */
74function randomID (len) {
75 var arr = new Uint8Array((len || 40) / 2)
76 window.crypto.getRandomValues(arr)
77 return Array.from(arr, _dec2hex).join('')
78};
79
Nils Diewald0e6992a2015-04-14 20:13:52 +000080
81define(function () {
Akronc1457bf2015-06-11 19:24:00 +020082 // Todo: That's double now!
Nils Diewald0e6992a2015-04-14 20:13:52 +000083 KorAP.API = KorAP.API || {};
Akronc1457bf2015-06-11 19:24:00 +020084 KorAP.Locale = KorAP.Locale || {};
Nils Diewald0e6992a2015-04-14 20:13:52 +000085
Akron0b489ad2018-02-02 16:49:32 +010086 const loc = KorAP.Locale;
Nils Diewald359a72c2015-04-20 17:40:29 +000087 loc.OR = loc.OR || 'or';
88 loc.AND = loc.AND || 'and';
89
90 // Add new stylesheet object lazily to document
91 KorAP.newStyleSheet = function () {
92 if (KorAP._sheet === undefined) {
93 var sElem = document.createElement('style');
94 document.head.appendChild(sElem);
95 KorAP._sheet = sElem.sheet;
96 };
97 return KorAP._sheet;
98 };
99
100
Nils Diewald0e6992a2015-04-14 20:13:52 +0000101 // Default log message
Akronc0a2da82018-07-04 15:27:37 +0200102 KorAP.log = KorAP.log || function (type, msg, src) {
103 if (src) msg += ' from ' + src;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000104 console.log(type + ": " + msg);
105 };
106
107 return KorAP;
108});