Introduce fieldType guesser in VC deserialization
Change-Id: Id279582a5e238e3fb494ca0706783a57b47c05ce
diff --git a/dev/js/spec/vcSpec.js b/dev/js/spec/vcSpec.js
index 5433fb1..8e745e1 100644
--- a/dev/js/spec/vcSpec.js
+++ b/dev/js/spec/vcSpec.js
@@ -2233,6 +2233,22 @@
// blur
document.body.click();
});
+
+
+ // Check json deserialization
+ it('should be initializable', function () {
+ vc = vcClass.create([
+ ['a', 'text'],
+ ['b', 'string'],
+ ['c', 'date']
+ ]).fromJson({
+ "@type" : "koral:doc",
+ "key":"a",
+ "value":"Baum"
+ });
+
+ expect(vc.element().firstChild.children[1].getAttribute('data-type')).toEqual('text');
+ });
});
diff --git a/dev/js/src/vc/doc.js b/dev/js/src/vc/doc.js
index 3df0f70..41b72ad 100644
--- a/dev/js/src/vc/doc.js
+++ b/dev/js/src/vc/doc.js
@@ -203,12 +203,19 @@
};
};
- // Type is unspecified
+ // Type is unspecified - but may be known by the menu
+ if (json["type"] === undefined && KorAP._vcKeyMenu) {
+
+ // Check the VC list if the field is known
+ var type = KorAP._vcKeyMenu.typeOf(this.key());
+ if (type != undefined) {
+ json["type"] = "type:" + type;
+ };
+ };
+
+ // Type is still undefined
if (json["type"] === undefined) {
-
- // TODO:
- // First check the VC list if the field is known
-
+
// Check match type
if (!KorAP._validUnspecMatchRE.test(this.matchop())) {
KorAP.log(802, errstr802);
diff --git a/dev/js/src/vc/menu.js b/dev/js/src/vc/menu.js
index 6a242e2..0f0d92a 100644
--- a/dev/js/src/vc/menu.js
+++ b/dev/js/src/vc/menu.js
@@ -21,6 +21,7 @@
return obj;
},
+
/**
* Register callback for click event.
*/
@@ -28,12 +29,26 @@
this._cb = cb;
},
+
/**
* A click event was released
*/
release : function (key, type) {
if (this._cb !== undefined)
this._cb(key, type);
+ },
+
+ /**
+ * Return a key type based on a key.
+ * This is a linear search, but should work okay for small
+ * VCs and small key lists.
+ */
+ typeOf : function (key) {
+ for (i in this._items) {
+ if (this._items[i].key() === key) {
+ return this._items[i].type();
+ }
+ };
}
};
});