Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 1 | /** |
| 2 | * Create virtual collections with a visual user interface. |
Nils Diewald | 4c22125 | 2015-04-21 20:19:25 +0000 | [diff] [blame] | 3 | * This resembles the collection type objects of a KoralQuery |
| 4 | * "collection" object. |
| 5 | * |
| 6 | * KoralQuery v0.3 is expected. |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 7 | * |
| 8 | * @author Nils Diewald |
| 9 | */ |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 10 | /* |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 11 | * This replaces a previous version written by Mengfei Zhou |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 12 | */ |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 13 | |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 14 | /* |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 15 | TODO: Disable "and" or "or" in case it's followed |
| 16 | by an unspecified document |
Akron | 0a6768f | 2016-07-13 18:00:43 +0200 | [diff] [blame] | 17 | TODO: Add "and"-method to root to add further constraints based on match- |
| 18 | input (like clicking on a pubDate timestamp in a match) |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 19 | TODO: Implement "persistence"-Option, |
Nils Diewald | 6e43ffd | 2015-03-25 18:55:39 +0000 | [diff] [blame] | 20 | injecting the current creation date stamp |
Nils Diewald | 709f52f | 2015-05-21 18:32:58 +0000 | [diff] [blame] | 21 | TODO: Implement vec-Type for document-id vectors |
| 22 | like docID in [1,2,3,4 ...] |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 23 | |
Nils Diewald | d599d54 | 2015-01-08 20:41:34 +0000 | [diff] [blame] | 24 | Error codes: |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 25 | 701: "JSON-LD group has no @type attribute" |
| 26 | 704: "Operation needs operand list" |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 27 | 802: "Match type is not supported by value type" |
| 28 | 804: "Unknown value type" |
| 29 | 805: "Value is invalid" |
| 30 | 806: "Value is not a valid date string" |
| 31 | 807: "Value is not a valid regular expression" |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 32 | 810: "Unknown document group operation" (like 711) |
| 33 | 811: "Document group expects operation" (like 703) |
| 34 | 812: "Operand not supported in document group" (like 744) |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 35 | 813: "Collection type is not supported" (like 713) |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 36 | 814: "Unknown rewrite operation" |
| 37 | 815: "Rewrite expects source" |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 38 | |
| 39 | Localization strings: |
| 40 | KorAP.Locale = { |
| 41 | EMPTY : '...', |
| 42 | AND : 'and', |
| 43 | OR : 'or', |
| 44 | DELETE : 'x' |
| 45 | } |
Nils Diewald | 4c22125 | 2015-04-21 20:19:25 +0000 | [diff] [blame] | 46 | and various field names with the prefix 'VC_' |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 47 | */ |
| 48 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 49 | define([ |
| 50 | 'vc/unspecified', |
| 51 | 'vc/doc', |
| 52 | 'vc/docgroup', |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 53 | 'vc/menu', |
Nils Diewald | 8750783 | 2015-05-01 23:36:41 +0000 | [diff] [blame] | 54 | 'datepicker', |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 55 | 'util' |
Nils Diewald | 8750783 | 2015-05-01 23:36:41 +0000 | [diff] [blame] | 56 | ], function (unspecDocClass, docClass, docGroupClass, menuClass, dpClass) { |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 57 | "use strict"; |
| 58 | |
Nils Diewald | 359a72c | 2015-04-20 17:40:29 +0000 | [diff] [blame] | 59 | // ??? |
Akron | 6a535d4 | 2015-08-26 20:16:58 +0200 | [diff] [blame] | 60 | KorAP._validStringMatchRE = new RegExp("^(?:eq|ne|contains(?:not)?|excludes)$"); |
Nils Diewald | 359a72c | 2015-04-20 17:40:29 +0000 | [diff] [blame] | 61 | KorAP._overrideStyles = false; |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 62 | |
Akron | 0b489ad | 2018-02-02 16:49:32 +0100 | [diff] [blame] | 63 | const loc = KorAP.Locale; |
Nils Diewald | d599d54 | 2015-01-08 20:41:34 +0000 | [diff] [blame] | 64 | |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 65 | KorAP._vcKeyMenu = undefined; |
Nils Diewald | 8750783 | 2015-05-01 23:36:41 +0000 | [diff] [blame] | 66 | KorAP._vcDatePicker = dpClass.create(); |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 67 | |
Akron | 712733a | 2018-04-05 18:17:47 +0200 | [diff] [blame^] | 68 | // Create match menus .... |
| 69 | KorAP._vcMatchopMenu = { |
| 70 | 'string' : menuClass.create([ |
| 71 | ['eq', null], |
| 72 | ['ne', null] |
| 73 | ]), |
| 74 | 'text' : menuClass.create([ |
| 75 | ['eq', null], // Requires exact match |
| 76 | ['ne', null], |
| 77 | ['contains', null], // Requires token sequence match |
| 78 | ['containsnot', null] |
| 79 | ]), |
| 80 | 'date' : menuClass.create([ |
| 81 | ['eq', null], |
| 82 | ['geq', null], |
| 83 | ['leq', null] |
| 84 | ]), |
| 85 | 'regex' : menuClass.create([ |
| 86 | ['eq', null], |
| 87 | ['ne', null] |
| 88 | ]) |
| 89 | }; |
| 90 | |
Nils Diewald | d599d54 | 2015-01-08 20:41:34 +0000 | [diff] [blame] | 91 | /** |
| 92 | * Virtual Collection |
| 93 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 94 | return { |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 95 | |
| 96 | /** |
| 97 | * The JSON-LD type of the virtual collection |
| 98 | */ |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 99 | ldType : function () { |
| 100 | return null; |
| 101 | }, |
Nils Diewald | d599d54 | 2015-01-08 20:41:34 +0000 | [diff] [blame] | 102 | |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 103 | // Initialize virtual collection |
| 104 | _init : function (keyList) { |
| 105 | |
| 106 | // Inject localized css styles |
Nils Diewald | 359a72c | 2015-04-20 17:40:29 +0000 | [diff] [blame] | 107 | if (!KorAP._overrideStyles) { |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 108 | var sheet = KorAP.newStyleSheet(); |
Nils Diewald | 359a72c | 2015-04-20 17:40:29 +0000 | [diff] [blame] | 109 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 110 | // Add css rule for OR operations |
| 111 | sheet.insertRule( |
| 112 | '.vc .docGroup[data-operation=or] > .doc::before,' + |
| 113 | '.vc .docGroup[data-operation=or] > .docGroup::before ' + |
| 114 | '{ content: "' + loc.OR + '" }', |
| 115 | 0 |
| 116 | ); |
Nils Diewald | 359a72c | 2015-04-20 17:40:29 +0000 | [diff] [blame] | 117 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 118 | // Add css rule for AND operations |
| 119 | sheet.insertRule( |
| 120 | '.vc .docGroup[data-operation=and] > .doc::before,' + |
| 121 | '.vc .docGroup[data-operation=and] > .docGroup::before ' + |
| 122 | '{ content: "' + loc.AND + '" }', |
| 123 | 1 |
| 124 | ); |
Nils Diewald | 359a72c | 2015-04-20 17:40:29 +0000 | [diff] [blame] | 125 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 126 | KorAP._overrideStyles = true; |
Nils Diewald | 359a72c | 2015-04-20 17:40:29 +0000 | [diff] [blame] | 127 | }; |
| 128 | |
Akron | 712733a | 2018-04-05 18:17:47 +0200 | [diff] [blame^] | 129 | // Create key menu |
| 130 | KorAP._vcKeyMenu = menuClass.create(keyList); |
| 131 | KorAP._vcKeyMenu.limit(6); |
| 132 | |
Nils Diewald | 359a72c | 2015-04-20 17:40:29 +0000 | [diff] [blame] | 133 | return this; |
| 134 | }, |
| 135 | |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 136 | /** |
| 137 | * Create a new virtual collection. |
| 138 | */ |
| 139 | create : function (keyList) { |
Nils Diewald | 6283d69 | 2015-04-23 20:32:53 +0000 | [diff] [blame] | 140 | var obj = Object.create(this)._init(keyList); |
Akron | 712733a | 2018-04-05 18:17:47 +0200 | [diff] [blame^] | 141 | console.log(keyList); |
Nils Diewald | 6283d69 | 2015-04-23 20:32:53 +0000 | [diff] [blame] | 142 | obj._root = unspecDocClass.create(obj); |
| 143 | return obj; |
Nils Diewald | 4019bd2 | 2015-01-08 19:57:50 +0000 | [diff] [blame] | 144 | }, |
Nils Diewald | d599d54 | 2015-01-08 20:41:34 +0000 | [diff] [blame] | 145 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 146 | |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 147 | /** |
| 148 | * Create and render a new virtual collection |
| 149 | * based on a KoralQuery collection document |
| 150 | */ |
Nils Diewald | 6283d69 | 2015-04-23 20:32:53 +0000 | [diff] [blame] | 151 | fromJson : function (json) { |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 152 | if (json !== undefined) { |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 153 | // Parse root document |
| 154 | if (json['@type'] == 'koral:doc') { |
| 155 | this._root = docClass.create(this, json); |
| 156 | } |
| 157 | // parse root group |
| 158 | else if (json['@type'] == 'koral:docGroup') { |
| 159 | this._root = docGroupClass.create(this, json); |
| 160 | } |
| 161 | // Unknown collection type |
| 162 | else { |
| 163 | KorAP.log(813, "Collection type is not supported"); |
| 164 | return; |
| 165 | }; |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | else { |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 169 | // Add unspecified object |
| 170 | this._root = unspecDocClass.create(this); |
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 |
Nils Diewald | 6283d69 | 2015-04-23 20:32:53 +0000 | [diff] [blame] | 174 | this.update(); |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 175 | |
Nils Diewald | 6283d69 | 2015-04-23 20:32:53 +0000 | [diff] [blame] | 176 | return this; |
| 177 | }, |
| 178 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 179 | |
Akron | 04671e7 | 2017-05-11 20:47:32 +0200 | [diff] [blame] | 180 | // Check if the virtual corpus contains a rewrite |
| 181 | // This is a class method |
| 182 | checkRewrite : function (json) { |
| 183 | |
| 184 | // There is a rewrite attribute |
| 185 | if (json['rewrites'] !== undefined) { |
| 186 | return true; |
| 187 | } |
| 188 | |
| 189 | // There is a group to check for rewrites |
| 190 | else if (json['@type'] === 'koral:docGroup') { |
| 191 | var ops = json['operands']; |
| 192 | if (ops === undefined) |
| 193 | return false; |
| 194 | |
| 195 | for (var i in ops) { |
| 196 | |
| 197 | // "this" is the class |
| 198 | if (this.checkRewrite(ops[i])) { |
| 199 | return true; |
| 200 | }; |
| 201 | }; |
| 202 | }; |
| 203 | return false; |
| 204 | }, |
| 205 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 206 | /** |
| 207 | * Clean the virtual document to uspecified doc. |
| 208 | */ |
Nils Diewald | 6283d69 | 2015-04-23 20:32:53 +0000 | [diff] [blame] | 209 | clean : function () { |
| 210 | if (this._root.ldType() !== "non") { |
Akron | 04671e7 | 2017-05-11 20:47:32 +0200 | [diff] [blame] | 211 | this._root.destroy(); |
| 212 | this.root(unspecDocClass.create(this)); |
Nils Diewald | 6283d69 | 2015-04-23 20:32:53 +0000 | [diff] [blame] | 213 | }; |
| 214 | return this; |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 215 | }, |
Nils Diewald | d599d54 | 2015-01-08 20:41:34 +0000 | [diff] [blame] | 216 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 217 | |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 218 | /** |
| 219 | * Get or set the root object of the |
| 220 | * virtual collection. |
| 221 | */ |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 222 | root : function (obj) { |
Nils Diewald | 8e7182e | 2015-01-08 15:02:07 +0000 | [diff] [blame] | 223 | if (arguments.length === 1) { |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 224 | var e = this.element(); |
Nils Diewald | 845282c | 2015-05-14 07:53:03 +0000 | [diff] [blame] | 225 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 226 | if (e.firstChild !== null) { |
| 227 | if (e.firstChild !== obj.element()) { |
| 228 | e.replaceChild(obj.element(), e.firstChild); |
| 229 | }; |
| 230 | } |
Nils Diewald | 8f6b610 | 2015-01-08 18:25:33 +0000 | [diff] [blame] | 231 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 232 | // Append root element |
| 233 | else { |
| 234 | e.appendChild(obj.element()); |
| 235 | }; |
Nils Diewald | 8f6b610 | 2015-01-08 18:25:33 +0000 | [diff] [blame] | 236 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 237 | // Update parent child relations |
| 238 | this._root = obj; |
| 239 | obj.parent(this); |
Nils Diewald | 8f6b610 | 2015-01-08 18:25:33 +0000 | [diff] [blame] | 240 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 241 | this.update(); |
Nils Diewald | 8e7182e | 2015-01-08 15:02:07 +0000 | [diff] [blame] | 242 | }; |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 243 | return this._root; |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 244 | }, |
Nils Diewald | 8f6b610 | 2015-01-08 18:25:33 +0000 | [diff] [blame] | 245 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 246 | |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 247 | /** |
| 248 | * Get the element associated with the virtual collection |
| 249 | */ |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 250 | element : function () { |
Akron | 12971a0 | 2018-01-03 16:38:10 +0100 | [diff] [blame] | 251 | if (this._element !== undefined) { |
| 252 | return this._element; |
| 253 | }; |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 254 | |
| 255 | this._element = document.createElement('div'); |
| 256 | this._element.setAttribute('class', 'vc'); |
Nils Diewald | 8e7182e | 2015-01-08 15:02:07 +0000 | [diff] [blame] | 257 | |
Nils Diewald | 8f6b610 | 2015-01-08 18:25:33 +0000 | [diff] [blame] | 258 | // Initialize root |
| 259 | this._element.appendChild(this._root.element()); |
Nils Diewald | 845282c | 2015-05-14 07:53:03 +0000 | [diff] [blame] | 260 | |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 261 | return this._element; |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 262 | }, |
Nils Diewald | d599d54 | 2015-01-08 20:41:34 +0000 | [diff] [blame] | 263 | |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 264 | |
| 265 | /** |
| 266 | * Update the whole object based on the underlying |
| 267 | * data structure |
| 268 | */ |
Nils Diewald | d599d54 | 2015-01-08 20:41:34 +0000 | [diff] [blame] | 269 | update : function () { |
| 270 | this._root.update(); |
| 271 | return this; |
| 272 | }, |
| 273 | |
Nils Diewald | 845282c | 2015-05-14 07:53:03 +0000 | [diff] [blame] | 274 | /** |
| 275 | * Make the vc persistant by injecting the current timestamp |
| 276 | * as a creation date limit criterion. |
| 277 | */ |
| 278 | makePersistant : function () { |
Akron | 12971a0 | 2018-01-03 16:38:10 +0100 | [diff] [blame] | 279 | // this.root().wrapOnRoot('and'); |
Nils Diewald | 845282c | 2015-05-14 07:53:03 +0000 | [diff] [blame] | 280 | var todayStr = KorAP._vcDatePicker.today(); |
| 281 | var doc = docClass.create(); |
| 282 | var root = this.root(); |
| 283 | |
| 284 | if (root.ldType() === 'docGroup' && |
Akron | 12971a0 | 2018-01-03 16:38:10 +0100 | [diff] [blame] | 285 | root.operation === 'and') { |
| 286 | root.append(cond); |
Nils Diewald | 845282c | 2015-05-14 07:53:03 +0000 | [diff] [blame] | 287 | } |
| 288 | else { |
Akron | 12971a0 | 2018-01-03 16:38:10 +0100 | [diff] [blame] | 289 | root.wrapOnRoot('and'); |
| 290 | root.append(doc); |
Nils Diewald | 845282c | 2015-05-14 07:53:03 +0000 | [diff] [blame] | 291 | }; |
| 292 | |
| 293 | doc.key("creationDate"); |
| 294 | doc.type("date"); |
| 295 | doc.matchop("leq"); |
| 296 | doc.value(todayStr); |
| 297 | |
| 298 | /* |
| 299 | { |
| 300 | "@type" : "koral:doc", |
| 301 | "key" : "creationDate", |
| 302 | "type" : "type:date", |
| 303 | "match" : "match:leq", |
| 304 | "value" : todayStr |
| 305 | } |
| 306 | this.root().append(cond); |
| 307 | */ |
| 308 | this.update(); |
| 309 | }, |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 310 | |
| 311 | /** |
| 312 | * Get the generated json string |
| 313 | */ |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 314 | toJson : function () { |
| 315 | return this._root.toJson(); |
| 316 | }, |
Nils Diewald | d599d54 | 2015-01-08 20:41:34 +0000 | [diff] [blame] | 317 | |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 318 | |
| 319 | /** |
| 320 | * Get the generated query string |
| 321 | */ |
Nils Diewald | d599d54 | 2015-01-08 20:41:34 +0000 | [diff] [blame] | 322 | toQuery : function () { |
| 323 | return this._root.toQuery(); |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 324 | } |
| 325 | }; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 326 | }); |