blob: 42edf44828024ae621f5beb4d2c2855712600bbd [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 */
Akron1cfde272021-06-14 18:32:39 +020092function initTogglePwdVisibility (element) {
93 const el = element.querySelectorAll("input[type=password].show-pwd");
Akron116eace2021-06-14 18:02:37 +020094 for (let x = 0; x < el.length; x++) {
95 const pwd = el[x];
96
97 const a = document.createElement('a');
Akron1cfde272021-06-14 18:32:39 +020098 a.classList.add('show-pwd');
Akron116eace2021-06-14 18:02:37 +020099 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
Akrona9c55802021-06-15 11:41:29 +0200113/**
114 * Add option to copy to clipboard.
115 */
116function initCopyToClipboard (element) {
117 const el = element.querySelectorAll("input.copy-to-clipboard");
118 for (let x = 0; x < el.length; x++) {
119 const text = el[x];
120 const a = document.createElement('a');
121 a.classList.add('copy-to-clipboard');
122 a.addEventListener('click', function () {
Akron131a8282021-06-18 07:57:52 +0200123 let back = false;
124 if (text.getAttribute("type") === 'password') {
125 text.setAttribute("type", "text");
126 back = true;
127 };
Akrona9c55802021-06-15 11:41:29 +0200128 text.select();
129 text.setSelectionRange(0, 99999);
130 document.execCommand("copy");
Akron131a8282021-06-18 07:57:52 +0200131 if (back) {
132 text.setAttribute("type", "password");
133 };
Akrona9c55802021-06-15 11:41:29 +0200134 });
135 text.parentNode.insertBefore(a, text.nextSibling);
136 };
137};
138
139
Nils Diewald0e6992a2015-04-14 20:13:52 +0000140define(function () {
Akronc1457bf2015-06-11 19:24:00 +0200141 // Todo: That's double now!
Nils Diewald0e6992a2015-04-14 20:13:52 +0000142 KorAP.API = KorAP.API || {};
Akronc1457bf2015-06-11 19:24:00 +0200143 KorAP.Locale = KorAP.Locale || {};
Nils Diewald0e6992a2015-04-14 20:13:52 +0000144
Akron0b489ad2018-02-02 16:49:32 +0100145 const loc = KorAP.Locale;
Nils Diewald359a72c2015-04-20 17:40:29 +0000146 loc.OR = loc.OR || 'or';
147 loc.AND = loc.AND || 'and';
148
149 // Add new stylesheet object lazily to document
150 KorAP.newStyleSheet = function () {
151 if (KorAP._sheet === undefined) {
Akrondf90c592020-10-20 08:42:50 +0200152 const sElem = document.createElement('style');
Nils Diewald359a72c2015-04-20 17:40:29 +0000153 document.head.appendChild(sElem);
154 KorAP._sheet = sElem.sheet;
155 };
156 return KorAP._sheet;
157 };
158
159
Nils Diewald0e6992a2015-04-14 20:13:52 +0000160 // Default log message
Akronc0a2da82018-07-04 15:27:37 +0200161 KorAP.log = KorAP.log || function (type, msg, src) {
Akrondf90c592020-10-20 08:42:50 +0200162 if (src)
163 msg += ' from ' + src;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000164 console.log(type + ": " + msg);
165 };
166
167 return KorAP;
168});
Leo Reppdedcf1a2021-08-18 18:57:47 +0200169
170/**
171 * A Method for generating an array of nodes, that are direct descendants of the passed
172 * element node, using a tag tagName as a parameter. Supposed to be used by the specification only.
173 * @param {HTMLNode} element The HTMLNode / element object whose children we are fetching
174 * @param {String} tagName The tag the children are looked for by
175 * @returns An array of children nodes with tag tagName
176 */
177function directElementChildrenByTagName (element, tagName) {
178 const tagElementsCollection=element.getElementsByTagName(tagName);
179 //var tagElements = Array.from(tagElementsCollection);
180 //var tagElements = [...tagElementsCollection];
181 //This one has the best compatability:
182 var tagElements = Array.prototype.slice.call(tagElementsCollection);
183 //filter by actually being direct child node
184 tagElements = tagElements.filter(subElement => subElement.parentNode === element);
185 return tagElements;
186};
187
188/**
189 * A Method for generating an array of nodes, that are direct descendants of the passed
190 * element node, using a class className as a parameter. Supposed to be used by the specification only.
191 * @param {HTMLNode} element The HTMLNode / element object whose children we are fetching
192 * @param {String} className The class the children are looked for by
193 * @returns An array of children nodes with class className
194 */
195 function directElementChildrenByClassName (element, className) {
196 const classElementsCollection=element.getElementsByTagName(className);
197 //var classElements = Array.from(classElementsCollection);
198 //var classElements = [...classElementsCollection];
199 //This one has the best compatability:
200 var classElements = Array.prototype.slice.call(classElementsCollection);
201 //filter by actually being direct child node
202 classElements = classElements.filter(subElement => subElement.parentNode === element);
203 return classElements;
204};