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