blob: f10dcf7ab36a7b809470ae94cc659b295daa7655 [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 () {
120 var menu = KorAP._vcKeyMenu;
121
122 // Add keyelement at the correct position
123 this._element.insertBefore(
124 menu.element(),
125 this._element.firstChild
126 );
127
128 var that = this;
129
130 // Set released method
131 menu.released(function (name, value, type) {
132 that.key(name);
133 // + ' # ' + type);
134 this.hide();
135 });
136
137 menu.show();
138 menu.focus();
139 }
Nils Diewald0e6992a2015-04-14 20:13:52 +0000140 };
141});