blob: b8be33b2c46ccda747b50590932001f692e1bdea [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
Akron116eace2021-06-14 18:02:37 +020089/**
90 * Add option to show passwords.
91 */
92function initTogglePwdVisibility () {
93 const el = document.querySelectorAll("input[type=password].show-pwd");
94 for (let x = 0; x < el.length; x++) {
95 const pwd = el[x];
96
97 const a = document.createElement('a');
98 a.classList.add('showPWD');
99 a.addEventListener('click', function () {
100 if (pwd.getAttribute("type") === "password") {
101 pwd.setAttribute("type", "text");
102 a.classList.add('hide');
103 return;
104 };
105 pwd.setAttribute("type", "password");
106 a.classList.remove('hide');
107 });
108 pwd.parentNode.insertBefore(a, pwd.nextSibling);
109 };
110};
111
112
Nils Diewald0e6992a2015-04-14 20:13:52 +0000113define(function () {
Akronc1457bf2015-06-11 19:24:00 +0200114 // Todo: That's double now!
Nils Diewald0e6992a2015-04-14 20:13:52 +0000115 KorAP.API = KorAP.API || {};
Akronc1457bf2015-06-11 19:24:00 +0200116 KorAP.Locale = KorAP.Locale || {};
Nils Diewald0e6992a2015-04-14 20:13:52 +0000117
Akron0b489ad2018-02-02 16:49:32 +0100118 const loc = KorAP.Locale;
Nils Diewald359a72c2015-04-20 17:40:29 +0000119 loc.OR = loc.OR || 'or';
120 loc.AND = loc.AND || 'and';
121
122 // Add new stylesheet object lazily to document
123 KorAP.newStyleSheet = function () {
124 if (KorAP._sheet === undefined) {
Akrondf90c592020-10-20 08:42:50 +0200125 const sElem = document.createElement('style');
Nils Diewald359a72c2015-04-20 17:40:29 +0000126 document.head.appendChild(sElem);
127 KorAP._sheet = sElem.sheet;
128 };
129 return KorAP._sheet;
130 };
131
132
Nils Diewald0e6992a2015-04-14 20:13:52 +0000133 // Default log message
Akronc0a2da82018-07-04 15:27:37 +0200134 KorAP.log = KorAP.log || function (type, msg, src) {
Akrondf90c592020-10-20 08:42:50 +0200135 if (src)
136 msg += ' from ' + src;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000137 console.log(type + ": " + msg);
138 };
139
140 return KorAP;
141});