blob: 31101ef1b1be417e588e289c63f6dc1ace99ca6c [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).
26 upgradeTo(this);
27
28 if (parent !== undefined)
29 obj._parent = parent;
30
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)
41 return null;
42
43 // Set JSON-LD type
44 var newDoc = docClass.create(this._parent, {
45 "@type" : "koral:doc",
46 "value" : "",
47 "key" : v
48 });
49
50 // Unspecified document on root
51 if (this._parent.ldType() === null) {
52 this._parent.root(newDoc);
53 this.destroy();
54 }
55
56 // Unspecified document in group
57 else {
58 this._parent.replaceOperand(this, newDoc);
59 };
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)
71 return this.element();
72
73 // Remove element content
74 _removeChildren(this._element);
75
76 var ellipsis = document.createElement('span');
77 ellipsis.appendChild(document.createTextNode(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) {
89 var op = this.operators(
90 false,
91 false,
92 true
93 );
94
95 this._element.appendChild(
96 op.element()
97 );
98 };
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)
109 return this._element;
110 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;
123
Nils Diewald4c221252015-04-21 20:19:25 +0000124 // Add key menu element at the correct position
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000125 this._element.insertBefore(
126 menu.element(),
127 this._element.firstChild
128 );
129
130 var that = this;
131
132 // Set released method
Nils Diewald4c221252015-04-21 20:19:25 +0000133 menu.released(function (key, type) {
134 // Set chosen key and type - will return a doc
135 that.key(key).type(type).update();
Nils Diewald1fcb2ad2015-04-20 19:19:18 +0000136 this.hide();
137 });
138
139 menu.show();
140 menu.focus();
141 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000142 };
143});