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) { |
| 216 | json["type"] = "type:" + type; |
| 217 | }; |
| 218 | }; |
| 219 | |
| 220 | // Type is still undefined |
Akron | 31d8994 | 2018-04-06 16:44:51 +0200 | [diff] [blame] | 221 | if (json["type"] === undefined) { |
Akron | ddc98a7 | 2018-04-06 17:33:52 +0200 | [diff] [blame] | 222 | |
Akron | 31d8994 | 2018-04-06 16:44:51 +0200 | [diff] [blame] | 223 | // Check match type |
| 224 | if (!KorAP._validUnspecMatchRE.test(this.matchop())) { |
| 225 | KorAP.log(802, errstr802); |
| 226 | |
| 227 | // Rewrite method |
| 228 | this.matchop('eq'); |
| 229 | rewrite = 'modification'; |
| 230 | }; |
| 231 | |
| 232 | // Set string value |
| 233 | this.value(json["value"]); |
| 234 | } |
| 235 | |
| 236 | // Field is string type |
| 237 | else if (json["type"] == "type:string") { |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 238 | this.type("string"); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 239 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 240 | // Check match type |
| 241 | if (!KorAP._validStringMatchRE.test(this.matchop())) { |
Akron | 31d8994 | 2018-04-06 16:44:51 +0200 | [diff] [blame] | 242 | KorAP.log(802, errstr802); |
| 243 | |
| 244 | // Rewrite method |
| 245 | this.matchop('eq'); |
| 246 | rewrite = 'modification'; |
| 247 | }; |
| 248 | |
| 249 | // Set string value |
| 250 | this.value(json["value"]); |
| 251 | } |
| 252 | |
| 253 | // Field is specified |
| 254 | else if (json["type"] == "type:text") { |
| 255 | this.type("text"); |
| 256 | |
| 257 | // Check match type |
| 258 | if (!KorAP._validTextMatchRE.test(this.matchop())) { |
| 259 | KorAP.log(802, errstr802); |
Akron | ea4e905 | 2017-07-06 16:12:05 +0200 | [diff] [blame] | 260 | |
| 261 | // Rewrite method |
| 262 | this.matchop('eq'); |
| 263 | rewrite = 'modification'; |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 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 | // Set string value |
| 267 | this.value(json["value"]); |
| 268 | } |
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 | // Key is a date |
| 271 | else if (json["type"] === "type:date") { |
| 272 | this.type("date"); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 273 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 274 | if (json["value"] !== undefined && |
| 275 | KorAP._validDateRE.test(json["value"])) { |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 276 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 277 | if (!KorAP._validDateMatchRE.test(this.matchop())) { |
Akron | 31d8994 | 2018-04-06 16:44:51 +0200 | [diff] [blame] | 278 | KorAP.log(802, errstr802); |
Akron | ea4e905 | 2017-07-06 16:12:05 +0200 | [diff] [blame] | 279 | |
| 280 | // Rewrite method |
| 281 | this.matchop('eq'); |
| 282 | rewrite = 'modification'; |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 283 | }; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 284 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 285 | // Set value |
| 286 | this.value(json["value"]); |
| 287 | } |
| 288 | else { |
| 289 | KorAP.log(806, "Value is not a valid date string"); |
| 290 | return; |
| 291 | }; |
| 292 | } |
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 | // Key is a regular expression |
| 295 | else if (json["type"] === "type:regex") { |
| 296 | this.type("regex"); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 297 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 298 | try { |
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 | // Try to create a regular expression |
| 301 | var check = new RegExp(json["value"]); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 302 | |
Akron | e65a88a | 2018-04-05 19:14:20 +0200 | [diff] [blame] | 303 | if (!KorAP._validStringMatchRE.test(this.matchop())) { |
Akron | 31d8994 | 2018-04-06 16:44:51 +0200 | [diff] [blame] | 304 | KorAP.log(802, errstr802); |
Akron | ea4e905 | 2017-07-06 16:12:05 +0200 | [diff] [blame] | 305 | |
| 306 | // Rewrite method |
| 307 | this.matchop('eq'); |
| 308 | rewrite = 'modification'; |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 309 | }; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 310 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 311 | this.value(json["value"]); |
| 312 | } |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 313 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 314 | catch (e) { |
| 315 | KorAP.log(807, "Value is not a valid regular expression"); |
| 316 | return; |
| 317 | }; |
| 318 | this.type("regex"); |
| 319 | } |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 320 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 321 | else { |
| 322 | KorAP.log(804, "Unknown value type"); |
| 323 | return; |
| 324 | }; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 325 | }; |
| 326 | |
Akron | ea4e905 | 2017-07-06 16:12:05 +0200 | [diff] [blame] | 327 | // Rewrite coming from the server |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 328 | if (json["rewrites"] !== undefined) { |
Akron | ea4e905 | 2017-07-06 16:12:05 +0200 | [diff] [blame] | 329 | this.rewrite(json["rewrites"]); |
| 330 | } |
| 331 | |
| 332 | // Rewrite coming from Kalamar |
| 333 | else if (rewrite !== undefined) { |
| 334 | this.rewrite(rewrite); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 335 | }; |
Akron | ea4e905 | 2017-07-06 16:12:05 +0200 | [diff] [blame] | 336 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 337 | return this; |
| 338 | }, |
| 339 | |
Nils Diewald | 4c22125 | 2015-04-21 20:19:25 +0000 | [diff] [blame] | 340 | /** |
| 341 | * Get or set the key |
| 342 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 343 | key : function (value) { |
| 344 | if (arguments.length === 1) { |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 345 | this._key = value; |
| 346 | this._changed(); |
| 347 | return this; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 348 | }; |
| 349 | return this._key; |
| 350 | }, |
| 351 | |
Nils Diewald | 4c22125 | 2015-04-21 20:19:25 +0000 | [diff] [blame] | 352 | // Click on the key, show me the menu |
| 353 | _changeKey : function (e) { |
| 354 | var menu = KorAP._vcKeyMenu; |
| 355 | |
| 356 | // Insert menu |
| 357 | this._element.insertBefore( |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 358 | menu.element(), |
| 359 | this._keyE |
Nils Diewald | 4c22125 | 2015-04-21 20:19:25 +0000 | [diff] [blame] | 360 | ); |
| 361 | |
| 362 | // Release event |
| 363 | var that = this; |
| 364 | menu.released(function (key, type) { |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 365 | var doc = that.key(key).type(type); |
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 | // This may not be compatible - then switch to default |
| 368 | doc.matchop(doc.matchop()); |
| 369 | doc.value(doc.value()); |
Nils Diewald | 4c22125 | 2015-04-21 20:19:25 +0000 | [diff] [blame] | 370 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 371 | // Update the doc |
| 372 | doc.update(); |
Nils Diewald | 4c22125 | 2015-04-21 20:19:25 +0000 | [diff] [blame] | 373 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 374 | // hide! |
| 375 | this.hide(); |
Nils Diewald | 4c22125 | 2015-04-21 20:19:25 +0000 | [diff] [blame] | 376 | }); |
| 377 | |
| 378 | // TODO: Highlight the old value! |
| 379 | menu.show(); |
| 380 | menu.focus(); |
| 381 | }, |
| 382 | |
| 383 | /** |
| 384 | * Get or set the match operator |
| 385 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 386 | matchop : function (match) { |
Akron | 31d8994 | 2018-04-06 16:44:51 +0200 | [diff] [blame] | 387 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 388 | if (arguments.length === 1) { |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 389 | var m = match.replace(/^match:/, ''); |
Akron | 31d8994 | 2018-04-06 16:44:51 +0200 | [diff] [blame] | 390 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 391 | if ( |
Akron | 31d8994 | 2018-04-06 16:44:51 +0200 | [diff] [blame] | 392 | (this._type == undefined) // && KorAP._validUnspecMatchRE.test(m)) |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 393 | || |
| 394 | ( |
| 395 | (this._type === 'string' || this._type === 'regex') && |
| 396 | KorAP._validStringMatchRE.test(m) |
| 397 | ) |
| 398 | || |
Akron | 31d8994 | 2018-04-06 16:44:51 +0200 | [diff] [blame] | 399 | (this._type === 'text' && KorAP._validTextMatchRE.test(m)) |
| 400 | || |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 401 | (this._type === 'date' && KorAP._validDateMatchRE.test(m)) |
| 402 | ) { |
| 403 | this._matchop = m; |
| 404 | } |
| 405 | else { |
| 406 | this._matchop = "eq"; |
| 407 | }; |
Nils Diewald | 4c22125 | 2015-04-21 20:19:25 +0000 | [diff] [blame] | 408 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 409 | this._changed(); |
Akron | 31d8994 | 2018-04-06 16:44:51 +0200 | [diff] [blame] | 410 | return this |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 411 | }; |
| 412 | return this._matchop || "eq"; |
| 413 | }, |
| 414 | |
Nils Diewald | 4c22125 | 2015-04-21 20:19:25 +0000 | [diff] [blame] | 415 | |
| 416 | // Click on the match operator, show me the menu |
| 417 | _changeMatchop : function (e) { |
| 418 | var menu = KorAP._vcMatchopMenu[this.type()]; |
| 419 | |
| 420 | if (menu === undefined) { |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 421 | KorAP.log(0, "Unable to init menu for " + this.type()); |
| 422 | return; |
Nils Diewald | 4c22125 | 2015-04-21 20:19:25 +0000 | [diff] [blame] | 423 | }; |
| 424 | |
| 425 | // Insert menu |
| 426 | this._element.insertBefore( |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 427 | menu.element(), |
| 428 | this._matchopE |
Nils Diewald | 4c22125 | 2015-04-21 20:19:25 +0000 | [diff] [blame] | 429 | ); |
| 430 | |
| 431 | // Release event |
| 432 | var that = this; |
| 433 | menu.released(function (mo) { |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 434 | that.matchop(mo).update(); |
| 435 | this.hide(); |
Nils Diewald | 4c22125 | 2015-04-21 20:19:25 +0000 | [diff] [blame] | 436 | }); |
| 437 | |
| 438 | menu.show(); |
| 439 | menu.focus(); |
| 440 | }, |
| 441 | |
| 442 | |
| 443 | /** |
| 444 | * Get or set the type |
| 445 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 446 | type : function (type) { |
| 447 | if (arguments.length === 1) { |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 448 | this._type = type; |
| 449 | this._changed(); |
| 450 | return this; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 451 | }; |
| 452 | return this._type || "string"; |
| 453 | }, |
| 454 | |
Nils Diewald | 4c22125 | 2015-04-21 20:19:25 +0000 | [diff] [blame] | 455 | |
| 456 | /** |
| 457 | * Get or set the value |
| 458 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 459 | value : function (value) { |
| 460 | if (arguments.length === 1) { |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 461 | if (this._type === 'date' && !KorAP._validDateRE.test(value)) { |
| 462 | delete this._value; |
| 463 | } |
| 464 | else { |
| 465 | this._value = value; |
| 466 | }; |
| 467 | this._changed(); |
| 468 | return this; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 469 | }; |
| 470 | return this._value; |
| 471 | }, |
| 472 | |
Nils Diewald | 4c22125 | 2015-04-21 20:19:25 +0000 | [diff] [blame] | 473 | |
| 474 | // Click on the match operator, show me the menu |
| 475 | _changeValue : function (e) { |
Nils Diewald | c4c4b83 | 2015-05-05 16:00:08 +0000 | [diff] [blame] | 476 | var v = this.value(); |
| 477 | var that = this; |
Akron | 8db5e3a | 2018-05-28 19:25:26 +0200 | [diff] [blame] | 478 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 479 | // Show datepicker |
Nils Diewald | 8750783 | 2015-05-01 23:36:41 +0000 | [diff] [blame] | 480 | if (this.type() === 'date') { |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 481 | var dp = KorAP._vcDatePicker; |
Akron | 516b6f9 | 2018-01-03 17:46:59 +0100 | [diff] [blame] | 482 | dp.fromString(v); |
Nils Diewald | 8750783 | 2015-05-01 23:36:41 +0000 | [diff] [blame] | 483 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 484 | // Todo: change this |
| 485 | dp.onclick(function (selected) { |
Nils Diewald | 8750783 | 2015-05-01 23:36:41 +0000 | [diff] [blame] | 486 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 487 | // There are values selected |
| 488 | if (selected['year']) { |
| 489 | that.value(this.toString()); |
| 490 | that.update(); |
| 491 | return; |
| 492 | }; |
Nils Diewald | 8750783 | 2015-05-01 23:36:41 +0000 | [diff] [blame] | 493 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 494 | // Remove datepicker |
| 495 | that._element.removeChild( |
| 496 | dp.element() |
| 497 | ); |
| 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 | // Get element of the date picker |
| 501 | var dpElem = dp.show(); |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 502 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 503 | this._element.insertBefore( |
| 504 | dpElem, |
| 505 | this._valueE |
| 506 | ); |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 507 | |
Akron | 516b6f9 | 2018-01-03 17:46:59 +0100 | [diff] [blame] | 508 | dp.input().focus(); |
Nils Diewald | 8750783 | 2015-05-01 23:36:41 +0000 | [diff] [blame] | 509 | } |
| 510 | else { |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 511 | var regex = this.type() === 'regex' ? true : false; |
| 512 | var str = stringValClass.create(this.value(), regex); |
| 513 | var strElem = str.element(); |
Nils Diewald | c4c4b83 | 2015-05-05 16:00:08 +0000 | [diff] [blame] | 514 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 515 | str.store = function (value, regex) { |
| 516 | that.value(value); |
| 517 | if (regex === true) |
| 518 | that.type('regex'); |
| 519 | else |
| 520 | that.type('string'); |
| 521 | |
| 522 | that._element.removeChild( |
| 523 | this._element |
| 524 | ); |
| 525 | that.update(); |
| 526 | }; |
Nils Diewald | c4c4b83 | 2015-05-05 16:00:08 +0000 | [diff] [blame] | 527 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 528 | // Insert element |
| 529 | this._element.insertBefore( |
| 530 | strElem, |
| 531 | this._valueE |
| 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 | str.focus(); |
Nils Diewald | 8750783 | 2015-05-01 23:36:41 +0000 | [diff] [blame] | 535 | }; |
Nils Diewald | 4c22125 | 2015-04-21 20:19:25 +0000 | [diff] [blame] | 536 | }, |
| 537 | |
| 538 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 539 | rewrites : function () { |
| 540 | return this._rewrites; |
| 541 | }, |
| 542 | |
Akron | ea4e905 | 2017-07-06 16:12:05 +0200 | [diff] [blame] | 543 | rewrite : function (value) { |
| 544 | if (typeof value === 'string') { |
| 545 | value = [{ |
| 546 | "@type" : "koral:rewrite", |
| 547 | "operation" : "operation:" + value, |
| 548 | "src" : "Kalamar" |
| 549 | }]; |
| 550 | }; |
| 551 | this._rewrites = rewriteListClass.create(value); |
| 552 | }, |
| 553 | |
Akron | 31d8994 | 2018-04-06 16:44:51 +0200 | [diff] [blame] | 554 | // Mark the underlying data as being changed. |
| 555 | // This is important for rerendering the dom. |
| 556 | // This will also remove rewrite markers, when the data |
| 557 | // change happened by the user |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 558 | _changed : function () { |
| 559 | this.__changed = true; |
Nils Diewald | 4347ee9 | 2015-05-04 20:32:48 +0000 | [diff] [blame] | 560 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 561 | if (this._rewrites === undefined) |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 562 | return; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 563 | |
Akron | ea4e905 | 2017-07-06 16:12:05 +0200 | [diff] [blame] | 564 | delete this["_rewrites"]; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 565 | |
| 566 | if (this._element === undefined) |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 567 | return; |
Akron | ea4e905 | 2017-07-06 16:12:05 +0200 | [diff] [blame] | 568 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 569 | this._element.classList.remove("rewritten"); |
| 570 | }, |
| 571 | |
Nils Diewald | 4c22125 | 2015-04-21 20:19:25 +0000 | [diff] [blame] | 572 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 573 | toJson : function () { |
| 574 | if (!this.matchop() || !this.key()) |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 575 | return {}; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 576 | |
| 577 | return { |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 578 | "@type" : "koral:" + this.ldType(), |
| 579 | "key" : this.key(), |
| 580 | "match" : "match:" + this.matchop(), |
| 581 | "value" : this.value() || '', |
| 582 | "type" : "type:" + this.type() |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 583 | }; |
| 584 | }, |
| 585 | |
Nils Diewald | 4c22125 | 2015-04-21 20:19:25 +0000 | [diff] [blame] | 586 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 587 | toQuery : function () { |
| 588 | if (!this.matchop() || !this.key()) |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 589 | return ""; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 590 | |
| 591 | // Build doc string based on key |
| 592 | var string = this.key() + ' '; |
| 593 | |
| 594 | // Add match operator |
| 595 | switch (this.matchop()) { |
| 596 | case "ne": |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 597 | string += '!='; |
| 598 | break; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 599 | case "contains": |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 600 | string += '~'; |
| 601 | break; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 602 | case "excludes": |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 603 | string += '!~'; |
| 604 | break; |
Akron | 6a535d4 | 2015-08-26 20:16:58 +0200 | [diff] [blame] | 605 | case "containsnot": |
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 "geq": |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 609 | string += 'since'; |
| 610 | break; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 611 | case "leq": |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 612 | string += 'until'; |
| 613 | break; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 614 | default: |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 615 | string += (this.type() == 'date') ? 'in' : '='; |
| 616 | break; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 617 | }; |
| 618 | |
| 619 | string += ' '; |
| 620 | |
| 621 | // Add value |
| 622 | switch (this.type()) { |
| 623 | case "date": |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 624 | return string + this.value(); |
| 625 | break; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 626 | case "regex": |
Akron | 8778f5d | 2017-06-30 21:25:55 +0200 | [diff] [blame] | 627 | return string + '/' + this.value().escapeRegex() + '/'; |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 628 | break; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 629 | case "string": |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 630 | return string + '"' + this.value().quote() + '"'; |
| 631 | break; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 632 | }; |
| 633 | |
| 634 | return ""; |
| 635 | } |
| 636 | }; |
| 637 | }); |