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