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; |
| 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 | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 470 | dpElem.focus(); |
| 471 | /* |
| 472 | dpElem.addEventListener('blur', function (e) { |
| 473 | e.halt(); |
| 474 | |
| 475 | // Remove datepicker |
| 476 | // TODO: If focus is not set to string input |
| 477 | that._element.removeChild(this); |
| 478 | }); |
| 479 | */ |
Nils Diewald | 8750783 | 2015-05-01 23:36:41 +0000 | [diff] [blame] | 480 | } |
| 481 | else { |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 482 | var regex = this.type() === 'regex' ? true : false; |
| 483 | var str = stringValClass.create(this.value(), regex); |
| 484 | var strElem = str.element(); |
Nils Diewald | c4c4b83 | 2015-05-05 16:00:08 +0000 | [diff] [blame] | 485 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 486 | str.store = function (value, regex) { |
| 487 | that.value(value); |
| 488 | if (regex === true) |
| 489 | that.type('regex'); |
| 490 | else |
| 491 | that.type('string'); |
| 492 | |
| 493 | that._element.removeChild( |
| 494 | this._element |
| 495 | ); |
| 496 | that.update(); |
| 497 | }; |
Nils Diewald | c4c4b83 | 2015-05-05 16:00:08 +0000 | [diff] [blame] | 498 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 499 | // Insert element |
| 500 | this._element.insertBefore( |
| 501 | strElem, |
| 502 | this._valueE |
| 503 | ); |
Nils Diewald | c4c4b83 | 2015-05-05 16:00:08 +0000 | [diff] [blame] | 504 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 505 | str.focus(); |
Nils Diewald | 8750783 | 2015-05-01 23:36:41 +0000 | [diff] [blame] | 506 | }; |
Nils Diewald | 4c22125 | 2015-04-21 20:19:25 +0000 | [diff] [blame] | 507 | }, |
| 508 | |
| 509 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 510 | rewrites : function () { |
| 511 | return this._rewrites; |
| 512 | }, |
| 513 | |
Akron | ea4e905 | 2017-07-06 16:12:05 +0200 | [diff] [blame^] | 514 | rewrite : function (value) { |
| 515 | if (typeof value === 'string') { |
| 516 | value = [{ |
| 517 | "@type" : "koral:rewrite", |
| 518 | "operation" : "operation:" + value, |
| 519 | "src" : "Kalamar" |
| 520 | }]; |
| 521 | }; |
| 522 | this._rewrites = rewriteListClass.create(value); |
| 523 | }, |
| 524 | |
| 525 | // Remove rewrite marker when the data changes |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 526 | _changed : function () { |
| 527 | this.__changed = true; |
Nils Diewald | 4347ee9 | 2015-05-04 20:32:48 +0000 | [diff] [blame] | 528 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 529 | if (this._rewrites === undefined) |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 530 | return; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 531 | |
Akron | ea4e905 | 2017-07-06 16:12:05 +0200 | [diff] [blame^] | 532 | delete this["_rewrites"]; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 533 | |
| 534 | if (this._element === undefined) |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 535 | return; |
Akron | ea4e905 | 2017-07-06 16:12:05 +0200 | [diff] [blame^] | 536 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 537 | this._element.classList.remove("rewritten"); |
| 538 | }, |
| 539 | |
Nils Diewald | 4c22125 | 2015-04-21 20:19:25 +0000 | [diff] [blame] | 540 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 541 | toJson : function () { |
| 542 | if (!this.matchop() || !this.key()) |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 543 | return {}; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 544 | |
| 545 | return { |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 546 | "@type" : "koral:" + this.ldType(), |
| 547 | "key" : this.key(), |
| 548 | "match" : "match:" + this.matchop(), |
| 549 | "value" : this.value() || '', |
| 550 | "type" : "type:" + this.type() |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 551 | }; |
| 552 | }, |
| 553 | |
Nils Diewald | 4c22125 | 2015-04-21 20:19:25 +0000 | [diff] [blame] | 554 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 555 | toQuery : function () { |
| 556 | if (!this.matchop() || !this.key()) |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 557 | return ""; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 558 | |
| 559 | // Build doc string based on key |
| 560 | var string = this.key() + ' '; |
| 561 | |
| 562 | // Add match operator |
| 563 | switch (this.matchop()) { |
| 564 | case "ne": |
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 "contains": |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 568 | string += '~'; |
| 569 | break; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 570 | case "excludes": |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 571 | string += '!~'; |
| 572 | break; |
Akron | 6a535d4 | 2015-08-26 20:16:58 +0200 | [diff] [blame] | 573 | case "containsnot": |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 574 | string += '!~'; |
| 575 | break; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 576 | case "geq": |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 577 | string += 'since'; |
| 578 | break; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 579 | case "leq": |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 580 | string += 'until'; |
| 581 | break; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 582 | default: |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 583 | string += (this.type() == 'date') ? 'in' : '='; |
| 584 | break; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 585 | }; |
| 586 | |
| 587 | string += ' '; |
| 588 | |
| 589 | // Add value |
| 590 | switch (this.type()) { |
| 591 | case "date": |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 592 | return string + this.value(); |
| 593 | break; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 594 | case "regex": |
Akron | 8778f5d | 2017-06-30 21:25:55 +0200 | [diff] [blame] | 595 | return string + '/' + this.value().escapeRegex() + '/'; |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 596 | break; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 597 | case "string": |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 598 | return string + '"' + this.value().quote() + '"'; |
| 599 | break; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 600 | }; |
| 601 | |
| 602 | return ""; |
| 603 | } |
| 604 | }; |
| 605 | }); |