Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame^] | 1 | /** |
| 2 | * Document criterion |
| 3 | */ |
| 4 | |
| 5 | /** |
| 6 | * Criterion in a KorAP.Doc |
| 7 | */ |
| 8 | function _changeKey () { |
| 9 | var doc = this.parentNode.refTo; |
| 10 | var key = doc.element().firstChild; |
| 11 | key.appendChild(fieldMenu.element()); |
| 12 | fieldMenu.show(); |
| 13 | fieldMenu.focus(); |
| 14 | // key, matchop, type, value |
| 15 | }; |
| 16 | |
| 17 | define([ |
| 18 | 'vc/jsonld', 'vc/menu', 'vc/rewritelist'], function (jsonldClass, menuClass, rewriteListClass) { |
| 19 | var fieldMenu = menuClass.create([ |
| 20 | ['Titel', 'title', 'string'], |
| 21 | ['Untertitel', 'subTitle', 'string'], |
| 22 | ['Veröffentlichungsdatum', 'pubDate', 'date'], |
| 23 | ['Autor', 'author', 'string'] |
| 24 | ]); |
| 25 | |
| 26 | fieldMenu.limit(5); |
| 27 | |
| 28 | _validRegexMatchRE = new RegExp("^(?:eq|ne)$"); |
| 29 | |
| 30 | return { |
| 31 | _ldType : "doc", |
| 32 | _obj : function () { return '???'; /*KorAP.Doc*/ }, |
| 33 | |
| 34 | create : function (parent, json) { |
| 35 | var obj = Object(jsonldClass). |
| 36 | create(). |
| 37 | upgradeTo(this). |
| 38 | fromJson(json); |
| 39 | |
| 40 | if (parent !== undefined) |
| 41 | obj._parent = parent; |
| 42 | |
| 43 | obj.__changed = true; |
| 44 | return obj; |
| 45 | }, |
| 46 | |
| 47 | update : function () { |
| 48 | if (this._element === undefined) |
| 49 | return this.element(); |
| 50 | |
| 51 | // Get element |
| 52 | var e = this._element; |
| 53 | |
| 54 | // Set ref - TODO: Cleanup! |
| 55 | e.refTo = this; |
| 56 | |
| 57 | // Check if there is a change |
| 58 | if (this.__changed) { |
| 59 | |
| 60 | // Was rewritten |
| 61 | if (this.rewrites() !== undefined) { |
| 62 | e.classList.add("rewritten"); |
| 63 | }; |
| 64 | |
| 65 | // Added key |
| 66 | var key = document.createElement('span'); |
| 67 | key.setAttribute('class', 'key'); |
| 68 | |
| 69 | // Change key |
| 70 | key.addEventListener('click', _changeKey, false); |
| 71 | |
| 72 | if (this.key()) |
| 73 | key.appendChild(document.createTextNode(this.key())); |
| 74 | |
| 75 | // Added match operator |
| 76 | var matchop = document.createElement('span'); |
| 77 | matchop.setAttribute('data-type', this.type()); |
| 78 | matchop.setAttribute('class', 'match'); |
| 79 | matchop.appendChild( |
| 80 | document.createTextNode(this.matchop()) |
| 81 | ); |
| 82 | |
| 83 | // Added match operator |
| 84 | var value = document.createElement('span'); |
| 85 | value.setAttribute('data-type', this.type()); |
| 86 | value.setAttribute('class', 'value'); |
| 87 | if (this.value()) |
| 88 | value.appendChild( |
| 89 | document.createTextNode(this.value()) |
| 90 | ); |
| 91 | |
| 92 | // Remove all element children |
| 93 | _removeChildren(e); |
| 94 | |
| 95 | // Add spans |
| 96 | e.appendChild(key); |
| 97 | e.appendChild(matchop); |
| 98 | e.appendChild(value); |
| 99 | |
| 100 | this.__changed = false; |
| 101 | }; |
| 102 | |
| 103 | if (this._rewrites !== undefined) { |
| 104 | e.appendChild(this._rewrites.element()); |
| 105 | }; |
| 106 | |
| 107 | if (this._parent !== undefined) { |
| 108 | // Set operators |
| 109 | var op = this.operators( |
| 110 | true, |
| 111 | true, |
| 112 | true |
| 113 | ); |
| 114 | |
| 115 | // Append new operators |
| 116 | e.appendChild(op.element()); |
| 117 | }; |
| 118 | |
| 119 | return e; |
| 120 | }, |
| 121 | |
| 122 | element : function () { |
| 123 | if (this._element !== undefined) |
| 124 | return this._element; |
| 125 | |
| 126 | this._element = document.createElement('div'); |
| 127 | this._element.setAttribute('class', 'doc'); |
| 128 | |
| 129 | this.update(); |
| 130 | return this._element; |
| 131 | }, |
| 132 | |
| 133 | // Wrap a new operation around the doc element |
| 134 | wrap : function (op) { |
| 135 | var parent = this.parent(); |
| 136 | var group = KorAP.DocGroup.create(parent); |
| 137 | group.operation(op); |
| 138 | group.append(this); |
| 139 | group.append(); |
| 140 | return parent.replaceOperand(this, group).update(); |
| 141 | }, |
| 142 | |
| 143 | // Deserialize from json |
| 144 | fromJson : function (json) { |
| 145 | if (json === undefined) |
| 146 | return this; |
| 147 | |
| 148 | if (json["@type"] === undefined) { |
| 149 | KorAP.log(701, "JSON-LD group has no @type attribute"); |
| 150 | return; |
| 151 | }; |
| 152 | |
| 153 | if (json["value"] === undefined || |
| 154 | typeof json["value"] != 'string') { |
| 155 | KorAP.log(805, "Value is invalid"); |
| 156 | return; |
| 157 | }; |
| 158 | |
| 159 | // There is a defined key |
| 160 | if (json["key"] !== undefined && |
| 161 | typeof json["key"] === 'string') { |
| 162 | |
| 163 | // Set key |
| 164 | this.key(json["key"]); |
| 165 | |
| 166 | // Set match operation |
| 167 | if (json["match"] !== undefined) { |
| 168 | if (typeof json["match"] === 'string') { |
| 169 | this.matchop(json["match"]); |
| 170 | } |
| 171 | else { |
| 172 | KorAP.log(802, "Match type is not supported by value type"); |
| 173 | return; |
| 174 | }; |
| 175 | }; |
| 176 | |
| 177 | // Key is a string |
| 178 | if (json["type"] === undefined || |
| 179 | json["type"] == "type:string") { |
| 180 | this.type("string"); |
| 181 | |
| 182 | // Check match type |
| 183 | if (!KorAP._validStringMatchRE.test(this.matchop())) { |
| 184 | KorAP.log(802, "Match type is not supported by value type"); |
| 185 | return; |
| 186 | }; |
| 187 | |
| 188 | // Set string value |
| 189 | this.value(json["value"]); |
| 190 | } |
| 191 | |
| 192 | // Key is a date |
| 193 | else if (json["type"] === "type:date") { |
| 194 | this.type("date"); |
| 195 | |
| 196 | if (json["value"] !== undefined && |
| 197 | KorAP._validDateRE.test(json["value"])) { |
| 198 | |
| 199 | if (!KorAP._validDateMatchRE.test(this.matchop())) { |
| 200 | KorAP.log(802, "Match type is not supported by value type"); |
| 201 | return; |
| 202 | }; |
| 203 | |
| 204 | // Set value |
| 205 | this.value(json["value"]); |
| 206 | } |
| 207 | else { |
| 208 | KorAP.log(806, "Value is not a valid date string"); |
| 209 | return; |
| 210 | }; |
| 211 | } |
| 212 | |
| 213 | // Key is a regular expression |
| 214 | else if (json["type"] === "type:regex") { |
| 215 | this.type("regex"); |
| 216 | |
| 217 | try { |
| 218 | |
| 219 | // Try to create a regular expression |
| 220 | var check = new RegExp(json["value"]); |
| 221 | |
| 222 | if (!_validRegexMatchRE.test(this.matchop())) { |
| 223 | KorAP.log(802, "Match type is not supported by value type"); |
| 224 | return; |
| 225 | }; |
| 226 | |
| 227 | this.value(json["value"]); |
| 228 | } |
| 229 | |
| 230 | catch (e) { |
| 231 | KorAP.log(807, "Value is not a valid regular expression"); |
| 232 | return; |
| 233 | }; |
| 234 | this.type("regex"); |
| 235 | } |
| 236 | |
| 237 | else { |
| 238 | KorAP.log(804, "Unknown value type"); |
| 239 | return; |
| 240 | }; |
| 241 | }; |
| 242 | |
| 243 | if (json["rewrites"] !== undefined) { |
| 244 | this._rewrites = rewriteListClass.create(json["rewrites"]); |
| 245 | }; |
| 246 | |
| 247 | return this; |
| 248 | }, |
| 249 | |
| 250 | key : function (value) { |
| 251 | if (arguments.length === 1) { |
| 252 | this._key = value; |
| 253 | this._changed(); |
| 254 | return this; |
| 255 | }; |
| 256 | return this._key; |
| 257 | }, |
| 258 | |
| 259 | matchop : function (match) { |
| 260 | if (arguments.length === 1) { |
| 261 | this._matchop = match.replace(/^match:/, ''); |
| 262 | this._changed(); |
| 263 | return this; |
| 264 | }; |
| 265 | return this._matchop || "eq"; |
| 266 | }, |
| 267 | |
| 268 | type : function (type) { |
| 269 | if (arguments.length === 1) { |
| 270 | this._type = type; |
| 271 | this._changed(); |
| 272 | return this; |
| 273 | }; |
| 274 | return this._type || "string"; |
| 275 | }, |
| 276 | |
| 277 | value : function (value) { |
| 278 | if (arguments.length === 1) { |
| 279 | this._value = value; |
| 280 | this._changed(); |
| 281 | return this; |
| 282 | }; |
| 283 | return this._value; |
| 284 | }, |
| 285 | |
| 286 | rewrites : function () { |
| 287 | return this._rewrites; |
| 288 | }, |
| 289 | |
| 290 | _changed : function () { |
| 291 | this.__changed = true; |
| 292 | |
| 293 | if (this._rewrites === undefined) |
| 294 | return; |
| 295 | |
| 296 | delete this["_rewrites"]; |
| 297 | |
| 298 | if (this._element === undefined) |
| 299 | return; |
| 300 | this._element.classList.remove("rewritten"); |
| 301 | }, |
| 302 | |
| 303 | toJson : function () { |
| 304 | if (!this.matchop() || !this.key()) |
| 305 | return {}; |
| 306 | |
| 307 | return { |
| 308 | "@type" : "koral:" + this.ldType(), |
| 309 | "key" : this.key(), |
| 310 | "match" : "match:" + this.matchop(), |
| 311 | "value" : this.value() || '', |
| 312 | "type" : "type:" + this.type() |
| 313 | }; |
| 314 | }, |
| 315 | |
| 316 | toQuery : function () { |
| 317 | if (!this.matchop() || !this.key()) |
| 318 | return ""; |
| 319 | |
| 320 | // Build doc string based on key |
| 321 | var string = this.key() + ' '; |
| 322 | |
| 323 | // Add match operator |
| 324 | switch (this.matchop()) { |
| 325 | case "ne": |
| 326 | string += '!='; |
| 327 | break; |
| 328 | case "contains": |
| 329 | string += '~'; |
| 330 | break; |
| 331 | case "excludes": |
| 332 | string += '!~'; |
| 333 | break; |
| 334 | case "geq": |
| 335 | string += 'since'; |
| 336 | break; |
| 337 | case "leq": |
| 338 | string += 'until'; |
| 339 | break; |
| 340 | default: |
| 341 | string += (this.type() == 'date') ? 'in' : '='; |
| 342 | break; |
| 343 | }; |
| 344 | |
| 345 | string += ' '; |
| 346 | |
| 347 | // Add value |
| 348 | switch (this.type()) { |
| 349 | case "date": |
| 350 | return string + this.value(); |
| 351 | break; |
| 352 | case "regex": |
| 353 | return string + '/' + this.value() + '/'; |
| 354 | break; |
| 355 | case "string": |
| 356 | return string + '"' + this.value().replace(KorAP._quote, '\\$1') + '"'; |
| 357 | break; |
| 358 | }; |
| 359 | |
| 360 | return ""; |
| 361 | } |
| 362 | }; |
| 363 | }); |