blob: 9b87ee287b66eda32afdb805be34c3fdc9d4d28f [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
12 // Create a VC doc
13 function _doc (op) {
14 var doc = document.createElement('div');
15 doc.setAttribute('class','doc');
16
17 var key = doc.addE('span');
18 key.setAttribute('class','key');
19 key.addT(op[0]);
20
21 var match = doc.addE('span');
22 match.setAttribute('class','match');
23 match.addT('eq');
24
25 var value = doc.addE('span');
26 value.setAttribute('class', 'value');
27 value.addT(op[1]);
28
29 return doc;
30 };
31
Akron889ec292018-11-19 17:56:01 +010032 // Return object
Akron68d28322018-08-27 15:02:42 +020033 return {
34
35 create : function () {
Akron889ec292018-11-19 17:56:01 +010036 const obj = Object.create(this);
Akron68d28322018-08-27 15:02:42 +020037 obj._operands = [];
38 return obj;
39 },
40
41
42 /**
43 * Add document constraint to fragment
44 */
45 add : function (key, value, type) {
Akron889ec292018-11-19 17:56:01 +010046 for (let i in this._operands) {
47 let op = this._operands[i];
Akron68d28322018-08-27 15:02:42 +020048 if (op[0] === key && op[1] === value) {
49 array.splice(index, 1);
50 };
51 };
52 this._operands.push([key, value, type]);
53 this.update();
54 },
55
56
57 /**
58 * Remove document constraint from fragment
59 */
60 remove : function (key, value) {
Akron889ec292018-11-19 17:56:01 +010061 for (let i in this._operands) {
62 let op = this._operands[i];
Akron68d28322018-08-27 15:02:42 +020063 if (op[0] === key && op[1] === value) {
64 this._operands.splice(i, 1);
65 this.update();
66 return;
67 };
68 };
69 return;
70 },
71
Akron889ec292018-11-19 17:56:01 +010072 /**
73 * Check, if the fragment contains any constraints
74 */
75 isEmpty : function () {
76 return this._operands.length > 0 ? false : true;
77 },
Akron68d28322018-08-27 15:02:42 +020078
79 /**
80 * Add fragment constraints to VC.
81 */
82 mergeWithVC : function () {
83 },
84
85
86 /**
87 * Get the element associated with the virtual corpus
88 */
89 element : function () {
90 if (this._element !== undefined) {
91 return this._element;
92 };
93
94 this._element = document.createElement('div');
95 this._element.classList.add('vc', 'fragment');
96 return this._element;
97 },
98
99
100 /**
101 * Update the whole object based on the underlying data structure
102 */
103 update : function() {
104
105 // <div class="docGroup" data-operation="and">
106 // <div class="doc">
107 // <span class="key">author</span>
108 // <span class="match">eq</span>
109 // <span class="value">Baum</span>
110 // </div>
111 // </div>
Akron889ec292018-11-19 17:56:01 +0100112 let root;
113 let l = this._operands.length;
Akron68d28322018-08-27 15:02:42 +0200114 if (l > 1) {
115
116 root = document.createElement('div');
117 root.setAttribute('class','docGroup');
118 root.setAttribute('data-operation', 'and');
119
Akron889ec292018-11-19 17:56:01 +0100120 for (let i in this._operands) {
Akron68d28322018-08-27 15:02:42 +0200121 root.appendChild(_doc(this._operands[i]));
122 };
123 }
124 else if (l == 1) {
125 root = _doc(this._operands[0]);
126 };
127
Akron889ec292018-11-19 17:56:01 +0100128 const e = this.element();
129 if (l === 0) {
130 _removeChildren(e);
131 }
132 else if (e.firstChild)
Akron68d28322018-08-27 15:02:42 +0200133 e.replaceChild(root, e.firstChild);
134 else
135 e.appendChild(root);
136
137 return this;
138 }
139 }
140});