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