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], |
| 126 | ['ne', null], |
| 127 | ['contains', null], |
| 128 | ['containsnot', null] |
| 129 | ]) |
| 130 | }; |
Nils Diewald | 359a72c | 2015-04-20 17:40:29 +0000 | [diff] [blame] | 131 | }; |
| 132 | |
| 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); |
| 141 | obj._root = unspecDocClass.create(obj); |
| 142 | return obj; |
Nils Diewald | 4019bd2 | 2015-01-08 19:57:50 +0000 | [diff] [blame] | 143 | }, |
Nils Diewald | d599d54 | 2015-01-08 20:41:34 +0000 | [diff] [blame] | 144 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 145 | |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 146 | /** |
| 147 | * Create and render a new virtual collection |
| 148 | * based on a KoralQuery collection document |
| 149 | */ |
Nils Diewald | 6283d69 | 2015-04-23 20:32:53 +0000 | [diff] [blame] | 150 | fromJson : function (json) { |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 151 | if (json !== undefined) { |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 152 | // Parse root document |
| 153 | if (json['@type'] == 'koral:doc') { |
| 154 | this._root = docClass.create(this, json); |
| 155 | } |
| 156 | // parse root group |
| 157 | else if (json['@type'] == 'koral:docGroup') { |
| 158 | this._root = docGroupClass.create(this, json); |
| 159 | } |
| 160 | // Unknown collection type |
| 161 | else { |
| 162 | KorAP.log(813, "Collection type is not supported"); |
| 163 | return; |
| 164 | }; |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | else { |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 168 | // Add unspecified object |
| 169 | this._root = unspecDocClass.create(this); |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 170 | }; |
| 171 | |
Nils Diewald | 8e7182e | 2015-01-08 15:02:07 +0000 | [diff] [blame] | 172 | // Init element and update |
Nils Diewald | 6283d69 | 2015-04-23 20:32:53 +0000 | [diff] [blame] | 173 | this.update(); |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 174 | |
Nils Diewald | 6283d69 | 2015-04-23 20:32:53 +0000 | [diff] [blame] | 175 | return this; |
| 176 | }, |
| 177 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 178 | |
Akron | 04671e7 | 2017-05-11 20:47:32 +0200 | [diff] [blame^] | 179 | // Check if the virtual corpus contains a rewrite |
| 180 | // This is a class method |
| 181 | checkRewrite : function (json) { |
| 182 | |
| 183 | // There is a rewrite attribute |
| 184 | if (json['rewrites'] !== undefined) { |
| 185 | return true; |
| 186 | } |
| 187 | |
| 188 | // There is a group to check for rewrites |
| 189 | else if (json['@type'] === 'koral:docGroup') { |
| 190 | var ops = json['operands']; |
| 191 | if (ops === undefined) |
| 192 | return false; |
| 193 | |
| 194 | for (var i in ops) { |
| 195 | |
| 196 | // "this" is the class |
| 197 | if (this.checkRewrite(ops[i])) { |
| 198 | return true; |
| 199 | }; |
| 200 | }; |
| 201 | }; |
| 202 | return false; |
| 203 | }, |
| 204 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 205 | /** |
| 206 | * Clean the virtual document to uspecified doc. |
| 207 | */ |
Nils Diewald | 6283d69 | 2015-04-23 20:32:53 +0000 | [diff] [blame] | 208 | clean : function () { |
| 209 | if (this._root.ldType() !== "non") { |
Akron | 04671e7 | 2017-05-11 20:47:32 +0200 | [diff] [blame^] | 210 | this._root.destroy(); |
| 211 | this.root(unspecDocClass.create(this)); |
Nils Diewald | 6283d69 | 2015-04-23 20:32:53 +0000 | [diff] [blame] | 212 | }; |
| 213 | return this; |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 214 | }, |
Nils Diewald | d599d54 | 2015-01-08 20:41:34 +0000 | [diff] [blame] | 215 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 216 | |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 217 | /** |
| 218 | * Get or set the root object of the |
| 219 | * virtual collection. |
| 220 | */ |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 221 | root : function (obj) { |
Nils Diewald | 8e7182e | 2015-01-08 15:02:07 +0000 | [diff] [blame] | 222 | if (arguments.length === 1) { |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 223 | var e = this.element(); |
Nils Diewald | 845282c | 2015-05-14 07:53:03 +0000 | [diff] [blame] | 224 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 225 | if (e.firstChild !== null) { |
| 226 | if (e.firstChild !== obj.element()) { |
| 227 | e.replaceChild(obj.element(), e.firstChild); |
| 228 | }; |
| 229 | } |
Nils Diewald | 8f6b610 | 2015-01-08 18:25:33 +0000 | [diff] [blame] | 230 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 231 | // Append root element |
| 232 | else { |
| 233 | e.appendChild(obj.element()); |
| 234 | }; |
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 | // Update parent child relations |
| 237 | this._root = obj; |
| 238 | obj.parent(this); |
Nils Diewald | 8f6b610 | 2015-01-08 18:25:33 +0000 | [diff] [blame] | 239 | |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame] | 240 | this.update(); |
Nils Diewald | 8e7182e | 2015-01-08 15:02:07 +0000 | [diff] [blame] | 241 | }; |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 242 | return this._root; |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 243 | }, |
Nils Diewald | 8f6b610 | 2015-01-08 18:25:33 +0000 | [diff] [blame] | 244 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 245 | |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 246 | /** |
| 247 | * Get the element associated with the virtual collection |
| 248 | */ |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 249 | element : function () { |
| 250 | if (this._element !== undefined) |
| 251 | return this._element; |
| 252 | |
| 253 | this._element = document.createElement('div'); |
| 254 | this._element.setAttribute('class', 'vc'); |
Nils Diewald | 8e7182e | 2015-01-08 15:02:07 +0000 | [diff] [blame] | 255 | |
Nils Diewald | 8f6b610 | 2015-01-08 18:25:33 +0000 | [diff] [blame] | 256 | // Initialize root |
| 257 | this._element.appendChild(this._root.element()); |
Nils Diewald | 845282c | 2015-05-14 07:53:03 +0000 | [diff] [blame] | 258 | |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 259 | return this._element; |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 260 | }, |
Nils Diewald | d599d54 | 2015-01-08 20:41:34 +0000 | [diff] [blame] | 261 | |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 262 | |
| 263 | /** |
| 264 | * Update the whole object based on the underlying |
| 265 | * data structure |
| 266 | */ |
Nils Diewald | d599d54 | 2015-01-08 20:41:34 +0000 | [diff] [blame] | 267 | update : function () { |
| 268 | this._root.update(); |
| 269 | return this; |
| 270 | }, |
| 271 | |
Nils Diewald | 845282c | 2015-05-14 07:53:03 +0000 | [diff] [blame] | 272 | /** |
| 273 | * Make the vc persistant by injecting the current timestamp |
| 274 | * as a creation date limit criterion. |
| 275 | */ |
| 276 | makePersistant : function () { |
| 277 | // this.root().wrapOnRoot('and'); |
| 278 | var todayStr = KorAP._vcDatePicker.today(); |
| 279 | var doc = docClass.create(); |
| 280 | var root = this.root(); |
| 281 | |
| 282 | if (root.ldType() === 'docGroup' && |
| 283 | root.operation === 'and') { |
| 284 | root.append(cond); |
| 285 | } |
| 286 | else { |
| 287 | root.wrapOnRoot('and'); |
| 288 | root.append(doc); |
| 289 | }; |
| 290 | |
| 291 | doc.key("creationDate"); |
| 292 | doc.type("date"); |
| 293 | doc.matchop("leq"); |
| 294 | doc.value(todayStr); |
| 295 | |
| 296 | /* |
| 297 | { |
| 298 | "@type" : "koral:doc", |
| 299 | "key" : "creationDate", |
| 300 | "type" : "type:date", |
| 301 | "match" : "match:leq", |
| 302 | "value" : todayStr |
| 303 | } |
| 304 | this.root().append(cond); |
| 305 | */ |
| 306 | this.update(); |
| 307 | }, |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 308 | |
| 309 | /** |
| 310 | * Get the generated json string |
| 311 | */ |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 312 | toJson : function () { |
| 313 | return this._root.toJson(); |
| 314 | }, |
Nils Diewald | d599d54 | 2015-01-08 20:41:34 +0000 | [diff] [blame] | 315 | |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 316 | |
| 317 | /** |
| 318 | * Get the generated query string |
| 319 | */ |
Nils Diewald | d599d54 | 2015-01-08 20:41:34 +0000 | [diff] [blame] | 320 | toQuery : function () { |
| 321 | return this._root.toQuery(); |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 322 | } |
| 323 | }; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 324 | }); |