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