Akron | 68d2832 | 2018-08-27 15:02:42 +0200 | [diff] [blame^] | 1 | /** |
| 2 | * Create a virtual corpus fragment, |
| 3 | * that can be shown and merge with the |
| 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 | |
| 32 | |
| 33 | return { |
| 34 | |
| 35 | create : function () { |
| 36 | var obj = Object.create(this); |
| 37 | obj._operands = []; |
| 38 | return obj; |
| 39 | }, |
| 40 | |
| 41 | |
| 42 | /** |
| 43 | * Add document constraint to fragment |
| 44 | */ |
| 45 | add : function (key, value, type) { |
| 46 | for (var i in this._operands) { |
| 47 | var op = this._operands[i]; |
| 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) { |
| 61 | for (var i in this._operands) { |
| 62 | var op = this._operands[i]; |
| 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 | |
| 72 | |
| 73 | /** |
| 74 | * Add fragment constraints to VC. |
| 75 | */ |
| 76 | mergeWithVC : function () { |
| 77 | }, |
| 78 | |
| 79 | |
| 80 | /** |
| 81 | * Get the element associated with the virtual corpus |
| 82 | */ |
| 83 | element : function () { |
| 84 | if (this._element !== undefined) { |
| 85 | return this._element; |
| 86 | }; |
| 87 | |
| 88 | this._element = document.createElement('div'); |
| 89 | this._element.classList.add('vc', 'fragment'); |
| 90 | return this._element; |
| 91 | }, |
| 92 | |
| 93 | |
| 94 | /** |
| 95 | * Update the whole object based on the underlying data structure |
| 96 | */ |
| 97 | update : function() { |
| 98 | |
| 99 | // <div class="docGroup" data-operation="and"> |
| 100 | // <div class="doc"> |
| 101 | // <span class="key">author</span> |
| 102 | // <span class="match">eq</span> |
| 103 | // <span class="value">Baum</span> |
| 104 | // </div> |
| 105 | // </div> |
| 106 | var root; |
| 107 | var l = this._operands.length; |
| 108 | if (l > 1) { |
| 109 | |
| 110 | root = document.createElement('div'); |
| 111 | root.setAttribute('class','docGroup'); |
| 112 | root.setAttribute('data-operation', 'and'); |
| 113 | |
| 114 | for (var i in this._operands) { |
| 115 | root.appendChild(_doc(this._operands[i])); |
| 116 | }; |
| 117 | } |
| 118 | else if (l == 1) { |
| 119 | root = _doc(this._operands[0]); |
| 120 | }; |
| 121 | |
| 122 | var e = this.element(); |
| 123 | if (e.firstChild) |
| 124 | e.replaceChild(root, e.firstChild); |
| 125 | else |
| 126 | e.appendChild(root); |
| 127 | |
| 128 | return this; |
| 129 | } |
| 130 | } |
| 131 | }); |