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