blob: 5b6c8f842c79f4fdc0b2100bdedd15d45a79afe3 [file] [log] [blame]
Akronc1457bf2015-06-11 19:24:00 +02001window.KorAP = window.KorAP || {};
2
Nils Diewald0e6992a2015-04-14 20:13:52 +00003// Don't let events bubble up
4if (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 Diewald7c8ced22015-04-15 19:21:00 +000012var _quoteRE = new RegExp("([\"\\\\])", 'g');
13String.prototype.quote = function () {
14 return this.replace(_quoteRE, '\\$1');
15};
16
Akron8778f5d2017-06-30 21:25:55 +020017var _escapeRE = new RegExp("([\/\\\\])", 'g');
18String.prototype.escapeRegex = function () {
19 return this.replace(_escapeRE, '\\$1');
20};
21
Nils Diewald0e6992a2015-04-14 20:13:52 +000022// Add toggleClass method similar to jquery
23HTMLElement.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
Akron0b489ad2018-02-02 16:49:32 +010035// Append element by tag name
Akron151bc872018-02-02 14:04:15 +010036HTMLElement.prototype.addE = function (tag) {
37 return this.appendChild(document.createElement(tag));
38};
39
Akron0b489ad2018-02-02 16:49:32 +010040// Append text node
Akron151bc872018-02-02 14:04:15 +010041HTMLElement.prototype.addT = function (text) {
42 return this.appendChild(document.createTextNode(text));
43};
44
45
Nils Diewald0e6992a2015-04-14 20:13:52 +000046// Utility for removing all children of a node
47function _removeChildren (node) {
48 // Remove everything underneath
49 while (node.firstChild)
50 node.removeChild(node.firstChild);
51};
52
Akron151bc872018-02-02 14:04:15 +010053
Akron6a535d42015-08-26 20:16:58 +020054// Utility to get either the charCode
55// or the keyCode of an event
56function _codeFromEvent (e) {
57 if ((e.charCode) && (e.keyCode==0))
58 return e.charCode
59 return e.keyCode;
60};
61
Nils Diewald0e6992a2015-04-14 20:13:52 +000062
63define(function () {
Akronc1457bf2015-06-11 19:24:00 +020064 // Todo: That's double now!
Nils Diewald0e6992a2015-04-14 20:13:52 +000065 KorAP.API = KorAP.API || {};
Akronc1457bf2015-06-11 19:24:00 +020066 KorAP.Locale = KorAP.Locale || {};
Nils Diewald0e6992a2015-04-14 20:13:52 +000067
Akron0b489ad2018-02-02 16:49:32 +010068 const loc = KorAP.Locale;
Nils Diewald359a72c2015-04-20 17:40:29 +000069 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 Diewald0e6992a2015-04-14 20:13:52 +000083 // Default log message
84 KorAP.log = KorAP.log || function (type, msg) {
85 console.log(type + ": " + msg);
86 };
87
88 return KorAP;
89});