Support for integer values in Kalamar
Change-Id: I13994db8006fbfc213621ec46095a53600f22a0d
diff --git a/dev/js/src/vc/doc.js b/dev/js/src/vc/doc.js
index 37b0e0b..d504642 100644
--- a/dev/js/src/vc/doc.js
+++ b/dev/js/src/vc/doc.js
@@ -7,9 +7,10 @@
'vc/jsonld',
'vc/rewritelist',
'vc/stringval',
+ 'vc/intval',
'vc/docgroupref',
'util'
-], function (jsonldClass, rewriteListClass, stringValClass, docGroupRefClass) {
+], function (jsonldClass, rewriteListClass, stringValClass, intValClass, docGroupRefClass) {
/*
* TODO:
@@ -219,8 +220,14 @@
return;
};
- if (json["value"] === undefined ||
- typeof json["value"] != 'string') {
+ if (json["value"] === undefined
+ ||
+ (typeof json["value"] != 'string'
+
+ && !(json["type"] != undefined &&
+ json["type"] == "type:integer" &&
+ typeof json["value"] == 'number')
+ )) {
KorAP.log(805, "Value is invalid");
return;
};
@@ -305,6 +312,24 @@
t.value(json["value"]);
}
+ // Key is integer
+ else if (json["type"] == "type:integer") {
+ t.type("integer");
+
+ // Check match type
+ if (!KorAP._validIntegerMatchRE.test(t.matchop())) {
+ KorAP.log(802, errstr802);
+
+ // Rewrite method
+ t.matchop('eq');
+ rewrite = 'modification';
+ };
+
+ // Set string value
+ t.value(json["value"]);
+ }
+
+
// Key is a date
else if (json["type"] === "type:date") {
t.type("date");
@@ -450,6 +475,8 @@
||
(t._type === 'text' && KorAP._validTextMatchRE.test(m))
||
+ (t._type === 'integer' && KorAP._validIntegerMatchRE.test(m))
+ ||
(t._type === 'date' && KorAP._validDateMatchRE.test(m))
) {
t._matchop = m;
@@ -557,32 +584,49 @@
dp.input().focus();
}
-
+
else {
- const regex = this.type() === 'regex' ? true : false;
- const str = stringValClass.create(this.value(), regex);
- const strElem = str.element();
- str.store = function (value, regex) {
- that.value(value);
- if (regex === true)
- that.type('regex');
- else
- that.type('string');
+ let vcVal;
+
+ if (this.type() === 'integer') {
+ vcVal = intValClass.create(this.value());
+
+ vcVal.store = function (value) {
+ that.value(value);
+ that.type('integer');
+ that._el.removeChild(
+ this._el
+ );
+ that.update();
+ };
+ }
+
+ else {
+ const regex = this.type() === 'regex' ? true : false;
+ vcVal = stringValClass.create(this.value(), regex);
+
+ vcVal.store = function (value, regex) {
+ that.value(value);
+ if (regex === true)
+ that.type('regex');
+ else
+ that.type('string');
- that._el.removeChild(
- this._el
- );
- that.update();
+ that._el.removeChild(
+ this._el
+ );
+ that.update();
+ };
};
// Insert element
this._el.insertBefore(
- strElem,
+ vcVal.element(),
this._valueE
);
- str.focus();
+ vcVal.focus();
};
},
@@ -666,10 +710,10 @@
string += '!~';
break;
case "geq":
- string += 'since';
+ string += (this.type() == 'date') ? 'since' : '>=';
break;
case "leq":
- string += 'until';
+ string += (this.type() == 'date') ? 'until' : '<=';
break;
default:
string += (this.type() == 'date') ? 'in' : '=';
@@ -681,6 +725,7 @@
// Add value
switch (this.type()) {
case "date":
+ case "integer":
return string + this.value();
case "regex":
return string + '/' + this.value().escapeRegex() + '/';
diff --git a/dev/js/src/vc/fragment.js b/dev/js/src/vc/fragment.js
index 72bd80e..e5b03a0 100644
--- a/dev/js/src/vc/fragment.js
+++ b/dev/js/src/vc/fragment.js
@@ -115,7 +115,7 @@
doc.key(item[0]);
doc.matchop("eq");
doc.value(item[1]);
- doc.type(item[2] === "date" ? "date" : "string");
+ doc.type(item[2] === "date" ? "date" : (item[2] === "integer" ? "integer" : "string"));
return doc;
}
);
diff --git a/dev/js/src/vc/intval.js b/dev/js/src/vc/intval.js
new file mode 100644
index 0000000..55a5829
--- /dev/null
+++ b/dev/js/src/vc/intval.js
@@ -0,0 +1,144 @@
+/**
+ * Add integer values to the virtual corpus
+ */
+"use strict";
+
+define(['util'], function () {
+
+ return {
+ /**
+ * Create new integer value helper.
+ */
+ create : function () {
+ const a = arguments;
+ let value = 0;
+
+ // Set value
+ if (a.length >= 1) {
+ if (a[0] !== undefined)
+ value = a[0];
+ };
+
+ return Object.create(this)._init(value);
+ },
+
+
+ // Initialize the integer value
+ _init : function (value) {
+ this.value(value);
+ return this;
+ },
+
+ /**
+ * Get or set the integer value.
+ */
+ value : function (val) {
+ if (arguments.length === 1) {
+
+ if (typeof val != "number")
+ val = parseInt(val);
+
+ if (isNaN(val))
+ val = 0;
+
+ this._value = val;
+ this._update();
+ };
+ return this._value;
+ },
+
+
+ // Update dom element
+ _update : function () {
+ if (this._el === undefined)
+ return;
+
+ this._value = this._input.value;
+ },
+
+
+ /**
+ * Store the integer value.
+ * This method should be overwritten.
+ * The method receives the value.
+ */
+ store : function (v) {},
+
+
+ /**
+ * Put focus on element
+ */
+ focus : function () {
+ this._el.children[0].focus();
+ },
+
+
+ /**
+ * Get the associated dom element.
+ */
+ element : function () {
+ if (this._el !== undefined)
+ return this._el;
+
+ // Create element
+ const e = this._el = document.createElement('div');
+ e.setAttribute('tabindex', 0);
+ e.style.outline = 0;
+
+ const cl = e.classList;
+ cl.add('value');
+
+ // Add input field
+ this._input = e.addE('input');
+ this._input.setAttribute("type", "number");
+
+ if (this.value() !== undefined) {
+ this._input.value = this.value();
+ };
+
+ // If the focus is not on the text field anymore,
+ // delegate focus to
+ this._input.addEventListener(
+ 'blur',
+ function (ev) {
+ const t = this;
+ if (!t._inField) {
+ t.value(t._input.value);
+ t.store(t.value());
+ };
+ ev.halt();
+ }.bind(this)
+ );
+
+ // Workaround to check the click is in the field
+ e.addEventListener(
+ 'mousedown',
+ function () {
+ this._inField = true;
+ }.bind(this)
+ );
+
+ e.addEventListener(
+ 'mouseup',
+ function () {
+ this._inField = false;
+ this._input.focus();
+ }.bind(this)
+ );
+
+ this._input.addEventListener(
+ 'keypress',
+ function (ev) {
+ const t = this;
+ if (ev.keyCode == 13) {
+ t.value(t._input.value);
+ t.store(t.value());
+ return false;
+ };
+ }.bind(this)
+ );
+
+ return e;
+ }
+ };
+});