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