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