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