Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 1 | /** |
Akron | cd42a14 | 2019-07-12 18:55:37 +0200 | [diff] [blame] | 2 | * Create virtual corpora with a visual user interface. This resembles the |
| 3 | * corpus/collection type objects of a KoralQuery "collection"/"corpus" object. |
hebasta | a79d69d | 2018-07-24 12:13:02 +0200 | [diff] [blame] | 4 | * |
Nils Diewald | 4c22125 | 2015-04-21 20:19:25 +0000 | [diff] [blame] | 5 | * KoralQuery v0.3 is expected. |
hebasta | a79d69d | 2018-07-24 12:13:02 +0200 | [diff] [blame] | 6 | * |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 7 | * @author Nils Diewald |
| 8 | */ |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 9 | /* |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 10 | * This replaces a previous version written by Mengfei Zhou |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 11 | */ |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 12 | |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 13 | /* |
hebasta | 8675939 | 2018-07-25 15:44:37 +0200 | [diff] [blame] | 14 | TODO: Disable "and" or "or" in case it's followed |
| 15 | by an unspecified document |
| 16 | TODO: Add "and"-method to root to add further constraints |
| 17 | based on match-input (like clicking on a pubDate timestamp in a match) |
| 18 | TODO: Implement "persistence"-Option, injecting the current creation |
| 19 | date stamp |
| 20 | TODO: Implement vec-Type for document-id vectors like docID in [1,2,3,4 ...] |
| 21 | |
| 22 | Error codes: |
| 23 | 701: "JSON-LD group has no @type attribute" |
| 24 | 704: "Operation needs operand list" |
| 25 | 802: "Match type is not supported by value type" |
| 26 | 804: "Unknown value type" |
| 27 | 805: "Value is invalid" |
| 28 | 806: "Value is not a valid date string" |
| 29 | 807: "Value is not a valid regular expression" |
| 30 | 810: "Unknown document group operation" (like 711) |
| 31 | 811: "Document group expects operation" (like 703) |
| 32 | 812: "Operand not supported in document group" (like 744) |
| 33 | 813: "Collection type is not supported" (like 713) |
| 34 | 814: "Unknown rewrite operation" |
| 35 | 815: "Rewrite expects source" |
| 36 | |
| 37 | Localization strings: |
| 38 | KorAP.Locale = { |
| 39 | EMPTY : '...', |
| 40 | AND : 'and', |
| 41 | OR : 'or', |
| 42 | DELETE : 'x' } |
| 43 | |
| 44 | and various field names with the prefix 'VC_' |
hebasta | a79d69d | 2018-07-24 12:13:02 +0200 | [diff] [blame] | 45 | */ |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 46 | "use strict"; |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 47 | |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 48 | define([ |
| 49 | 'vc/unspecified', |
| 50 | 'vc/doc', |
| 51 | 'vc/docgroup', |
| 52 | 'vc/docgroupref', |
| 53 | 'vc/menu', |
| 54 | 'vc/statistic', |
| 55 | 'datepicker', |
| 56 | 'buttongroup', |
Akron | aa61322 | 2019-11-19 13:57:12 +0100 | [diff] [blame] | 57 | 'panel/vc', |
| 58 | 'view/vc/corpstatv', |
Akron | ec6bb8e | 2018-08-29 13:07:56 +0200 | [diff] [blame] | 59 | 'buttongroup', |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 60 | 'util' |
| 61 | ], function( |
| 62 | unspecDocClass, |
| 63 | docClass, |
| 64 | docGroupClass, |
| 65 | docGroupRefClass, |
| 66 | menuClass, |
| 67 | statClass, |
| 68 | dpClass, |
| 69 | buttonGrClass, |
hebasta | 2535c76 | 2018-11-21 16:27:33 +0100 | [diff] [blame] | 70 | vcPanelClass, |
Akron | ec6bb8e | 2018-08-29 13:07:56 +0200 | [diff] [blame] | 71 | corpStatVClass, |
| 72 | buttonGroupClass) { |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 73 | |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 74 | KorAP._validUnspecMatchRE = new RegExp( |
| 75 | "^(?:eq|ne|contains(?:not)?|excludes)$"); |
| 76 | KorAP._validStringMatchRE = new RegExp("^(?:eq|ne)$"); |
| 77 | KorAP._validTextMatchRE = KorAP._validUnspecMatchRE; |
| 78 | KorAP._validTextOnlyMatchRE = new RegExp( |
| 79 | "^(?:contains(?:not)?|excludes)$"); |
| 80 | KorAP._overrideStyles = false; |
| 81 | // KorAP._validDateMatchRE is defined in datepicker.js! |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 82 | |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 83 | const loc = KorAP.Locale; |
hebasta | a0282be | 2018-12-05 16:58:00 +0100 | [diff] [blame] | 84 | loc.SHOW_STAT = loc.SHOW_STAT || 'Statistics'; |
| 85 | loc.VERB_SHOWSTAT = loc.VERB_SHOWSTAT || 'Corpus Statistics'; |
Akron | 8a67016 | 2018-08-28 10:09:13 +0200 | [diff] [blame] | 86 | loc.VC_allCorpora = loc.VC_allCorpora || 'all corpora'; |
| 87 | loc.VC_oneCollection = loc.VC_oneCollection || 'a virtual corpus'; |
Akron | ec6bb8e | 2018-08-29 13:07:56 +0200 | [diff] [blame] | 88 | loc.MINIMIZE = loc.MINIMIZE || 'Minimize'; |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 89 | |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 90 | KorAP._vcKeyMenu = undefined; |
| 91 | KorAP._vcDatePicker = dpClass.create(); |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 92 | |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 93 | // Create match menus .... |
| 94 | KorAP._vcMatchopMenu = { |
| 95 | 'string' : menuClass.create([ |
| 96 | [ 'eq', null ], |
| 97 | [ 'ne', null ] |
| 98 | ]), |
| 99 | 'text' : menuClass.create([ |
| 100 | [ 'eq', null ], // Requires exact match |
| 101 | [ 'ne', null ], |
| 102 | [ 'contains', null ], // Requires token sequence match |
| 103 | [ 'containsnot', null ] |
| 104 | ]), |
| 105 | 'date' : menuClass.create([ |
| 106 | [ 'eq', null ], |
| 107 | [ 'ne', null ], |
| 108 | [ 'geq', null ], |
| 109 | [ 'leq', null ] |
| 110 | ]), |
| 111 | 'regex' : menuClass.create([ |
| 112 | [ 'eq', null ], |
| 113 | [ 'ne', null ] |
| 114 | ]) |
| 115 | }; |
| 116 | |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 117 | |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 118 | /** |
Akron | cd42a14 | 2019-07-12 18:55:37 +0200 | [diff] [blame] | 119 | * Virtual corpus |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 120 | */ |
| 121 | return { |
| 122 | |
| 123 | /** |
Akron | cd42a14 | 2019-07-12 18:55:37 +0200 | [diff] [blame] | 124 | * The JSON-LD type of the virtual corpus |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 125 | */ |
| 126 | ldType : function() { |
| 127 | return null; |
| 128 | }, |
| 129 | |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 130 | |
Akron | cd42a14 | 2019-07-12 18:55:37 +0200 | [diff] [blame] | 131 | // Initialize virtual corpus |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 132 | _init : function(keyList) { |
| 133 | |
| 134 | // Inject localized css styles |
| 135 | if (!KorAP._overrideStyles) { |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 136 | |
| 137 | const sheet = KorAP.newStyleSheet(); |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 138 | |
| 139 | // Add css rule for OR operations |
| 140 | sheet.insertRule('.vc .docGroup[data-operation=or] > .doc::before,' |
| 141 | + '.vc .docGroup[data-operation=or] > .docGroup::before ' |
| 142 | + '{ content: "' + loc.OR + '" }', 0); |
| 143 | |
| 144 | // Add css rule for AND operations |
| 145 | sheet.insertRule( |
| 146 | '.vc .docGroup[data-operation=and] > .doc::before,' |
| 147 | + '.vc .docGroup[data-operation=and] > .docGroup::before ' |
| 148 | + '{ content: "' + loc.AND + '" }', 1); |
| 149 | |
| 150 | KorAP._overrideStyles = true; |
Nils Diewald | 359a72c | 2015-04-20 17:40:29 +0000 | [diff] [blame] | 151 | }; |
| 152 | |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 153 | let l; |
Akron | 3ad4694 | 2018-08-22 16:47:14 +0200 | [diff] [blame] | 154 | if (keyList) { |
Akron | adab5e5 | 2018-08-20 13:50:53 +0200 | [diff] [blame] | 155 | l = keyList.slice(); |
Akron | 3ad4694 | 2018-08-22 16:47:14 +0200 | [diff] [blame] | 156 | l.unshift(['referTo', 'ref']); |
| 157 | } |
| 158 | else { |
| 159 | l = [['referTo', 'ref']]; |
| 160 | } |
Akron | adab5e5 | 2018-08-20 13:50:53 +0200 | [diff] [blame] | 161 | |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 162 | // Create key menu |
Akron | 3ad4694 | 2018-08-22 16:47:14 +0200 | [diff] [blame] | 163 | KorAP._vcKeyMenu = menuClass.create(l); |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 164 | KorAP._vcKeyMenu.limit(6); |
Akron | 712733a | 2018-04-05 18:17:47 +0200 | [diff] [blame] | 165 | |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 166 | return this; |
| 167 | }, |
Nils Diewald | 359a72c | 2015-04-20 17:40:29 +0000 | [diff] [blame] | 168 | |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 169 | |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 170 | /** |
Akron | cd42a14 | 2019-07-12 18:55:37 +0200 | [diff] [blame] | 171 | * Create a new virtual corpus |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 172 | */ |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 173 | create : function (keyList) { |
| 174 | const obj = Object.create(this)._init(keyList); |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 175 | obj._root = unspecDocClass.create(obj); |
| 176 | return obj; |
| 177 | }, |
Nils Diewald | d599d54 | 2015-01-08 20:41:34 +0000 | [diff] [blame] | 178 | |
Akron | 8a67016 | 2018-08-28 10:09:13 +0200 | [diff] [blame] | 179 | |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 180 | /** |
Akron | cd42a14 | 2019-07-12 18:55:37 +0200 | [diff] [blame] | 181 | * Create and render a new virtual corpus based on a KoralQuery |
| 182 | * corpus document |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 183 | */ |
| 184 | fromJson : function(json) { |
Akron | 13af2f4 | 2019-07-25 15:06:21 +0200 | [diff] [blame] | 185 | |
| 186 | let obj; |
| 187 | |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 188 | if (json !== undefined) { |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 189 | |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 190 | // Parse root document |
| 191 | if (json['@type'] == 'koral:doc') { |
Akron | 13af2f4 | 2019-07-25 15:06:21 +0200 | [diff] [blame] | 192 | obj = docClass.create(this, json); |
hebasta | a79d69d | 2018-07-24 12:13:02 +0200 | [diff] [blame] | 193 | } |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 194 | |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 195 | // parse root group |
| 196 | else if (json['@type'] == 'koral:docGroup') { |
Akron | 13af2f4 | 2019-07-25 15:06:21 +0200 | [diff] [blame] | 197 | obj = docGroupClass.create(this, json); |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | // parse root reference |
| 201 | else if (json['@type'] == 'koral:docGroupRef') { |
Akron | 13af2f4 | 2019-07-25 15:06:21 +0200 | [diff] [blame] | 202 | obj = docGroupRefClass.create(this, json); |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | // Unknown collection type |
| 206 | else { |
| 207 | KorAP.log(813, "Collection type is not supported"); |
| 208 | return; |
| 209 | }; |
| 210 | } |
| 211 | |
| 212 | else { |
| 213 | // Add unspecified object |
Akron | 13af2f4 | 2019-07-25 15:06:21 +0200 | [diff] [blame] | 214 | obj = unspecDocClass.create(this); |
Nils Diewald | 845282c | 2015-05-14 07:53:03 +0000 | [diff] [blame] | 215 | }; |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 216 | |
| 217 | // Init element and update |
Akron | 13af2f4 | 2019-07-25 15:06:21 +0200 | [diff] [blame] | 218 | this.root(obj); |
| 219 | |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 220 | return this; |
| 221 | }, |
Akron | d2474aa | 2018-08-28 12:06:27 +0200 | [diff] [blame] | 222 | |
| 223 | |
| 224 | // Check if the virtual corpus contains a rerite |
| 225 | wasRewritten : function (obj) { |
| 226 | |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 227 | if (arguments.length !== 1) { |
Akron | d2474aa | 2018-08-28 12:06:27 +0200 | [diff] [blame] | 228 | obj = this._root; |
| 229 | }; |
| 230 | |
| 231 | // Check for rewrite |
| 232 | if (obj.rewrites() && obj.rewrites().length() > 0) { |
| 233 | return true; |
| 234 | } |
| 235 | |
| 236 | // Check recursively |
| 237 | else if (obj.ldType() === 'docGroup') { |
Akron | 678c26f | 2020-10-09 08:52:50 +0200 | [diff] [blame] | 238 | |
| 239 | // If there was a rewritten object |
| 240 | if (obj.operands().find(op => this.wasRewritten(op)) !== undefined) { |
| 241 | return true; |
Akron | d2474aa | 2018-08-28 12:06:27 +0200 | [diff] [blame] | 242 | }; |
| 243 | }; |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 244 | |
Akron | d2474aa | 2018-08-28 12:06:27 +0200 | [diff] [blame] | 245 | return false; |
| 246 | }, |
Akron | 43c5cc6 | 2018-08-28 13:10:25 +0200 | [diff] [blame] | 247 | |
Akron | 8a67016 | 2018-08-28 10:09:13 +0200 | [diff] [blame] | 248 | |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 249 | /** |
hebasta | 3f4be92 | 2018-12-11 10:41:46 +0100 | [diff] [blame] | 250 | * Clean the virtual document to unspecified doc. |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 251 | */ |
| 252 | clean : function() { |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 253 | const t = this; |
| 254 | if (t._root.ldType() !== "non") { |
| 255 | t._root.destroy(); |
| 256 | t.root(unspecDocClass.create(t)); |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 257 | }; |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 258 | |
| 259 | // update for graying corpus statistic by deleting the first line of the vc builder |
| 260 | t.update(); |
| 261 | return t; |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 262 | }, |
| 263 | |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 264 | |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 265 | /** |
Akron | cd42a14 | 2019-07-12 18:55:37 +0200 | [diff] [blame] | 266 | * Get or set the root object of the virtual corpus |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 267 | */ |
| 268 | root : function(obj) { |
| 269 | if (arguments.length === 1) { |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 270 | const e = this.builder(); |
| 271 | |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 272 | if (e.firstChild !== null) { |
Akron | adab5e5 | 2018-08-20 13:50:53 +0200 | [diff] [blame] | 273 | |
| 274 | // Object not yet set |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 275 | if (e.firstChild !== obj.element()) { |
| 276 | e.replaceChild(obj.element(), e.firstChild); |
| 277 | }; |
| 278 | } |
| 279 | |
| 280 | // Append root element |
| 281 | else { |
| 282 | e.appendChild(obj.element()); |
| 283 | }; |
| 284 | |
| 285 | // Update parent child relations |
| 286 | this._root = obj; |
| 287 | obj.parent(this); |
| 288 | |
| 289 | this.update(); |
| 290 | }; |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 291 | |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 292 | return this._root; |
| 293 | }, |
| 294 | |
Akron | adab5e5 | 2018-08-20 13:50:53 +0200 | [diff] [blame] | 295 | |
| 296 | /** |
| 297 | * Get the wrapper element associated with the vc |
| 298 | */ |
| 299 | builder : function () { |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 300 | const t = this; |
Akron | adab5e5 | 2018-08-20 13:50:53 +0200 | [diff] [blame] | 301 | |
| 302 | // Initialize if necessary |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 303 | if (t._builder !== undefined) |
| 304 | return t._builder; |
Akron | adab5e5 | 2018-08-20 13:50:53 +0200 | [diff] [blame] | 305 | |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 306 | t.element(); |
| 307 | return t._builder; |
Akron | adab5e5 | 2018-08-20 13:50:53 +0200 | [diff] [blame] | 308 | }, |
| 309 | |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 310 | |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 311 | /** |
Akron | 68d2832 | 2018-08-27 15:02:42 +0200 | [diff] [blame] | 312 | * Get the element associated with the virtual corpus |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 313 | */ |
| 314 | element : function() { |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 315 | const t = this; |
Akron | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame] | 316 | let e = t._el; |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 317 | |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 318 | if (e !== undefined) |
| 319 | return e; |
Akron | ec6bb8e | 2018-08-29 13:07:56 +0200 | [diff] [blame] | 320 | |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 321 | |
Akron | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame] | 322 | e = t._el = document.createElement('div'); |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 323 | e.classList.add('vc'); |
Akron | adab5e5 | 2018-08-20 13:50:53 +0200 | [diff] [blame] | 324 | |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 325 | |
| 326 | t._builder = e.addE('div'); |
| 327 | t._builder.setAttribute('class', 'builder'); |
| 328 | |
| 329 | const btn = buttonGroupClass.create( |
Akron | ec6bb8e | 2018-08-29 13:07:56 +0200 | [diff] [blame] | 330 | ['action','button-view'] |
| 331 | ); |
Akron | ec6bb8e | 2018-08-29 13:07:56 +0200 | [diff] [blame] | 332 | |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 333 | btn.add(loc.MINIMIZE, {'cls':['button-icon','minimize']}, function () { |
| 334 | this.minimize(); |
| 335 | }.bind(t)); |
| 336 | |
| 337 | e.appendChild(btn.element()); |
| 338 | |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 339 | // Initialize root |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 340 | t._builder.appendChild(t._root.element()); |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 341 | |
| 342 | // Add panel to display corpus statistic, ... |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 343 | t.addVcInfPanel(); |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 344 | |
hebasta | 4dd77bc | 2019-02-07 12:57:57 +0100 | [diff] [blame] | 345 | //Adds EventListener for corpus changes |
Akron | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame] | 346 | t._el.addEventListener('vcChange', function (e) { |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 347 | this.checkStatActive(e.detail); |
| 348 | }.bind(t), false); |
hebasta | 4dd77bc | 2019-02-07 12:57:57 +0100 | [diff] [blame] | 349 | |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 350 | return e; |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 351 | }, |
| 352 | |
Akron | ec6bb8e | 2018-08-29 13:07:56 +0200 | [diff] [blame] | 353 | |
| 354 | /** |
| 355 | * Check, if the VC is open |
| 356 | */ |
| 357 | isOpen : function () { |
Akron | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame] | 358 | if (!this._el) |
Akron | ec6bb8e | 2018-08-29 13:07:56 +0200 | [diff] [blame] | 359 | return false; |
Akron | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame] | 360 | return this._el.classList.contains('active'); |
Akron | ec6bb8e | 2018-08-29 13:07:56 +0200 | [diff] [blame] | 361 | }, |
| 362 | |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 363 | |
Akron | ec6bb8e | 2018-08-29 13:07:56 +0200 | [diff] [blame] | 364 | /** |
| 365 | * Open the VC view |
| 366 | */ |
| 367 | open : function () { |
| 368 | this.element().classList.add('active'); |
| 369 | if (this.onOpen) |
| 370 | this.onOpen(); |
| 371 | }, |
| 372 | |
| 373 | |
| 374 | /** |
| 375 | * Minimize the VC view |
| 376 | */ |
| 377 | minimize : function () { |
| 378 | this.element().classList.remove('active'); |
| 379 | if (this.onMinimize) |
| 380 | this.onMinimize(); |
| 381 | }, |
| 382 | |
| 383 | |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 384 | /** |
| 385 | * Update the whole object based on the underlying data structure |
hebasta | a0282be | 2018-12-05 16:58:00 +0100 | [diff] [blame] | 386 | */ |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 387 | update : function() { |
| 388 | this._root.update(); |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 389 | if (KorAP.vc) { |
| 390 | this.element().dispatchEvent( |
| 391 | new CustomEvent('vcChange', {'detail':this}) |
| 392 | ); |
hebasta | 4dd77bc | 2019-02-07 12:57:57 +0100 | [diff] [blame] | 393 | }; |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 394 | return this; |
| 395 | }, |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 396 | |
| 397 | |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 398 | /** |
| 399 | * Make the vc persistant by injecting the current timestamp as a |
| 400 | * creation date limit criterion. |
| 401 | * THIS IS CURRENTLY NOT USED |
| 402 | */ |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 403 | /* |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 404 | makePersistant : function() { |
| 405 | // this.root().wrapOnRoot('and'); |
| 406 | var todayStr = KorAP._vcDatePicker.today(); |
| 407 | var doc = docClass.create(); |
| 408 | var root = this.root(); |
| 409 | |
| 410 | if (root.ldType() === 'docGroup' && root.operation === 'and') { |
| 411 | root.append(cond); |
| 412 | } else { |
| 413 | root.wrapOnRoot('and'); |
| 414 | root.append(doc); |
| 415 | }; |
| 416 | |
| 417 | doc.key("creationDate"); |
| 418 | doc.type("date"); |
| 419 | doc.matchop("leq"); |
| 420 | doc.value(todayStr); |
| 421 | |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 422 | // { "@type" : "koral:doc", "key" : "creationDate", "type" : |
| 423 | // "type:date", "match" : "match:leq", "value" : todayStr } |
| 424 | // this.root().append(cond); |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 425 | this.update(); |
| 426 | }, |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 427 | */ |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 428 | |
Akron | 8a67016 | 2018-08-28 10:09:13 +0200 | [diff] [blame] | 429 | |
| 430 | // Get the reference name |
| 431 | getName : function () { |
| 432 | if (this._root.ldType() === 'non') { |
| 433 | return loc.VC_allCorpora; |
| 434 | } |
| 435 | else if (this._root.ldType() === 'docGroupRef') { |
| 436 | return this._root.ref(); |
| 437 | } |
| 438 | else { |
| 439 | return loc.VC_oneCollection; |
| 440 | } |
| 441 | }, |
| 442 | |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 443 | |
Akron | 4feec9d | 2018-11-20 17:00:50 +0100 | [diff] [blame] | 444 | // Add "and" constraint to VC |
| 445 | addRequired : function (doc) { |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 446 | const root = this.root(); |
| 447 | const ldType = root.ldType(); |
| 448 | const parent = root.parent(); |
Akron | 4feec9d | 2018-11-20 17:00:50 +0100 | [diff] [blame] | 449 | |
Akron | 4feec9d | 2018-11-20 17:00:50 +0100 | [diff] [blame] | 450 | if (ldType === 'non') { |
| 451 | parent.root(doc); |
| 452 | } |
| 453 | |
| 454 | // root is doc |
| 455 | else if ( |
| 456 | ldType === 'doc' || |
| 457 | ldType === 'docGroupRef' || |
| 458 | (ldType === 'docGroup' && |
| 459 | root.operation() === 'or' |
| 460 | )) { |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 461 | const group = require('vc/docgroup').create( |
Akron | 4feec9d | 2018-11-20 17:00:50 +0100 | [diff] [blame] | 462 | parent |
| 463 | ); |
| 464 | group.operation("and"); |
| 465 | group.append(root); |
| 466 | group.append(doc); |
| 467 | group.element(); // Init (seems to be necessary) |
| 468 | parent.root(group); |
| 469 | } |
| 470 | |
| 471 | // root is a docGroup |
| 472 | // and is already an 'and'-Group |
| 473 | else if (ldType === 'docGroup') { |
| 474 | root.append(doc); |
| 475 | } |
| 476 | |
| 477 | // Unknown |
| 478 | else { |
| 479 | console.log("Unknown root object"); |
| 480 | }; |
| 481 | |
| 482 | // Init element and update |
| 483 | this.update(); |
| 484 | }, |
Akron | 8a67016 | 2018-08-28 10:09:13 +0200 | [diff] [blame] | 485 | |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 486 | |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 487 | /** |
| 488 | * Get the generated json string |
| 489 | */ |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 490 | toJson : function () { |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 491 | return this._root.toJson(); |
| 492 | }, |
| 493 | |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 494 | |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 495 | /** |
| 496 | * Get the generated query string |
| 497 | */ |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 498 | toQuery : function () { |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 499 | return this._root.toQuery(); |
| 500 | }, |
| 501 | |
hebasta | 2535c76 | 2018-11-21 16:27:33 +0100 | [diff] [blame] | 502 | |
| 503 | /** |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 504 | * Add panel to display virtual corpus information |
| 505 | */ |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 506 | addVcInfPanel : function () { |
| 507 | // Create panel |
hebasta | 2535c76 | 2018-11-21 16:27:33 +0100 | [diff] [blame] | 508 | this.panel = vcPanelClass.create(this); |
Akron | 24aa005 | 2020-11-10 11:00:34 +0100 | [diff] [blame] | 509 | this._el.addE('div').appendChild(this.panel.element()); |
hebasta | a0282be | 2018-12-05 16:58:00 +0100 | [diff] [blame] | 510 | |
hebasta | 2535c76 | 2018-11-21 16:27:33 +0100 | [diff] [blame] | 511 | }, |
| 512 | |
hebasta | a0282be | 2018-12-05 16:58:00 +0100 | [diff] [blame] | 513 | /** |
hebasta | 48842cf | 2018-12-11 12:57:38 +0100 | [diff] [blame] | 514 | * Checks if corpus statistic has to be disabled, |
hebasta | a0282be | 2018-12-05 16:58:00 +0100 | [diff] [blame] | 515 | * and to be updated after clicking at the "reload-button" |
| 516 | */ |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 517 | checkStatActive : function (){ |
| 518 | if (this.panel !== undefined && this.panel.statView !== undefined){ |
hebasta | 48842cf | 2018-12-11 12:57:38 +0100 | [diff] [blame] | 519 | this.panel.statView.checkStatActive(); |
Akron | 88d237e | 2020-10-21 08:05:18 +0200 | [diff] [blame] | 520 | }; |
hebasta | a0282be | 2018-12-05 16:58:00 +0100 | [diff] [blame] | 521 | } |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 522 | }; |
| 523 | }); |