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