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