blob: 5bda78cc554b057b8dfa27f87323ca7fe8bc94cb [file] [log] [blame]
Nils Diewald0e6992a2015-04-14 20:13:52 +00001/**
Akroncd42a142019-07-12 18:55:37 +02002 * An unspecified criterion in a virtual corpus.
Nils Diewald1fcb2ad2015-04-20 19:19:18 +00003 * Inherits everything from jsonld
Nils Diewald0e6992a2015-04-14 20:13:52 +00004 */
Akron88d237e2020-10-21 08:05:18 +02005"use strict";
6
Nils Diewald1fcb2ad2015-04-20 19:19:18 +00007define([
8 'vc/jsonld',
9 'vc/doc',
Akronb19803c2018-08-16 16:39:42 +020010 'vc/docgroupref',
Nils Diewald1fcb2ad2015-04-20 19:19:18 +000011 'util'
Akronb19803c2018-08-16 16:39:42 +020012], function (jsonldClass, docClass, docGroupRefClass) {
Nils Diewald0e6992a2015-04-14 20:13:52 +000013
Nils Diewald1fcb2ad2015-04-20 19:19:18 +000014 // Localize empty string
Akron88d237e2020-10-21 08:05:18 +020015 const loc = KorAP.Locale;
Nils Diewald0e6992a2015-04-14 20:13:52 +000016 loc.EMPTY = loc.EMPTY || '⋯';
17
18 return {
Nils Diewald1fcb2ad2015-04-20 19:19:18 +000019
20 // The ld-type
Nils Diewald0e6992a2015-04-14 20:13:52 +000021 _ldType : "non",
Nils Diewald1fcb2ad2015-04-20 19:19:18 +000022
23 /**
24 * Create new unspecified criterion
25 * with a link to the parent object
26 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000027 create : function (parent) {
Akron88d237e2020-10-21 08:05:18 +020028 const obj = Object.create(jsonldClass).
Akron0b489ad2018-02-02 16:49:32 +010029 upgradeTo(this);
Nils Diewald0e6992a2015-04-14 20:13:52 +000030
31 if (parent !== undefined)
Akron0b489ad2018-02-02 16:49:32 +010032 obj._parent = parent;
Nils Diewald0e6992a2015-04-14 20:13:52 +000033
34 return obj;
35 },
36
Akron88d237e2020-10-21 08:05:18 +020037
Nils Diewald1fcb2ad2015-04-20 19:19:18 +000038 /**
39 * Set the key; this will spawn a new doc
40 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000041 key : function (v) {
42
43 // Not replaceable
44 if (this._parent === undefined)
Akron0b489ad2018-02-02 16:49:32 +010045 return null;
Nils Diewald0e6992a2015-04-14 20:13:52 +000046
Akron88d237e2020-10-21 08:05:18 +020047 let newDoc;
48 const keyType = KorAP._vcKeyMenu.typeOf(v);
Akronb19803c2018-08-16 16:39:42 +020049
Nils Diewald0e6992a2015-04-14 20:13:52 +000050 // Set JSON-LD type
Akronb19803c2018-08-16 16:39:42 +020051 if (keyType && keyType === 'ref') {
52 newDoc = docGroupRefClass.create(this._parent);
53 }
54 else {
55 newDoc = docClass.create(this._parent);
56 newDoc.key(v);
57 newDoc.type(keyType);
58 };
Akron55a343b2018-04-06 19:57:36 +020059
Nils Diewald0e6992a2015-04-14 20:13:52 +000060 // Unspecified document on root
61 if (this._parent.ldType() === null) {
Akron0b489ad2018-02-02 16:49:32 +010062 this._parent.root(newDoc);
63 this.destroy();
Nils Diewald0e6992a2015-04-14 20:13:52 +000064 }
65
66 // Unspecified document in group
67 else {
Akron0b489ad2018-02-02 16:49:32 +010068 this._parent.replaceOperand(this, newDoc);
Nils Diewald0e6992a2015-04-14 20:13:52 +000069 };
70 this._parent.update();
71 return newDoc;
72 },
73
Nils Diewald1fcb2ad2015-04-20 19:19:18 +000074
75 /**
76 * Update the element
77 */
Nils Diewald0e6992a2015-04-14 20:13:52 +000078 update : function () {
Akron88d237e2020-10-21 08:05:18 +020079 const t = this;
Nils Diewald0e6992a2015-04-14 20:13:52 +000080
Akron24aa0052020-11-10 11:00:34 +010081 if (t._el === undefined)
Akron88d237e2020-10-21 08:05:18 +020082 return t.element();
Nils Diewald0e6992a2015-04-14 20:13:52 +000083
84 // Remove element content
Akron24aa0052020-11-10 11:00:34 +010085 _removeChildren(t._el);
Nils Diewald0e6992a2015-04-14 20:13:52 +000086
Akron88d237e2020-10-21 08:05:18 +020087 const ellipsis = document.createElement('span');
Akron0b489ad2018-02-02 16:49:32 +010088 ellipsis.addT(loc.EMPTY);
Nils Diewald1fcb2ad2015-04-20 19:19:18 +000089
90 // Click on empty criterion
Akron88d237e2020-10-21 08:05:18 +020091 ellipsis.addEventListener('click', t.onclick.bind(t));
Nils Diewald1fcb2ad2015-04-20 19:19:18 +000092
Akron24aa0052020-11-10 11:00:34 +010093 t._el.appendChild(ellipsis);
Nils Diewald0e6992a2015-04-14 20:13:52 +000094
95 // Set ref - TODO: Cleanup!
Akron24aa0052020-11-10 11:00:34 +010096 t._el.refTo = t;
Nils Diewald0e6992a2015-04-14 20:13:52 +000097
98 // Set operators
Akron88d237e2020-10-21 08:05:18 +020099 if (t._parent !== undefined &&
100 t.parent().ldType() !== null) {
101
Akron24aa0052020-11-10 11:00:34 +0100102 t._el.appendChild(
Akron88d237e2020-10-21 08:05:18 +0200103 t.operators(
104 false,
105 false,
106 true
107 ).element()
Akron0b489ad2018-02-02 16:49:32 +0100108 );
Nils Diewald0e6992a2015-04-14 20:13:52 +0000109 };
110
Akron88d237e2020-10-21 08:05:18 +0200111 return t.element();
Nils Diewald0e6992a2015-04-14 20:13:52 +0000112 },
113
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000114
115 /**
116 * Get the associated element
117 */
Nils Diewald0e6992a2015-04-14 20:13:52 +0000118 element : function () {
Akron88d237e2020-10-21 08:05:18 +0200119 const t = this;
Akron24aa0052020-11-10 11:00:34 +0100120 if (t._el !== undefined)
121 return t._el;
122 t._el = document.createElement('div');
123 t._el.setAttribute('class', 'doc unspecified');
Akron88d237e2020-10-21 08:05:18 +0200124 t.update();
Akron24aa0052020-11-10 11:00:34 +0100125 return t._el;
Nils Diewald0e6992a2015-04-14 20:13:52 +0000126 },
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000127
hebastaa0282be2018-12-05 16:58:00 +0100128
129 incomplete : function () {
130 return true;
131 },
132
Akron88d237e2020-10-21 08:05:18 +0200133
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000134 /**
135 * Click on the unspecified object
136 */
137 onclick : function () {
Nils Diewald4c221252015-04-21 20:19:25 +0000138
139 // Get the key menu
Akron88d237e2020-10-21 08:05:18 +0200140 const menu = KorAP._vcKeyMenu;
Akron55a343b2018-04-06 19:57:36 +0200141
Nils Diewald4c221252015-04-21 20:19:25 +0000142 // Add key menu element at the correct position
Akron24aa0052020-11-10 11:00:34 +0100143 this._el.insertBefore(
Akron0b489ad2018-02-02 16:49:32 +0100144 menu.element(),
Akron24aa0052020-11-10 11:00:34 +0100145 this._el.firstChild
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000146 );
147
Akron88d237e2020-10-21 08:05:18 +0200148 const that = this;
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000149
150 // Set released method
Akronb19803c2018-08-16 16:39:42 +0200151 menu.released(function (key) {
Akron0b489ad2018-02-02 16:49:32 +0100152 // Set chosen key and type - will return a doc
Akronb19803c2018-08-16 16:39:42 +0200153 that.key(key).update();
Akron0b489ad2018-02-02 16:49:32 +0100154 this.hide();
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000155 });
156
157 menu.show();
158 menu.focus();
159 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000160 };
161});