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