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 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 62 | |
| 63 | define(function () { |
Akron | c1457bf | 2015-06-11 19:24:00 +0200 | [diff] [blame] | 64 | // Todo: That's double now! |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 65 | KorAP.API = KorAP.API || {}; |
Akron | c1457bf | 2015-06-11 19:24:00 +0200 | [diff] [blame] | 66 | KorAP.Locale = KorAP.Locale || {}; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 67 | |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame^] | 68 | const loc = KorAP.Locale; |
Nils Diewald | 359a72c | 2015-04-20 17:40:29 +0000 | [diff] [blame] | 69 | loc.OR = loc.OR || 'or'; |
| 70 | loc.AND = loc.AND || 'and'; |
| 71 | |
| 72 | // Add new stylesheet object lazily to document |
| 73 | KorAP.newStyleSheet = function () { |
| 74 | if (KorAP._sheet === undefined) { |
| 75 | var sElem = document.createElement('style'); |
| 76 | document.head.appendChild(sElem); |
| 77 | KorAP._sheet = sElem.sheet; |
| 78 | }; |
| 79 | return KorAP._sheet; |
| 80 | }; |
| 81 | |
| 82 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 83 | // Default log message |
| 84 | KorAP.log = KorAP.log || function (type, msg) { |
| 85 | console.log(type + ": " + msg); |
| 86 | }; |
| 87 | |
| 88 | return KorAP; |
| 89 | }); |