blob: 56d08698cb02403c3ba27ed0c7fb0ada14cbc10f [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
Akron55a343b2018-04-06 19:57:36 +020044 var newDoc = docClass.create(this._parent);
45 newDoc.key(v);
46
Nils Diewald0e6992a2015-04-14 20:13:52 +000047 // Unspecified document on root
48 if (this._parent.ldType() === null) {
Akron0b489ad2018-02-02 16:49:32 +010049 this._parent.root(newDoc);
50 this.destroy();
Nils Diewald0e6992a2015-04-14 20:13:52 +000051 }
52
53 // Unspecified document in group
54 else {
Akron0b489ad2018-02-02 16:49:32 +010055 this._parent.replaceOperand(this, newDoc);
Nils Diewald0e6992a2015-04-14 20:13:52 +000056 };
57 this._parent.update();
58 return newDoc;
59 },
60
Nils Diewald1fcb2ad2015-04-20 19:19:18 +000061
62 /**
63 * Update the element
64 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000065 update : function () {
66
67 if (this._element === undefined)
Akron0b489ad2018-02-02 16:49:32 +010068 return this.element();
Nils Diewald0e6992a2015-04-14 20:13:52 +000069
70 // Remove element content
Akron0b489ad2018-02-02 16:49:32 +010071 _removeChildren(this._element);
Nils Diewald0e6992a2015-04-14 20:13:52 +000072
73 var ellipsis = document.createElement('span');
Akron0b489ad2018-02-02 16:49:32 +010074 ellipsis.addT(loc.EMPTY);
Nils Diewald1fcb2ad2015-04-20 19:19:18 +000075
76 // Click on empty criterion
77 ellipsis.addEventListener('click', this.onclick.bind(this));
78
Nils Diewald0e6992a2015-04-14 20:13:52 +000079 this._element.appendChild(ellipsis);
80
81 // Set ref - TODO: Cleanup!
82 this._element.refTo = this;
83
84 // Set operators
85 if (this._parent !== undefined && this.parent().ldType() !== null) {
Akron0b489ad2018-02-02 16:49:32 +010086 var op = this.operators(
87 false,
88 false,
89 true
90 );
91
92 this._element.appendChild(
93 op.element()
94 );
Nils Diewald0e6992a2015-04-14 20:13:52 +000095 };
96
97 return this.element();
98 },
99
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000100
101 /**
102 * Get the associated element
103 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000104 element : function () {
105 if (this._element !== undefined)
Akron0b489ad2018-02-02 16:49:32 +0100106 return this._element;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000107 this._element = document.createElement('div');
108 this._element.setAttribute('class', 'doc unspecified');
109 this.update();
110 return this._element;
111 },
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000112
113 /**
114 * Click on the unspecified object
115 */
116 onclick : function () {
Nils Diewald4c221252015-04-21 20:19:25 +0000117
118 // Get the key menu
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000119 var menu = KorAP._vcKeyMenu;
Akron55a343b2018-04-06 19:57:36 +0200120
Nils Diewald4c221252015-04-21 20:19:25 +0000121 // Add key menu element at the correct position
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000122 this._element.insertBefore(
Akron0b489ad2018-02-02 16:49:32 +0100123 menu.element(),
124 this._element.firstChild
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000125 );
126
127 var that = this;
128
129 // Set released method
Nils Diewald4c221252015-04-21 20:19:25 +0000130 menu.released(function (key, type) {
Akron0b489ad2018-02-02 16:49:32 +0100131 // Set chosen key and type - will return a doc
132 that.key(key).type(type).update();
133 this.hide();
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000134 });
135
136 menu.show();
137 menu.focus();
138 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000139 };
140});