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