Akron | c1457bf | 2015-06-11 19:24:00 +0200 | [diff] [blame] | 1 | window.KorAP = window.KorAP || {}; |
| 2 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 3 | // Don't let events bubble up |
| 4 | if (Event.halt === undefined) { |
| 5 | // Don't let events bubble up |
| 6 | Event.prototype.halt = function () { |
| 7 | this.stopPropagation(); |
| 8 | this.preventDefault(); |
| 9 | }; |
| 10 | }; |
| 11 | |
Akron | 0c4cd22 | 2019-07-19 16:33:34 +0200 | [diff] [blame] | 12 | const _quoteRE = new RegExp("([\"\\\\])", 'g'); |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 13 | String.prototype.quote = function () { |
Akron | 0c4cd22 | 2019-07-19 16:33:34 +0200 | [diff] [blame] | 14 | return '"' + this.replace(_quoteRE, '\\$1') + '"'; |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 15 | }; |
| 16 | |
Akron | 0c4cd22 | 2019-07-19 16:33:34 +0200 | [diff] [blame] | 17 | const _escapeRE = new RegExp("([\/\\\\])", 'g'); |
Akron | 8778f5d | 2017-06-30 21:25:55 +0200 | [diff] [blame] | 18 | String.prototype.escapeRegex = function () { |
| 19 | return this.replace(_escapeRE, '\\$1'); |
| 20 | }; |
| 21 | |
Akron | 0c4cd22 | 2019-07-19 16:33:34 +0200 | [diff] [blame] | 22 | const _slug1RE = new RegExp("[^-a-zA-Z0-9_\\s]+", 'g'); |
| 23 | const _slug2RE = new RegExp("[-\\s]+", 'g'); |
| 24 | String.prototype.slugify = function () { |
| 25 | return this.toLowerCase().replace(_slug1RE, '').replace(_slug2RE, '-'); |
| 26 | }; |
| 27 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 28 | // Add toggleClass method similar to jquery |
| 29 | HTMLElement.prototype.toggleClass = function (c1, c2) { |
Akron | df90c59 | 2020-10-20 08:42:50 +0200 | [diff] [blame] | 30 | const cl = this.classList; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 31 | if (cl.contains(c1)) { |
| 32 | cl.add(c2); |
| 33 | cl.remove(c1); |
| 34 | } |
| 35 | else { |
| 36 | cl.remove(c2); |
| 37 | cl.add(c1); |
| 38 | }; |
| 39 | }; |
| 40 | |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 41 | // Append element by tag name |
Akron | 151bc87 | 2018-02-02 14:04:15 +0100 | [diff] [blame] | 42 | HTMLElement.prototype.addE = function (tag) { |
| 43 | return this.appendChild(document.createElement(tag)); |
| 44 | }; |
| 45 | |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 46 | // Append text node |
Akron | 151bc87 | 2018-02-02 14:04:15 +0100 | [diff] [blame] | 47 | HTMLElement.prototype.addT = function (text) { |
| 48 | return this.appendChild(document.createTextNode(text)); |
| 49 | }; |
| 50 | |
| 51 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 52 | // Utility for removing all children of a node |
| 53 | function _removeChildren (node) { |
| 54 | // Remove everything underneath |
| 55 | while (node.firstChild) |
| 56 | node.removeChild(node.firstChild); |
| 57 | }; |
| 58 | |
Akron | 151bc87 | 2018-02-02 14:04:15 +0100 | [diff] [blame] | 59 | |
Akron | 6a535d4 | 2015-08-26 20:16:58 +0200 | [diff] [blame] | 60 | // Utility to get either the charCode |
| 61 | // or the keyCode of an event |
| 62 | function _codeFromEvent (e) { |
| 63 | if ((e.charCode) && (e.keyCode==0)) |
| 64 | return e.charCode |
| 65 | return e.keyCode; |
| 66 | }; |
| 67 | |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 68 | function _dec2hex (dec) { |
| 69 | return ('0' + dec.toString(16)).substr(-2) |
| 70 | }; |
| 71 | |
| 72 | |
| 73 | /** |
| 74 | * Create random identifiers |
| 75 | */ |
| 76 | /* |
| 77 | * code based on |
| 78 | * https://stackoverflow.com/questions/1349404/generate-random-string-characters-in-javascript#8084248 |
| 79 | */ |
| 80 | function randomID (len) { |
Akron | df90c59 | 2020-10-20 08:42:50 +0200 | [diff] [blame] | 81 | const arr = new Uint8Array((len || 40) / 2) |
Akron | a6c32b9 | 2018-07-02 18:39:42 +0200 | [diff] [blame] | 82 | window.crypto.getRandomValues(arr) |
| 83 | return Array.from(arr, _dec2hex).join('') |
| 84 | }; |
| 85 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 86 | |
| 87 | define(function () { |
Akron | c1457bf | 2015-06-11 19:24:00 +0200 | [diff] [blame] | 88 | // Todo: That's double now! |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 89 | KorAP.API = KorAP.API || {}; |
Akron | c1457bf | 2015-06-11 19:24:00 +0200 | [diff] [blame] | 90 | KorAP.Locale = KorAP.Locale || {}; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 91 | |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 92 | const loc = KorAP.Locale; |
Nils Diewald | 359a72c | 2015-04-20 17:40:29 +0000 | [diff] [blame] | 93 | loc.OR = loc.OR || 'or'; |
| 94 | loc.AND = loc.AND || 'and'; |
| 95 | |
| 96 | // Add new stylesheet object lazily to document |
| 97 | KorAP.newStyleSheet = function () { |
| 98 | if (KorAP._sheet === undefined) { |
Akron | df90c59 | 2020-10-20 08:42:50 +0200 | [diff] [blame] | 99 | const sElem = document.createElement('style'); |
Nils Diewald | 359a72c | 2015-04-20 17:40:29 +0000 | [diff] [blame] | 100 | document.head.appendChild(sElem); |
| 101 | KorAP._sheet = sElem.sheet; |
| 102 | }; |
| 103 | return KorAP._sheet; |
| 104 | }; |
| 105 | |
| 106 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 107 | // Default log message |
Akron | c0a2da8 | 2018-07-04 15:27:37 +0200 | [diff] [blame] | 108 | KorAP.log = KorAP.log || function (type, msg, src) { |
Akron | df90c59 | 2020-10-20 08:42:50 +0200 | [diff] [blame] | 109 | if (src) |
| 110 | msg += ' from ' + src; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 111 | console.log(type + ": " + msg); |
| 112 | }; |
| 113 | |
| 114 | return KorAP; |
| 115 | }); |