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 |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 17 | TODO: Implement "persistence"-Option, |
Nils Diewald | 6e43ffd | 2015-03-25 18:55:39 +0000 | [diff] [blame] | 18 | injecting the current creation date stamp |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 19 | |
Nils Diewald | d599d54 | 2015-01-08 20:41:34 +0000 | [diff] [blame] | 20 | Error codes: |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 21 | 701: "JSON-LD group has no @type attribute" |
| 22 | 704: "Operation needs operand list" |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 23 | 802: "Match type is not supported by value type" |
| 24 | 804: "Unknown value type" |
| 25 | 805: "Value is invalid" |
| 26 | 806: "Value is not a valid date string" |
| 27 | 807: "Value is not a valid regular expression" |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 28 | 810: "Unknown document group operation" (like 711) |
| 29 | 811: "Document group expects operation" (like 703) |
| 30 | 812: "Operand not supported in document group" (like 744) |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 31 | 813: "Collection type is not supported" (like 713) |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 32 | 814: "Unknown rewrite operation" |
| 33 | 815: "Rewrite expects source" |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 34 | |
| 35 | Localization strings: |
| 36 | KorAP.Locale = { |
| 37 | EMPTY : '...', |
| 38 | AND : 'and', |
| 39 | OR : 'or', |
| 40 | DELETE : 'x' |
| 41 | } |
Nils Diewald | 4c22125 | 2015-04-21 20:19:25 +0000 | [diff] [blame] | 42 | and various field names with the prefix 'VC_' |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 43 | */ |
| 44 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 45 | define([ |
| 46 | 'vc/unspecified', |
| 47 | 'vc/doc', |
| 48 | 'vc/docgroup', |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 49 | 'vc/menu', |
Nils Diewald | 8750783 | 2015-05-01 23:36:41 +0000 | [diff] [blame] | 50 | 'datepicker', |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 51 | 'util' |
Nils Diewald | 8750783 | 2015-05-01 23:36:41 +0000 | [diff] [blame] | 52 | ], function (unspecDocClass, docClass, docGroupClass, menuClass, dpClass) { |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 53 | "use strict"; |
| 54 | |
Nils Diewald | 359a72c | 2015-04-20 17:40:29 +0000 | [diff] [blame] | 55 | // ??? |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 56 | KorAP._validStringMatchRE = new RegExp("^(?:eq|ne|contains|excludes)$"); |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 57 | KorAP._validDateMatchRE = new RegExp("^[lg]?eq$"); |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 58 | KorAP._validDateRE = new RegExp("^(?:\\d{4})(?:-\\d\\d(?:-\\d\\d)?)?$"); |
Nils Diewald | 359a72c | 2015-04-20 17:40:29 +0000 | [diff] [blame] | 59 | KorAP._overrideStyles = false; |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 60 | |
Nils Diewald | 359a72c | 2015-04-20 17:40:29 +0000 | [diff] [blame] | 61 | var loc = KorAP.Locale; |
Nils Diewald | d599d54 | 2015-01-08 20:41:34 +0000 | [diff] [blame] | 62 | |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 63 | KorAP._vcKeyMenu = undefined; |
Nils Diewald | 8750783 | 2015-05-01 23:36:41 +0000 | [diff] [blame] | 64 | KorAP._vcDatePicker = dpClass.create(); |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 65 | |
Nils Diewald | d599d54 | 2015-01-08 20:41:34 +0000 | [diff] [blame] | 66 | /** |
| 67 | * Virtual Collection |
| 68 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 69 | return { |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 70 | |
| 71 | /** |
| 72 | * The JSON-LD type of the virtual collection |
| 73 | */ |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 74 | ldType : function () { |
| 75 | return null; |
| 76 | }, |
Nils Diewald | d599d54 | 2015-01-08 20:41:34 +0000 | [diff] [blame] | 77 | |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 78 | // Initialize virtual collection |
| 79 | _init : function (keyList) { |
| 80 | |
| 81 | // Inject localized css styles |
Nils Diewald | 359a72c | 2015-04-20 17:40:29 +0000 | [diff] [blame] | 82 | if (!KorAP._overrideStyles) { |
| 83 | var sheet = KorAP.newStyleSheet(); |
| 84 | |
| 85 | // Add css rule for OR operations |
| 86 | sheet.insertRule( |
| 87 | '.vc .docGroup[data-operation=or] > .doc::before,' + |
| 88 | '.vc .docGroup[data-operation=or] > .docGroup::before ' + |
| 89 | '{ content: "' + loc.OR + '" }', |
| 90 | 0 |
| 91 | ); |
| 92 | |
| 93 | // Add css rule for AND operations |
| 94 | sheet.insertRule( |
| 95 | '.vc .docGroup[data-operation=and] > .doc::before,' + |
| 96 | '.vc .docGroup[data-operation=and] > .docGroup::before ' + |
| 97 | '{ content: "' + loc.AND + '" }', |
| 98 | 1 |
| 99 | ); |
| 100 | |
Nils Diewald | 359a72c | 2015-04-20 17:40:29 +0000 | [diff] [blame] | 101 | KorAP._overrideStyles = true; |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 102 | |
| 103 | // Create key menu |
| 104 | KorAP._vcKeyMenu = menuClass.create(keyList); |
Nils Diewald | 4c22125 | 2015-04-21 20:19:25 +0000 | [diff] [blame] | 105 | KorAP._vcKeyMenu.limit(6); |
| 106 | |
| 107 | // Create match menus .... |
| 108 | KorAP._vcMatchopMenu = { |
| 109 | 'string' : menuClass.create([ |
| 110 | ['eq', null], |
| 111 | ['ne', null], |
| 112 | ['contains', null], |
| 113 | ['excludes', null] |
| 114 | ]), |
| 115 | 'date' : menuClass.create([ |
| 116 | ['eq', null], |
| 117 | ['geq', null], |
| 118 | ['leq', null] |
| 119 | ]), |
| 120 | 'regex' : menuClass.create([ |
| 121 | ['eq', null], |
| 122 | ['ne', null], |
| 123 | ['contains', null], |
| 124 | ['excludes', null] |
| 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 | |
| 148 | if (json !== undefined) { |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 149 | // Parse root document |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 150 | if (json['@type'] == 'koral:doc') { |
Nils Diewald | 6283d69 | 2015-04-23 20:32:53 +0000 | [diff] [blame] | 151 | this._root = docClass.create(this, json); |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 152 | } |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 153 | // parse root group |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 154 | else if (json['@type'] == 'koral:docGroup') { |
Nils Diewald | 6283d69 | 2015-04-23 20:32:53 +0000 | [diff] [blame] | 155 | this._root = docGroupClass.create(this, json); |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 156 | } |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 157 | // Unknown collection type |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 158 | else { |
| 159 | KorAP.log(813, "Collection type is not supported"); |
| 160 | return; |
| 161 | }; |
| 162 | } |
| 163 | |
| 164 | else { |
| 165 | // Add unspecified object |
Nils Diewald | 6283d69 | 2015-04-23 20:32:53 +0000 | [diff] [blame] | 166 | this._root = unspecDocClass.create(this); |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 167 | }; |
| 168 | |
Nils Diewald | 8e7182e | 2015-01-08 15:02:07 +0000 | [diff] [blame] | 169 | // Init element and update |
Nils Diewald | 6283d69 | 2015-04-23 20:32:53 +0000 | [diff] [blame] | 170 | this.update(); |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 171 | |
Nils Diewald | 6283d69 | 2015-04-23 20:32:53 +0000 | [diff] [blame] | 172 | return this; |
| 173 | }, |
| 174 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 175 | |
| 176 | /** |
| 177 | * Clean the virtual document to uspecified doc. |
| 178 | */ |
Nils Diewald | 6283d69 | 2015-04-23 20:32:53 +0000 | [diff] [blame] | 179 | clean : function () { |
| 180 | if (this._root.ldType() !== "non") { |
| 181 | this._root.destroy(); |
| 182 | this.root(unspecDocClass.create(this)); |
| 183 | }; |
| 184 | return this; |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 185 | }, |
Nils Diewald | d599d54 | 2015-01-08 20:41:34 +0000 | [diff] [blame] | 186 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame] | 187 | |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 188 | /** |
| 189 | * Get or set the root object of the |
| 190 | * virtual collection. |
| 191 | */ |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 192 | root : function (obj) { |
Nils Diewald | 8e7182e | 2015-01-08 15:02:07 +0000 | [diff] [blame] | 193 | if (arguments.length === 1) { |
Nils Diewald | 8f6b610 | 2015-01-08 18:25:33 +0000 | [diff] [blame] | 194 | var e = this.element(); |
Nils Diewald | 845282c | 2015-05-14 07:53:03 +0000 | [diff] [blame] | 195 | |
Nils Diewald | 8f6b610 | 2015-01-08 18:25:33 +0000 | [diff] [blame] | 196 | if (e.firstChild !== null) { |
Nils Diewald | d5070b0 | 2015-01-11 01:44:47 +0000 | [diff] [blame] | 197 | if (e.firstChild !== obj.element()) { |
Nils Diewald | 8f6b610 | 2015-01-08 18:25:33 +0000 | [diff] [blame] | 198 | e.replaceChild(obj.element(), e.firstChild); |
Nils Diewald | d5070b0 | 2015-01-11 01:44:47 +0000 | [diff] [blame] | 199 | }; |
Nils Diewald | 8f6b610 | 2015-01-08 18:25:33 +0000 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | // Append root element |
| 203 | else { |
| 204 | e.appendChild(obj.element()); |
| 205 | }; |
| 206 | |
| 207 | // Update parent child relations |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 208 | this._root = obj; |
Nils Diewald | 8f6b610 | 2015-01-08 18:25:33 +0000 | [diff] [blame] | 209 | obj.parent(this); |
| 210 | |
Nils Diewald | 8e7182e | 2015-01-08 15:02:07 +0000 | [diff] [blame] | 211 | this.update(); |
| 212 | }; |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 213 | return this._root; |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 214 | }, |
Nils Diewald | 8f6b610 | 2015-01-08 18:25:33 +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 the element associated with the virtual collection |
| 219 | */ |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 220 | element : function () { |
| 221 | if (this._element !== undefined) |
| 222 | return this._element; |
| 223 | |
| 224 | this._element = document.createElement('div'); |
| 225 | this._element.setAttribute('class', 'vc'); |
Nils Diewald | 8e7182e | 2015-01-08 15:02:07 +0000 | [diff] [blame] | 226 | |
Nils Diewald | 8f6b610 | 2015-01-08 18:25:33 +0000 | [diff] [blame] | 227 | // Initialize root |
| 228 | this._element.appendChild(this._root.element()); |
Nils Diewald | 845282c | 2015-05-14 07:53:03 +0000 | [diff] [blame] | 229 | |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 230 | return this._element; |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 231 | }, |
Nils Diewald | d599d54 | 2015-01-08 20:41:34 +0000 | [diff] [blame] | 232 | |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 233 | |
| 234 | /** |
| 235 | * Update the whole object based on the underlying |
| 236 | * data structure |
| 237 | */ |
Nils Diewald | d599d54 | 2015-01-08 20:41:34 +0000 | [diff] [blame] | 238 | update : function () { |
| 239 | this._root.update(); |
| 240 | return this; |
| 241 | }, |
| 242 | |
Nils Diewald | 845282c | 2015-05-14 07:53:03 +0000 | [diff] [blame] | 243 | /** |
| 244 | * Make the vc persistant by injecting the current timestamp |
| 245 | * as a creation date limit criterion. |
| 246 | */ |
| 247 | makePersistant : function () { |
| 248 | // this.root().wrapOnRoot('and'); |
| 249 | var todayStr = KorAP._vcDatePicker.today(); |
| 250 | var doc = docClass.create(); |
| 251 | var root = this.root(); |
| 252 | |
| 253 | if (root.ldType() === 'docGroup' && |
| 254 | root.operation === 'and') { |
| 255 | root.append(cond); |
| 256 | } |
| 257 | else { |
| 258 | root.wrapOnRoot('and'); |
| 259 | root.append(doc); |
| 260 | }; |
| 261 | |
| 262 | doc.key("creationDate"); |
| 263 | doc.type("date"); |
| 264 | doc.matchop("leq"); |
| 265 | doc.value(todayStr); |
| 266 | |
| 267 | /* |
| 268 | { |
| 269 | "@type" : "koral:doc", |
| 270 | "key" : "creationDate", |
| 271 | "type" : "type:date", |
| 272 | "match" : "match:leq", |
| 273 | "value" : todayStr |
| 274 | } |
| 275 | this.root().append(cond); |
| 276 | */ |
| 277 | this.update(); |
| 278 | }, |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 279 | |
| 280 | /** |
| 281 | * Get the generated json string |
| 282 | */ |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 283 | toJson : function () { |
| 284 | return this._root.toJson(); |
| 285 | }, |
Nils Diewald | d599d54 | 2015-01-08 20:41:34 +0000 | [diff] [blame] | 286 | |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 287 | |
| 288 | /** |
| 289 | * Get the generated query string |
| 290 | */ |
Nils Diewald | d599d54 | 2015-01-08 20:41:34 +0000 | [diff] [blame] | 291 | toQuery : function () { |
| 292 | return this._root.toQuery(); |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 293 | } |
| 294 | }; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 295 | }); |