blob: a03e845246581ea4ee7995c6aa68834d90214451 [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
9define(['util'], function () {
10 "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
Akron889ec292018-11-19 17:56:01 +010075 /**
76 * Check, if the fragment contains any constraints
77 */
78 isEmpty : function () {
79 return this._operands.length > 0 ? false : true;
80 },
Akron68d28322018-08-27 15:02:42 +020081
82 /**
83 * Add fragment constraints to VC.
84 */
85 mergeWithVC : function () {
86 },
87
88
89 /**
90 * Get the element associated with the virtual corpus
91 */
92 element : function () {
93 if (this._element !== undefined) {
94 return this._element;
95 };
96
Akrond45a1702018-11-19 18:15:17 +010097 // Initialize element
Akron68d28322018-08-27 15:02:42 +020098 this._element = document.createElement('div');
99 this._element.classList.add('vc', 'fragment');
Akrond45a1702018-11-19 18:15:17 +0100100
101 // Prepend info text
102 this._element.addE('span').addT(loc.NEW_CONSTRAINT + ':');
103 this._frag = this._element.addE('div');
104
Akron68d28322018-08-27 15:02:42 +0200105 return this._element;
106 },
107
108
109 /**
110 * Update the whole object based on the underlying data structure
111 */
112 update : function() {
113
114 // <div class="docGroup" data-operation="and">
115 // <div class="doc">
116 // <span class="key">author</span>
117 // <span class="match">eq</span>
118 // <span class="value">Baum</span>
119 // </div>
120 // </div>
Akron889ec292018-11-19 17:56:01 +0100121 let root;
122 let l = this._operands.length;
Akron68d28322018-08-27 15:02:42 +0200123 if (l > 1) {
124
125 root = document.createElement('div');
126 root.setAttribute('class','docGroup');
127 root.setAttribute('data-operation', 'and');
128
Akron889ec292018-11-19 17:56:01 +0100129 for (let i in this._operands) {
Akron68d28322018-08-27 15:02:42 +0200130 root.appendChild(_doc(this._operands[i]));
131 };
132 }
133 else if (l == 1) {
134 root = _doc(this._operands[0]);
135 };
136
Akrond45a1702018-11-19 18:15:17 +0100137 this.element();
138 const e = this._frag;
Akron889ec292018-11-19 17:56:01 +0100139 if (l === 0) {
140 _removeChildren(e);
141 }
142 else if (e.firstChild)
Akron68d28322018-08-27 15:02:42 +0200143 e.replaceChild(root, e.firstChild);
144 else
145 e.appendChild(root);
146
147 return this;
Akronfa32c9d2018-11-20 15:39:18 +0100148 },
149
150 toQuery : function () {
151 if (this._operands.length === 0)
152 return '';
153
154 let str = '(' + this._operands.map(
155 function (item) {
156 return item[0] + ' = "' + new String(item[1]).quote() + '"';
157 }
158 ).join(" & ");
159
160 return str + ')';
161 }
Akron68d28322018-08-27 15:02:42 +0200162 }
163});