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