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'; |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 84 | |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame^] | 85 | KorAP._vcKeyMenu = undefined; |
| 86 | KorAP._vcDatePicker = dpClass.create(); |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 87 | |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame^] | 88 | // Create match menus .... |
| 89 | KorAP._vcMatchopMenu = { |
| 90 | 'string' : menuClass.create([ |
| 91 | [ 'eq', null ], |
| 92 | [ 'ne', null ] |
| 93 | ]), |
| 94 | 'text' : menuClass.create([ |
| 95 | [ 'eq', null ], // Requires exact match |
| 96 | [ 'ne', null ], |
| 97 | [ 'contains', null ], // Requires token sequence match |
| 98 | [ 'containsnot', null ] |
| 99 | ]), |
| 100 | 'date' : menuClass.create([ |
| 101 | [ 'eq', null ], |
| 102 | [ 'ne', null ], |
| 103 | [ 'geq', null ], |
| 104 | [ 'leq', null ] |
| 105 | ]), |
| 106 | 'regex' : menuClass.create([ |
| 107 | [ 'eq', null ], |
| 108 | [ 'ne', null ] |
| 109 | ]) |
| 110 | }; |
| 111 | |
| 112 | /** |
| 113 | * Virtual Collection |
| 114 | */ |
| 115 | return { |
| 116 | |
| 117 | /** |
| 118 | * The JSON-LD type of the virtual collection |
| 119 | */ |
| 120 | ldType : function() { |
| 121 | return null; |
| 122 | }, |
| 123 | |
| 124 | // Initialize virtual collection |
| 125 | _init : function(keyList) { |
| 126 | |
| 127 | // Inject localized css styles |
| 128 | if (!KorAP._overrideStyles) { |
| 129 | var sheet = KorAP.newStyleSheet(); |
| 130 | |
| 131 | // Add css rule for OR operations |
| 132 | sheet.insertRule('.vc .docGroup[data-operation=or] > .doc::before,' |
| 133 | + '.vc .docGroup[data-operation=or] > .docGroup::before ' |
| 134 | + '{ content: "' + loc.OR + '" }', 0); |
| 135 | |
| 136 | // Add css rule for AND operations |
| 137 | sheet.insertRule( |
| 138 | '.vc .docGroup[data-operation=and] > .doc::before,' |
| 139 | + '.vc .docGroup[data-operation=and] > .docGroup::before ' |
| 140 | + '{ content: "' + loc.AND + '" }', 1); |
| 141 | |
| 142 | KorAP._overrideStyles = true; |
Nils Diewald | 359a72c | 2015-04-20 17:40:29 +0000 | [diff] [blame] | 143 | }; |
| 144 | |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame^] | 145 | // Create key menu |
| 146 | KorAP._vcKeyMenu = menuClass.create(keyList); |
| 147 | KorAP._vcKeyMenu.limit(6); |
Akron | 712733a | 2018-04-05 18:17:47 +0200 | [diff] [blame] | 148 | |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame^] | 149 | return this; |
| 150 | }, |
Nils Diewald | 359a72c | 2015-04-20 17:40:29 +0000 | [diff] [blame] | 151 | |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame^] | 152 | /** |
| 153 | * Create a new virtual collection. |
| 154 | */ |
| 155 | create : function(keyList) { |
| 156 | var obj = Object.create(this)._init(keyList); |
| 157 | obj._root = unspecDocClass.create(obj); |
| 158 | return obj; |
| 159 | }, |
Nils Diewald | d599d54 | 2015-01-08 20:41:34 +0000 | [diff] [blame] | 160 | |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame^] | 161 | /** |
| 162 | * Create and render a new virtual collection based on a KoralQuery |
| 163 | * collection document |
| 164 | */ |
| 165 | fromJson : function(json) { |
| 166 | if (json !== undefined) { |
| 167 | // Parse root document |
| 168 | if (json['@type'] == 'koral:doc') { |
| 169 | this._root = docClass.create(this, json); |
hebasta | a79d69d | 2018-07-24 12:13:02 +0200 | [diff] [blame] | 170 | } |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame^] | 171 | // parse root group |
| 172 | else if (json['@type'] == 'koral:docGroup') { |
| 173 | this._root = docGroupClass.create(this, json); |
| 174 | } |
| 175 | |
| 176 | // parse root reference |
| 177 | else if (json['@type'] == 'koral:docGroupRef') { |
| 178 | this._root = docGroupRefClass.create(this, json); |
| 179 | } |
| 180 | |
| 181 | // Unknown collection type |
| 182 | else { |
| 183 | KorAP.log(813, "Collection type is not supported"); |
| 184 | return; |
| 185 | }; |
| 186 | } |
| 187 | |
| 188 | else { |
| 189 | // Add unspecified object |
| 190 | this._root = unspecDocClass.create(this); |
Nils Diewald | 845282c | 2015-05-14 07:53:03 +0000 | [diff] [blame] | 191 | }; |
Akron | b19803c | 2018-08-16 16:39:42 +0200 | [diff] [blame^] | 192 | |
| 193 | // Init element and update |
| 194 | this.update(); |
| 195 | |
| 196 | return this; |
| 197 | }, |
| 198 | |
| 199 | // Check if the virtual corpus contains a rewrite |
| 200 | // This is a class method |
| 201 | checkRewrite : function(json) { |
| 202 | |
| 203 | // There is a rewrite attribute |
| 204 | if (json['rewrites'] !== undefined) { |
| 205 | return true; |
| 206 | } |
| 207 | |
| 208 | // There is a group to check for rewrites |
| 209 | else if (json['@type'] === 'koral:docGroup') { |
| 210 | var ops = json['operands']; |
| 211 | if (ops === undefined) |
| 212 | return false; |
| 213 | |
| 214 | for ( var i in ops) { |
| 215 | |
| 216 | // "this" is the class |
| 217 | if (this.checkRewrite(ops[i])) { |
| 218 | return true; |
| 219 | }; |
| 220 | }; |
| 221 | }; |
| 222 | return false; |
| 223 | }, |
| 224 | |
| 225 | /** |
| 226 | * Clean the virtual document to uspecified doc. |
| 227 | */ |
| 228 | clean : function() { |
| 229 | if (this._root.ldType() !== "non") { |
| 230 | this._root.destroy(); |
| 231 | this.root(unspecDocClass.create(this)); |
| 232 | }; |
| 233 | return this; |
| 234 | }, |
| 235 | |
| 236 | /** |
| 237 | * Get or set the root object of the virtual collection. |
| 238 | */ |
| 239 | root : function(obj) { |
| 240 | if (arguments.length === 1) { |
| 241 | var e = this.element(); |
| 242 | |
| 243 | if (e.firstChild !== null) { |
| 244 | if (e.firstChild !== obj.element()) { |
| 245 | e.replaceChild(obj.element(), e.firstChild); |
| 246 | }; |
| 247 | } |
| 248 | |
| 249 | // Append root element |
| 250 | else { |
| 251 | e.appendChild(obj.element()); |
| 252 | }; |
| 253 | |
| 254 | // Update parent child relations |
| 255 | this._root = obj; |
| 256 | obj.parent(this); |
| 257 | |
| 258 | this.update(); |
| 259 | }; |
| 260 | return this._root; |
| 261 | }, |
| 262 | |
| 263 | /** |
| 264 | * Get the element associated with the virtual collection |
| 265 | */ |
| 266 | element : function() { |
| 267 | |
| 268 | |
| 269 | if (this._element !== undefined) { |
| 270 | return this._element; |
| 271 | }; |
| 272 | |
| 273 | this._element = document.createElement('div'); |
| 274 | this._element.setAttribute('class', 'vc'); |
| 275 | |
| 276 | // Initialize root |
| 277 | this._element.appendChild(this._root.element()); |
| 278 | |
| 279 | /* |
| 280 | * TODO by Helge Hack! additional div, because statistic button is |
| 281 | * removed after choosing and/or/x in vc builder. REMOVE this lines |
| 282 | * after solving the problem!!!! |
| 283 | */ |
| 284 | this._element.addE('div'); |
| 285 | this._element.addE('div'); |
| 286 | this._element.addE('div'); |
| 287 | |
| 288 | // Add panel to display corpus statistic, ... |
| 289 | this.addVcInfPanel(); |
| 290 | |
| 291 | return this._element; |
| 292 | }, |
| 293 | |
| 294 | /** |
| 295 | * Update the whole object based on the underlying data structure |
| 296 | */ |
| 297 | update : function() { |
| 298 | this._root.update(); |
| 299 | return this; |
| 300 | }, |
| 301 | |
| 302 | /** |
| 303 | * Make the vc persistant by injecting the current timestamp as a |
| 304 | * creation date limit criterion. |
| 305 | * THIS IS CURRENTLY NOT USED |
| 306 | */ |
| 307 | makePersistant : function() { |
| 308 | // this.root().wrapOnRoot('and'); |
| 309 | var todayStr = KorAP._vcDatePicker.today(); |
| 310 | var doc = docClass.create(); |
| 311 | var root = this.root(); |
| 312 | |
| 313 | if (root.ldType() === 'docGroup' && root.operation === 'and') { |
| 314 | root.append(cond); |
| 315 | } else { |
| 316 | root.wrapOnRoot('and'); |
| 317 | root.append(doc); |
| 318 | }; |
| 319 | |
| 320 | doc.key("creationDate"); |
| 321 | doc.type("date"); |
| 322 | doc.matchop("leq"); |
| 323 | doc.value(todayStr); |
| 324 | |
| 325 | /* |
| 326 | * { "@type" : "koral:doc", "key" : "creationDate", "type" : |
| 327 | * "type:date", "match" : "match:leq", "value" : todayStr } |
| 328 | * this.root().append(cond); |
| 329 | */ |
| 330 | this.update(); |
| 331 | }, |
| 332 | |
| 333 | /** |
| 334 | * Get the generated json string |
| 335 | */ |
| 336 | toJson : function() { |
| 337 | return this._root.toJson(); |
| 338 | }, |
| 339 | |
| 340 | /** |
| 341 | * Get the generated query string |
| 342 | */ |
| 343 | toQuery : function() { |
| 344 | return this._root.toQuery(); |
| 345 | }, |
| 346 | |
| 347 | |
| 348 | /* |
| 349 | * Add panel to display virtual corpus information |
| 350 | */ |
| 351 | addVcInfPanel : function() { |
| 352 | |
| 353 | var dv = this._element.addE('div'); |
| 354 | var panel = panelClass.create([ 'vcinfo' ]); |
| 355 | dv.appendChild(panel.element()); |
| 356 | |
| 357 | var that = this; |
| 358 | var actions = panel.actions; |
| 359 | var statView; |
| 360 | |
| 361 | actions.add(loc.SHOW_STAT, [ 'statistic' ], function() { |
| 362 | if (statView === undefined || !statView.shown()) { |
| 363 | statView = corpStatVClass.create(that); |
| 364 | panel.add(statView); |
| 365 | } |
| 366 | }); |
| 367 | } |
| 368 | }; |
| 369 | }); |