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