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