Akron | 68d2832 | 2018-08-27 15:02:42 +0200 | [diff] [blame] | 1 | /** |
| 2 | * Create a virtual corpus fragment, |
Akron | 889ec29 | 2018-11-19 17:56:01 +0100 | [diff] [blame^] | 3 | * that can be shown and merged with the |
Akron | 68d2832 | 2018-08-27 15:02:42 +0200 | [diff] [blame] | 4 | * main VC. |
| 5 | * |
| 6 | * @author Nils Diewald |
| 7 | */ |
| 8 | |
| 9 | define(['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 | |
Akron | 889ec29 | 2018-11-19 17:56:01 +0100 | [diff] [blame^] | 32 | // Return object |
Akron | 68d2832 | 2018-08-27 15:02:42 +0200 | [diff] [blame] | 33 | return { |
| 34 | |
| 35 | create : function () { |
Akron | 889ec29 | 2018-11-19 17:56:01 +0100 | [diff] [blame^] | 36 | const obj = Object.create(this); |
Akron | 68d2832 | 2018-08-27 15:02:42 +0200 | [diff] [blame] | 37 | obj._operands = []; |
| 38 | return obj; |
| 39 | }, |
| 40 | |
| 41 | |
| 42 | /** |
| 43 | * Add document constraint to fragment |
| 44 | */ |
| 45 | add : function (key, value, type) { |
Akron | 889ec29 | 2018-11-19 17:56:01 +0100 | [diff] [blame^] | 46 | for (let i in this._operands) { |
| 47 | let op = this._operands[i]; |
Akron | 68d2832 | 2018-08-27 15:02:42 +0200 | [diff] [blame] | 48 | 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) { |
Akron | 889ec29 | 2018-11-19 17:56:01 +0100 | [diff] [blame^] | 61 | for (let i in this._operands) { |
| 62 | let op = this._operands[i]; |
Akron | 68d2832 | 2018-08-27 15:02:42 +0200 | [diff] [blame] | 63 | if (op[0] === key && op[1] === value) { |
| 64 | this._operands.splice(i, 1); |
| 65 | this.update(); |
| 66 | return; |
| 67 | }; |
| 68 | }; |
| 69 | return; |
| 70 | }, |
| 71 | |
Akron | 889ec29 | 2018-11-19 17:56:01 +0100 | [diff] [blame^] | 72 | /** |
| 73 | * Check, if the fragment contains any constraints |
| 74 | */ |
| 75 | isEmpty : function () { |
| 76 | return this._operands.length > 0 ? false : true; |
| 77 | }, |
Akron | 68d2832 | 2018-08-27 15:02:42 +0200 | [diff] [blame] | 78 | |
| 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> |
Akron | 889ec29 | 2018-11-19 17:56:01 +0100 | [diff] [blame^] | 112 | let root; |
| 113 | let l = this._operands.length; |
Akron | 68d2832 | 2018-08-27 15:02:42 +0200 | [diff] [blame] | 114 | if (l > 1) { |
| 115 | |
| 116 | root = document.createElement('div'); |
| 117 | root.setAttribute('class','docGroup'); |
| 118 | root.setAttribute('data-operation', 'and'); |
| 119 | |
Akron | 889ec29 | 2018-11-19 17:56:01 +0100 | [diff] [blame^] | 120 | for (let i in this._operands) { |
Akron | 68d2832 | 2018-08-27 15:02:42 +0200 | [diff] [blame] | 121 | root.appendChild(_doc(this._operands[i])); |
| 122 | }; |
| 123 | } |
| 124 | else if (l == 1) { |
| 125 | root = _doc(this._operands[0]); |
| 126 | }; |
| 127 | |
Akron | 889ec29 | 2018-11-19 17:56:01 +0100 | [diff] [blame^] | 128 | const e = this.element(); |
| 129 | if (l === 0) { |
| 130 | _removeChildren(e); |
| 131 | } |
| 132 | else if (e.firstChild) |
Akron | 68d2832 | 2018-08-27 15:02:42 +0200 | [diff] [blame] | 133 | e.replaceChild(root, e.firstChild); |
| 134 | else |
| 135 | e.appendChild(root); |
| 136 | |
| 137 | return this; |
| 138 | } |
| 139 | } |
| 140 | }); |