Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 1 | /** |
| 2 | * Create virtual collections with a visual user interface. |
| 3 | * |
| 4 | * @author Nils Diewald |
| 5 | */ |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 6 | /* |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame^] | 7 | * This replaces a previous version written by Mengfei Zhou |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 8 | */ |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 9 | |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 10 | /* |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 11 | TODO: Disable "and" or "or" in case it's followed |
| 12 | by an unspecified document |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame^] | 13 | TODO: Implement "persistence"-Option, |
Nils Diewald | 6e43ffd | 2015-03-25 18:55:39 +0000 | [diff] [blame] | 14 | injecting the current creation date stamp |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 15 | |
Nils Diewald | d599d54 | 2015-01-08 20:41:34 +0000 | [diff] [blame] | 16 | Error codes: |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 17 | 701: "JSON-LD group has no @type attribute" |
| 18 | 704: "Operation needs operand list" |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 19 | 802: "Match type is not supported by value type" |
| 20 | 804: "Unknown value type" |
| 21 | 805: "Value is invalid" |
| 22 | 806: "Value is not a valid date string" |
| 23 | 807: "Value is not a valid regular expression" |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 24 | 810: "Unknown document group operation" (like 711) |
| 25 | 811: "Document group expects operation" (like 703) |
| 26 | 812: "Operand not supported in document group" (like 744) |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 27 | 813: "Collection type is not supported" (like 713) |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 28 | 814: "Unknown rewrite operation" |
| 29 | 815: "Rewrite expects source" |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame^] | 30 | |
| 31 | Localization strings: |
| 32 | KorAP.Locale = { |
| 33 | EMPTY : '...', |
| 34 | AND : 'and', |
| 35 | OR : 'or', |
| 36 | DELETE : 'x' |
| 37 | } |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 38 | */ |
| 39 | |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 40 | define([ |
| 41 | 'vc/unspecified', |
| 42 | 'vc/doc', |
| 43 | 'vc/docgroup', |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame^] | 44 | 'vc/menu', |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 45 | 'util' |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame^] | 46 | ], function (unspecDocClass, docClass, docGroupClass, menuClass) { |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 47 | "use strict"; |
| 48 | |
Nils Diewald | 359a72c | 2015-04-20 17:40:29 +0000 | [diff] [blame] | 49 | // ??? |
Nils Diewald | 86dad5b | 2015-01-28 15:09:07 +0000 | [diff] [blame] | 50 | KorAP._validStringMatchRE = new RegExp("^(?:eq|ne|contains|excludes)$"); |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 51 | KorAP._validDateMatchRE = new RegExp("^[lg]?eq$"); |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 52 | KorAP._validDateRE = new RegExp("^(?:\\d{4})(?:-\\d\\d(?:-\\d\\d)?)?$"); |
Nils Diewald | 359a72c | 2015-04-20 17:40:29 +0000 | [diff] [blame] | 53 | KorAP._overrideStyles = false; |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 54 | |
Nils Diewald | 359a72c | 2015-04-20 17:40:29 +0000 | [diff] [blame] | 55 | var loc = KorAP.Locale; |
Nils Diewald | d599d54 | 2015-01-08 20:41:34 +0000 | [diff] [blame] | 56 | |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame^] | 57 | KorAP._vcKeyMenu = undefined; |
| 58 | |
Nils Diewald | d599d54 | 2015-01-08 20:41:34 +0000 | [diff] [blame] | 59 | /** |
| 60 | * Virtual Collection |
| 61 | */ |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 62 | return { |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame^] | 63 | |
| 64 | /** |
| 65 | * The JSON-LD type of the virtual collection |
| 66 | */ |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 67 | ldType : function () { |
| 68 | return null; |
| 69 | }, |
Nils Diewald | d599d54 | 2015-01-08 20:41:34 +0000 | [diff] [blame] | 70 | |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame^] | 71 | // Initialize virtual collection |
| 72 | _init : function (keyList) { |
| 73 | |
| 74 | // Inject localized css styles |
Nils Diewald | 359a72c | 2015-04-20 17:40:29 +0000 | [diff] [blame] | 75 | if (!KorAP._overrideStyles) { |
| 76 | var sheet = KorAP.newStyleSheet(); |
| 77 | |
| 78 | // Add css rule for OR operations |
| 79 | sheet.insertRule( |
| 80 | '.vc .docGroup[data-operation=or] > .doc::before,' + |
| 81 | '.vc .docGroup[data-operation=or] > .docGroup::before ' + |
| 82 | '{ content: "' + loc.OR + '" }', |
| 83 | 0 |
| 84 | ); |
| 85 | |
| 86 | // Add css rule for AND operations |
| 87 | sheet.insertRule( |
| 88 | '.vc .docGroup[data-operation=and] > .doc::before,' + |
| 89 | '.vc .docGroup[data-operation=and] > .docGroup::before ' + |
| 90 | '{ content: "' + loc.AND + '" }', |
| 91 | 1 |
| 92 | ); |
| 93 | |
Nils Diewald | 359a72c | 2015-04-20 17:40:29 +0000 | [diff] [blame] | 94 | KorAP._overrideStyles = true; |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame^] | 95 | |
| 96 | // Create key menu |
| 97 | KorAP._vcKeyMenu = menuClass.create(keyList); |
| 98 | KorAP._vcKeyMenu.limit(5); |
Nils Diewald | 359a72c | 2015-04-20 17:40:29 +0000 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | return this; |
| 102 | }, |
| 103 | |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame^] | 104 | /** |
| 105 | * Create a new virtual collection. |
| 106 | */ |
| 107 | create : function (keyList) { |
| 108 | return Object.create(this)._init(keyList); |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 109 | }, |
Nils Diewald | d599d54 | 2015-01-08 20:41:34 +0000 | [diff] [blame] | 110 | |
Nils Diewald | 4019bd2 | 2015-01-08 19:57:50 +0000 | [diff] [blame] | 111 | clean : function () { |
| 112 | if (this._root.ldType() !== "non") { |
| 113 | this._root.destroy(); |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 114 | this.root(unspecDocClass.create(this)); |
Nils Diewald | 4019bd2 | 2015-01-08 19:57:50 +0000 | [diff] [blame] | 115 | }; |
| 116 | return this; |
| 117 | }, |
Nils Diewald | d599d54 | 2015-01-08 20:41:34 +0000 | [diff] [blame] | 118 | |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame^] | 119 | /** |
| 120 | * Create and render a new virtual collection |
| 121 | * based on a KoralQuery collection document |
| 122 | */ |
| 123 | render : function (json, keyList) { |
| 124 | var obj = Object.create(this)._init(keyList); |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 125 | |
| 126 | if (json !== undefined) { |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame^] | 127 | // Parse root document |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 128 | if (json['@type'] == 'koral:doc') { |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 129 | obj._root = docClass.create(obj, json); |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 130 | } |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame^] | 131 | // parse root group |
Nils Diewald | 2fe12e1 | 2015-03-06 16:47:06 +0000 | [diff] [blame] | 132 | else if (json['@type'] == 'koral:docGroup') { |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 133 | obj._root = docGroupClass.create(obj, json); |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 134 | } |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame^] | 135 | // Unknown collection type |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 136 | else { |
| 137 | KorAP.log(813, "Collection type is not supported"); |
| 138 | return; |
| 139 | }; |
| 140 | } |
| 141 | |
| 142 | else { |
| 143 | // Add unspecified object |
Nils Diewald | 7c8ced2 | 2015-04-15 19:21:00 +0000 | [diff] [blame] | 144 | obj._root = unspecDocClass.create(obj); |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 145 | }; |
| 146 | |
Nils Diewald | 8e7182e | 2015-01-08 15:02:07 +0000 | [diff] [blame] | 147 | // Init element and update |
| 148 | obj.update(); |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 149 | |
| 150 | return obj; |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 151 | }, |
Nils Diewald | d599d54 | 2015-01-08 20:41:34 +0000 | [diff] [blame] | 152 | |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame^] | 153 | /** |
| 154 | * Get or set the root object of the |
| 155 | * virtual collection. |
| 156 | */ |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 157 | root : function (obj) { |
Nils Diewald | 8e7182e | 2015-01-08 15:02:07 +0000 | [diff] [blame] | 158 | if (arguments.length === 1) { |
Nils Diewald | 8f6b610 | 2015-01-08 18:25:33 +0000 | [diff] [blame] | 159 | var e = this.element(); |
| 160 | if (e.firstChild !== null) { |
Nils Diewald | d5070b0 | 2015-01-11 01:44:47 +0000 | [diff] [blame] | 161 | if (e.firstChild !== obj.element()) { |
Nils Diewald | 8f6b610 | 2015-01-08 18:25:33 +0000 | [diff] [blame] | 162 | e.replaceChild(obj.element(), e.firstChild); |
Nils Diewald | d5070b0 | 2015-01-11 01:44:47 +0000 | [diff] [blame] | 163 | }; |
Nils Diewald | 8f6b610 | 2015-01-08 18:25:33 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | // Append root element |
| 167 | else { |
| 168 | e.appendChild(obj.element()); |
| 169 | }; |
| 170 | |
| 171 | // Update parent child relations |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 172 | this._root = obj; |
Nils Diewald | 8f6b610 | 2015-01-08 18:25:33 +0000 | [diff] [blame] | 173 | obj.parent(this); |
| 174 | |
Nils Diewald | 8e7182e | 2015-01-08 15:02:07 +0000 | [diff] [blame] | 175 | this.update(); |
| 176 | }; |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 177 | return this._root; |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 178 | }, |
Nils Diewald | 8f6b610 | 2015-01-08 18:25:33 +0000 | [diff] [blame] | 179 | |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame^] | 180 | /** |
| 181 | * Get the element associated with the virtual collection |
| 182 | */ |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 183 | element : function () { |
| 184 | if (this._element !== undefined) |
| 185 | return this._element; |
| 186 | |
| 187 | this._element = document.createElement('div'); |
| 188 | this._element.setAttribute('class', 'vc'); |
Nils Diewald | 8e7182e | 2015-01-08 15:02:07 +0000 | [diff] [blame] | 189 | |
Nils Diewald | 8f6b610 | 2015-01-08 18:25:33 +0000 | [diff] [blame] | 190 | // Initialize root |
| 191 | this._element.appendChild(this._root.element()); |
| 192 | |
Nils Diewald | d077049 | 2014-12-19 03:55:00 +0000 | [diff] [blame] | 193 | return this._element; |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 194 | }, |
Nils Diewald | d599d54 | 2015-01-08 20:41:34 +0000 | [diff] [blame] | 195 | |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame^] | 196 | |
| 197 | /** |
| 198 | * Update the whole object based on the underlying |
| 199 | * data structure |
| 200 | */ |
Nils Diewald | d599d54 | 2015-01-08 20:41:34 +0000 | [diff] [blame] | 201 | update : function () { |
| 202 | this._root.update(); |
| 203 | return this; |
| 204 | }, |
| 205 | |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame^] | 206 | |
| 207 | /** |
| 208 | * Get the generated json string |
| 209 | */ |
Nils Diewald | f219eb8 | 2015-01-07 20:15:42 +0000 | [diff] [blame] | 210 | toJson : function () { |
| 211 | return this._root.toJson(); |
| 212 | }, |
Nils Diewald | d599d54 | 2015-01-08 20:41:34 +0000 | [diff] [blame] | 213 | |
Nils Diewald | 1fcb2ad | 2015-04-20 19:19:18 +0000 | [diff] [blame^] | 214 | |
| 215 | /** |
| 216 | * Get the generated query string |
| 217 | */ |
Nils Diewald | d599d54 | 2015-01-08 20:41:34 +0000 | [diff] [blame] | 218 | toQuery : function () { |
| 219 | return this._root.toQuery(); |
Nils Diewald | 3a2d802 | 2014-12-16 02:45:41 +0000 | [diff] [blame] | 220 | } |
| 221 | }; |
Nils Diewald | 0e6992a | 2015-04-14 20:13:52 +0000 | [diff] [blame] | 222 | }); |