blob: f0c139725fa0b8c12a0970399621be4721e124e7 [file] [log] [blame]
Akron68d28322018-08-27 15:02:42 +02001/**
2 * Create a virtual corpus fragment,
Akron889ec292018-11-19 17:56:01 +01003 * that can be shown and merged with the
Akron68d28322018-08-27 15:02:42 +02004 * main VC.
5 *
6 * @author Nils Diewald
7 */
8
Akroncfe8ecc2018-11-20 18:46:16 +01009define(['vc/doc', 'util'], function (docClass) {
Akron68d28322018-08-27 15:02:42 +020010 "use strict";
11
Akrond45a1702018-11-19 18:15:17 +010012 const loc = KorAP.Locale;
13 loc.NEW_CONSTRAINT = loc.NEW_CONSTRAINT || 'New Constraint';
14
Akron68d28322018-08-27 15:02:42 +020015 // Create a VC doc
16 function _doc (op) {
17 var doc = document.createElement('div');
18 doc.setAttribute('class','doc');
19
20 var key = doc.addE('span');
21 key.setAttribute('class','key');
22 key.addT(op[0]);
23
24 var match = doc.addE('span');
25 match.setAttribute('class','match');
26 match.addT('eq');
27
28 var value = doc.addE('span');
29 value.setAttribute('class', 'value');
30 value.addT(op[1]);
31
32 return doc;
33 };
34
Akron889ec292018-11-19 17:56:01 +010035 // Return object
Akron68d28322018-08-27 15:02:42 +020036 return {
37
38 create : function () {
Akron889ec292018-11-19 17:56:01 +010039 const obj = Object.create(this);
Akron68d28322018-08-27 15:02:42 +020040 obj._operands = [];
41 return obj;
42 },
43
44
45 /**
46 * Add document constraint to fragment
47 */
48 add : function (key, value, type) {
Akron889ec292018-11-19 17:56:01 +010049 for (let i in this._operands) {
50 let op = this._operands[i];
Akron68d28322018-08-27 15:02:42 +020051 if (op[0] === key && op[1] === value) {
52 array.splice(index, 1);
53 };
54 };
55 this._operands.push([key, value, type]);
56 this.update();
57 },
58
59
60 /**
61 * Remove document constraint from fragment
62 */
63 remove : function (key, value) {
Akron889ec292018-11-19 17:56:01 +010064 for (let i in this._operands) {
65 let op = this._operands[i];
Akron68d28322018-08-27 15:02:42 +020066 if (op[0] === key && op[1] === value) {
67 this._operands.splice(i, 1);
68 this.update();
69 return;
70 };
71 };
72 return;
73 },
74
Akroncfe8ecc2018-11-20 18:46:16 +010075
Akron889ec292018-11-19 17:56:01 +010076 /**
77 * Check, if the fragment contains any constraints
78 */
79 isEmpty : function () {
80 return this._operands.length > 0 ? false : true;
81 },
Akron68d28322018-08-27 15:02:42 +020082
Akron68d28322018-08-27 15:02:42 +020083
84 /**
85 * Get the element associated with the virtual corpus
86 */
87 element : function () {
88 if (this._element !== undefined) {
89 return this._element;
90 };
91
Akrond45a1702018-11-19 18:15:17 +010092 // Initialize element
Akron68d28322018-08-27 15:02:42 +020093 this._element = document.createElement('div');
94 this._element.classList.add('vc', 'fragment');
Akrond45a1702018-11-19 18:15:17 +010095
96 // Prepend info text
97 this._element.addE('span').addT(loc.NEW_CONSTRAINT + ':');
98 this._frag = this._element.addE('div');
99
Akron68d28322018-08-27 15:02:42 +0200100 return this._element;
101 },
102
103
104 /**
Akroncfe8ecc2018-11-20 18:46:16 +0100105 * Return operands as document objects
106 */
107 documents : function () {
108 return this._operands.map(
109 function (item) {
110 let doc = docClass.create();
111 doc.key(item[0]);
112 doc.matchop("eq");
113 doc.value(item[1]);
114 if (item[2] === "date") {
115 doc.type("date");
116 }
117 else {
118 doc.type("string");
119 };
120 return doc;
121 }
122 );
123 },
124
125 /**
Akron68d28322018-08-27 15:02:42 +0200126 * Update the whole object based on the underlying data structure
127 */
128 update : function() {
129
130 // <div class="docGroup" data-operation="and">
131 // <div class="doc">
132 // <span class="key">author</span>
133 // <span class="match">eq</span>
134 // <span class="value">Baum</span>
135 // </div>
136 // </div>
Akron889ec292018-11-19 17:56:01 +0100137 let root;
138 let l = this._operands.length;
Akron68d28322018-08-27 15:02:42 +0200139 if (l > 1) {
140
141 root = document.createElement('div');
142 root.setAttribute('class','docGroup');
143 root.setAttribute('data-operation', 'and');
144
Akron889ec292018-11-19 17:56:01 +0100145 for (let i in this._operands) {
Akron68d28322018-08-27 15:02:42 +0200146 root.appendChild(_doc(this._operands[i]));
147 };
148 }
149 else if (l == 1) {
150 root = _doc(this._operands[0]);
151 };
152
Akrond45a1702018-11-19 18:15:17 +0100153 this.element();
154 const e = this._frag;
Akron889ec292018-11-19 17:56:01 +0100155 if (l === 0) {
156 _removeChildren(e);
157 }
158 else if (e.firstChild)
Akron68d28322018-08-27 15:02:42 +0200159 e.replaceChild(root, e.firstChild);
160 else
161 e.appendChild(root);
162
163 return this;
Akronfa32c9d2018-11-20 15:39:18 +0100164 },
165
Akroncfe8ecc2018-11-20 18:46:16 +0100166
167 /**
168 * Stringification
169 */
Akronfa32c9d2018-11-20 15:39:18 +0100170 toQuery : function () {
171 if (this._operands.length === 0)
172 return '';
173
Akroncfe8ecc2018-11-20 18:46:16 +0100174 return this._operands.map(
Akronfa32c9d2018-11-20 15:39:18 +0100175 function (item) {
Akroncfe8ecc2018-11-20 18:46:16 +0100176 if (item[2] === "date") {
177 return item[0] + ' in ' + item[1];
178 };
Akron0c4cd222019-07-19 16:33:34 +0200179 return item[0] + ' = ' + new String(item[1]).quote();
Akronfa32c9d2018-11-20 15:39:18 +0100180 }
181 ).join(" & ");
Akronfa32c9d2018-11-20 15:39:18 +0100182 }
Akron68d28322018-08-27 15:02:42 +0200183 }
184});