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