blob: bf1905e21262c37a6e798ea56956b69a21ef6e25 [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) {
Akron2761d882020-10-13 10:35:09 +020010
Akron68d28322018-08-27 15:02:42 +020011 "use strict";
12
Akrond45a1702018-11-19 18:15:17 +010013 const loc = KorAP.Locale;
14 loc.NEW_CONSTRAINT = loc.NEW_CONSTRAINT || 'New Constraint';
15
Akron68d28322018-08-27 15:02:42 +020016 // Create a VC doc
17 function _doc (op) {
Akron2761d882020-10-13 10:35:09 +020018 const doc = document.createElement('div');
Akron68d28322018-08-27 15:02:42 +020019 doc.setAttribute('class','doc');
20
Akron2761d882020-10-13 10:35:09 +020021 const key = doc.addE('span');
Akron68d28322018-08-27 15:02:42 +020022 key.setAttribute('class','key');
23 key.addT(op[0]);
24
Akron2761d882020-10-13 10:35:09 +020025 const match = doc.addE('span');
Akron68d28322018-08-27 15:02:42 +020026 match.setAttribute('class','match');
27 match.addT('eq');
28
Akron2761d882020-10-13 10:35:09 +020029 const value = doc.addE('span');
Akron68d28322018-08-27 15:02:42 +020030 value.setAttribute('class', 'value');
31 value.addT(op[1]);
32
33 return doc;
34 };
35
Akron2761d882020-10-13 10:35:09 +020036
Akron889ec292018-11-19 17:56:01 +010037 // Return object
Akron68d28322018-08-27 15:02:42 +020038 return {
39
Akron2761d882020-10-13 10:35:09 +020040 /**
41 * Construct a new VC fragment.
42 */
Akron68d28322018-08-27 15:02:42 +020043 create : function () {
Akron889ec292018-11-19 17:56:01 +010044 const obj = Object.create(this);
Akron68d28322018-08-27 15:02:42 +020045 obj._operands = [];
46 return obj;
47 },
48
49
50 /**
51 * Add document constraint to fragment
52 */
53 add : function (key, value, type) {
Akron2761d882020-10-13 10:35:09 +020054 this._operands.forEach(function (op,i,arr) {
Akron68d28322018-08-27 15:02:42 +020055 if (op[0] === key && op[1] === value) {
Akron2761d882020-10-13 10:35:09 +020056 arr.splice(i,1);
57 }
58 });
Akron68d28322018-08-27 15:02:42 +020059 this._operands.push([key, value, type]);
60 this.update();
61 },
62
63
64 /**
65 * Remove document constraint from fragment
66 */
67 remove : function (key, value) {
Akron2761d882020-10-13 10:35:09 +020068 for (let i = 0; i < this._operands.length; i++) {
Akron889ec292018-11-19 17:56:01 +010069 let op = this._operands[i];
Akron68d28322018-08-27 15:02:42 +020070 if (op[0] === key && op[1] === value) {
71 this._operands.splice(i, 1);
72 this.update();
73 return;
74 };
75 };
76 return;
77 },
78
Akroncfe8ecc2018-11-20 18:46:16 +010079
Akron889ec292018-11-19 17:56:01 +010080 /**
81 * Check, if the fragment contains any constraints
82 */
83 isEmpty : function () {
84 return this._operands.length > 0 ? false : true;
85 },
Akron68d28322018-08-27 15:02:42 +020086
Akron68d28322018-08-27 15:02:42 +020087
88 /**
89 * Get the element associated with the virtual corpus
90 */
91 element : function () {
92 if (this._element !== undefined) {
93 return this._element;
94 };
95
Akrond45a1702018-11-19 18:15:17 +010096 // Initialize element
Akron68d28322018-08-27 15:02:42 +020097 this._element = document.createElement('div');
98 this._element.classList.add('vc', 'fragment');
Akrond45a1702018-11-19 18:15:17 +010099
100 // Prepend info text
101 this._element.addE('span').addT(loc.NEW_CONSTRAINT + ':');
102 this._frag = this._element.addE('div');
103
Akron68d28322018-08-27 15:02:42 +0200104 return this._element;
105 },
106
107
108 /**
Akroncfe8ecc2018-11-20 18:46:16 +0100109 * Return operands as document objects
110 */
111 documents : function () {
112 return this._operands.map(
113 function (item) {
Akron2761d882020-10-13 10:35:09 +0200114 const doc = docClass.create();
Akroncfe8ecc2018-11-20 18:46:16 +0100115 doc.key(item[0]);
116 doc.matchop("eq");
117 doc.value(item[1]);
118 if (item[2] === "date") {
119 doc.type("date");
120 }
121 else {
122 doc.type("string");
123 };
124 return doc;
125 }
126 );
127 },
128
129 /**
Akron68d28322018-08-27 15:02:42 +0200130 * Update the whole object based on the underlying data structure
131 */
132 update : function() {
133
134 // <div class="docGroup" data-operation="and">
135 // <div class="doc">
136 // <span class="key">author</span>
137 // <span class="match">eq</span>
138 // <span class="value">Baum</span>
139 // </div>
140 // </div>
Akron889ec292018-11-19 17:56:01 +0100141 let root;
Akron2761d882020-10-13 10:35:09 +0200142 const l = this._operands.length;
Akron68d28322018-08-27 15:02:42 +0200143
Akron2761d882020-10-13 10:35:09 +0200144 if (l > 1) {
Akron68d28322018-08-27 15:02:42 +0200145 root = document.createElement('div');
146 root.setAttribute('class','docGroup');
147 root.setAttribute('data-operation', 'and');
Akron678c26f2020-10-09 08:52:50 +0200148 this._operands.forEach(i => root.appendChild(_doc(i)));
Akron68d28322018-08-27 15:02:42 +0200149 }
Akron2761d882020-10-13 10:35:09 +0200150
Akron68d28322018-08-27 15:02:42 +0200151 else if (l == 1) {
152 root = _doc(this._operands[0]);
153 };
154
Akron2761d882020-10-13 10:35:09 +0200155 // Init element
Akrond45a1702018-11-19 18:15:17 +0100156 this.element();
Akron2761d882020-10-13 10:35:09 +0200157
Akrond45a1702018-11-19 18:15:17 +0100158 const e = this._frag;
Akron889ec292018-11-19 17:56:01 +0100159 if (l === 0) {
160 _removeChildren(e);
161 }
162 else if (e.firstChild)
Akron68d28322018-08-27 15:02:42 +0200163 e.replaceChild(root, e.firstChild);
164 else
165 e.appendChild(root);
166
167 return this;
Akronfa32c9d2018-11-20 15:39:18 +0100168 },
169
Akroncfe8ecc2018-11-20 18:46:16 +0100170
171 /**
172 * Stringification
173 */
Akronfa32c9d2018-11-20 15:39:18 +0100174 toQuery : function () {
Akron2761d882020-10-13 10:35:09 +0200175
Akronfa32c9d2018-11-20 15:39:18 +0100176 if (this._operands.length === 0)
177 return '';
178
Akroncfe8ecc2018-11-20 18:46:16 +0100179 return this._operands.map(
Akronfa32c9d2018-11-20 15:39:18 +0100180 function (item) {
Akroncfe8ecc2018-11-20 18:46:16 +0100181 if (item[2] === "date") {
182 return item[0] + ' in ' + item[1];
183 };
Akron0c4cd222019-07-19 16:33:34 +0200184 return item[0] + ' = ' + new String(item[1]).quote();
Akronfa32c9d2018-11-20 15:39:18 +0100185 }
186 ).join(" & ");
Akronfa32c9d2018-11-20 15:39:18 +0100187 }
Akron68d28322018-08-27 15:02:42 +0200188 }
189});