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