Akron | e51eaa3 | 2020-11-10 09:35:53 +0100 | [diff] [blame] | 1 | "use strict"; |
| 2 | |
Akron | c1457bf | 2015-06-11 19:24:00 +0200 | [diff] [blame] | 3 | window.KorAP = window.KorAP || {}; |
| 4 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 5 | // Don't let events bubble up |
| 6 | if (Event.halt === undefined) { |
| 7 | // Don't let events bubble up |
| 8 | Event.prototype.halt = function () { |
| 9 | this.stopPropagation(); |
| 10 | this.preventDefault(); |
| 11 | }; |
| 12 | }; |
| 13 | |
Akron | 0c4cd22 | 2019-07-19 16:33:34 +0200 | [diff] [blame] | 14 | const _quoteRE = new RegExp("([\"\\\\])", 'g'); |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 15 | String.prototype.quote = function () { |
Akron | 0c4cd22 | 2019-07-19 16:33:34 +0200 | [diff] [blame] | 16 | return '"' + this.replace(_quoteRE, '\\$1') + '"'; |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 17 | }; |
| 18 | |
Akron | 0c4cd22 | 2019-07-19 16:33:34 +0200 | [diff] [blame] | 19 | const _escapeRE = new RegExp("([\/\\\\])", 'g'); |
Akron | 8778f5d | 2017-06-30 21:25:55 +0200 | [diff] [blame] | 20 | String.prototype.escapeRegex = function () { |
| 21 | return this.replace(_escapeRE, '\\$1'); |
| 22 | }; |
| 23 | |
Akron | 0c4cd22 | 2019-07-19 16:33:34 +0200 | [diff] [blame] | 24 | const _slug1RE = new RegExp("[^-a-zA-Z0-9_\\s]+", 'g'); |
| 25 | const _slug2RE = new RegExp("[-\\s]+", 'g'); |
| 26 | String.prototype.slugify = function () { |
| 27 | return this.toLowerCase().replace(_slug1RE, '').replace(_slug2RE, '-'); |
| 28 | }; |
| 29 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 30 | // Add toggleClass method similar to jquery |
| 31 | HTMLElement.prototype.toggleClass = function (c1, c2) { |
Akron | df90c59 | 2020-10-20 08:42:50 +0200 | [diff] [blame] | 32 | const cl = this.classList; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 33 | 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 | |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 43 | // Append element by tag name |
Akron | 151bc87 | 2018-02-02 14:04:15 +0100 | [diff] [blame] | 44 | HTMLElement.prototype.addE = function (tag) { |
| 45 | return this.appendChild(document.createElement(tag)); |
| 46 | }; |
| 47 | |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 48 | // Append text node |
Akron | 151bc87 | 2018-02-02 14:04:15 +0100 | [diff] [blame] | 49 | HTMLElement.prototype.addT = function (text) { |
| 50 | return this.appendChild(document.createTextNode(text)); |
| 51 | }; |
| 52 | |
| 53 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 54 | // Utility for removing all children of a node |
| 55 | function _removeChildren (node) { |
| 56 | // Remove everything underneath |
| 57 | while (node.firstChild) |
| 58 | node.removeChild(node.firstChild); |
| 59 | }; |
| 60 | |
Akron | 151bc87 | 2018-02-02 14:04:15 +0100 | [diff] [blame] | 61 | |
Akron | 6a535d4 | 2015-08-26 20:16:58 +0200 | [diff] [blame] | 62 | // Utility to get either the charCode |
| 63 | // or the keyCode of an event |
| 64 | function _codeFromEvent (e) { |
| 65 | if ((e.charCode) && (e.keyCode==0)) |
| 66 | return e.charCode |
| 67 | return e.keyCode; |
| 68 | }; |
| 69 | |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 70 | function _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 | */ |
| 82 | function randomID (len) { |
Akron | df90c59 | 2020-10-20 08:42:50 +0200 | [diff] [blame] | 83 | const arr = new Uint8Array((len || 40) / 2) |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 84 | window.crypto.getRandomValues(arr) |
| 85 | return Array.from(arr, _dec2hex).join('') |
| 86 | }; |
| 87 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 88 | |
Akron | 116eace | 2021-06-14 18:02:37 +0200 | [diff] [blame] | 89 | /** |
| 90 | * Add option to show passwords. |
| 91 | */ |
Akron | 1cfde27 | 2021-06-14 18:32:39 +0200 | [diff] [blame] | 92 | function initTogglePwdVisibility (element) { |
| 93 | const el = element.querySelectorAll("input[type=password].show-pwd"); |
Akron | 116eace | 2021-06-14 18:02:37 +0200 | [diff] [blame] | 94 | for (let x = 0; x < el.length; x++) { |
| 95 | const pwd = el[x]; |
| 96 | |
| 97 | const a = document.createElement('a'); |
Akron | 1cfde27 | 2021-06-14 18:32:39 +0200 | [diff] [blame] | 98 | a.classList.add('show-pwd'); |
Akron | 116eace | 2021-06-14 18:02:37 +0200 | [diff] [blame] | 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 | |
Akron | a9c5580 | 2021-06-15 11:41:29 +0200 | [diff] [blame] | 113 | /** |
| 114 | * Add option to copy to clipboard. |
| 115 | */ |
| 116 | function 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 () { |
Akron | 131a828 | 2021-06-18 07:57:52 +0200 | [diff] [blame^] | 123 | let back = false; |
| 124 | if (text.getAttribute("type") === 'password') { |
| 125 | text.setAttribute("type", "text"); |
| 126 | back = true; |
| 127 | }; |
Akron | a9c5580 | 2021-06-15 11:41:29 +0200 | [diff] [blame] | 128 | text.select(); |
| 129 | text.setSelectionRange(0, 99999); |
| 130 | document.execCommand("copy"); |
Akron | 131a828 | 2021-06-18 07:57:52 +0200 | [diff] [blame^] | 131 | if (back) { |
| 132 | text.setAttribute("type", "password"); |
| 133 | }; |
Akron | a9c5580 | 2021-06-15 11:41:29 +0200 | [diff] [blame] | 134 | }); |
| 135 | text.parentNode.insertBefore(a, text.nextSibling); |
| 136 | }; |
| 137 | }; |
| 138 | |
| 139 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 140 | define(function () { |
Akron | c1457bf | 2015-06-11 19:24:00 +0200 | [diff] [blame] | 141 | // Todo: That's double now! |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 142 | KorAP.API = KorAP.API || {}; |
Akron | c1457bf | 2015-06-11 19:24:00 +0200 | [diff] [blame] | 143 | KorAP.Locale = KorAP.Locale || {}; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 144 | |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 145 | const loc = KorAP.Locale; |
Nils Diewald | 359a72c | 2015-04-20 17:40:29 +0000 | [diff] [blame] | 146 | 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) { |
Akron | df90c59 | 2020-10-20 08:42:50 +0200 | [diff] [blame] | 152 | const sElem = document.createElement('style'); |
Nils Diewald | 359a72c | 2015-04-20 17:40:29 +0000 | [diff] [blame] | 153 | document.head.appendChild(sElem); |
| 154 | KorAP._sheet = sElem.sheet; |
| 155 | }; |
| 156 | return KorAP._sheet; |
| 157 | }; |
| 158 | |
| 159 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 160 | // Default log message |
Akron | c0a2da8 | 2018-07-04 15:27:37 +0200 | [diff] [blame] | 161 | KorAP.log = KorAP.log || function (type, msg, src) { |
Akron | df90c59 | 2020-10-20 08:42:50 +0200 | [diff] [blame] | 162 | if (src) |
| 163 | msg += ' from ' + src; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 164 | console.log(type + ": " + msg); |
| 165 | }; |
| 166 | |
| 167 | return KorAP; |
| 168 | }); |