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(); |
| 195 | if (e.firstChild !== null) { |
Nils Diewald | d5070b0 | 2015-01-11 01:44:47 +0000 | [diff] [blame] | 196 | if (e.firstChild !== obj.element()) { |
Nils Diewald | 8f6b610 | 2015-01-08 18:25:33 +0000 | [diff] [blame] | 197 | e.replaceChild(obj.element(), e.firstChild); |
Nils Diewald | d5070b0 | 2015-01-11 01:44:47 +0000 | [diff] [blame] | 198 | }; |
Nils Diewald | 8f6b610 | 2015-01-08 18:25:33 +0000 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | // Append root element |
| 202 | else { |
| 203 | e.appendChild(obj.element()); |
| 204 | }; |
| 205 | |
| 206 | // Update parent child relations |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 207 | this._root = obj; |
Nils Diewald | 8f6b610 | 2015-01-08 18:25:33 +0000 | [diff] [blame] | 208 | obj.parent(this); |
| 209 | |
Nils Diewald | 8e7182e | 2015-01-08 15:02:07 +0000 | [diff] [blame] | 210 | this.update(); |
| 211 | }; |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 212 | return this._root; |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 213 | }, |
Nils Diewald | 8f6b610 | 2015-01-08 18:25:33 +0000 | [diff] [blame] | 214 | |
Nils Diewald | 7148c6f | 2015-05-04 15:07:53 +0000 | [diff] [blame^] | 215 | |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 216 | /** |
| 217 | * Get the element associated with the virtual collection |
| 218 | */ |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 219 | element : function () { |
| 220 | if (this._element !== undefined) |
| 221 | return this._element; |
| 222 | |
| 223 | this._element = document.createElement('div'); |
| 224 | this._element.setAttribute('class', 'vc'); |
Nils Diewald | 8e7182e | 2015-01-08 15:02:07 +0000 | [diff] [blame] | 225 | |
Nils Diewald | 8f6b610 | 2015-01-08 18:25:33 +0000 | [diff] [blame] | 226 | // Initialize root |
| 227 | this._element.appendChild(this._root.element()); |
| 228 | |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 229 | return this._element; |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 230 | }, |
Nils Diewald | d599d54 | 2015-01-08 20:41:34 +0000 | [diff] [blame] | 231 | |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 232 | |
| 233 | /** |
| 234 | * Update the whole object based on the underlying |
| 235 | * data structure |
| 236 | */ |
Nils Diewald | d599d54 | 2015-01-08 20:41:34 +0000 | [diff] [blame] | 237 | update : function () { |
| 238 | this._root.update(); |
| 239 | return this; |
| 240 | }, |
| 241 | |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 242 | |
| 243 | /** |
| 244 | * Get the generated json string |
| 245 | */ |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 246 | toJson : function () { |
| 247 | return this._root.toJson(); |
| 248 | }, |
Nils Diewald | d599d54 | 2015-01-08 20:41:34 +0000 | [diff] [blame] | 249 | |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 250 | |
| 251 | /** |
| 252 | * Get the generated query string |
| 253 | */ |
Nils Diewald | d599d54 | 2015-01-08 20:41:34 +0000 | [diff] [blame] | 254 | toQuery : function () { |
| 255 | return this._root.toQuery(); |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 256 | } |
| 257 | }; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 258 | }); |