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