blob: 0f155ff0c634b9a30c05decf7a1d0836fb925d13 [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
Akron88d237e2020-10-21 08:05:18 +02009"use strict";
Akron2761d882020-10-13 10:35:09 +020010
Akron88d237e2020-10-21 08:05:18 +020011define(['vc/doc', 'util'], function (docClass) {
Akron68d28322018-08-27 15:02:42 +020012
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]);
Akron88d237e2020-10-21 08:05:18 +020032
Akron68d28322018-08-27 15:02:42 +020033 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) {
Akron88d237e2020-10-21 08:05:18 +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
Akron88d237e2020-10-21 08:05:18 +020097 const e = this._element = document.createElement('div');
98 e.classList.add('vc', 'fragment');
Akrond45a1702018-11-19 18:15:17 +010099
100 // Prepend info text
Akron88d237e2020-10-21 08:05:18 +0200101 e.addE('span').addT(loc.NEW_CONSTRAINT + ':');
102 this._frag = e.addE('div');
Akrond45a1702018-11-19 18:15:17 +0100103
Akron88d237e2020-10-21 08:05:18 +0200104 return e;
Akron68d28322018-08-27 15:02:42 +0200105 },
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]);
Akron88d237e2020-10-21 08:05:18 +0200118 doc.type(item[2] === "date" ? "date" : "string");
Akroncfe8ecc2018-11-20 18:46:16 +0100119 return doc;
120 }
121 );
122 },
123
Akron88d237e2020-10-21 08:05:18 +0200124
Akroncfe8ecc2018-11-20 18:46:16 +0100125 /**
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;
Akron2761d882020-10-13 10:35:09 +0200138 const l = this._operands.length;
Akron68d28322018-08-27 15:02:42 +0200139
Akron2761d882020-10-13 10:35:09 +0200140 if (l > 1) {
Akron68d28322018-08-27 15:02:42 +0200141 root = document.createElement('div');
142 root.setAttribute('class','docGroup');
143 root.setAttribute('data-operation', 'and');
Akron678c26f2020-10-09 08:52:50 +0200144 this._operands.forEach(i => root.appendChild(_doc(i)));
Akron68d28322018-08-27 15:02:42 +0200145 }
Akron2761d882020-10-13 10:35:09 +0200146
Akron68d28322018-08-27 15:02:42 +0200147 else if (l == 1) {
148 root = _doc(this._operands[0]);
149 };
150
Akron2761d882020-10-13 10:35:09 +0200151 // Init element
Akrond45a1702018-11-19 18:15:17 +0100152 this.element();
Akron2761d882020-10-13 10:35:09 +0200153
Akrond45a1702018-11-19 18:15:17 +0100154 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 () {
Akron2761d882020-10-13 10:35:09 +0200171
Akronfa32c9d2018-11-20 15:39:18 +0100172 if (this._operands.length === 0)
173 return '';
174
Akroncfe8ecc2018-11-20 18:46:16 +0100175 return this._operands.map(
Akronfa32c9d2018-11-20 15:39:18 +0100176 function (item) {
Akroncfe8ecc2018-11-20 18:46:16 +0100177 if (item[2] === "date") {
178 return item[0] + ' in ' + item[1];
179 };
Akron0c4cd222019-07-19 16:33:34 +0200180 return item[0] + ' = ' + new String(item[1]).quote();
Akronfa32c9d2018-11-20 15:39:18 +0100181 }
182 ).join(" & ");
Akronfa32c9d2018-11-20 15:39:18 +0100183 }
Akron68d28322018-08-27 15:02:42 +0200184 }
185});