Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 1 | /** |
hebasta | a79d69d | 2018-07-24 12:13:02 +0200 | [diff] [blame] | 2 | * Create virtual collections with a visual user interface. This resembles the |
| 3 | * collection type objects of a KoralQuery "collection" object. |
| 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 | */ |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 46 | |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 47 | define([ |
| 48 | 'vc/unspecified', |
| 49 | 'vc/doc', |
| 50 | 'vc/docgroup', |
| 51 | 'vc/docgroupref', |
| 52 | 'vc/menu', |
| 53 | 'vc/statistic', |
| 54 | 'datepicker', |
| 55 | 'buttongroup', |
| 56 | 'panel', |
| 57 | 'view/corpstatv', |
| 58 | 'util' |
| 59 | ], function( |
| 60 | unspecDocClass, |
| 61 | docClass, |
| 62 | docGroupClass, |
| 63 | docGroupRefClass, |
| 64 | menuClass, |
| 65 | statClass, |
| 66 | dpClass, |
| 67 | buttonGrClass, |
| 68 | panelClass, |
| 69 | corpStatVClass) { |
| 70 | "use strict"; |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 71 | |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 72 | KorAP._validUnspecMatchRE = new RegExp( |
| 73 | "^(?:eq|ne|contains(?:not)?|excludes)$"); |
| 74 | KorAP._validStringMatchRE = new RegExp("^(?:eq|ne)$"); |
| 75 | KorAP._validTextMatchRE = KorAP._validUnspecMatchRE; |
| 76 | KorAP._validTextOnlyMatchRE = new RegExp( |
| 77 | "^(?:contains(?:not)?|excludes)$"); |
| 78 | KorAP._overrideStyles = false; |
| 79 | // KorAP._validDateMatchRE is defined in datepicker.js! |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 80 | |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 81 | const loc = KorAP.Locale; |
| 82 | loc.SHOW_STAT = loc.SHOW_STAT || 'Statistics'; |
| 83 | loc.VERB_SHOWSTAT = loc.VERB_SHOWSTAT || 'Corpus Statistics'; |
Akron | 8a67016 | 2018-08-28 10:09:13 +0200 | [diff] [blame] | 84 | loc.VC_allCorpora = loc.VC_allCorpora || 'all corpora'; |
| 85 | loc.VC_oneCollection = loc.VC_oneCollection || 'a virtual corpus'; |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 86 | |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 87 | KorAP._vcKeyMenu = undefined; |
| 88 | KorAP._vcDatePicker = dpClass.create(); |
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 | // Create match menus .... |
| 91 | KorAP._vcMatchopMenu = { |
| 92 | 'string' : menuClass.create([ |
| 93 | [ 'eq', null ], |
| 94 | [ 'ne', null ] |
| 95 | ]), |
| 96 | 'text' : menuClass.create([ |
| 97 | [ 'eq', null ], // Requires exact match |
| 98 | [ 'ne', null ], |
| 99 | [ 'contains', null ], // Requires token sequence match |
| 100 | [ 'containsnot', null ] |
| 101 | ]), |
| 102 | 'date' : menuClass.create([ |
| 103 | [ 'eq', null ], |
| 104 | [ 'ne', null ], |
| 105 | [ 'geq', null ], |
| 106 | [ 'leq', null ] |
| 107 | ]), |
| 108 | 'regex' : menuClass.create([ |
| 109 | [ 'eq', null ], |
| 110 | [ 'ne', null ] |
| 111 | ]) |
| 112 | }; |
| 113 | |
| 114 | /** |
| 115 | * Virtual Collection |
| 116 | */ |
| 117 | return { |
| 118 | |
| 119 | /** |
| 120 | * The JSON-LD type of the virtual collection |
| 121 | */ |
| 122 | ldType : function() { |
| 123 | return null; |
| 124 | }, |
| 125 | |
| 126 | // Initialize virtual collection |
| 127 | _init : function(keyList) { |
| 128 | |
| 129 | // Inject localized css styles |
| 130 | if (!KorAP._overrideStyles) { |
| 131 | var sheet = KorAP.newStyleSheet(); |
| 132 | |
| 133 | // Add css rule for OR operations |
| 134 | sheet.insertRule('.vc .docGroup[data-operation=or] > .doc::before,' |
| 135 | + '.vc .docGroup[data-operation=or] > .docGroup::before ' |
| 136 | + '{ content: "' + loc.OR + '" }', 0); |
| 137 | |
| 138 | // Add css rule for AND operations |
| 139 | sheet.insertRule( |
| 140 | '.vc .docGroup[data-operation=and] > .doc::before,' |
| 141 | + '.vc .docGroup[data-operation=and] > .docGroup::before ' |
| 142 | + '{ content: "' + loc.AND + '" }', 1); |
| 143 | |
| 144 | KorAP._overrideStyles = true; |
Nils Diewald | 359a72c | 2015-04-20 17:40:29 +0000 | [diff] [blame] | 145 | }; |
| 146 | |
Akron | 3ad4694 | 2018-08-22 16:47:14 +0200 | [diff] [blame] | 147 | var l; |
| 148 | if (keyList) { |
| 149 | l = keyList.slice(); |
| 150 | l.unshift(['referTo', 'ref']); |
| 151 | } |
| 152 | else { |
| 153 | l = [['referTo', 'ref']]; |
| 154 | } |
| 155 | |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 156 | // Create key menu |
Akron | 3ad4694 | 2018-08-22 16:47:14 +0200 | [diff] [blame] | 157 | KorAP._vcKeyMenu = menuClass.create(l); |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 158 | KorAP._vcKeyMenu.limit(6); |
Akron | 712733a | 2018-04-05 18:17:47 +0200 | [diff] [blame] | 159 | |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 160 | return this; |
| 161 | }, |
Nils Diewald | 359a72c | 2015-04-20 17:40:29 +0000 | [diff] [blame] | 162 | |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 163 | /** |
| 164 | * Create a new virtual collection. |
| 165 | */ |
| 166 | create : function(keyList) { |
| 167 | var obj = Object.create(this)._init(keyList); |
| 168 | obj._root = unspecDocClass.create(obj); |
| 169 | return obj; |
| 170 | }, |
Nils Diewald | d599d54 | 2015-01-08 20:41:34 +0000 | [diff] [blame] | 171 | |
Akron | 8a67016 | 2018-08-28 10:09:13 +0200 | [diff] [blame] | 172 | |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 173 | /** |
| 174 | * Create and render a new virtual collection based on a KoralQuery |
| 175 | * collection document |
| 176 | */ |
| 177 | fromJson : function(json) { |
| 178 | if (json !== undefined) { |
| 179 | // Parse root document |
| 180 | if (json['@type'] == 'koral:doc') { |
| 181 | this._root = docClass.create(this, json); |
hebasta | a79d69d | 2018-07-24 12:13:02 +0200 | [diff] [blame] | 182 | } |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 183 | // parse root group |
| 184 | else if (json['@type'] == 'koral:docGroup') { |
| 185 | this._root = docGroupClass.create(this, json); |
| 186 | } |
| 187 | |
| 188 | // parse root reference |
| 189 | else if (json['@type'] == 'koral:docGroupRef') { |
| 190 | this._root = docGroupRefClass.create(this, json); |
| 191 | } |
| 192 | |
| 193 | // Unknown collection type |
| 194 | else { |
| 195 | KorAP.log(813, "Collection type is not supported"); |
| 196 | return; |
| 197 | }; |
| 198 | } |
| 199 | |
| 200 | else { |
| 201 | // Add unspecified object |
| 202 | this._root = unspecDocClass.create(this); |
Nils Diewald | 845282c | 2015-05-14 07:53:03 +0000 | [diff] [blame] | 203 | }; |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 204 | |
| 205 | // Init element and update |
| 206 | this.update(); |
| 207 | |
| 208 | return this; |
| 209 | }, |
Akron | d2474aa | 2018-08-28 12:06:27 +0200 | [diff] [blame] | 210 | |
| 211 | |
| 212 | // Check if the virtual corpus contains a rerite |
| 213 | wasRewritten : function (obj) { |
| 214 | |
| 215 | var obj; |
| 216 | if (arguments.length === 1) { |
| 217 | obj = arguments[0]; |
| 218 | } |
| 219 | else { |
| 220 | obj = this._root; |
| 221 | }; |
| 222 | |
| 223 | // Check for rewrite |
| 224 | if (obj.rewrites() && obj.rewrites().length() > 0) { |
| 225 | return true; |
| 226 | } |
| 227 | |
| 228 | // Check recursively |
| 229 | else if (obj.ldType() === 'docGroup') { |
| 230 | for (var i in obj.operands()) { |
| 231 | if (this.wasRewritten(obj.getOperand(i))) { |
| 232 | return true; |
| 233 | } |
| 234 | }; |
| 235 | }; |
| 236 | |
| 237 | return false; |
| 238 | }, |
Akron | 43c5cc6 | 2018-08-28 13:10:25 +0200 | [diff] [blame] | 239 | |
Akron | 8a67016 | 2018-08-28 10:09:13 +0200 | [diff] [blame] | 240 | |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 241 | /** |
| 242 | * Clean the virtual document to uspecified doc. |
| 243 | */ |
| 244 | clean : function() { |
| 245 | if (this._root.ldType() !== "non") { |
| 246 | this._root.destroy(); |
| 247 | this.root(unspecDocClass.create(this)); |
| 248 | }; |
| 249 | return this; |
| 250 | }, |
| 251 | |
| 252 | /** |
| 253 | * Get or set the root object of the virtual collection. |
| 254 | */ |
| 255 | root : function(obj) { |
| 256 | if (arguments.length === 1) { |
| 257 | var e = this.element(); |
| 258 | |
| 259 | if (e.firstChild !== null) { |
| 260 | if (e.firstChild !== obj.element()) { |
| 261 | e.replaceChild(obj.element(), e.firstChild); |
| 262 | }; |
| 263 | } |
| 264 | |
| 265 | // Append root element |
| 266 | else { |
| 267 | e.appendChild(obj.element()); |
| 268 | }; |
| 269 | |
| 270 | // Update parent child relations |
| 271 | this._root = obj; |
| 272 | obj.parent(this); |
| 273 | |
| 274 | this.update(); |
| 275 | }; |
| 276 | return this._root; |
| 277 | }, |
| 278 | |
| 279 | /** |
| 280 | * Get the element associated with the virtual collection |
| 281 | */ |
| 282 | element : function() { |
| 283 | |
| 284 | |
| 285 | if (this._element !== undefined) { |
| 286 | return this._element; |
| 287 | }; |
| 288 | |
| 289 | this._element = document.createElement('div'); |
| 290 | this._element.setAttribute('class', 'vc'); |
| 291 | |
| 292 | // Initialize root |
| 293 | this._element.appendChild(this._root.element()); |
| 294 | |
| 295 | /* |
| 296 | * TODO by Helge Hack! additional div, because statistic button is |
| 297 | * removed after choosing and/or/x in vc builder. REMOVE this lines |
| 298 | * after solving the problem!!!! |
| 299 | */ |
| 300 | this._element.addE('div'); |
| 301 | this._element.addE('div'); |
| 302 | this._element.addE('div'); |
| 303 | |
| 304 | // Add panel to display corpus statistic, ... |
| 305 | this.addVcInfPanel(); |
| 306 | |
| 307 | return this._element; |
| 308 | }, |
| 309 | |
| 310 | /** |
| 311 | * Update the whole object based on the underlying data structure |
| 312 | */ |
| 313 | update : function() { |
| 314 | this._root.update(); |
| 315 | return this; |
| 316 | }, |
| 317 | |
| 318 | /** |
| 319 | * Make the vc persistant by injecting the current timestamp as a |
| 320 | * creation date limit criterion. |
| 321 | * THIS IS CURRENTLY NOT USED |
| 322 | */ |
| 323 | makePersistant : function() { |
| 324 | // this.root().wrapOnRoot('and'); |
| 325 | var todayStr = KorAP._vcDatePicker.today(); |
| 326 | var doc = docClass.create(); |
| 327 | var root = this.root(); |
| 328 | |
| 329 | if (root.ldType() === 'docGroup' && root.operation === 'and') { |
| 330 | root.append(cond); |
| 331 | } else { |
| 332 | root.wrapOnRoot('and'); |
| 333 | root.append(doc); |
| 334 | }; |
| 335 | |
| 336 | doc.key("creationDate"); |
| 337 | doc.type("date"); |
| 338 | doc.matchop("leq"); |
| 339 | doc.value(todayStr); |
| 340 | |
| 341 | /* |
| 342 | * { "@type" : "koral:doc", "key" : "creationDate", "type" : |
| 343 | * "type:date", "match" : "match:leq", "value" : todayStr } |
| 344 | * this.root().append(cond); |
| 345 | */ |
| 346 | this.update(); |
| 347 | }, |
| 348 | |
Akron | 8a67016 | 2018-08-28 10:09:13 +0200 | [diff] [blame] | 349 | |
| 350 | // Get the reference name |
| 351 | getName : function () { |
| 352 | if (this._root.ldType() === 'non') { |
| 353 | return loc.VC_allCorpora; |
| 354 | } |
| 355 | else if (this._root.ldType() === 'docGroupRef') { |
| 356 | return this._root.ref(); |
| 357 | } |
| 358 | else { |
| 359 | return loc.VC_oneCollection; |
| 360 | } |
| 361 | }, |
| 362 | |
| 363 | |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame] | 364 | /** |
| 365 | * Get the generated json string |
| 366 | */ |
| 367 | toJson : function() { |
| 368 | return this._root.toJson(); |
| 369 | }, |
| 370 | |
| 371 | /** |
| 372 | * Get the generated query string |
| 373 | */ |
| 374 | toQuery : function() { |
| 375 | return this._root.toQuery(); |
| 376 | }, |
| 377 | |
| 378 | |
| 379 | /* |
| 380 | * Add panel to display virtual corpus information |
| 381 | */ |
| 382 | addVcInfPanel : function() { |
| 383 | |
| 384 | var dv = this._element.addE('div'); |
| 385 | var panel = panelClass.create([ 'vcinfo' ]); |
| 386 | dv.appendChild(panel.element()); |
| 387 | |
| 388 | var that = this; |
| 389 | var actions = panel.actions; |
| 390 | var statView; |
| 391 | |
| 392 | actions.add(loc.SHOW_STAT, [ 'statistic' ], function() { |
| 393 | if (statView === undefined || !statView.shown()) { |
| 394 | statView = corpStatVClass.create(that); |
| 395 | panel.add(statView); |
| 396 | } |
| 397 | }); |
| 398 | } |
| 399 | }; |
| 400 | }); |