Modernize utility scripts
Change-Id: I7f673519d91fbebf1ba00f4821c896fda839c93b
diff --git a/dev/js/src/state.js b/dev/js/src/state.js
index 69e2d54..7d0d25e 100644
--- a/dev/js/src/state.js
+++ b/dev/js/src/state.js
@@ -12,10 +12,9 @@
* can easily be serialized and kept between page turns (via cookie
* and/or query param)
*/
+"use strict";
+
define(function () {
-
- "use strict";
-
return {
/**
@@ -25,22 +24,25 @@
return Object.create(this)._init(value);
},
+
// Initialize
_init : function (values) {
- this._assoc = [];
+ const t = this;
+ t._assoc = [];
if (values == undefined) {
- this.values = [false,true];
+ t.values = [false,true];
}
else if (Array.isArray(values)) {
- this.values = values;
+ t.values = values;
}
else {
- this.values = [values];
+ t.values = [values];
}
- this.value = this.values[0];
- return this;
+ t.value = t.values[0];
+ return t;
},
+
/**
* Associate the state with some objects.
*/
@@ -55,6 +57,7 @@
}
},
+
/**
* Set the state to a certain value.
* This will set the state to all associated objects as well.
@@ -66,6 +69,7 @@
};
},
+
/**
* Get the state value
*/
@@ -81,6 +85,7 @@
return this._assoc.length;
},
+
/**
* Clear all associated objects
*/
@@ -88,6 +93,7 @@
return this._assoc = [];
},
+
/**
* Roll to the next value.
* This may be used for toggling.
diff --git a/dev/js/src/tagger.js b/dev/js/src/tagger.js
index 3aeeb59..afd9de3 100644
--- a/dev/js/src/tagger.js
+++ b/dev/js/src/tagger.js
@@ -1,19 +1,18 @@
-define(['palette','util'], function (p) {
+"use strict";
- // "use strict";
+define(['palette','util'], function (p) {
return {
create : function (elem) {
- var obj = Object.create(this);
+ const obj = Object.create(this);
obj._nr = 0;
obj._elem = elem;
elem.addE('div');
- var newCat = elem.addE('input');
+ const newCat = elem.addE('input');
newCat.setAttribute('type', 'text');
-
newCat.addEventListener('keypress', function (e) {
- var key = e.keyCode || e.which;
+ const key = e.keyCode || e.which;
if (key === 13) {
obj.addTag(this.value);
this.value = '';
@@ -25,15 +24,12 @@
addTag : function (name) {
this._cat.push(name);
-
this._nr++;
- var cat = document.createElement('span');
+ const cat = document.createElement('span');
cat.addT(name);
cat.addE('span').setAttribute('class','close');
-
cat.style.backgroundColor = p.getC(this._nr);
-
this._elem.firstChild.appendChild(cat);
}
};
diff --git a/dev/js/src/util.js b/dev/js/src/util.js
index 5eab4b3..8e40c12 100644
--- a/dev/js/src/util.js
+++ b/dev/js/src/util.js
@@ -27,7 +27,7 @@
// Add toggleClass method similar to jquery
HTMLElement.prototype.toggleClass = function (c1, c2) {
- var cl = this.classList;
+ const cl = this.classList;
if (cl.contains(c1)) {
cl.add(c2);
cl.remove(c1);
@@ -78,7 +78,7 @@
* https://stackoverflow.com/questions/1349404/generate-random-string-characters-in-javascript#8084248
*/
function randomID (len) {
- let arr = new Uint8Array((len || 40) / 2)
+ const arr = new Uint8Array((len || 40) / 2)
window.crypto.getRandomValues(arr)
return Array.from(arr, _dec2hex).join('')
};
@@ -96,7 +96,7 @@
// Add new stylesheet object lazily to document
KorAP.newStyleSheet = function () {
if (KorAP._sheet === undefined) {
- var sElem = document.createElement('style');
+ const sElem = document.createElement('style');
document.head.appendChild(sElem);
KorAP._sheet = sElem.sheet;
};
@@ -106,7 +106,8 @@
// Default log message
KorAP.log = KorAP.log || function (type, msg, src) {
- if (src) msg += ' from ' + src;
+ if (src)
+ msg += ' from ' + src;
console.log(type + ": " + msg);
};