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 | |
| 89 | define(function () { |
Akron | c1457bf | 2015-06-11 19:24:00 +0200 | [diff] [blame] | 90 | // Todo: That's double now! |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 91 | KorAP.API = KorAP.API || {}; |
Akron | c1457bf | 2015-06-11 19:24:00 +0200 | [diff] [blame] | 92 | KorAP.Locale = KorAP.Locale || {}; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 93 | |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 94 | const loc = KorAP.Locale; |
Nils Diewald | 359a72c | 2015-04-20 17:40:29 +0000 | [diff] [blame] | 95 | loc.OR = loc.OR || 'or'; |
| 96 | loc.AND = loc.AND || 'and'; |
| 97 | |
| 98 | // Add new stylesheet object lazily to document |
| 99 | KorAP.newStyleSheet = function () { |
| 100 | if (KorAP._sheet === undefined) { |
Akron | df90c59 | 2020-10-20 08:42:50 +0200 | [diff] [blame] | 101 | const sElem = document.createElement('style'); |
Nils Diewald | 359a72c | 2015-04-20 17:40:29 +0000 | [diff] [blame] | 102 | document.head.appendChild(sElem); |
| 103 | KorAP._sheet = sElem.sheet; |
| 104 | }; |
| 105 | return KorAP._sheet; |
| 106 | }; |
| 107 | |
| 108 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 109 | // Default log message |
Akron | c0a2da8 | 2018-07-04 15:27:37 +0200 | [diff] [blame] | 110 | KorAP.log = KorAP.log || function (type, msg, src) { |
Akron | df90c59 | 2020-10-20 08:42:50 +0200 | [diff] [blame] | 111 | if (src) |
| 112 | msg += ' from ' + src; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 113 | console.log(type + ": " + msg); |
| 114 | }; |
| 115 | |
| 116 | return KorAP; |
| 117 | }); |