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