blob: 86e6214e21d3920b6203050d405d156bd6ada50e [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 () {
123 text.select();
124 text.setSelectionRange(0, 99999);
125 document.execCommand("copy");
126 });
127 text.parentNode.insertBefore(a, text.nextSibling);
128 };
129};
130
131
Nils Diewald0e6992a2015-04-14 20:13:52 +0000132define(function () {
Akronc1457bf2015-06-11 19:24:00 +0200133 // Todo: That's double now!
Nils Diewald0e6992a2015-04-14 20:13:52 +0000134 KorAP.API = KorAP.API || {};
Akronc1457bf2015-06-11 19:24:00 +0200135 KorAP.Locale = KorAP.Locale || {};
Nils Diewald0e6992a2015-04-14 20:13:52 +0000136
Akron0b489ad2018-02-02 16:49:32 +0100137 const loc = KorAP.Locale;
Nils Diewald359a72c2015-04-20 17:40:29 +0000138 loc.OR = loc.OR || 'or';
139 loc.AND = loc.AND || 'and';
140
141 // Add new stylesheet object lazily to document
142 KorAP.newStyleSheet = function () {
143 if (KorAP._sheet === undefined) {
Akrondf90c592020-10-20 08:42:50 +0200144 const sElem = document.createElement('style');
Nils Diewald359a72c2015-04-20 17:40:29 +0000145 document.head.appendChild(sElem);
146 KorAP._sheet = sElem.sheet;
147 };
148 return KorAP._sheet;
149 };
150
151
Nils Diewald0e6992a2015-04-14 20:13:52 +0000152 // Default log message
Akronc0a2da82018-07-04 15:27:37 +0200153 KorAP.log = KorAP.log || function (type, msg, src) {
Akrondf90c592020-10-20 08:42:50 +0200154 if (src)
155 msg += ' from ' + src;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000156 console.log(type + ": " + msg);
157 };
158
159 return KorAP;
160});