blob: 416ad85923226e6e9d2cfa2f89a6895f4eb0e82c [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',
Akronb19803c2018-08-16 16:39:42 +02008 'vc/docgroupref',
Nils Diewald1fcb2ad2015-04-20 19:19:18 +00009 'util'
Akronb19803c2018-08-16 16:39:42 +020010], function (jsonldClass, docClass, docGroupRefClass) {
Nils Diewald0e6992a2015-04-14 20:13:52 +000011
Nils Diewald1fcb2ad2015-04-20 19:19:18 +000012 // Localize empty string
Nils Diewald0e6992a2015-04-14 20:13:52 +000013 var loc = KorAP.Locale;
14 loc.EMPTY = loc.EMPTY || '⋯';
15
16 return {
Nils Diewald1fcb2ad2015-04-20 19:19:18 +000017
18 // The ld-type
Nils Diewald0e6992a2015-04-14 20:13:52 +000019 _ldType : "non",
Nils Diewald1fcb2ad2015-04-20 19:19:18 +000020
21 /**
22 * Create new unspecified criterion
23 * with a link to the parent object
24 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000025 create : function (parent) {
26 var obj = Object.create(jsonldClass).
Akron0b489ad2018-02-02 16:49:32 +010027 upgradeTo(this);
Nils Diewald0e6992a2015-04-14 20:13:52 +000028
29 if (parent !== undefined)
Akron0b489ad2018-02-02 16:49:32 +010030 obj._parent = parent;
Nils Diewald0e6992a2015-04-14 20:13:52 +000031
32 return obj;
33 },
34
Nils Diewald1fcb2ad2015-04-20 19:19:18 +000035 /**
36 * Set the key; this will spawn a new doc
37 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000038 key : function (v) {
39
40 // Not replaceable
41 if (this._parent === undefined)
Akron0b489ad2018-02-02 16:49:32 +010042 return null;
Nils Diewald0e6992a2015-04-14 20:13:52 +000043
Akronb19803c2018-08-16 16:39:42 +020044 var newDoc;
45 var keyType = KorAP._vcKeyMenu.typeOf(v);
46
Nils Diewald0e6992a2015-04-14 20:13:52 +000047 // Set JSON-LD type
Akronb19803c2018-08-16 16:39:42 +020048 if (keyType && keyType === 'ref') {
49 newDoc = docGroupRefClass.create(this._parent);
50 }
51 else {
52 newDoc = docClass.create(this._parent);
53 newDoc.key(v);
54 newDoc.type(keyType);
55 };
Akron55a343b2018-04-06 19:57:36 +020056
Nils Diewald0e6992a2015-04-14 20:13:52 +000057 // Unspecified document on root
58 if (this._parent.ldType() === null) {
Akron0b489ad2018-02-02 16:49:32 +010059 this._parent.root(newDoc);
60 this.destroy();
Nils Diewald0e6992a2015-04-14 20:13:52 +000061 }
62
63 // Unspecified document in group
64 else {
Akron0b489ad2018-02-02 16:49:32 +010065 this._parent.replaceOperand(this, newDoc);
Nils Diewald0e6992a2015-04-14 20:13:52 +000066 };
67 this._parent.update();
68 return newDoc;
69 },
70
Nils Diewald1fcb2ad2015-04-20 19:19:18 +000071
72 /**
73 * Update the element
74 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000075 update : function () {
76
77 if (this._element === undefined)
Akron0b489ad2018-02-02 16:49:32 +010078 return this.element();
Nils Diewald0e6992a2015-04-14 20:13:52 +000079
80 // Remove element content
Akron0b489ad2018-02-02 16:49:32 +010081 _removeChildren(this._element);
Nils Diewald0e6992a2015-04-14 20:13:52 +000082
83 var ellipsis = document.createElement('span');
Akron0b489ad2018-02-02 16:49:32 +010084 ellipsis.addT(loc.EMPTY);
Nils Diewald1fcb2ad2015-04-20 19:19:18 +000085
86 // Click on empty criterion
87 ellipsis.addEventListener('click', this.onclick.bind(this));
88
Nils Diewald0e6992a2015-04-14 20:13:52 +000089 this._element.appendChild(ellipsis);
90
91 // Set ref - TODO: Cleanup!
92 this._element.refTo = this;
93
94 // Set operators
Akronb19803c2018-08-16 16:39:42 +020095 if (this._parent !== undefined &&
96 this.parent().ldType() !== null) {
Akron0b489ad2018-02-02 16:49:32 +010097 var op = this.operators(
98 false,
99 false,
100 true
101 );
102
103 this._element.appendChild(
104 op.element()
105 );
Nils Diewald0e6992a2015-04-14 20:13:52 +0000106 };
107
108 return this.element();
109 },
110
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000111
112 /**
113 * Get the associated element
114 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000115 element : function () {
116 if (this._element !== undefined)
Akron0b489ad2018-02-02 16:49:32 +0100117 return this._element;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000118 this._element = document.createElement('div');
119 this._element.setAttribute('class', 'doc unspecified');
120 this.update();
121 return this._element;
122 },
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000123
hebastaa0282be2018-12-05 16:58:00 +0100124
125 incomplete : function () {
126 return true;
127 },
128
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000129 /**
130 * Click on the unspecified object
131 */
132 onclick : function () {
Nils Diewald4c221252015-04-21 20:19:25 +0000133
134 // Get the key menu
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000135 var menu = KorAP._vcKeyMenu;
Akron55a343b2018-04-06 19:57:36 +0200136
Nils Diewald4c221252015-04-21 20:19:25 +0000137 // Add key menu element at the correct position
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000138 this._element.insertBefore(
Akron0b489ad2018-02-02 16:49:32 +0100139 menu.element(),
140 this._element.firstChild
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000141 );
142
143 var that = this;
144
145 // Set released method
Akronb19803c2018-08-16 16:39:42 +0200146 menu.released(function (key) {
Akron0b489ad2018-02-02 16:49:32 +0100147 // Set chosen key and type - will return a doc
Akronb19803c2018-08-16 16:39:42 +0200148 that.key(key).update();
Akron0b489ad2018-02-02 16:49:32 +0100149 this.hide();
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000150 });
151
152 menu.show();
153 menu.focus();
154 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000155 };
156});