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