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