Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 1 | /** |
Nils Diewald | 4c22125 | 2015-04-21 20:19:25 +0000 | [diff] [blame] | 2 | * A new document criterion |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 3 | */ |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 4 | "use strict"; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 5 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 6 | define([ |
Nils Diewald | 4c22125 | 2015-04-21 20:19:25 +0000 | [diff] [blame] | 7 | 'vc/jsonld', |
| 8 | 'vc/rewritelist', |
Nils Diewald | f0c4f11 | 2015-05-05 12:56:59 +0000 | [diff] [blame] | 9 | 'vc/stringval', |
Akron | 587e4d9 | 2018-08-31 12:44:26 +0200 | [diff] [blame] | 10 | 'vc/docgroupref', |
Nils Diewald | 4c22125 | 2015-04-21 20:19:25 +0000 | [diff] [blame] | 11 | 'util' |
Akron | 587e4d9 | 2018-08-31 12:44:26 +0200 | [diff] [blame] | 12 | ], function (jsonldClass, rewriteListClass, stringValClass, docGroupRefClass) { |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 13 | |
Akron | 1bdf527 | 2018-07-24 18:51:30 +0200 | [diff] [blame] | 14 | /* |
| 15 | * TODO: |
| 16 | * Improve error handling by using window.onerror() to |
| 17 | * capture thrown errors and log them. |
| 18 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 19 | |
Akron | 31d8994 | 2018-04-06 16:44:51 +0200 | [diff] [blame] | 20 | const errstr802 = "Match type is not supported by value type"; |
Akron | 1bdf527 | 2018-07-24 18:51:30 +0200 | [diff] [blame] | 21 | const errstr804 = "Unknown value type"; |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 22 | const loc = KorAP.Locale; |
Nils Diewald | 4c22125 | 2015-04-21 20:19:25 +0000 | [diff] [blame] | 23 | loc.EMPTY = loc.EMPTY || '⋯'; |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 24 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 25 | return { |
Nils Diewald | 4c22125 | 2015-04-21 20:19:25 +0000 | [diff] [blame] | 26 | |
| 27 | // The JSON-LD type |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 28 | _ldType : "doc", |
Nils Diewald | 4c22125 | 2015-04-21 20:19:25 +0000 | [diff] [blame] | 29 | |
| 30 | // The object ... maybe not important |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 31 | _obj : function () { return '???'; /*KorAP.Doc*/ }, |
| 32 | |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 33 | |
Nils Diewald | 4c22125 | 2015-04-21 20:19:25 +0000 | [diff] [blame] | 34 | /** |
| 35 | * Create a new document criterion |
| 36 | * by passing the parent object and a json construct. |
| 37 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 38 | create : function (parent, json) { |
Nils Diewald | 4c22125 | 2015-04-21 20:19:25 +0000 | [diff] [blame] | 39 | |
| 40 | // Create the object, inheriting from Json-LD class |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 41 | const obj = Object(jsonldClass). |
| 42 | create(). |
| 43 | upgradeTo(this). |
| 44 | fromJson(json); |
Nils Diewald | 4c22125 | 2015-04-21 20:19:25 +0000 | [diff] [blame] | 45 | |
Akron | 55a343b | 2018-04-06 19:57:36 +0200 | [diff] [blame] | 46 | if (obj === undefined) { |
| 47 | console.log(json); |
Akron | d2474aa | 2018-08-28 12:06:27 +0200 | [diff] [blame] | 48 | return; |
Akron | 55a343b | 2018-04-06 19:57:36 +0200 | [diff] [blame] | 49 | }; |
| 50 | |
Nils Diewald | 4c22125 | 2015-04-21 20:19:25 +0000 | [diff] [blame] | 51 | // Bind the parent |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 52 | if (parent !== undefined) |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 53 | obj._parent = parent; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 54 | |
| 55 | obj.__changed = true; |
| 56 | return obj; |
| 57 | }, |
| 58 | |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 59 | |
Nils Diewald | 4c22125 | 2015-04-21 20:19:25 +0000 | [diff] [blame] | 60 | /** |
| 61 | * Update the elements content. |
| 62 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 63 | update : function () { |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 64 | const t = this; |
| 65 | |
| 66 | if (t._element === undefined) |
| 67 | return t.element(); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 68 | |
| 69 | // Get element |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 70 | const e = t._element; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 71 | |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 72 | // Check if there is a change in the underlying data |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 73 | if (!t.__changed) |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 74 | return e; |
| 75 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 76 | // Set ref - TODO: Cleanup! |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 77 | e.refTo = t; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 78 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 79 | |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 80 | // Was rewritten |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 81 | if (t.rewrites() !== undefined) { |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 82 | e.classList.add("rewritten"); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 83 | }; |
| 84 | |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 85 | // Added key |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 86 | const keyE = t._keyE = document.createElement('span'); |
| 87 | keyE.setAttribute('class', 'key'); |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 88 | |
| 89 | // Change key |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 90 | keyE.addEventListener('click', t._changeKey.bind(t)); |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 91 | |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 92 | if (t.key()) { |
| 93 | const k = t.key(); |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 94 | if (loc['VC_' + k] !== undefined) |
| 95 | k = loc['VC_' + k]; |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 96 | keyE.addT(k); |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | // Added match operator |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 100 | const matchopE = t._matchopE = document.createElement('span'); |
| 101 | matchopE.setAttribute('data-type', t.type()); |
| 102 | matchopE.setAttribute('class', 'match'); |
| 103 | matchopE.addT(t.matchop()); |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 104 | |
| 105 | // Change matchop |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 106 | t._matchopE.addEventListener( |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 107 | 'click', |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 108 | t._changeMatchop.bind(t) |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 109 | ); |
| 110 | |
| 111 | // Added value operator |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 112 | const valueE = t._valueE = document.createElement('span'); |
| 113 | valueE.setAttribute('data-type', t.type()); |
| 114 | valueE.setAttribute('class', 'value'); |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 115 | |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 116 | if (t.value()) { |
| 117 | valueE.addT(t.value()); |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 118 | } |
| 119 | else { |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 120 | valueE.addT(loc.EMPTY); |
| 121 | valueE.classList.add('unspecified'); |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 122 | }; |
| 123 | |
| 124 | // Change value |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 125 | valueE.addEventListener( |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 126 | 'click', |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 127 | t._changeValue.bind(t) |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 128 | ); |
| 129 | |
| 130 | // Remove all element children |
| 131 | _removeChildren(e); |
| 132 | |
| 133 | // Add spans |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 134 | e.appendChild(keyE); |
| 135 | e.appendChild(matchopE); |
| 136 | e.appendChild(valueE); |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 137 | |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 138 | t.__changed = false; |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 139 | |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 140 | if (t._rewrites !== undefined) { |
| 141 | e.appendChild(t._rewrites.element()); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 142 | }; |
| 143 | |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 144 | if (t._parent !== undefined) { |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 145 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 146 | // Set operators |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 147 | // Append new operators |
| 148 | e.appendChild(t.operators( |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 149 | true, |
| 150 | true, |
| 151 | true |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 152 | ).element()); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 153 | }; |
| 154 | |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 155 | if (KorAP.vc){ |
| 156 | KorAP.vc.element().dispatchEvent( |
| 157 | new CustomEvent('vcChange', {'detail' : t}) |
| 158 | ); |
hebasta | a0282be | 2018-12-05 16:58:00 +0100 | [diff] [blame] | 159 | } |
| 160 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 161 | return e; |
| 162 | }, |
| 163 | |
Nils Diewald | 4c22125 | 2015-04-21 20:19:25 +0000 | [diff] [blame] | 164 | |
| 165 | /** |
| 166 | * Get the associated element |
| 167 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 168 | element : function () { |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 169 | const t = this; |
| 170 | if (t._element !== undefined) |
| 171 | return t._element; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 172 | |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 173 | t._element = document.createElement('div'); |
| 174 | t._element.setAttribute('class', 'doc'); |
| 175 | t.update(); |
| 176 | return t._element; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 177 | }, |
| 178 | |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 179 | |
Nils Diewald | 4c22125 | 2015-04-21 20:19:25 +0000 | [diff] [blame] | 180 | /** |
| 181 | * Wrap a new operation around the doc element |
| 182 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 183 | wrap : function (op) { |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 184 | const parent = this.parent(); |
| 185 | const group = require('vc/docgroup').create(parent); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 186 | group.operation(op); |
| 187 | group.append(this); |
| 188 | group.append(); |
| 189 | return parent.replaceOperand(this, group).update(); |
| 190 | }, |
| 191 | |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 192 | |
Akron | 587e4d9 | 2018-08-31 12:44:26 +0200 | [diff] [blame] | 193 | replaceWith : function (op) { |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 194 | const p = this.parent(); |
Akron | 587e4d9 | 2018-08-31 12:44:26 +0200 | [diff] [blame] | 195 | |
| 196 | if (p.ldType() === 'docGroup') { |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 197 | p.replaceOperand(this, op); |
Akron | 587e4d9 | 2018-08-31 12:44:26 +0200 | [diff] [blame] | 198 | } |
| 199 | else if (p.ldType() == null) { |
| 200 | p.root(op); |
| 201 | }; |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 202 | |
Akron | 587e4d9 | 2018-08-31 12:44:26 +0200 | [diff] [blame] | 203 | p.update(); |
| 204 | |
| 205 | this.destroy(); |
| 206 | }, |
| 207 | |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 208 | |
Nils Diewald | 4c22125 | 2015-04-21 20:19:25 +0000 | [diff] [blame] | 209 | /** |
| 210 | * Deserialize from json |
| 211 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 212 | fromJson : function (json) { |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 213 | const t = this; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 214 | if (json === undefined) |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 215 | return t; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 216 | |
| 217 | if (json["@type"] === undefined) { |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 218 | KorAP.log(701, "JSON-LD group has no @type attribute"); |
| 219 | return; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 220 | }; |
| 221 | |
| 222 | if (json["value"] === undefined || |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 223 | typeof json["value"] != 'string') { |
| 224 | KorAP.log(805, "Value is invalid"); |
| 225 | return; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 226 | }; |
| 227 | |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 228 | let rewrite; |
Akron | ea4e905 | 2017-07-06 16:12:05 +0200 | [diff] [blame] | 229 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 230 | // There is a defined key |
| 231 | if (json["key"] !== undefined && |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 232 | typeof json["key"] === 'string') { |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 233 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 234 | // Set key |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 235 | t.key(json["key"]); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 236 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 237 | // Set match operation |
| 238 | if (json["match"] !== undefined) { |
| 239 | if (typeof json["match"] === 'string') { |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 240 | t.matchop(json["match"]); |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 241 | } |
| 242 | else { |
Akron | 31d8994 | 2018-04-06 16:44:51 +0200 | [diff] [blame] | 243 | KorAP.log(802, errstr802); |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 244 | return; |
| 245 | }; |
| 246 | }; |
Akron | 31d8994 | 2018-04-06 16:44:51 +0200 | [diff] [blame] | 247 | |
Akron | ddc98a7 | 2018-04-06 17:33:52 +0200 | [diff] [blame] | 248 | // Type is unspecified - but may be known by the menu |
| 249 | if (json["type"] === undefined && KorAP._vcKeyMenu) { |
| 250 | |
| 251 | // Check the VC list if the field is known |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 252 | const type = KorAP._vcKeyMenu.typeOf(t.key()); |
Akron | ddc98a7 | 2018-04-06 17:33:52 +0200 | [diff] [blame] | 253 | if (type != undefined) { |
| 254 | json["type"] = "type:" + type; |
| 255 | }; |
| 256 | }; |
| 257 | |
| 258 | // Type is still undefined |
Akron | 31d8994 | 2018-04-06 16:44:51 +0200 | [diff] [blame] | 259 | if (json["type"] === undefined) { |
Akron | ddc98a7 | 2018-04-06 17:33:52 +0200 | [diff] [blame] | 260 | |
Akron | 31d8994 | 2018-04-06 16:44:51 +0200 | [diff] [blame] | 261 | // Check match type |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 262 | if (!KorAP._validUnspecMatchRE.test(t.matchop())) { |
Akron | 31d8994 | 2018-04-06 16:44:51 +0200 | [diff] [blame] | 263 | KorAP.log(802, errstr802); |
| 264 | |
| 265 | // Rewrite method |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 266 | t.matchop('eq'); |
Akron | 31d8994 | 2018-04-06 16:44:51 +0200 | [diff] [blame] | 267 | rewrite = 'modification'; |
| 268 | }; |
| 269 | |
| 270 | // Set string value |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 271 | t.value(json["value"]); |
Akron | 31d8994 | 2018-04-06 16:44:51 +0200 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | // Field is string type |
| 275 | else if (json["type"] == "type:string") { |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 276 | t.type("string"); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 277 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 278 | // Check match type |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 279 | if (!KorAP._validStringMatchRE.test(t.matchop())) { |
Akron | 31d8994 | 2018-04-06 16:44:51 +0200 | [diff] [blame] | 280 | KorAP.log(802, errstr802); |
| 281 | |
| 282 | // Rewrite method |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 283 | t.matchop('eq'); |
Akron | 31d8994 | 2018-04-06 16:44:51 +0200 | [diff] [blame] | 284 | rewrite = 'modification'; |
| 285 | }; |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 286 | |
Akron | 31d8994 | 2018-04-06 16:44:51 +0200 | [diff] [blame] | 287 | // Set string value |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 288 | t.value(json["value"]); |
Akron | 31d8994 | 2018-04-06 16:44:51 +0200 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | // Field is specified |
| 292 | else if (json["type"] == "type:text") { |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 293 | t.type("text"); |
Akron | 31d8994 | 2018-04-06 16:44:51 +0200 | [diff] [blame] | 294 | |
| 295 | // Check match type |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 296 | if (!KorAP._validTextMatchRE.test(t.matchop())) { |
Akron | 31d8994 | 2018-04-06 16:44:51 +0200 | [diff] [blame] | 297 | KorAP.log(802, errstr802); |
Akron | ea4e905 | 2017-07-06 16:12:05 +0200 | [diff] [blame] | 298 | |
| 299 | // Rewrite method |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 300 | t.matchop('eq'); |
Akron | ea4e905 | 2017-07-06 16:12:05 +0200 | [diff] [blame] | 301 | rewrite = 'modification'; |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 302 | }; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 303 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 304 | // Set string value |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 305 | t.value(json["value"]); |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 306 | } |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 307 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 308 | // Key is a date |
| 309 | else if (json["type"] === "type:date") { |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 310 | t.type("date"); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 311 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 312 | if (json["value"] !== undefined && |
| 313 | KorAP._validDateRE.test(json["value"])) { |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 314 | |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 315 | if (!KorAP._validDateMatchRE.test(t.matchop())) { |
Akron | 31d8994 | 2018-04-06 16:44:51 +0200 | [diff] [blame] | 316 | KorAP.log(802, errstr802); |
Akron | ea4e905 | 2017-07-06 16:12:05 +0200 | [diff] [blame] | 317 | |
| 318 | // Rewrite method |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 319 | t.matchop('eq'); |
Akron | ea4e905 | 2017-07-06 16:12:05 +0200 | [diff] [blame] | 320 | rewrite = 'modification'; |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 321 | }; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 322 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 323 | // Set value |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 324 | t.value(json["value"]); |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 325 | } |
| 326 | else { |
| 327 | KorAP.log(806, "Value is not a valid date string"); |
| 328 | return; |
| 329 | }; |
| 330 | } |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 331 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 332 | // Key is a regular expression |
| 333 | else if (json["type"] === "type:regex") { |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 334 | t.type("regex"); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 335 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 336 | try { |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 337 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 338 | // Try to create a regular expression |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 339 | let check = new RegExp(json["value"]); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 340 | |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 341 | if (!KorAP._validStringMatchRE.test(t.matchop())) { |
Akron | 31d8994 | 2018-04-06 16:44:51 +0200 | [diff] [blame] | 342 | KorAP.log(802, errstr802); |
Akron | ea4e905 | 2017-07-06 16:12:05 +0200 | [diff] [blame] | 343 | |
| 344 | // Rewrite method |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 345 | t.matchop('eq'); |
Akron | ea4e905 | 2017-07-06 16:12:05 +0200 | [diff] [blame] | 346 | rewrite = 'modification'; |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 347 | }; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 348 | |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 349 | t.value(json["value"]); |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 350 | } |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 351 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 352 | catch (e) { |
| 353 | KorAP.log(807, "Value is not a valid regular expression"); |
| 354 | return; |
| 355 | }; |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 356 | t.type("regex"); |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 357 | } |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 358 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 359 | else { |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 360 | KorAP.log(804, errstr804 + ": " + t.type()); |
| 361 | throw new Error(errstr804 + ": " + t.type()); |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 362 | }; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 363 | }; |
| 364 | |
Akron | ea4e905 | 2017-07-06 16:12:05 +0200 | [diff] [blame] | 365 | // Rewrite coming from the server |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 366 | if (json["rewrites"] !== undefined) { |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 367 | t.rewrite(json["rewrites"]); |
Akron | ea4e905 | 2017-07-06 16:12:05 +0200 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | // Rewrite coming from Kalamar |
| 371 | else if (rewrite !== undefined) { |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 372 | t.rewrite(rewrite); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 373 | }; |
Akron | ea4e905 | 2017-07-06 16:12:05 +0200 | [diff] [blame] | 374 | |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 375 | return t; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 376 | }, |
| 377 | |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 378 | |
Nils Diewald | 4c22125 | 2015-04-21 20:19:25 +0000 | [diff] [blame] | 379 | /** |
| 380 | * Get or set the key |
| 381 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 382 | key : function (value) { |
| 383 | if (arguments.length === 1) { |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 384 | this._key = value; |
| 385 | this._changed(); |
| 386 | return this; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 387 | }; |
| 388 | return this._key; |
| 389 | }, |
| 390 | |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 391 | |
Nils Diewald | 4c22125 | 2015-04-21 20:19:25 +0000 | [diff] [blame] | 392 | // Click on the key, show me the menu |
| 393 | _changeKey : function (e) { |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 394 | const menu = KorAP._vcKeyMenu; |
Nils Diewald | 4c22125 | 2015-04-21 20:19:25 +0000 | [diff] [blame] | 395 | |
| 396 | // Insert menu |
| 397 | this._element.insertBefore( |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 398 | menu.element(), |
| 399 | this._keyE |
Nils Diewald | 4c22125 | 2015-04-21 20:19:25 +0000 | [diff] [blame] | 400 | ); |
| 401 | |
| 402 | // Release event |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 403 | const that = this; |
Nils Diewald | 4c22125 | 2015-04-21 20:19:25 +0000 | [diff] [blame] | 404 | menu.released(function (key, type) { |
Nils Diewald | 4c22125 | 2015-04-21 20:19:25 +0000 | [diff] [blame] | 405 | |
Akron | 587e4d9 | 2018-08-31 12:44:26 +0200 | [diff] [blame] | 406 | if (type === 'ref') { |
| 407 | // KorAP._delete.bind(that); |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 408 | that.replaceWith( |
| 409 | docGroupRefClass.create(that.parent()) |
| 410 | ); |
Akron | 587e4d9 | 2018-08-31 12:44:26 +0200 | [diff] [blame] | 411 | } |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 412 | |
Akron | 587e4d9 | 2018-08-31 12:44:26 +0200 | [diff] [blame] | 413 | else { |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 414 | const doc = that.key(key).type(type); |
Nils Diewald | 4c22125 | 2015-04-21 20:19:25 +0000 | [diff] [blame] | 415 | |
Akron | 587e4d9 | 2018-08-31 12:44:26 +0200 | [diff] [blame] | 416 | // This may not be compatible - then switch to default |
| 417 | doc.matchop(doc.matchop()); |
| 418 | doc.value(doc.value()); |
| 419 | |
| 420 | // Update the doc |
| 421 | doc.update(); |
| 422 | }; |
Nils Diewald | 4c22125 | 2015-04-21 20:19:25 +0000 | [diff] [blame] | 423 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 424 | // hide! |
| 425 | this.hide(); |
Nils Diewald | 4c22125 | 2015-04-21 20:19:25 +0000 | [diff] [blame] | 426 | }); |
| 427 | |
| 428 | // TODO: Highlight the old value! |
| 429 | menu.show(); |
| 430 | menu.focus(); |
| 431 | }, |
| 432 | |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 433 | |
Nils Diewald | 4c22125 | 2015-04-21 20:19:25 +0000 | [diff] [blame] | 434 | /** |
| 435 | * Get or set the match operator |
| 436 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 437 | matchop : function (match) { |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 438 | const t = this; |
Akron | 31d8994 | 2018-04-06 16:44:51 +0200 | [diff] [blame] | 439 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 440 | if (arguments.length === 1) { |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 441 | const m = match.replace(/^match:/, ''); |
Akron | 31d8994 | 2018-04-06 16:44:51 +0200 | [diff] [blame] | 442 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 443 | if ( |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 444 | (t._type == undefined) // && KorAP._validUnspecMatchRE.test(m)) |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 445 | || |
| 446 | ( |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 447 | (t._type === 'string' || t._type === 'regex') && |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 448 | KorAP._validStringMatchRE.test(m) |
| 449 | ) |
| 450 | || |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 451 | (t._type === 'text' && KorAP._validTextMatchRE.test(m)) |
Akron | 31d8994 | 2018-04-06 16:44:51 +0200 | [diff] [blame] | 452 | || |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 453 | (t._type === 'date' && KorAP._validDateMatchRE.test(m)) |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 454 | ) { |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 455 | t._matchop = m; |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 456 | } |
| 457 | else { |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 458 | t._matchop = "eq"; |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 459 | }; |
Nils Diewald | 4c22125 | 2015-04-21 20:19:25 +0000 | [diff] [blame] | 460 | |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 461 | t._changed(); |
| 462 | return t; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 463 | }; |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 464 | |
| 465 | return t._matchop || "eq"; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 466 | }, |
| 467 | |
Nils Diewald | 4c22125 | 2015-04-21 20:19:25 +0000 | [diff] [blame] | 468 | |
| 469 | // Click on the match operator, show me the menu |
| 470 | _changeMatchop : function (e) { |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 471 | const menu = KorAP._vcMatchopMenu[this.type()]; |
Nils Diewald | 4c22125 | 2015-04-21 20:19:25 +0000 | [diff] [blame] | 472 | |
| 473 | if (menu === undefined) { |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 474 | KorAP.log(0, "Unable to init menu for " + this.type()); |
| 475 | return; |
Nils Diewald | 4c22125 | 2015-04-21 20:19:25 +0000 | [diff] [blame] | 476 | }; |
| 477 | |
| 478 | // Insert menu |
| 479 | this._element.insertBefore( |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 480 | menu.element(), |
| 481 | this._matchopE |
Nils Diewald | 4c22125 | 2015-04-21 20:19:25 +0000 | [diff] [blame] | 482 | ); |
| 483 | |
| 484 | // Release event |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 485 | const that = this; |
Nils Diewald | 4c22125 | 2015-04-21 20:19:25 +0000 | [diff] [blame] | 486 | menu.released(function (mo) { |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 487 | that.matchop(mo).update(); |
| 488 | this.hide(); |
Nils Diewald | 4c22125 | 2015-04-21 20:19:25 +0000 | [diff] [blame] | 489 | }); |
| 490 | |
| 491 | menu.show(); |
| 492 | menu.focus(); |
| 493 | }, |
| 494 | |
| 495 | |
| 496 | /** |
| 497 | * Get or set the type |
| 498 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 499 | type : function (type) { |
| 500 | if (arguments.length === 1) { |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 501 | this._type = type; |
| 502 | this._changed(); |
| 503 | return this; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 504 | }; |
| 505 | return this._type || "string"; |
| 506 | }, |
| 507 | |
Nils Diewald | 4c22125 | 2015-04-21 20:19:25 +0000 | [diff] [blame] | 508 | |
| 509 | /** |
| 510 | * Get or set the value |
| 511 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 512 | value : function (value) { |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 513 | const t = this; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 514 | if (arguments.length === 1) { |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 515 | if (t._type === 'date' && !KorAP._validDateRE.test(value)) { |
| 516 | delete t._value; |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 517 | } |
| 518 | else { |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 519 | t._value = value; |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 520 | }; |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 521 | t._changed(); |
| 522 | return t; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 523 | }; |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 524 | return t._value; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 525 | }, |
| 526 | |
Nils Diewald | 4c22125 | 2015-04-21 20:19:25 +0000 | [diff] [blame] | 527 | |
| 528 | // Click on the match operator, show me the menu |
| 529 | _changeValue : function (e) { |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 530 | const that = this; |
Akron | 8db5e3a | 2018-05-28 19:25:26 +0200 | [diff] [blame] | 531 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 532 | // Show datepicker |
Nils Diewald | 8750783 | 2015-05-01 23:36:41 +0000 | [diff] [blame] | 533 | if (this.type() === 'date') { |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 534 | const dp = KorAP._vcDatePicker; |
| 535 | dp.fromString(this.value()); |
Nils Diewald | 8750783 | 2015-05-01 23:36:41 +0000 | [diff] [blame] | 536 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 537 | // Todo: change this |
| 538 | dp.onclick(function (selected) { |
Nils Diewald | 8750783 | 2015-05-01 23:36:41 +0000 | [diff] [blame] | 539 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 540 | // There are values selected |
| 541 | if (selected['year']) { |
| 542 | that.value(this.toString()); |
| 543 | that.update(); |
| 544 | return; |
| 545 | }; |
Nils Diewald | 8750783 | 2015-05-01 23:36:41 +0000 | [diff] [blame] | 546 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 547 | // Remove datepicker |
| 548 | that._element.removeChild( |
| 549 | dp.element() |
| 550 | ); |
| 551 | }); |
Nils Diewald | 8750783 | 2015-05-01 23:36:41 +0000 | [diff] [blame] | 552 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 553 | this._element.insertBefore( |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 554 | dp.show(), // Get element of the date picker |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 555 | this._valueE |
| 556 | ); |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 557 | |
Akron | 516b6f9 | 2018-01-03 17:46:59 +0100 | [diff] [blame] | 558 | dp.input().focus(); |
Nils Diewald | 8750783 | 2015-05-01 23:36:41 +0000 | [diff] [blame] | 559 | } |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 560 | |
Nils Diewald | 8750783 | 2015-05-01 23:36:41 +0000 | [diff] [blame] | 561 | else { |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 562 | const regex = this.type() === 'regex' ? true : false; |
| 563 | const str = stringValClass.create(this.value(), regex); |
| 564 | const strElem = str.element(); |
Nils Diewald | c4c4b83 | 2015-05-05 16:00:08 +0000 | [diff] [blame] | 565 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 566 | str.store = function (value, regex) { |
| 567 | that.value(value); |
| 568 | if (regex === true) |
| 569 | that.type('regex'); |
| 570 | else |
| 571 | that.type('string'); |
| 572 | |
| 573 | that._element.removeChild( |
| 574 | this._element |
| 575 | ); |
| 576 | that.update(); |
| 577 | }; |
Nils Diewald | c4c4b83 | 2015-05-05 16:00:08 +0000 | [diff] [blame] | 578 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 579 | // Insert element |
| 580 | this._element.insertBefore( |
| 581 | strElem, |
| 582 | this._valueE |
| 583 | ); |
Nils Diewald | c4c4b83 | 2015-05-05 16:00:08 +0000 | [diff] [blame] | 584 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 585 | str.focus(); |
Nils Diewald | 8750783 | 2015-05-01 23:36:41 +0000 | [diff] [blame] | 586 | }; |
Nils Diewald | 4c22125 | 2015-04-21 20:19:25 +0000 | [diff] [blame] | 587 | }, |
| 588 | |
| 589 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 590 | rewrites : function () { |
| 591 | return this._rewrites; |
| 592 | }, |
| 593 | |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 594 | |
Akron | ea4e905 | 2017-07-06 16:12:05 +0200 | [diff] [blame] | 595 | rewrite : function (value) { |
| 596 | if (typeof value === 'string') { |
| 597 | value = [{ |
| 598 | "@type" : "koral:rewrite", |
| 599 | "operation" : "operation:" + value, |
| 600 | "src" : "Kalamar" |
| 601 | }]; |
| 602 | }; |
| 603 | this._rewrites = rewriteListClass.create(value); |
| 604 | }, |
| 605 | |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 606 | |
Akron | 31d8994 | 2018-04-06 16:44:51 +0200 | [diff] [blame] | 607 | // Mark the underlying data as being changed. |
| 608 | // This is important for rerendering the dom. |
| 609 | // This will also remove rewrite markers, when the data |
| 610 | // change happened by the user |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 611 | _changed : function () { |
| 612 | this.__changed = true; |
Nils Diewald | 4347ee9 | 2015-05-04 20:32:48 +0000 | [diff] [blame] | 613 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 614 | if (this._rewrites === undefined) |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 615 | return; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 616 | |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 617 | delete this["_rewrites"]; |
| 618 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 619 | if (this._element === undefined) |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 620 | return; |
Akron | ea4e905 | 2017-07-06 16:12:05 +0200 | [diff] [blame] | 621 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 622 | this._element.classList.remove("rewritten"); |
| 623 | }, |
| 624 | |
Nils Diewald | 4c22125 | 2015-04-21 20:19:25 +0000 | [diff] [blame] | 625 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 626 | toJson : function () { |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 627 | const t = this; |
| 628 | |
| 629 | if (!t.matchop() || !t.key()) |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 630 | return {}; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 631 | |
| 632 | return { |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 633 | "@type" : "koral:" + t.ldType(), |
| 634 | "key" : t.key(), |
| 635 | "match" : "match:" + t.matchop(), |
| 636 | "value" : t.value() || '', |
| 637 | "type" : "type:" + t.type() |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 638 | }; |
| 639 | }, |
| 640 | |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 641 | |
hebasta | a0282be | 2018-12-05 16:58:00 +0100 | [diff] [blame] | 642 | incomplete : function () { |
| 643 | return !(this.matchop() && this.key() && this.value()); |
| 644 | }, |
Nils Diewald | 4c22125 | 2015-04-21 20:19:25 +0000 | [diff] [blame] | 645 | |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 646 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 647 | toQuery : function () { |
hebasta | a0282be | 2018-12-05 16:58:00 +0100 | [diff] [blame] | 648 | if (this.incomplete()) |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 649 | return ""; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 650 | |
| 651 | // Build doc string based on key |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 652 | let string = this.key() + ' '; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 653 | |
| 654 | // Add match operator |
| 655 | switch (this.matchop()) { |
| 656 | case "ne": |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 657 | string += '!='; |
| 658 | break; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 659 | case "contains": |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 660 | string += '~'; |
| 661 | break; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 662 | case "excludes": |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 663 | string += '!~'; |
| 664 | break; |
Akron | 6a535d4 | 2015-08-26 20:16:58 +0200 | [diff] [blame] | 665 | case "containsnot": |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 666 | string += '!~'; |
| 667 | break; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 668 | case "geq": |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 669 | string += 'since'; |
| 670 | break; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 671 | case "leq": |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 672 | string += 'until'; |
| 673 | break; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 674 | default: |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 675 | string += (this.type() == 'date') ? 'in' : '='; |
| 676 | break; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 677 | }; |
| 678 | |
| 679 | string += ' '; |
| 680 | |
| 681 | // Add value |
| 682 | switch (this.type()) { |
| 683 | case "date": |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 684 | return string + this.value(); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 685 | case "regex": |
Akron | 8778f5d | 2017-06-30 21:25:55 +0200 | [diff] [blame] | 686 | return string + '/' + this.value().escapeRegex() + '/'; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 687 | case "string": |
Akron | 1bdf527 | 2018-07-24 18:51:30 +0200 | [diff] [blame] | 688 | case "text": |
Akron | 0c4cd22 | 2019-07-19 16:33:34 +0200 | [diff] [blame] | 689 | return string + this.value().quote(); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 690 | }; |
| 691 | |
| 692 | return ""; |
| 693 | } |
| 694 | }; |
| 695 | }); |