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 | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 141 | /** |
| 142 | * Create and render a new virtual collection |
| 143 | * based on a KoralQuery collection document |
| 144 | */ |
Nils Diewald | 6283d69 | 2015-04-23 20:32:53 +0000 | [diff] [blame] | 145 | fromJson : function (json) { |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 146 | |
| 147 | if (json !== undefined) { |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 148 | // Parse root document |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 149 | if (json['@type'] == 'koral:doc') { |
Nils Diewald | 6283d69 | 2015-04-23 20:32:53 +0000 | [diff] [blame] | 150 | this._root = docClass.create(this, json); |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 151 | } |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 152 | // parse root group |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 153 | else if (json['@type'] == 'koral:docGroup') { |
Nils Diewald | 6283d69 | 2015-04-23 20:32:53 +0000 | [diff] [blame] | 154 | this._root = docGroupClass.create(this, json); |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 155 | } |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 156 | // Unknown collection type |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 157 | else { |
| 158 | KorAP.log(813, "Collection type is not supported"); |
| 159 | return; |
| 160 | }; |
| 161 | } |
| 162 | |
| 163 | else { |
| 164 | // Add unspecified object |
Nils Diewald | 6283d69 | 2015-04-23 20:32:53 +0000 | [diff] [blame] | 165 | this._root = unspecDocClass.create(this); |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 166 | }; |
| 167 | |
Nils Diewald | 8e7182e | 2015-01-08 15:02:07 +0000 | [diff] [blame] | 168 | // Init element and update |
Nils Diewald | 6283d69 | 2015-04-23 20:32:53 +0000 | [diff] [blame] | 169 | this.update(); |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 170 | |
Nils Diewald | 6283d69 | 2015-04-23 20:32:53 +0000 | [diff] [blame] | 171 | return this; |
| 172 | }, |
| 173 | |
| 174 | clean : function () { |
| 175 | if (this._root.ldType() !== "non") { |
| 176 | this._root.destroy(); |
| 177 | this.root(unspecDocClass.create(this)); |
| 178 | }; |
| 179 | return this; |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 180 | }, |
Nils Diewald | d599d54 | 2015-01-08 20:41:34 +0000 | [diff] [blame] | 181 | |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 182 | /** |
| 183 | * Get or set the root object of the |
| 184 | * virtual collection. |
| 185 | */ |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 186 | root : function (obj) { |
Nils Diewald | 8e7182e | 2015-01-08 15:02:07 +0000 | [diff] [blame] | 187 | if (arguments.length === 1) { |
Nils Diewald | 8f6b610 | 2015-01-08 18:25:33 +0000 | [diff] [blame] | 188 | var e = this.element(); |
| 189 | if (e.firstChild !== null) { |
Nils Diewald | d5070b0 | 2015-01-11 01:44:47 +0000 | [diff] [blame] | 190 | if (e.firstChild !== obj.element()) { |
Nils Diewald | 8f6b610 | 2015-01-08 18:25:33 +0000 | [diff] [blame] | 191 | e.replaceChild(obj.element(), e.firstChild); |
Nils Diewald | d5070b0 | 2015-01-11 01:44:47 +0000 | [diff] [blame] | 192 | }; |
Nils Diewald | 8f6b610 | 2015-01-08 18:25:33 +0000 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | // Append root element |
| 196 | else { |
| 197 | e.appendChild(obj.element()); |
| 198 | }; |
| 199 | |
| 200 | // Update parent child relations |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 201 | this._root = obj; |
Nils Diewald | 8f6b610 | 2015-01-08 18:25:33 +0000 | [diff] [blame] | 202 | obj.parent(this); |
| 203 | |
Nils Diewald | 8e7182e | 2015-01-08 15:02:07 +0000 | [diff] [blame] | 204 | this.update(); |
| 205 | }; |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 206 | return this._root; |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 207 | }, |
Nils Diewald | 8f6b610 | 2015-01-08 18:25:33 +0000 | [diff] [blame] | 208 | |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 209 | /** |
| 210 | * Get the element associated with the virtual collection |
| 211 | */ |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 212 | element : function () { |
| 213 | if (this._element !== undefined) |
| 214 | return this._element; |
| 215 | |
| 216 | this._element = document.createElement('div'); |
| 217 | this._element.setAttribute('class', 'vc'); |
Nils Diewald | 8e7182e | 2015-01-08 15:02:07 +0000 | [diff] [blame] | 218 | |
Nils Diewald | 8f6b610 | 2015-01-08 18:25:33 +0000 | [diff] [blame] | 219 | // Initialize root |
| 220 | this._element.appendChild(this._root.element()); |
| 221 | |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 222 | return this._element; |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 223 | }, |
Nils Diewald | d599d54 | 2015-01-08 20:41:34 +0000 | [diff] [blame] | 224 | |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 225 | |
| 226 | /** |
| 227 | * Update the whole object based on the underlying |
| 228 | * data structure |
| 229 | */ |
Nils Diewald | d599d54 | 2015-01-08 20:41:34 +0000 | [diff] [blame] | 230 | update : function () { |
| 231 | this._root.update(); |
| 232 | return this; |
| 233 | }, |
| 234 | |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 235 | |
| 236 | /** |
| 237 | * Get the generated json string |
| 238 | */ |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 239 | toJson : function () { |
| 240 | return this._root.toJson(); |
| 241 | }, |
Nils Diewald | d599d54 | 2015-01-08 20:41:34 +0000 | [diff] [blame] | 242 | |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 243 | |
| 244 | /** |
| 245 | * Get the generated query string |
| 246 | */ |
Nils Diewald | d599d54 | 2015-01-08 20:41:34 +0000 | [diff] [blame] | 247 | toQuery : function () { |
| 248 | return this._root.toQuery(); |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 249 | } |
| 250 | }; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 251 | }); |