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