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