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)$"); |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 35 | KorAP._quote = new RegExp("([\"\\\\])", 'g'); |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 36 | |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 37 | var loc = (KorAP.Locale = KorAP.Locale || {} ); |
| 38 | loc.AND = loc.AND || 'and'; |
| 39 | loc.OR = loc.OR || 'or'; |
| 40 | loc.DEL = loc.DEL || '×'; |
| 41 | loc.EMPTY = loc.EMPTY || '⋯' |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 42 | |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 43 | function _bool (bool) { |
| 44 | return (bool === undefined || bool === false) ? false : true; |
| 45 | }; |
| 46 | |
| 47 | function _removeChildren (node) { |
| 48 | // Remove everything underneath |
Nils Diewald | 8f6b610 | 2015-01-08 18:25:33 +0000 | [diff] [blame] | 49 | while (node.firstChild) |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 50 | node.removeChild(node.firstChild); |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 51 | }; |
| 52 | |
Nils Diewald | 4019bd2 | 2015-01-08 19:57:50 +0000 | [diff] [blame^] | 53 | |
| 54 | // Add 'or'-criterion |
| 55 | KorAP._or = function (e) { |
| 56 | var obj = this.parentNode.refTo; |
| 57 | }; |
| 58 | |
| 59 | |
| 60 | // Add 'and'-criterion |
| 61 | KorAP._and = function (e) { |
| 62 | var obj = this.parentNode.refTo; |
| 63 | }; |
| 64 | |
| 65 | // Remove doc or docGroup |
| 66 | KorAP._delete = function (e) { |
| 67 | var obj = this.parentNode.refTo; |
| 68 | if (obj.parent().ldType() !== null) |
| 69 | obj.parent().delOperand(obj).update(); |
| 70 | else |
| 71 | obj.parent().clean(); |
| 72 | }; |
| 73 | |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 74 | KorAP.VirtualCollection = { |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 75 | ldType : function () { |
| 76 | return null; |
| 77 | }, |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 78 | create : function () { |
| 79 | return Object.create(KorAP.VirtualCollection); |
| 80 | }, |
Nils Diewald | 4019bd2 | 2015-01-08 19:57:50 +0000 | [diff] [blame^] | 81 | clean : function () { |
| 82 | if (this._root.ldType() !== "non") { |
| 83 | this._root.destroy(); |
| 84 | this.root(KorAP.UnspecifiedDoc.create(this)); |
| 85 | }; |
| 86 | return this; |
| 87 | }, |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 88 | render : function (json) { |
| 89 | var obj = Object.create(KorAP.VirtualCollection); |
| 90 | |
| 91 | if (json !== undefined) { |
| 92 | // Root object |
| 93 | if (json['@type'] == 'korap:doc') { |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 94 | obj._root = KorAP.Doc.create(obj, json); |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 95 | } |
| 96 | else if (json['@type'] == 'korap:docGroup') { |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 97 | obj._root = KorAP.DocGroup.create(obj, json); |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 98 | } |
| 99 | else { |
| 100 | KorAP.log(813, "Collection type is not supported"); |
| 101 | return; |
| 102 | }; |
| 103 | } |
| 104 | |
| 105 | else { |
| 106 | // Add unspecified object |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 107 | obj._root = KorAP.UnspecifiedDoc.create(obj); |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 108 | }; |
| 109 | |
Nils Diewald | 8e7182e | 2015-01-08 15:02:07 +0000 | [diff] [blame] | 110 | // Init element and update |
| 111 | obj.update(); |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 112 | |
| 113 | return obj; |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 114 | }, |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 115 | root : function (obj) { |
Nils Diewald | 8e7182e | 2015-01-08 15:02:07 +0000 | [diff] [blame] | 116 | if (arguments.length === 1) { |
Nils Diewald | 8f6b610 | 2015-01-08 18:25:33 +0000 | [diff] [blame] | 117 | var e = this.element(); |
| 118 | if (e.firstChild !== null) { |
| 119 | if (e.firstChild !== obj.element()) |
| 120 | e.replaceChild(obj.element(), e.firstChild); |
| 121 | } |
| 122 | |
| 123 | // Append root element |
| 124 | else { |
| 125 | e.appendChild(obj.element()); |
| 126 | }; |
| 127 | |
| 128 | // Update parent child relations |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 129 | this._root = obj; |
Nils Diewald | 8f6b610 | 2015-01-08 18:25:33 +0000 | [diff] [blame] | 130 | obj.parent(this); |
| 131 | |
Nils Diewald | 8e7182e | 2015-01-08 15:02:07 +0000 | [diff] [blame] | 132 | this.update(); |
| 133 | }; |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 134 | return this._root; |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 135 | }, |
Nils Diewald | 8f6b610 | 2015-01-08 18:25:33 +0000 | [diff] [blame] | 136 | |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 137 | element : function () { |
| 138 | if (this._element !== undefined) |
| 139 | return this._element; |
| 140 | |
| 141 | this._element = document.createElement('div'); |
| 142 | this._element.setAttribute('class', 'vc'); |
Nils Diewald | 8e7182e | 2015-01-08 15:02:07 +0000 | [diff] [blame] | 143 | |
Nils Diewald | 8f6b610 | 2015-01-08 18:25:33 +0000 | [diff] [blame] | 144 | // Initialize root |
| 145 | this._element.appendChild(this._root.element()); |
| 146 | |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 147 | return this._element; |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 148 | }, |
| 149 | toJson : function () { |
| 150 | return this._root.toJson(); |
| 151 | }, |
| 152 | toString : function () { |
| 153 | return this._root.toString(); |
Nils Diewald | 8e7182e | 2015-01-08 15:02:07 +0000 | [diff] [blame] | 154 | }, |
| 155 | update : function () { |
Nils Diewald | 8f6b610 | 2015-01-08 18:25:33 +0000 | [diff] [blame] | 156 | this._root.update(); |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 157 | } |
| 158 | }; |
| 159 | |
Nils Diewald | 8f4e254 | 2014-12-19 04:42:09 +0000 | [diff] [blame] | 160 | /** |
| 161 | * Operators for criteria |
| 162 | */ |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 163 | KorAP.Operators = { |
| 164 | create : function (and, or, del) { |
| 165 | var op = Object.create(KorAP.Operators); |
| 166 | op.and(and); |
| 167 | op.or(or); |
| 168 | op.del(del); |
| 169 | return op; |
| 170 | }, |
| 171 | update : function () { |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 172 | // Init the element |
| 173 | if (this._element === undefined) |
| 174 | return this.element(); |
| 175 | |
| 176 | var op = this._element; |
| 177 | |
Nils Diewald | 0297ba1 | 2015-01-05 21:56:12 +0000 | [diff] [blame] | 178 | op.refTo = this.parent(); |
| 179 | |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 180 | // Remove everything underneath |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 181 | _removeChildren(op); |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 182 | |
| 183 | // Add and button |
| 184 | if (this._and === true) { |
| 185 | var andE = document.createElement('span'); |
| 186 | andE.setAttribute('class', 'and'); |
Nils Diewald | 0297ba1 | 2015-01-05 21:56:12 +0000 | [diff] [blame] | 187 | andE.addEventListener('click', KorAP._and, false); |
Nils Diewald | 8f6b610 | 2015-01-08 18:25:33 +0000 | [diff] [blame] | 188 | andE.appendChild( |
| 189 | document.createTextNode(KorAP.Locale.AND) |
| 190 | ); |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 191 | op.appendChild(andE); |
| 192 | }; |
| 193 | |
| 194 | // Add or button |
| 195 | if (this._or === true) { |
| 196 | var orE = document.createElement('span'); |
| 197 | orE.setAttribute('class', 'or'); |
Nils Diewald | 0297ba1 | 2015-01-05 21:56:12 +0000 | [diff] [blame] | 198 | orE.addEventListener('click', KorAP._or, false); |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 199 | orE.appendChild(document.createTextNode(KorAP.Locale.OR)); |
| 200 | op.appendChild(orE); |
| 201 | }; |
| 202 | |
| 203 | // Add delete button |
| 204 | if (this._del === true) { |
| 205 | var delE = document.createElement('span'); |
| 206 | delE.setAttribute('class', 'delete'); |
| 207 | delE.appendChild(document.createTextNode(KorAP.Locale.DEL)); |
Nils Diewald | 0297ba1 | 2015-01-05 21:56:12 +0000 | [diff] [blame] | 208 | delE.addEventListener('click', KorAP._delete, false); |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 209 | op.appendChild(delE); |
| 210 | }; |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 211 | |
| 212 | return op; |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 213 | }, |
Nils Diewald | 0297ba1 | 2015-01-05 21:56:12 +0000 | [diff] [blame] | 214 | |
| 215 | // Be aware! This may be cyclic |
| 216 | parent : function (obj) { |
| 217 | if (arguments.length === 1) |
| 218 | this._parent = obj; |
| 219 | return this._parent; |
| 220 | }, |
Nils Diewald | 5c817a4 | 2015-01-06 01:08:56 +0000 | [diff] [blame] | 221 | |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 222 | element : function () { |
| 223 | |
| 224 | // Return existing element |
| 225 | if (this._element !== undefined) |
| 226 | return this._element; |
| 227 | |
| 228 | this._element = document.createElement('div'); |
| 229 | this._element.setAttribute('class', 'operators'); |
| 230 | |
| 231 | // Init elements |
| 232 | this.update(); |
| 233 | return this._element; |
| 234 | }, |
| 235 | and : function (bool) { |
| 236 | if (arguments.length === 1) |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 237 | this._and = _bool(bool); |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 238 | return this._and; |
| 239 | }, |
| 240 | or : function (bool) { |
| 241 | if (arguments.length === 1) |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 242 | this._or = _bool(bool); |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 243 | return this._or; |
| 244 | }, |
| 245 | del : function (bool) { |
| 246 | if (arguments.length === 1) |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 247 | this._del = _bool(bool); |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 248 | return this._del; |
| 249 | } |
| 250 | }; |
| 251 | |
| 252 | |
| 253 | /** |
| 254 | * Unspecified criterion |
| 255 | */ |
Nils Diewald | 8f4e254 | 2014-12-19 04:42:09 +0000 | [diff] [blame] | 256 | KorAP.UnspecifiedDoc = { |
Nils Diewald | 4019bd2 | 2015-01-08 19:57:50 +0000 | [diff] [blame^] | 257 | _ldType : "non", |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 258 | create : function (parent) { |
Nils Diewald | 8f6b610 | 2015-01-08 18:25:33 +0000 | [diff] [blame] | 259 | var obj = Object.create(KorAP.JsonLD). |
| 260 | upgradeTo(KorAP.UnspecifiedDoc); |
| 261 | |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 262 | if (parent !== undefined) |
| 263 | obj._parent = parent; |
Nils Diewald | 8f6b610 | 2015-01-08 18:25:33 +0000 | [diff] [blame] | 264 | |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 265 | return obj; |
| 266 | }, |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 267 | update : function () { |
Nils Diewald | 4019bd2 | 2015-01-08 19:57:50 +0000 | [diff] [blame^] | 268 | |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 269 | if (this._element === undefined) |
| 270 | return this.element(); |
| 271 | |
Nils Diewald | 8f6b610 | 2015-01-08 18:25:33 +0000 | [diff] [blame] | 272 | // Remove element content |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 273 | _removeChildren(this._element); |
| 274 | |
| 275 | var ellipsis = document.createElement('span'); |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 276 | ellipsis.appendChild(document.createTextNode(loc.EMPTY)); |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 277 | this._element.appendChild(ellipsis); |
| 278 | |
| 279 | // Set operators |
| 280 | var op = this.operators( |
| 281 | false, |
| 282 | false, |
| 283 | // No delete object, if this is the root |
Nils Diewald | 8f6b610 | 2015-01-08 18:25:33 +0000 | [diff] [blame] | 284 | (this._parent !== undefined && |
| 285 | this.parent().ldType() !== null) ? true : false |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 286 | ); |
| 287 | |
| 288 | this._element.appendChild( |
| 289 | op.element() |
| 290 | ); |
| 291 | |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 292 | return this.element(); |
| 293 | }, |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 294 | element : function () { |
| 295 | if (this._element !== undefined) |
| 296 | return this._element; |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 297 | |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 298 | this._element = document.createElement('div'); |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 299 | this._element.setAttribute('class', 'unspecified'); |
| 300 | |
| 301 | this.update(); |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 302 | return this._element; |
| 303 | } |
| 304 | }; |
| 305 | |
| 306 | |
Nils Diewald | 8f4e254 | 2014-12-19 04:42:09 +0000 | [diff] [blame] | 307 | /** |
| 308 | * Virtual collection doc criterion. |
| 309 | */ |
| 310 | KorAP.Doc = { |
| 311 | _ldType : "doc", |
| 312 | _obj : function () { return KorAP.Doc; }, |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 313 | |
| 314 | create : function (parent, json) { |
Nils Diewald | 8f6b610 | 2015-01-08 18:25:33 +0000 | [diff] [blame] | 315 | var obj = Object(KorAP.JsonLD). |
| 316 | create(). |
| 317 | upgradeTo(KorAP.Doc). |
| 318 | fromJson(json); |
| 319 | |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 320 | if (parent !== undefined) |
| 321 | obj._parent = parent; |
Nils Diewald | 8f6b610 | 2015-01-08 18:25:33 +0000 | [diff] [blame] | 322 | |
| 323 | obj._changed = true; |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 324 | return obj; |
| 325 | }, |
Nils Diewald | 5c817a4 | 2015-01-06 01:08:56 +0000 | [diff] [blame] | 326 | |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 327 | update : function () { |
| 328 | if (this._element === undefined) |
| 329 | return this.element(); |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 330 | |
Nils Diewald | 8f6b610 | 2015-01-08 18:25:33 +0000 | [diff] [blame] | 331 | // Get element |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 332 | var e = this._element; |
| 333 | |
Nils Diewald | 8f6b610 | 2015-01-08 18:25:33 +0000 | [diff] [blame] | 334 | // Check if there is a change |
| 335 | if (this._changed) { |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 336 | |
Nils Diewald | 8f6b610 | 2015-01-08 18:25:33 +0000 | [diff] [blame] | 337 | // Added key |
| 338 | var key = document.createElement('span'); |
| 339 | key.setAttribute('class', 'key'); |
| 340 | if (this.key()) |
| 341 | key.appendChild(document.createTextNode(this.key())); |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 342 | |
Nils Diewald | 8f6b610 | 2015-01-08 18:25:33 +0000 | [diff] [blame] | 343 | // Added match operator |
| 344 | var matchop = document.createElement('span'); |
| 345 | matchop.setAttribute('data-type', this.type()); |
| 346 | matchop.setAttribute('class', 'match'); |
| 347 | matchop.appendChild( |
| 348 | document.createTextNode(this.matchop()) |
| 349 | ); |
| 350 | |
| 351 | // Added match operator |
| 352 | var value = document.createElement('span'); |
| 353 | value.setAttribute('data-type', this.type()); |
| 354 | value.setAttribute('class', 'value'); |
| 355 | if (this.value()) |
| 356 | value.appendChild( |
| 357 | document.createTextNode(this.value()) |
| 358 | ); |
| 359 | |
| 360 | // Remove all element children |
| 361 | _removeChildren(e); |
| 362 | |
| 363 | // Add spans |
| 364 | e.appendChild(key); |
| 365 | e.appendChild(matchop); |
| 366 | e.appendChild(value); |
| 367 | |
| 368 | this._changed = false; |
| 369 | }; |
| 370 | |
| 371 | if (this._parent !== undefined) { |
| 372 | // Set operators |
| 373 | var op = this.operators( |
| 374 | true, |
| 375 | true, |
Nils Diewald | 4019bd2 | 2015-01-08 19:57:50 +0000 | [diff] [blame^] | 376 | true |
Nils Diewald | 8f6b610 | 2015-01-08 18:25:33 +0000 | [diff] [blame] | 377 | ); |
| 378 | |
| 379 | // Append new operators |
| 380 | e.appendChild(op.element()); |
| 381 | }; |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 382 | |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 383 | return e; |
| 384 | }, |
| 385 | |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 386 | element : function () { |
| 387 | if (this._element !== undefined) |
| 388 | return this._element; |
| 389 | |
| 390 | this._element = document.createElement('div'); |
| 391 | this._element.setAttribute('class', 'doc'); |
| 392 | |
| 393 | this.update(); |
| 394 | return this._element; |
| 395 | }, |
| 396 | |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 397 | // Wrap a new operation around the doc element |
| 398 | wrap : function (op) { |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 399 | /* |
Nils Diewald | 8f4e254 | 2014-12-19 04:42:09 +0000 | [diff] [blame] | 400 | var group = KorAP.DocGroup.create(undefined); |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 401 | group.append(this); |
| 402 | group.append(null); |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 403 | this.parent(group); |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 404 | var div = document.createElement('div'); |
| 405 | div.setAttribute('data-operation', op); |
| 406 | var parent = this.element.parent; |
| 407 | parent.removeChild(this.element); |
| 408 | parent.appendChild(div); |
| 409 | div.appendChild(this.element); |
| 410 | return div; |
| 411 | */ |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 412 | }, |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 413 | // Deserialize from json |
| 414 | fromJson : function (json) { |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 415 | if (json === undefined) |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 416 | return this; |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 417 | |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 418 | if (json["@type"] !== "korap:doc") { |
| 419 | KorAP.log(701, "JSON-LD group has no @type attribute"); |
| 420 | return; |
| 421 | }; |
| 422 | |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 423 | if (json["value"] === undefined || |
| 424 | typeof json["value"] != 'string') { |
| 425 | KorAP.log(805, "Value is invalid"); |
| 426 | return; |
| 427 | }; |
| 428 | |
| 429 | // There is a defined key |
| 430 | if (json["key"] !== undefined && |
| 431 | typeof json["key"] === 'string') { |
| 432 | |
| 433 | // Set key |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 434 | this.key(json["key"]); |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 435 | |
| 436 | // Set match operation |
| 437 | if (json["match"] !== undefined) { |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 438 | if (typeof json["match"] === 'string') { |
| 439 | this.matchop(json["match"]); |
| 440 | } |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 441 | else { |
| 442 | KorAP.log(802, "Match type is not supported by value type"); |
| 443 | return; |
| 444 | }; |
| 445 | }; |
| 446 | |
| 447 | // Key is a string |
| 448 | if (json["type"] === undefined || |
| 449 | json["type"] == "type:string") { |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 450 | this.type("string"); |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 451 | |
| 452 | // Check match type |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 453 | if (!KorAP._validStringMatchRE.test(this.matchop())) { |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 454 | KorAP.log(802, "Match type is not supported by value type"); |
| 455 | return; |
| 456 | }; |
| 457 | |
| 458 | // Set string value |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 459 | this.value(json["value"]); |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | // Key is a date |
| 463 | else if (json["type"] === "type:date") { |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 464 | this.type("date"); |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 465 | |
| 466 | if (json["value"] !== undefined && |
| 467 | KorAP._validDateRE.test(json["value"])) { |
| 468 | |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 469 | if (!KorAP._validDateMatchRE.test(this.matchop())) { |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 470 | KorAP.log(802, "Match type is not supported by value type"); |
| 471 | return; |
| 472 | }; |
| 473 | |
| 474 | // Set value |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 475 | this.value(json["value"]); |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 476 | } |
| 477 | else { |
| 478 | KorAP.log(806, "Value is not a valid date string"); |
| 479 | return; |
| 480 | }; |
| 481 | } |
| 482 | |
| 483 | // Key is a regular expression |
| 484 | else if (json["type"] === "type:regex") { |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 485 | this.type("regex"); |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 486 | |
| 487 | try { |
| 488 | |
| 489 | // Try to create a regular expression |
| 490 | var check = new RegExp(json["value"]); |
| 491 | |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 492 | if (!KorAP._validRegexMatchRE.test(this.matchop())) { |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 493 | KorAP.log(802, "Match type is not supported by value type"); |
| 494 | return; |
| 495 | }; |
| 496 | |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 497 | this.value(json["value"]); |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 498 | } |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 499 | |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 500 | catch (e) { |
| 501 | KorAP.log(807, "Value is not a valid regular expression"); |
| 502 | return; |
| 503 | }; |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 504 | this.type("regex"); |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | else { |
| 508 | KorAP.log(804, "Unknown value type"); |
| 509 | return; |
| 510 | }; |
| 511 | }; |
| 512 | |
| 513 | return this; |
| 514 | }, |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 515 | key : function (value) { |
Nils Diewald | 8f6b610 | 2015-01-08 18:25:33 +0000 | [diff] [blame] | 516 | if (arguments.length === 1) { |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 517 | this._key = value; |
Nils Diewald | 8f6b610 | 2015-01-08 18:25:33 +0000 | [diff] [blame] | 518 | this._changed = true; |
| 519 | }; |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 520 | return this._key; |
| 521 | }, |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 522 | matchop : function (match) { |
Nils Diewald | 8f6b610 | 2015-01-08 18:25:33 +0000 | [diff] [blame] | 523 | if (arguments.length === 1) { |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 524 | this._matchop = match.replace(/^match:/, ''); |
Nils Diewald | 8f6b610 | 2015-01-08 18:25:33 +0000 | [diff] [blame] | 525 | this._changed = true; |
| 526 | }; |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 527 | return this._matchop || "eq"; |
| 528 | }, |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 529 | type : function (type) { |
Nils Diewald | 8f6b610 | 2015-01-08 18:25:33 +0000 | [diff] [blame] | 530 | if (arguments.length === 1) { |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 531 | this._type = type; |
Nils Diewald | 8f6b610 | 2015-01-08 18:25:33 +0000 | [diff] [blame] | 532 | this._changed = true; |
| 533 | }; |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 534 | return this._type || "string"; |
| 535 | }, |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 536 | value : function (value) { |
Nils Diewald | 8f6b610 | 2015-01-08 18:25:33 +0000 | [diff] [blame] | 537 | if (arguments.length === 1) { |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 538 | this._value = value; |
Nils Diewald | 8f6b610 | 2015-01-08 18:25:33 +0000 | [diff] [blame] | 539 | this._changed = true; |
| 540 | }; |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 541 | return this._value; |
| 542 | }, |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 543 | toJson : function () { |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 544 | if (!this.matchop() || !this.key()) |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 545 | return {}; |
| 546 | |
| 547 | return { |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 548 | "@type" : "korap:" + this.ldType(), |
| 549 | "key" : this.key(), |
| 550 | "match" : "match:" + this.matchop(), |
| 551 | "value" : this.value() || '', |
| 552 | "type" : "type:" + this.type() |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 553 | }; |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 554 | }, |
| 555 | toString : function () { |
| 556 | if (!this.matchop() || !this.key()) |
| 557 | return ""; |
| 558 | |
| 559 | // Build doc string based on key |
| 560 | var string = this.key() + ' '; |
| 561 | |
| 562 | // Add match operator |
| 563 | switch (this.matchop()) { |
| 564 | case "ne": |
| 565 | string += '!='; |
| 566 | break; |
| 567 | case "contains": |
| 568 | string += '~'; |
| 569 | break; |
| 570 | case "geq": |
| 571 | string += 'since'; |
| 572 | break; |
| 573 | case "leq": |
| 574 | string += 'until'; |
| 575 | break; |
| 576 | default: |
| 577 | string += (this.type() == 'date') ? 'in' : '='; |
| 578 | break; |
| 579 | }; |
| 580 | |
| 581 | string += ' '; |
| 582 | |
| 583 | // Add value |
| 584 | switch (this.type()) { |
| 585 | case "date": |
| 586 | return string + this.value(); |
| 587 | break; |
| 588 | case "regex": |
| 589 | return string + '/' + this.value() + '/'; |
| 590 | break; |
| 591 | case "string": |
| 592 | return string + '"' + this.value().replace(KorAP._quote, '\\$1') + '"'; |
| 593 | break; |
| 594 | }; |
| 595 | |
| 596 | return "..."; |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 597 | } |
| 598 | }; |
Nils Diewald | 8f4e254 | 2014-12-19 04:42:09 +0000 | [diff] [blame] | 599 | |
| 600 | /** |
| 601 | * Virtual collection group criterion. |
| 602 | */ |
| 603 | KorAP.DocGroup = { |
| 604 | _ldType : "docGroup", |
| 605 | |
| 606 | create : function (parent, json) { |
| 607 | var obj = Object.create(KorAP.JsonLD).upgradeTo(KorAP.DocGroup); |
| 608 | obj._operands = []; |
| 609 | obj.fromJson(json); |
| 610 | if (parent !== undefined) |
| 611 | obj._parent = parent; |
| 612 | return obj; |
| 613 | }, |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 614 | append : function (operand) { |
| 615 | |
| 616 | // Append unspecified object |
| 617 | if (operand === undefined) { |
| 618 | |
| 619 | // Be aware of cyclic structures! |
| 620 | operand = KorAP.UnspecifiedDoc.create(this); |
| 621 | this._operands.push(operand); |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 622 | return operand; |
| 623 | }; |
| 624 | |
Nils Diewald | 8f4e254 | 2014-12-19 04:42:09 +0000 | [diff] [blame] | 625 | switch (operand["@type"]) { |
| 626 | case undefined: |
| 627 | if (operand["ldType"] !== undefined) { |
| 628 | if (operand.ldType() !== 'doc' && |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 629 | operand.ldType() !== 'docGroup') { |
Nils Diewald | 8f4e254 | 2014-12-19 04:42:09 +0000 | [diff] [blame] | 630 | KorAP.log(812, "Operand not supported in document group"); |
| 631 | return; |
| 632 | }; |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 633 | // Be aware of cyclic structures! |
| 634 | operand.parent(this); |
Nils Diewald | 8f4e254 | 2014-12-19 04:42:09 +0000 | [diff] [blame] | 635 | this._operands.push(operand); |
| 636 | return operand; |
| 637 | }; |
| 638 | |
| 639 | KorAP.log(701, "JSON-LD group has no @type attribute"); |
| 640 | return; |
| 641 | |
| 642 | case "korap:doc": |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 643 | // Be aware of cyclic structures! |
| 644 | var doc = KorAP.Doc.create(this, operand); |
Nils Diewald | 8f4e254 | 2014-12-19 04:42:09 +0000 | [diff] [blame] | 645 | if (doc === undefined) |
| 646 | return; |
| 647 | this._operands.push(doc); |
| 648 | return doc; |
| 649 | |
| 650 | case "korap:docGroup": |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 651 | // Be aware of cyclic structures! |
| 652 | var docGroup = KorAP.DocGroup.create(this, operand); |
Nils Diewald | 8f4e254 | 2014-12-19 04:42:09 +0000 | [diff] [blame] | 653 | if (docGroup === undefined) |
| 654 | return; |
| 655 | this._operands.push(docGroup); |
| 656 | return docGroup; |
| 657 | |
| 658 | default: |
| 659 | KorAP.log(812, "Operand not supported in document group"); |
| 660 | return; |
| 661 | }; |
| 662 | }, |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 663 | update : function () { |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 664 | // There is only one operand in group |
| 665 | if (this._operands.length === 1) { |
| 666 | var parent = this.parent(); |
Nils Diewald | 8e7182e | 2015-01-08 15:02:07 +0000 | [diff] [blame] | 667 | var op = this.getOperand(0); |
| 668 | |
Nils Diewald | 8f6b610 | 2015-01-08 18:25:33 +0000 | [diff] [blame] | 669 | // This will prevent destruction of |
| 670 | // the operand |
| 671 | this._operands = []; |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 672 | |
| 673 | // Parent is a group |
| 674 | if (parent.ldType() !== null) { |
Nils Diewald | 8e7182e | 2015-01-08 15:02:07 +0000 | [diff] [blame] | 675 | return parent.replaceOperand(this, op).update(); |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 676 | } |
| 677 | |
Nils Diewald | 8f6b610 | 2015-01-08 18:25:33 +0000 | [diff] [blame] | 678 | // Parent is vc |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 679 | else { |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 680 | this.destroy(); |
Nils Diewald | 8f6b610 | 2015-01-08 18:25:33 +0000 | [diff] [blame] | 681 | // Cyclic madness |
| 682 | parent.root(op); |
| 683 | op.parent(parent); |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 684 | return parent.root(); |
Nils Diewald | 5c817a4 | 2015-01-06 01:08:56 +0000 | [diff] [blame] | 685 | }; |
| 686 | }; |
| 687 | |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 688 | if (this._element === undefined) |
Nils Diewald | 5c817a4 | 2015-01-06 01:08:56 +0000 | [diff] [blame] | 689 | return this; |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 690 | |
Nils Diewald | 5c817a4 | 2015-01-06 01:08:56 +0000 | [diff] [blame] | 691 | var group = this._element; |
| 692 | group.setAttribute('data-operation', this.operation()); |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 693 | |
Nils Diewald | 5c817a4 | 2015-01-06 01:08:56 +0000 | [diff] [blame] | 694 | _removeChildren(group); |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 695 | |
| 696 | // Append operands |
Nils Diewald | 5c817a4 | 2015-01-06 01:08:56 +0000 | [diff] [blame] | 697 | for (var i in this._operands) { |
| 698 | group.appendChild( |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 699 | this.getOperand(i).element() |
| 700 | ); |
| 701 | }; |
| 702 | |
| 703 | // Set operators |
| 704 | var op = this.operators( |
| 705 | this.operation() == 'and' ? false : true, |
| 706 | this.operation() == 'or' ? false : true, |
| 707 | true |
| 708 | ); |
| 709 | |
Nils Diewald | 5c817a4 | 2015-01-06 01:08:56 +0000 | [diff] [blame] | 710 | group.appendChild( |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 711 | op.element() |
| 712 | ); |
| 713 | |
Nils Diewald | 5c817a4 | 2015-01-06 01:08:56 +0000 | [diff] [blame] | 714 | return this; |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 715 | }, |
Nils Diewald | 8f4e254 | 2014-12-19 04:42:09 +0000 | [diff] [blame] | 716 | element : function () { |
| 717 | if (this._element !== undefined) |
| 718 | return this._element; |
| 719 | |
| 720 | this._element = document.createElement('div'); |
| 721 | this._element.setAttribute('class', 'docGroup'); |
Nils Diewald | 8f4e254 | 2014-12-19 04:42:09 +0000 | [diff] [blame] | 722 | |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 723 | // Update the object - including optimization |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 724 | this.update(); |
Nils Diewald | 8f4e254 | 2014-12-19 04:42:09 +0000 | [diff] [blame] | 725 | |
| 726 | return this._element; |
| 727 | }, |
| 728 | operation : function (op) { |
| 729 | if (arguments.length === 1) { |
| 730 | if (KorAP._validGroupOpRE.test(op)) { |
| 731 | this._op = op; |
| 732 | } |
| 733 | else { |
| 734 | KorAP.log(810, "Unknown operation type"); |
| 735 | return; |
| 736 | }; |
| 737 | }; |
| 738 | return this._op || 'and'; |
| 739 | }, |
| 740 | operands : function () { |
| 741 | return this._operands; |
| 742 | }, |
| 743 | getOperand : function (index) { |
| 744 | return this._operands[index]; |
| 745 | }, |
| 746 | |
Nils Diewald | 5c817a4 | 2015-01-06 01:08:56 +0000 | [diff] [blame] | 747 | // Replace operand |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 748 | replaceOperand : function (oldOp, newOp) { |
Nils Diewald | 8e7182e | 2015-01-08 15:02:07 +0000 | [diff] [blame] | 749 | |
Nils Diewald | 5c817a4 | 2015-01-06 01:08:56 +0000 | [diff] [blame] | 750 | for (var i in this._operands) { |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 751 | if (this._operands[i] === oldOp) { |
| 752 | |
| 753 | // Just insert a doc |
| 754 | if (newOp.ldType() === "doc") { |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 755 | this._operands[i] = newOp; |
Nils Diewald | 8f6b610 | 2015-01-08 18:25:33 +0000 | [diff] [blame] | 756 | newOp.parent(this); |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 757 | } |
| 758 | // Insert a group of a different operation |
| 759 | // (i.e. "and" in "or"/"or" in "and") |
Nils Diewald | 8e7182e | 2015-01-08 15:02:07 +0000 | [diff] [blame] | 760 | else if (newOp.operation() != this.operation()) { |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 761 | this._operands[i] = newOp; |
Nils Diewald | 8f6b610 | 2015-01-08 18:25:33 +0000 | [diff] [blame] | 762 | newOp.parent(this); |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | // Flatten the group |
| 766 | else { |
Nils Diewald | 8e7182e | 2015-01-08 15:02:07 +0000 | [diff] [blame] | 767 | // Remove old group |
| 768 | this._operands.splice(i, 1); |
| 769 | |
| 770 | // Inject new operands |
Nils Diewald | 8f6b610 | 2015-01-08 18:25:33 +0000 | [diff] [blame] | 771 | for (var op in newOp.operands().reverse()) { |
| 772 | this._operands.splice(i, 0, newOp.getOperand(op)); |
| 773 | newOp.getOperand(0).parent(this); |
| 774 | }; |
| 775 | // Prevent destruction of operands |
| 776 | newOp._operands = []; |
| 777 | newOp.destroy(); |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 778 | }; |
| 779 | oldOp.destroy(); |
| 780 | return this; |
Nils Diewald | 5c817a4 | 2015-01-06 01:08:56 +0000 | [diff] [blame] | 781 | } |
| 782 | }; |
| 783 | return false; |
| 784 | }, |
| 785 | |
Nils Diewald | 0297ba1 | 2015-01-05 21:56:12 +0000 | [diff] [blame] | 786 | // Delete operand from group |
| 787 | delOperand : function (obj) { |
| 788 | for (var i in this._operands) { |
| 789 | if (this._operands[i] === obj) { |
Nils Diewald | 8e7182e | 2015-01-08 15:02:07 +0000 | [diff] [blame] | 790 | |
Nils Diewald | 8f6b610 | 2015-01-08 18:25:33 +0000 | [diff] [blame] | 791 | // Delete identified operand |
Nils Diewald | 0297ba1 | 2015-01-05 21:56:12 +0000 | [diff] [blame] | 792 | this._operands.splice(i,1); |
| 793 | |
Nils Diewald | 5c817a4 | 2015-01-06 01:08:56 +0000 | [diff] [blame] | 794 | // Destroy object for cyclic references |
| 795 | obj.destroy(); |
| 796 | |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 797 | return this; |
Nils Diewald | 0297ba1 | 2015-01-05 21:56:12 +0000 | [diff] [blame] | 798 | }; |
| 799 | }; |
Nils Diewald | 5c817a4 | 2015-01-06 01:08:56 +0000 | [diff] [blame] | 800 | |
| 801 | // Operand not found |
| 802 | return undefined; |
Nils Diewald | 0297ba1 | 2015-01-05 21:56:12 +0000 | [diff] [blame] | 803 | }, |
| 804 | |
Nils Diewald | 8f4e254 | 2014-12-19 04:42:09 +0000 | [diff] [blame] | 805 | // Deserialize from json |
| 806 | fromJson : function (json) { |
| 807 | if (json === undefined) |
| 808 | return this; |
| 809 | |
| 810 | if (json["@type"] !== "korap:docGroup") { |
| 811 | KorAP.log(701, "JSON-LD group has no @type attribute"); |
| 812 | return; |
| 813 | }; |
| 814 | |
| 815 | if (json["operation"] === undefined || |
| 816 | typeof json["operation"] !== 'string') { |
| 817 | KorAP.log(811, "Document group expects operation"); |
| 818 | return; |
| 819 | }; |
| 820 | |
| 821 | var operation = json["operation"]; |
| 822 | |
| 823 | this.operation(operation.replace(/^operation:/,'')); |
| 824 | |
| 825 | if (json["operands"] === undefined || |
| 826 | !(json["operands"] instanceof Array)) { |
| 827 | KorAP.log(704, "Operation needs operand list") |
| 828 | return; |
| 829 | }; |
| 830 | |
| 831 | // Add all documents |
| 832 | for (var i in json["operands"]) { |
| 833 | var operand = json["operands"][i]; |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 834 | this.append(operand); |
Nils Diewald | 8f4e254 | 2014-12-19 04:42:09 +0000 | [diff] [blame] | 835 | }; |
| 836 | |
| 837 | return this; |
| 838 | }, |
| 839 | toJson : function () { |
| 840 | var opArray = new Array(); |
| 841 | for (var i in this._operands) { |
| 842 | opArray.push(this._operands[i].toJson()); |
| 843 | }; |
| 844 | return { |
| 845 | "@type" : "korap:" + this.ldType(), |
| 846 | "operation" : "operation:" + this.operation(), |
| 847 | "operands" : opArray |
| 848 | }; |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 849 | }, |
| 850 | toString : function () { |
| 851 | return this._operands. |
| 852 | map(function (op) { |
| 853 | return op.ldType() === 'docGroup' ? '(' + op.toString() + ')' : op.toString() |
| 854 | }). |
| 855 | join(this.operation() === 'or' ? ' | ' : ' & ') |
Nils Diewald | 8f4e254 | 2014-12-19 04:42:09 +0000 | [diff] [blame] | 856 | } |
| 857 | }; |
| 858 | |
| 859 | |
| 860 | // Abstract JsonLD object |
| 861 | KorAP.JsonLD = { |
Nils Diewald | 8f6b610 | 2015-01-08 18:25:33 +0000 | [diff] [blame] | 862 | _changed : false, |
| 863 | |
Nils Diewald | 8f4e254 | 2014-12-19 04:42:09 +0000 | [diff] [blame] | 864 | create : function () { |
| 865 | return Object.create(KorAP.JsonLD); |
| 866 | }, |
| 867 | |
| 868 | /** |
| 869 | * Upgrade this object to another object |
| 870 | * while private data stays intact |
| 871 | */ |
| 872 | upgradeTo : function (props) { |
| 873 | for (var prop in props) { |
| 874 | this[prop] = props[prop]; |
| 875 | }; |
| 876 | return this; |
| 877 | }, |
Nils Diewald | 8f6b610 | 2015-01-08 18:25:33 +0000 | [diff] [blame] | 878 | |
Nils Diewald | 8f4e254 | 2014-12-19 04:42:09 +0000 | [diff] [blame] | 879 | ldType : function (type) { |
| 880 | if (arguments.length === 1) |
| 881 | this._ldType = type; |
| 882 | return this._ldType; |
| 883 | }, |
Nils Diewald | 8f6b610 | 2015-01-08 18:25:33 +0000 | [diff] [blame] | 884 | |
Nils Diewald | 8f4e254 | 2014-12-19 04:42:09 +0000 | [diff] [blame] | 885 | parent : function (obj) { |
Nils Diewald | 8f6b610 | 2015-01-08 18:25:33 +0000 | [diff] [blame] | 886 | if (arguments.length === 1) { |
Nils Diewald | 8f4e254 | 2014-12-19 04:42:09 +0000 | [diff] [blame] | 887 | this._parent = obj; |
Nils Diewald | 8f6b610 | 2015-01-08 18:25:33 +0000 | [diff] [blame] | 888 | this._changed = true; |
| 889 | }; |
Nils Diewald | 8f4e254 | 2014-12-19 04:42:09 +0000 | [diff] [blame] | 890 | return this._parent; |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 891 | }, |
Nils Diewald | 0297ba1 | 2015-01-05 21:56:12 +0000 | [diff] [blame] | 892 | |
Nils Diewald | 5c817a4 | 2015-01-06 01:08:56 +0000 | [diff] [blame] | 893 | // Destroy object - especially for |
| 894 | // acyclic structures! |
| 895 | // I'm a paranoid chicken! |
| 896 | destroy : function () { |
| 897 | if (this._ops != undefined) { |
| 898 | this._ops._parent = undefined; |
| 899 | if (this._ops._element !== undefined) |
| 900 | this._ops._element.refTo = undefined; |
| 901 | this._ops = undefined; |
| 902 | }; |
| 903 | if (this._element !== undefined) |
| 904 | this._element = undefined; |
| 905 | |
| 906 | // In case of a group, destroy all operands |
| 907 | if (this._operands !== undefined) { |
| 908 | for (var i in this._operands) |
| 909 | this.getOperand(i).destroy(); |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 910 | this._operands = []; |
Nils Diewald | 5c817a4 | 2015-01-06 01:08:56 +0000 | [diff] [blame] | 911 | }; |
| 912 | }, |
| 913 | |
Nils Diewald | 0297ba1 | 2015-01-05 21:56:12 +0000 | [diff] [blame] | 914 | // Be aware! This may be cyclic |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 915 | operators : function (and, or, del) { |
| 916 | if (arguments === 0) |
| 917 | return this._ops; |
| 918 | this._ops = KorAP.Operators.create( |
| 919 | and, or, del |
| 920 | ); |
Nils Diewald | 0297ba1 | 2015-01-05 21:56:12 +0000 | [diff] [blame] | 921 | this._ops.parent(this); |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 922 | return this._ops; |
| 923 | }, |
Nils Diewald | 8f6b610 | 2015-01-08 18:25:33 +0000 | [diff] [blame] | 924 | |
Nils Diewald | 966abf1 | 2014-12-20 02:27:45 +0000 | [diff] [blame] | 925 | toJson : function () { |
| 926 | return { |
| 927 | // Unspecified object |
| 928 | "@type" : "korap:" + this.ldType() |
| 929 | }; |
Nils Diewald | 8f6b610 | 2015-01-08 18:25:33 +0000 | [diff] [blame] | 930 | }, |
| 931 | toString : function () { |
| 932 | return loc.EMPTY; |
Nils Diewald | 8f4e254 | 2014-12-19 04:42:09 +0000 | [diff] [blame] | 933 | } |
| 934 | }; |
| 935 | |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 936 | }(this.KorAP)); |