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 | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 50 | 'util' |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 51 | ], function (unspecDocClass, docClass, docGroupClass, menuClass) { |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 52 | "use strict"; |
| 53 | |
Nils Diewald | 359a72c | 2015-04-20 17:40:29 +0000 | [diff] [blame] | 54 | // ??? |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 55 | KorAP._validStringMatchRE = new RegExp("^(?:eq|ne|contains|excludes)$"); |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 56 | KorAP._validDateMatchRE = new RegExp("^[lg]?eq$"); |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 57 | KorAP._validDateRE = new RegExp("^(?:\\d{4})(?:-\\d\\d(?:-\\d\\d)?)?$"); |
Nils Diewald | 359a72c | 2015-04-20 17:40:29 +0000 | [diff] [blame] | 58 | KorAP._overrideStyles = false; |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 59 | |
Nils Diewald | 359a72c | 2015-04-20 17:40:29 +0000 | [diff] [blame] | 60 | var loc = KorAP.Locale; |
Nils Diewald | d599d54 | 2015-01-08 20:41:34 +0000 | [diff] [blame] | 61 | |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 62 | KorAP._vcKeyMenu = undefined; |
| 63 | |
Nils Diewald | d599d54 | 2015-01-08 20:41:34 +0000 | [diff] [blame] | 64 | /** |
| 65 | * Virtual Collection |
| 66 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 67 | return { |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 68 | |
| 69 | /** |
| 70 | * The JSON-LD type of the virtual collection |
| 71 | */ |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 72 | ldType : function () { |
| 73 | return null; |
| 74 | }, |
Nils Diewald | d599d54 | 2015-01-08 20:41:34 +0000 | [diff] [blame] | 75 | |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 76 | // Initialize virtual collection |
| 77 | _init : function (keyList) { |
| 78 | |
| 79 | // Inject localized css styles |
Nils Diewald | 359a72c | 2015-04-20 17:40:29 +0000 | [diff] [blame] | 80 | if (!KorAP._overrideStyles) { |
| 81 | var sheet = KorAP.newStyleSheet(); |
| 82 | |
| 83 | // Add css rule for OR operations |
| 84 | sheet.insertRule( |
| 85 | '.vc .docGroup[data-operation=or] > .doc::before,' + |
| 86 | '.vc .docGroup[data-operation=or] > .docGroup::before ' + |
| 87 | '{ content: "' + loc.OR + '" }', |
| 88 | 0 |
| 89 | ); |
| 90 | |
| 91 | // Add css rule for AND operations |
| 92 | sheet.insertRule( |
| 93 | '.vc .docGroup[data-operation=and] > .doc::before,' + |
| 94 | '.vc .docGroup[data-operation=and] > .docGroup::before ' + |
| 95 | '{ content: "' + loc.AND + '" }', |
| 96 | 1 |
| 97 | ); |
| 98 | |
Nils Diewald | 359a72c | 2015-04-20 17:40:29 +0000 | [diff] [blame] | 99 | KorAP._overrideStyles = true; |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 100 | |
| 101 | // Create key menu |
| 102 | KorAP._vcKeyMenu = menuClass.create(keyList); |
Nils Diewald | 4c22125 | 2015-04-21 20:19:25 +0000 | [diff] [blame] | 103 | KorAP._vcKeyMenu.limit(6); |
| 104 | |
| 105 | // Create match menus .... |
| 106 | KorAP._vcMatchopMenu = { |
| 107 | 'string' : menuClass.create([ |
| 108 | ['eq', null], |
| 109 | ['ne', null], |
| 110 | ['contains', null], |
| 111 | ['excludes', null] |
| 112 | ]), |
| 113 | 'date' : menuClass.create([ |
| 114 | ['eq', null], |
| 115 | ['geq', null], |
| 116 | ['leq', null] |
| 117 | ]), |
| 118 | 'regex' : menuClass.create([ |
| 119 | ['eq', null], |
| 120 | ['ne', null], |
| 121 | ['contains', null], |
| 122 | ['excludes', null] |
| 123 | ]) |
| 124 | }; |
Nils Diewald | 359a72c | 2015-04-20 17:40:29 +0000 | [diff] [blame] | 125 | }; |
| 126 | |
| 127 | return this; |
| 128 | }, |
| 129 | |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 130 | /** |
| 131 | * Create a new virtual collection. |
| 132 | */ |
| 133 | create : function (keyList) { |
Nils Diewald | 6283d69 | 2015-04-23 20:32:53 +0000 | [diff] [blame^] | 134 | var obj = Object.create(this)._init(keyList); |
| 135 | obj._root = unspecDocClass.create(obj); |
| 136 | return obj; |
Nils Diewald | 4019bd2 | 2015-01-08 19:57:50 +0000 | [diff] [blame] | 137 | }, |
Nils Diewald | d599d54 | 2015-01-08 20:41:34 +0000 | [diff] [blame] | 138 | |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 139 | /** |
| 140 | * Create and render a new virtual collection |
| 141 | * based on a KoralQuery collection document |
| 142 | */ |
Nils Diewald | 6283d69 | 2015-04-23 20:32:53 +0000 | [diff] [blame^] | 143 | fromJson : function (json) { |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 144 | |
| 145 | if (json !== undefined) { |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 146 | // Parse root document |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 147 | if (json['@type'] == 'koral:doc') { |
Nils Diewald | 6283d69 | 2015-04-23 20:32:53 +0000 | [diff] [blame^] | 148 | this._root = docClass.create(this, json); |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 149 | } |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 150 | // parse root group |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 151 | else if (json['@type'] == 'koral:docGroup') { |
Nils Diewald | 6283d69 | 2015-04-23 20:32:53 +0000 | [diff] [blame^] | 152 | this._root = docGroupClass.create(this, json); |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 153 | } |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 154 | // Unknown collection type |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 155 | else { |
| 156 | KorAP.log(813, "Collection type is not supported"); |
| 157 | return; |
| 158 | }; |
| 159 | } |
| 160 | |
| 161 | else { |
| 162 | // Add unspecified object |
Nils Diewald | 6283d69 | 2015-04-23 20:32:53 +0000 | [diff] [blame^] | 163 | this._root = unspecDocClass.create(this); |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 164 | }; |
| 165 | |
Nils Diewald | 8e7182e | 2015-01-08 15:02:07 +0000 | [diff] [blame] | 166 | // Init element and update |
Nils Diewald | 6283d69 | 2015-04-23 20:32:53 +0000 | [diff] [blame^] | 167 | this.update(); |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 168 | |
Nils Diewald | 6283d69 | 2015-04-23 20:32:53 +0000 | [diff] [blame^] | 169 | return this; |
| 170 | }, |
| 171 | |
| 172 | clean : function () { |
| 173 | if (this._root.ldType() !== "non") { |
| 174 | this._root.destroy(); |
| 175 | this.root(unspecDocClass.create(this)); |
| 176 | }; |
| 177 | return this; |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 178 | }, |
Nils Diewald | d599d54 | 2015-01-08 20:41:34 +0000 | [diff] [blame] | 179 | |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 180 | /** |
| 181 | * Get or set the root object of the |
| 182 | * virtual collection. |
| 183 | */ |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 184 | root : function (obj) { |
Nils Diewald | 8e7182e | 2015-01-08 15:02:07 +0000 | [diff] [blame] | 185 | if (arguments.length === 1) { |
Nils Diewald | 8f6b610 | 2015-01-08 18:25:33 +0000 | [diff] [blame] | 186 | var e = this.element(); |
| 187 | if (e.firstChild !== null) { |
Nils Diewald | d5070b0 | 2015-01-11 01:44:47 +0000 | [diff] [blame] | 188 | if (e.firstChild !== obj.element()) { |
Nils Diewald | 8f6b610 | 2015-01-08 18:25:33 +0000 | [diff] [blame] | 189 | e.replaceChild(obj.element(), e.firstChild); |
Nils Diewald | d5070b0 | 2015-01-11 01:44:47 +0000 | [diff] [blame] | 190 | }; |
Nils Diewald | 8f6b610 | 2015-01-08 18:25:33 +0000 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | // Append root element |
| 194 | else { |
| 195 | e.appendChild(obj.element()); |
| 196 | }; |
| 197 | |
| 198 | // Update parent child relations |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 199 | this._root = obj; |
Nils Diewald | 8f6b610 | 2015-01-08 18:25:33 +0000 | [diff] [blame] | 200 | obj.parent(this); |
| 201 | |
Nils Diewald | 8e7182e | 2015-01-08 15:02:07 +0000 | [diff] [blame] | 202 | this.update(); |
| 203 | }; |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 204 | return this._root; |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 205 | }, |
Nils Diewald | 8f6b610 | 2015-01-08 18:25:33 +0000 | [diff] [blame] | 206 | |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 207 | /** |
| 208 | * Get the element associated with the virtual collection |
| 209 | */ |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 210 | element : function () { |
| 211 | if (this._element !== undefined) |
| 212 | return this._element; |
| 213 | |
| 214 | this._element = document.createElement('div'); |
| 215 | this._element.setAttribute('class', 'vc'); |
Nils Diewald | 8e7182e | 2015-01-08 15:02:07 +0000 | [diff] [blame] | 216 | |
Nils Diewald | 8f6b610 | 2015-01-08 18:25:33 +0000 | [diff] [blame] | 217 | // Initialize root |
| 218 | this._element.appendChild(this._root.element()); |
| 219 | |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 220 | return this._element; |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 221 | }, |
Nils Diewald | d599d54 | 2015-01-08 20:41:34 +0000 | [diff] [blame] | 222 | |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 223 | |
| 224 | /** |
| 225 | * Update the whole object based on the underlying |
| 226 | * data structure |
| 227 | */ |
Nils Diewald | d599d54 | 2015-01-08 20:41:34 +0000 | [diff] [blame] | 228 | update : function () { |
| 229 | this._root.update(); |
| 230 | return this; |
| 231 | }, |
| 232 | |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 233 | |
| 234 | /** |
| 235 | * Get the generated json string |
| 236 | */ |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 237 | toJson : function () { |
| 238 | return this._root.toJson(); |
| 239 | }, |
Nils Diewald | d599d54 | 2015-01-08 20:41:34 +0000 | [diff] [blame] | 240 | |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame] | 241 | |
| 242 | /** |
| 243 | * Get the generated query string |
| 244 | */ |
Nils Diewald | d599d54 | 2015-01-08 20:41:34 +0000 | [diff] [blame] | 245 | toQuery : function () { |
| 246 | return this._root.toQuery(); |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 247 | } |
| 248 | }; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 249 | }); |