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