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