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