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