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