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