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