blob: 0395685ea028f045906f409735524661f0af75d1 [file] [log] [blame]
Nils Diewald0e6992a2015-04-14 20:13:52 +00001/**
Nils Diewald1fcb2ad2015-04-20 19:19:18 +00002 * An unspecified criterion in a virtual collection.
3 * Inherits everything from jsonld
Nils Diewald0e6992a2015-04-14 20:13:52 +00004 */
Nils Diewald1fcb2ad2015-04-20 19:19:18 +00005define([
6 'vc/jsonld',
7 'vc/doc',
8 'util'
9], function (jsonldClass, docClass) {
Nils Diewald0e6992a2015-04-14 20:13:52 +000010
Nils Diewald1fcb2ad2015-04-20 19:19:18 +000011 // Localize empty string
Nils Diewald0e6992a2015-04-14 20:13:52 +000012 var loc = KorAP.Locale;
13 loc.EMPTY = loc.EMPTY || '⋯';
14
15 return {
Nils Diewald1fcb2ad2015-04-20 19:19:18 +000016
17 // The ld-type
Nils Diewald0e6992a2015-04-14 20:13:52 +000018 _ldType : "non",
Nils Diewald1fcb2ad2015-04-20 19:19:18 +000019
20 /**
21 * Create new unspecified criterion
22 * with a link to the parent object
23 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000024 create : function (parent) {
25 var obj = Object.create(jsonldClass).
Akron0b489ad2018-02-02 16:49:32 +010026 upgradeTo(this);
Nils Diewald0e6992a2015-04-14 20:13:52 +000027
28 if (parent !== undefined)
Akron0b489ad2018-02-02 16:49:32 +010029 obj._parent = parent;
Nils Diewald0e6992a2015-04-14 20:13:52 +000030
31 return obj;
32 },
33
Nils Diewald1fcb2ad2015-04-20 19:19:18 +000034 /**
35 * Set the key; this will spawn a new doc
36 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000037 key : function (v) {
38
39 // Not replaceable
40 if (this._parent === undefined)
Akron0b489ad2018-02-02 16:49:32 +010041 return null;
Nils Diewald0e6992a2015-04-14 20:13:52 +000042
43 // Set JSON-LD type
44 var newDoc = docClass.create(this._parent, {
Akron0b489ad2018-02-02 16:49:32 +010045 "@type" : "koral:doc",
46 "value" : "",
47 "key" : v
Nils Diewald0e6992a2015-04-14 20:13:52 +000048 });
49
50 // Unspecified document on root
51 if (this._parent.ldType() === null) {
Akron0b489ad2018-02-02 16:49:32 +010052 this._parent.root(newDoc);
53 this.destroy();
Nils Diewald0e6992a2015-04-14 20:13:52 +000054 }
55
56 // Unspecified document in group
57 else {
Akron0b489ad2018-02-02 16:49:32 +010058 this._parent.replaceOperand(this, newDoc);
Nils Diewald0e6992a2015-04-14 20:13:52 +000059 };
60 this._parent.update();
61 return newDoc;
62 },
63
Nils Diewald1fcb2ad2015-04-20 19:19:18 +000064
65 /**
66 * Update the element
67 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000068 update : function () {
69
70 if (this._element === undefined)
Akron0b489ad2018-02-02 16:49:32 +010071 return this.element();
Nils Diewald0e6992a2015-04-14 20:13:52 +000072
73 // Remove element content
Akron0b489ad2018-02-02 16:49:32 +010074 _removeChildren(this._element);
Nils Diewald0e6992a2015-04-14 20:13:52 +000075
76 var ellipsis = document.createElement('span');
Akron0b489ad2018-02-02 16:49:32 +010077 ellipsis.addT(loc.EMPTY);
Nils Diewald1fcb2ad2015-04-20 19:19:18 +000078
79 // Click on empty criterion
80 ellipsis.addEventListener('click', this.onclick.bind(this));
81
Nils Diewald0e6992a2015-04-14 20:13:52 +000082 this._element.appendChild(ellipsis);
83
84 // Set ref - TODO: Cleanup!
85 this._element.refTo = this;
86
87 // Set operators
88 if (this._parent !== undefined && this.parent().ldType() !== null) {
Akron0b489ad2018-02-02 16:49:32 +010089 var op = this.operators(
90 false,
91 false,
92 true
93 );
94
95 this._element.appendChild(
96 op.element()
97 );
Nils Diewald0e6992a2015-04-14 20:13:52 +000098 };
99
100 return this.element();
101 },
102
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000103
104 /**
105 * Get the associated element
106 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000107 element : function () {
108 if (this._element !== undefined)
Akron0b489ad2018-02-02 16:49:32 +0100109 return this._element;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000110 this._element = document.createElement('div');
111 this._element.setAttribute('class', 'doc unspecified');
112 this.update();
113 return this._element;
114 },
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000115
116 /**
117 * Click on the unspecified object
118 */
119 onclick : function () {
Nils Diewald4c221252015-04-21 20:19:25 +0000120
121 // Get the key menu
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000122 var menu = KorAP._vcKeyMenu;
Nils Diewald4c221252015-04-21 20:19:25 +0000123 // Add key menu element at the correct position
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000124 this._element.insertBefore(
Akron0b489ad2018-02-02 16:49:32 +0100125 menu.element(),
126 this._element.firstChild
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000127 );
128
129 var that = this;
130
131 // Set released method
Nils Diewald4c221252015-04-21 20:19:25 +0000132 menu.released(function (key, type) {
Akron0b489ad2018-02-02 16:49:32 +0100133 // Set chosen key and type - will return a doc
134 that.key(key).type(type).update();
135 this.hide();
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000136 });
137
138 menu.show();
139 menu.focus();
140 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000141 };
142});