Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 1 | var KorAP = KorAP || {}; |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 2 | |
Nils Diewald | 8f4e254 | 2014-12-19 04:42:09 +0000 | [diff] [blame] | 3 | // TODO: Implement a working localization solution! |
| 4 | // TODO: Support 'update' method to update elements on change |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 5 | // TODO: Implement "toQuery" |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 6 | |
| 7 | /* |
| 8 | * Error codes: |
| 9 | 701: "JSON-LD group has no @type attribute" |
| 10 | 704: "Operation needs operand list" |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 11 | 802: "Match type is not supported by value type" |
| 12 | 804: "Unknown value type" |
| 13 | 805: "Value is invalid" |
| 14 | 806: "Value is not a valid date string" |
| 15 | 807: "Value is not a valid regular expression" |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 16 | 810: "Unknown document group operation" (like 711) |
| 17 | 811: "Document group expects operation" (like 703) |
| 18 | 812: "Operand not supported in document group" (like 744) |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 19 | 813: "Collection type is not supported" (like 713) |
| 20 | */ |
| 21 | |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 22 | (function (KorAP) { |
| 23 | "use strict"; |
| 24 | |
| 25 | // Default log message |
| 26 | KorAP.log = KorAP.log || function (type, msg) { |
| 27 | console.log(type + ": " + msg); |
| 28 | }; |
| 29 | |
| 30 | KorAP._validStringMatchRE = new RegExp("^(?:eq|ne|contains)$"); |
| 31 | KorAP._validRegexMatchRE = new RegExp("^(?:eq|ne)$"); |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 32 | KorAP._validDateMatchRE = new RegExp("^[lg]?eq$"); |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 33 | KorAP._validDateRE = new RegExp("^(?:\\d{4})(?:-\\d\\d(?:-\\d\\d)?)?$"); |
| 34 | KorAP._validGroupOpRE = new RegExp("^(?:and|or)$"); |
| 35 | |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 36 | var loc = (KorAP.Locale = KorAP.Locale || {} ); |
| 37 | loc.AND = loc.AND || 'and'; |
| 38 | loc.OR = loc.OR || 'or'; |
| 39 | loc.DEL = loc.DEL || '×'; |
| 40 | |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 41 | function _bool (bool) { |
| 42 | return (bool === undefined || bool === false) ? false : true; |
| 43 | }; |
| 44 | |
| 45 | function _removeChildren (node) { |
| 46 | // Remove everything underneath |
| 47 | while (node.firstChild) { |
| 48 | node.removeChild(node.firstChild); |
| 49 | }; |
| 50 | }; |
| 51 | |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 52 | KorAP.VirtualCollection = { |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 53 | _root : undefined, |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 54 | create : function () { |
| 55 | return Object.create(KorAP.VirtualCollection); |
| 56 | }, |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 57 | render : function (json) { |
| 58 | var obj = Object.create(KorAP.VirtualCollection); |
| 59 | |
| 60 | if (json !== undefined) { |
| 61 | // Root object |
| 62 | if (json['@type'] == 'korap:doc') { |
Nils Diewald | 8f4e254 | 2014-12-19 04:42:09 +0000 | [diff] [blame] | 63 | obj._root = KorAP.Doc.create(undefined, json); |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 64 | } |
| 65 | else if (json['@type'] == 'korap:docGroup') { |
Nils Diewald | 8f4e254 | 2014-12-19 04:42:09 +0000 | [diff] [blame] | 66 | obj._root = KorAP.DocGroup.create(undefined, json); |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 67 | } |
| 68 | else { |
| 69 | KorAP.log(813, "Collection type is not supported"); |
| 70 | return; |
| 71 | }; |
| 72 | } |
| 73 | |
| 74 | else { |
| 75 | // Add unspecified object |
Nils Diewald | 8f4e254 | 2014-12-19 04:42:09 +0000 | [diff] [blame] | 76 | obj._root = KorAP.UnspecifiedDoc.create(); |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 77 | }; |
| 78 | |
| 79 | // Add root element to root node |
| 80 | obj.element().appendChild( |
| 81 | obj._root.element() |
| 82 | ); |
| 83 | |
| 84 | return obj; |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 85 | }, |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 86 | root : function () { |
| 87 | return this._root; |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 88 | }, |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 89 | element : function () { |
| 90 | if (this._element !== undefined) |
| 91 | return this._element; |
| 92 | |
| 93 | this._element = document.createElement('div'); |
| 94 | this._element.setAttribute('class', 'vc'); |
| 95 | return this._element; |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 96 | } |
| 97 | }; |
| 98 | |
Nils Diewald | 8f4e254 | 2014-12-19 04:42:09 +0000 | [diff] [blame] | 99 | /** |
| 100 | * Operators for criteria |
| 101 | */ |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 102 | KorAP.Operators = { |
| 103 | create : function (and, or, del) { |
| 104 | var op = Object.create(KorAP.Operators); |
| 105 | op.and(and); |
| 106 | op.or(or); |
| 107 | op.del(del); |
| 108 | return op; |
| 109 | }, |
| 110 | update : function () { |
| 111 | |
| 112 | // Init the element |
| 113 | if (this._element === undefined) |
| 114 | return this.element(); |
| 115 | |
| 116 | var op = this._element; |
| 117 | |
| 118 | // Remove everything underneath |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 119 | _removeChildren(op); |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 120 | |
| 121 | // Add and button |
| 122 | if (this._and === true) { |
| 123 | var andE = document.createElement('span'); |
| 124 | andE.setAttribute('class', 'and'); |
| 125 | andE.appendChild(document.createTextNode(KorAP.Locale.AND)); |
| 126 | op.appendChild(andE); |
| 127 | }; |
| 128 | |
| 129 | // Add or button |
| 130 | if (this._or === true) { |
| 131 | var orE = document.createElement('span'); |
| 132 | orE.setAttribute('class', 'or'); |
| 133 | orE.appendChild(document.createTextNode(KorAP.Locale.OR)); |
| 134 | op.appendChild(orE); |
| 135 | }; |
| 136 | |
| 137 | // Add delete button |
| 138 | if (this._del === true) { |
| 139 | var delE = document.createElement('span'); |
| 140 | delE.setAttribute('class', 'delete'); |
| 141 | delE.appendChild(document.createTextNode(KorAP.Locale.DEL)); |
| 142 | op.appendChild(delE); |
| 143 | }; |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 144 | |
| 145 | return op; |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 146 | }, |
| 147 | element : function () { |
| 148 | |
| 149 | // Return existing element |
| 150 | if (this._element !== undefined) |
| 151 | return this._element; |
| 152 | |
| 153 | this._element = document.createElement('div'); |
| 154 | this._element.setAttribute('class', 'operators'); |
| 155 | |
| 156 | // Init elements |
| 157 | this.update(); |
| 158 | return this._element; |
| 159 | }, |
| 160 | and : function (bool) { |
| 161 | if (arguments.length === 1) |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 162 | this._and = _bool(bool); |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 163 | return this._and; |
| 164 | }, |
| 165 | or : function (bool) { |
| 166 | if (arguments.length === 1) |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 167 | this._or = _bool(bool); |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 168 | return this._or; |
| 169 | }, |
| 170 | del : function (bool) { |
| 171 | if (arguments.length === 1) |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 172 | this._del = _bool(bool); |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 173 | return this._del; |
| 174 | } |
| 175 | }; |
| 176 | |
| 177 | |
| 178 | /** |
| 179 | * Unspecified criterion |
| 180 | */ |
Nils Diewald | 8f4e254 | 2014-12-19 04:42:09 +0000 | [diff] [blame] | 181 | KorAP.UnspecifiedDoc = { |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 182 | _ldType : "doc", |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 183 | create : function (parent) { |
Nils Diewald | 8f4e254 | 2014-12-19 04:42:09 +0000 | [diff] [blame] | 184 | var obj = Object.create(KorAP.JsonLD).upgradeTo(KorAP.UnspecifiedDoc); |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 185 | if (parent !== undefined) |
| 186 | obj._parent = parent; |
| 187 | return obj; |
| 188 | }, |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 189 | update : function () { |
| 190 | if (this._element === undefined) |
| 191 | return this.element(); |
| 192 | |
| 193 | _removeChildren(this._element); |
| 194 | |
| 195 | var ellipsis = document.createElement('span'); |
| 196 | ellipsis.appendChild(document.createTextNode('⋯')); |
| 197 | this._element.appendChild(ellipsis); |
| 198 | |
| 199 | // Set operators |
| 200 | var op = this.operators( |
| 201 | false, |
| 202 | false, |
| 203 | // No delete object, if this is the root |
| 204 | this._parent !== undefined ? true : false |
| 205 | ); |
| 206 | |
| 207 | this._element.appendChild( |
| 208 | op.element() |
| 209 | ); |
| 210 | |
| 211 | |
| 212 | return this.element(); |
| 213 | }, |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 214 | element : function () { |
| 215 | if (this._element !== undefined) |
| 216 | return this._element; |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 217 | |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 218 | this._element = document.createElement('div'); |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 219 | this._element.setAttribute('class', 'unspecified'); |
| 220 | |
| 221 | this.update(); |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 222 | return this._element; |
| 223 | } |
| 224 | }; |
| 225 | |
| 226 | |
Nils Diewald | 8f4e254 | 2014-12-19 04:42:09 +0000 | [diff] [blame] | 227 | /** |
| 228 | * Virtual collection doc criterion. |
| 229 | */ |
| 230 | KorAP.Doc = { |
| 231 | _ldType : "doc", |
| 232 | _obj : function () { return KorAP.Doc; }, |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 233 | |
| 234 | create : function (parent, json) { |
Nils Diewald | 8f4e254 | 2014-12-19 04:42:09 +0000 | [diff] [blame] | 235 | var obj = Object(KorAP.JsonLD).create().upgradeTo(KorAP.Doc).fromJson(json); |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 236 | if (parent !== undefined) |
| 237 | obj._parent = parent; |
| 238 | return obj; |
| 239 | }, |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 240 | update : function () { |
| 241 | if (this._element === undefined) |
| 242 | return this.element(); |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 243 | |
| 244 | // Added key |
| 245 | var key = document.createElement('span'); |
| 246 | key.setAttribute('class', 'key'); |
| 247 | if (this.key()) |
| 248 | key.appendChild(document.createTextNode(this.key())); |
| 249 | |
| 250 | // Added match operator |
| 251 | var matchop = document.createElement('span'); |
| 252 | matchop.setAttribute('data-type', this.type()); |
| 253 | matchop.setAttribute('class', 'match'); |
| 254 | matchop.appendChild(document.createTextNode(this.matchop())); |
| 255 | |
| 256 | // Added match operator |
| 257 | var value = document.createElement('span'); |
| 258 | value.setAttribute('data-type', this.type()); |
| 259 | value.setAttribute('class', 'value'); |
| 260 | if (this.value()) |
| 261 | value.appendChild(document.createTextNode(this.value())); |
| 262 | |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 263 | var e = this._element; |
| 264 | |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 265 | // Add spans |
| 266 | e.appendChild(key); |
| 267 | e.appendChild(matchop); |
| 268 | e.appendChild(value); |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 269 | |
| 270 | // Set operators |
| 271 | var op = this.operators(true, true, true); |
| 272 | |
| 273 | e.appendChild(op.element()); |
| 274 | |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 275 | return e; |
| 276 | }, |
| 277 | |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 278 | element : function () { |
| 279 | if (this._element !== undefined) |
| 280 | return this._element; |
| 281 | |
| 282 | this._element = document.createElement('div'); |
| 283 | this._element.setAttribute('class', 'doc'); |
| 284 | |
| 285 | this.update(); |
| 286 | return this._element; |
| 287 | }, |
| 288 | |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 289 | // Wrap a new operation around the doc element |
| 290 | wrap : function (op) { |
Nils Diewald | 8f4e254 | 2014-12-19 04:42:09 +0000 | [diff] [blame] | 291 | var group = KorAP.DocGroup.create(undefined); |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 292 | group.append(this); |
| 293 | group.append(null); |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 294 | this.parent(group); |
| 295 | /* |
| 296 | var div = document.createElement('div'); |
| 297 | div.setAttribute('data-operation', op); |
| 298 | var parent = this.element.parent; |
| 299 | parent.removeChild(this.element); |
| 300 | parent.appendChild(div); |
| 301 | div.appendChild(this.element); |
| 302 | return div; |
| 303 | */ |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 304 | }, |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 305 | // Deserialize from json |
| 306 | fromJson : function (json) { |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 307 | if (json === undefined) |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 308 | return this; |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 309 | |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 310 | if (json["@type"] !== "korap:doc") { |
| 311 | KorAP.log(701, "JSON-LD group has no @type attribute"); |
| 312 | return; |
| 313 | }; |
| 314 | |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 315 | if (json["value"] === undefined || |
| 316 | typeof json["value"] != 'string') { |
| 317 | KorAP.log(805, "Value is invalid"); |
| 318 | return; |
| 319 | }; |
| 320 | |
| 321 | // There is a defined key |
| 322 | if (json["key"] !== undefined && |
| 323 | typeof json["key"] === 'string') { |
| 324 | |
| 325 | // Set key |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 326 | this.key(json["key"]); |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 327 | |
| 328 | // Set match operation |
| 329 | if (json["match"] !== undefined) { |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 330 | if (typeof json["match"] === 'string') { |
| 331 | this.matchop(json["match"]); |
| 332 | } |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 333 | else { |
| 334 | KorAP.log(802, "Match type is not supported by value type"); |
| 335 | return; |
| 336 | }; |
| 337 | }; |
| 338 | |
| 339 | // Key is a string |
| 340 | if (json["type"] === undefined || |
| 341 | json["type"] == "type:string") { |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 342 | this.type("string"); |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 343 | |
| 344 | // Check match type |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 345 | if (!KorAP._validStringMatchRE.test(this.matchop())) { |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 346 | KorAP.log(802, "Match type is not supported by value type"); |
| 347 | return; |
| 348 | }; |
| 349 | |
| 350 | // Set string value |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 351 | this.value(json["value"]); |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | // Key is a date |
| 355 | else if (json["type"] === "type:date") { |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 356 | this.type("date"); |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 357 | |
| 358 | if (json["value"] !== undefined && |
| 359 | KorAP._validDateRE.test(json["value"])) { |
| 360 | |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 361 | if (!KorAP._validDateMatchRE.test(this.matchop())) { |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 362 | KorAP.log(802, "Match type is not supported by value type"); |
| 363 | return; |
| 364 | }; |
| 365 | |
| 366 | // Set value |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 367 | this.value(json["value"]); |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 368 | } |
| 369 | else { |
| 370 | KorAP.log(806, "Value is not a valid date string"); |
| 371 | return; |
| 372 | }; |
| 373 | } |
| 374 | |
| 375 | // Key is a regular expression |
| 376 | else if (json["type"] === "type:regex") { |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 377 | this.type("regex"); |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 378 | |
| 379 | try { |
| 380 | |
| 381 | // Try to create a regular expression |
| 382 | var check = new RegExp(json["value"]); |
| 383 | |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 384 | if (!KorAP._validRegexMatchRE.test(this.matchop())) { |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 385 | KorAP.log(802, "Match type is not supported by value type"); |
| 386 | return; |
| 387 | }; |
| 388 | |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 389 | this.value(json["value"]); |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 390 | } |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 391 | |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 392 | catch (e) { |
| 393 | KorAP.log(807, "Value is not a valid regular expression"); |
| 394 | return; |
| 395 | }; |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 396 | this.type("regex"); |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | else { |
| 400 | KorAP.log(804, "Unknown value type"); |
| 401 | return; |
| 402 | }; |
| 403 | }; |
| 404 | |
| 405 | return this; |
| 406 | }, |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 407 | key : function (value) { |
| 408 | if (arguments.length === 1) |
| 409 | this._key = value; |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 410 | return this._key; |
| 411 | }, |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 412 | matchop : function (match) { |
| 413 | if (arguments.length === 1) |
| 414 | this._matchop = match.replace(/^match:/, ''); |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 415 | return this._matchop || "eq"; |
| 416 | }, |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 417 | type : function (type) { |
| 418 | if (arguments.length === 1) |
| 419 | this._type = type; |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 420 | return this._type || "string"; |
| 421 | }, |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 422 | value : function (value) { |
| 423 | if (arguments.length === 1) |
| 424 | this._value = value; |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 425 | return this._value; |
| 426 | }, |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 427 | toJson : function () { |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 428 | if (!this.matchop() || !this.key()) |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 429 | return {}; |
| 430 | |
| 431 | return { |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 432 | "@type" : "korap:" + this.ldType(), |
| 433 | "key" : this.key(), |
| 434 | "match" : "match:" + this.matchop(), |
| 435 | "value" : this.value() || '', |
| 436 | "type" : "type:" + this.type() |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 437 | }; |
| 438 | } |
| 439 | }; |
Nils Diewald | 8f4e254 | 2014-12-19 04:42:09 +0000 | [diff] [blame] | 440 | |
| 441 | /** |
| 442 | * Virtual collection group criterion. |
| 443 | */ |
| 444 | KorAP.DocGroup = { |
| 445 | _ldType : "docGroup", |
| 446 | |
| 447 | create : function (parent, json) { |
| 448 | var obj = Object.create(KorAP.JsonLD).upgradeTo(KorAP.DocGroup); |
| 449 | obj._operands = []; |
| 450 | obj.fromJson(json); |
| 451 | if (parent !== undefined) |
| 452 | obj._parent = parent; |
| 453 | return obj; |
| 454 | }, |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 455 | append : function (operand) { |
| 456 | |
| 457 | // Append unspecified object |
| 458 | if (operand === undefined) { |
| 459 | |
| 460 | // Be aware of cyclic structures! |
| 461 | operand = KorAP.UnspecifiedDoc.create(this); |
| 462 | this._operands.push(operand); |
| 463 | this.update(); |
| 464 | return operand; |
| 465 | }; |
| 466 | |
Nils Diewald | 8f4e254 | 2014-12-19 04:42:09 +0000 | [diff] [blame] | 467 | switch (operand["@type"]) { |
| 468 | case undefined: |
| 469 | if (operand["ldType"] !== undefined) { |
| 470 | if (operand.ldType() !== 'doc' && |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 471 | operand.ldType() !== 'docGroup') { |
Nils Diewald | 8f4e254 | 2014-12-19 04:42:09 +0000 | [diff] [blame] | 472 | KorAP.log(812, "Operand not supported in document group"); |
| 473 | return; |
| 474 | }; |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 475 | // Be aware of cyclic structures! |
| 476 | operand.parent(this); |
Nils Diewald | 8f4e254 | 2014-12-19 04:42:09 +0000 | [diff] [blame] | 477 | this._operands.push(operand); |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 478 | this.update(); |
Nils Diewald | 8f4e254 | 2014-12-19 04:42:09 +0000 | [diff] [blame] | 479 | return operand; |
| 480 | }; |
| 481 | |
| 482 | KorAP.log(701, "JSON-LD group has no @type attribute"); |
| 483 | return; |
| 484 | |
| 485 | case "korap:doc": |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 486 | // Be aware of cyclic structures! |
| 487 | var doc = KorAP.Doc.create(this, operand); |
Nils Diewald | 8f4e254 | 2014-12-19 04:42:09 +0000 | [diff] [blame] | 488 | if (doc === undefined) |
| 489 | return; |
| 490 | this._operands.push(doc); |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 491 | this.update(); |
Nils Diewald | 8f4e254 | 2014-12-19 04:42:09 +0000 | [diff] [blame] | 492 | return doc; |
| 493 | |
| 494 | case "korap:docGroup": |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 495 | // Be aware of cyclic structures! |
| 496 | var docGroup = KorAP.DocGroup.create(this, operand); |
Nils Diewald | 8f4e254 | 2014-12-19 04:42:09 +0000 | [diff] [blame] | 497 | if (docGroup === undefined) |
| 498 | return; |
| 499 | this._operands.push(docGroup); |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 500 | this.update(); |
Nils Diewald | 8f4e254 | 2014-12-19 04:42:09 +0000 | [diff] [blame] | 501 | return docGroup; |
| 502 | |
| 503 | default: |
| 504 | KorAP.log(812, "Operand not supported in document group"); |
| 505 | return; |
| 506 | }; |
| 507 | }, |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 508 | update : function () { |
| 509 | if (this._element === undefined) |
| 510 | return this.element(); |
| 511 | |
| 512 | var op = this._element; |
| 513 | op.setAttribute('data-operation', this.operation()); |
| 514 | |
| 515 | _removeChildren(op); |
| 516 | |
| 517 | // Append operands |
| 518 | for (var i in this.operands()) { |
| 519 | op.appendChild( |
| 520 | this.getOperand(i).element() |
| 521 | ); |
| 522 | }; |
| 523 | |
| 524 | // Set operators |
| 525 | var op = this.operators( |
| 526 | this.operation() == 'and' ? false : true, |
| 527 | this.operation() == 'or' ? false : true, |
| 528 | true |
| 529 | ); |
| 530 | |
| 531 | this._element.appendChild( |
| 532 | op.element() |
| 533 | ); |
| 534 | |
| 535 | return op; |
| 536 | }, |
Nils Diewald | 8f4e254 | 2014-12-19 04:42:09 +0000 | [diff] [blame] | 537 | element : function () { |
| 538 | if (this._element !== undefined) |
| 539 | return this._element; |
| 540 | |
| 541 | this._element = document.createElement('div'); |
| 542 | this._element.setAttribute('class', 'docGroup'); |
Nils Diewald | 8f4e254 | 2014-12-19 04:42:09 +0000 | [diff] [blame] | 543 | |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 544 | this.update(); |
Nils Diewald | 8f4e254 | 2014-12-19 04:42:09 +0000 | [diff] [blame] | 545 | |
| 546 | return this._element; |
| 547 | }, |
| 548 | operation : function (op) { |
| 549 | if (arguments.length === 1) { |
| 550 | if (KorAP._validGroupOpRE.test(op)) { |
| 551 | this._op = op; |
| 552 | } |
| 553 | else { |
| 554 | KorAP.log(810, "Unknown operation type"); |
| 555 | return; |
| 556 | }; |
| 557 | }; |
| 558 | return this._op || 'and'; |
| 559 | }, |
| 560 | operands : function () { |
| 561 | return this._operands; |
| 562 | }, |
| 563 | getOperand : function (index) { |
| 564 | return this._operands[index]; |
| 565 | }, |
| 566 | |
| 567 | // Deserialize from json |
| 568 | fromJson : function (json) { |
| 569 | if (json === undefined) |
| 570 | return this; |
| 571 | |
| 572 | if (json["@type"] !== "korap:docGroup") { |
| 573 | KorAP.log(701, "JSON-LD group has no @type attribute"); |
| 574 | return; |
| 575 | }; |
| 576 | |
| 577 | if (json["operation"] === undefined || |
| 578 | typeof json["operation"] !== 'string') { |
| 579 | KorAP.log(811, "Document group expects operation"); |
| 580 | return; |
| 581 | }; |
| 582 | |
| 583 | var operation = json["operation"]; |
| 584 | |
| 585 | this.operation(operation.replace(/^operation:/,'')); |
| 586 | |
| 587 | if (json["operands"] === undefined || |
| 588 | !(json["operands"] instanceof Array)) { |
| 589 | KorAP.log(704, "Operation needs operand list") |
| 590 | return; |
| 591 | }; |
| 592 | |
| 593 | // Add all documents |
| 594 | for (var i in json["operands"]) { |
| 595 | var operand = json["operands"][i]; |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 596 | this.append(operand); |
Nils Diewald | 8f4e254 | 2014-12-19 04:42:09 +0000 | [diff] [blame] | 597 | }; |
| 598 | |
| 599 | return this; |
| 600 | }, |
| 601 | toJson : function () { |
| 602 | var opArray = new Array(); |
| 603 | for (var i in this._operands) { |
| 604 | opArray.push(this._operands[i].toJson()); |
| 605 | }; |
| 606 | return { |
| 607 | "@type" : "korap:" + this.ldType(), |
| 608 | "operation" : "operation:" + this.operation(), |
| 609 | "operands" : opArray |
| 610 | }; |
| 611 | } |
| 612 | }; |
| 613 | |
| 614 | |
| 615 | // Abstract JsonLD object |
| 616 | KorAP.JsonLD = { |
| 617 | create : function () { |
| 618 | return Object.create(KorAP.JsonLD); |
| 619 | }, |
| 620 | |
| 621 | /** |
| 622 | * Upgrade this object to another object |
| 623 | * while private data stays intact |
| 624 | */ |
| 625 | upgradeTo : function (props) { |
| 626 | for (var prop in props) { |
| 627 | this[prop] = props[prop]; |
| 628 | }; |
| 629 | return this; |
| 630 | }, |
| 631 | ldType : function (type) { |
| 632 | if (arguments.length === 1) |
| 633 | this._ldType = type; |
| 634 | return this._ldType; |
| 635 | }, |
| 636 | parent : function (obj) { |
| 637 | if (arguments.length === 1) |
| 638 | this._parent = obj; |
| 639 | return this._parent; |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 640 | }, |
| 641 | operators : function (and, or, del) { |
| 642 | if (arguments === 0) |
| 643 | return this._ops; |
| 644 | this._ops = KorAP.Operators.create( |
| 645 | and, or, del |
| 646 | ); |
| 647 | return this._ops; |
| 648 | }, |
| 649 | toJson : function () { |
| 650 | return { |
| 651 | // Unspecified object |
| 652 | "@type" : "korap:" + this.ldType() |
| 653 | }; |
Nils Diewald | 8f4e254 | 2014-12-19 04:42:09 +0000 | [diff] [blame] | 654 | } |
| 655 | }; |
| 656 | |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 657 | }(this.KorAP)); |