|
| 1 | +// Custom events for use with the native CustomEvent API |
| 2 | +const DATASETTE_EVENTS = { |
| 3 | + INIT: "datasette_init", // returns datasette manager instance in evt.detail |
| 4 | +}; |
| 5 | + |
| 6 | +// Datasette "core" -> Methods/APIs that are foundational |
| 7 | +// Plugins will have greater stability if they use the functional hooks- but if they do decide to hook into |
| 8 | +// literal DOM selectors, they'll have an easier time using these addresses. |
| 9 | +const DOM_SELECTORS = { |
| 10 | + /** Should have one match */ |
| 11 | + jsonExportLink: ".export-links a[href*=json]", |
| 12 | + |
| 13 | + /** Event listeners that go outside of the main table, e.g. existing scroll listener */ |
| 14 | + tableWrapper: ".table-wrapper", |
| 15 | + table: "table.rows-and-columns", |
| 16 | + aboveTablePanel: ".above-table-panel", |
| 17 | + |
| 18 | + // These could have multiple matches |
| 19 | + /** Used for selecting table headers. Use makeColumnActions if you want to add menu items. */ |
| 20 | + tableHeaders: `table.rows-and-columns th`, |
| 21 | + |
| 22 | + /** Used to add "where" clauses to query using direct manipulation */ |
| 23 | + filterRows: ".filter-row", |
| 24 | + /** Used to show top available enum values for a column ("facets") */ |
| 25 | + facetResults: ".facet-results [data-column]", |
| 26 | +}; |
| 27 | + |
| 28 | +/** |
| 29 | + * Monolith class for interacting with Datasette JS API |
| 30 | + * Imported with DEFER, runs after main document parsed |
| 31 | + * For now, manually synced with datasette/version.py |
| 32 | + */ |
| 33 | +const datasetteManager = { |
| 34 | + VERSION: window.datasetteVersion, |
| 35 | + |
| 36 | + // TODO: Should order of registration matter more? |
| 37 | + |
| 38 | + // Should plugins be allowed to clobber others or is it last-in takes priority? |
| 39 | + // Does pluginMetadata need to be serializable, or can we let it be stateful / have functions? |
| 40 | + plugins: new Map(), |
| 41 | + |
| 42 | + registerPlugin: (name, pluginMetadata) => { |
| 43 | + if (datasetteManager.plugins.has(name)) { |
| 44 | + console.warn(`Warning -> plugin ${name} was redefined`); |
| 45 | + } |
| 46 | + datasetteManager.plugins.set(name, pluginMetadata); |
| 47 | + |
| 48 | + // If the plugin participates in the panel... update the panel. |
| 49 | + if (pluginMetadata.makeAboveTablePanelConfigs) { |
| 50 | + datasetteManager.renderAboveTablePanel(); |
| 51 | + } |
| 52 | + }, |
| 53 | + |
| 54 | + /** |
| 55 | + * New DOM elements are created on each click, so the data is not stale. |
| 56 | + * |
| 57 | + * Items |
| 58 | + * - must provide label (text) |
| 59 | + * - might provide href (string) or an onclick ((evt) => void) |
| 60 | + * |
| 61 | + * columnMeta is metadata stored on the column header (TH) as a DOMStringMap |
| 62 | + * - column: string |
| 63 | + * - columnNotNull: boolean |
| 64 | + * - columnType: sqlite datatype enum (text, number, etc) |
| 65 | + * - isPk: boolean |
| 66 | + */ |
| 67 | + makeColumnActions: (columnMeta) => { |
| 68 | + let columnActions = []; |
| 69 | + |
| 70 | + // Accept function that returns list of columnActions with keys |
| 71 | + // Required: label (text) |
| 72 | + // Optional: onClick or href |
| 73 | + datasetteManager.plugins.forEach((plugin) => { |
| 74 | + if (plugin.makeColumnActions) { |
| 75 | + // Plugins can provide multiple columnActions if they want |
| 76 | + // If multiple try to create entry with same label, the last one deletes the others |
| 77 | + columnActions.push(...plugin.makeColumnActions(columnMeta)); |
| 78 | + } |
| 79 | + }); |
| 80 | + |
| 81 | + // TODO: Validate columnAction configs and give informative error message if missing keys. |
| 82 | + return columnActions; |
| 83 | + }, |
| 84 | + |
| 85 | + /** |
| 86 | + * In MVP, each plugin can only have 1 instance. |
| 87 | + * In future, panels could be repeated. We omit that for now since so many plugins depend on |
| 88 | + * shared URL state, so having multiple instances of plugin at same time is problematic. |
| 89 | + * Currently, we never destroy any panels, we just hide them. |
| 90 | + * |
| 91 | + * TODO: nicer panel css, show panel selection state. |
| 92 | + * TODO: does this hook need to take any arguments? |
| 93 | + */ |
| 94 | + renderAboveTablePanel: () => { |
| 95 | + const aboveTablePanel = document.querySelector( |
| 96 | + DOM_SELECTORS.aboveTablePanel |
| 97 | + ); |
| 98 | + |
| 99 | + if (!aboveTablePanel) { |
| 100 | + console.warn( |
| 101 | + "This page does not have a table, the renderAboveTablePanel cannot be used." |
| 102 | + ); |
| 103 | + return; |
| 104 | + } |
| 105 | + |
| 106 | + let aboveTablePanelWrapper = aboveTablePanel.querySelector(".panels"); |
| 107 | + |
| 108 | + // First render: create wrappers. Otherwise, reuse previous. |
| 109 | + if (!aboveTablePanelWrapper) { |
| 110 | + aboveTablePanelWrapper = document.createElement("div"); |
| 111 | + aboveTablePanelWrapper.classList.add("tab-contents"); |
| 112 | + const panelNav = document.createElement("div"); |
| 113 | + panelNav.classList.add("tab-controls"); |
| 114 | + |
| 115 | + // Temporary: css for minimal amount of breathing room. |
| 116 | + panelNav.style.display = "flex"; |
| 117 | + panelNav.style.gap = "8px"; |
| 118 | + panelNav.style.marginTop = "4px"; |
| 119 | + panelNav.style.marginBottom = "20px"; |
| 120 | + |
| 121 | + aboveTablePanel.appendChild(panelNav); |
| 122 | + aboveTablePanel.appendChild(aboveTablePanelWrapper); |
| 123 | + } |
| 124 | + |
| 125 | + datasetteManager.plugins.forEach((plugin, pluginName) => { |
| 126 | + const { makeAboveTablePanelConfigs } = plugin; |
| 127 | + |
| 128 | + if (makeAboveTablePanelConfigs) { |
| 129 | + const controls = aboveTablePanel.querySelector(".tab-controls"); |
| 130 | + const contents = aboveTablePanel.querySelector(".tab-contents"); |
| 131 | + |
| 132 | + // Each plugin can make multiple panels |
| 133 | + const configs = makeAboveTablePanelConfigs(); |
| 134 | + |
| 135 | + configs.forEach((config, i) => { |
| 136 | + const nodeContentId = `${pluginName}_${config.id}_panel-content`; |
| 137 | + |
| 138 | + // quit if we've already registered this plugin |
| 139 | + // TODO: look into whether plugins should be allowed to ask |
| 140 | + // parent to re-render, or if they should manage that internally. |
| 141 | + if (document.getElementById(nodeContentId)) { |
| 142 | + return; |
| 143 | + } |
| 144 | + |
| 145 | + // Add tab control button |
| 146 | + const pluginControl = document.createElement("button"); |
| 147 | + pluginControl.textContent = config.label; |
| 148 | + pluginControl.onclick = () => { |
| 149 | + contents.childNodes.forEach((node) => { |
| 150 | + if (node.id === nodeContentId) { |
| 151 | + node.style.display = "block"; |
| 152 | + } else { |
| 153 | + node.style.display = "none"; |
| 154 | + } |
| 155 | + }); |
| 156 | + }; |
| 157 | + controls.appendChild(pluginControl); |
| 158 | + |
| 159 | + // Add plugin content area |
| 160 | + const pluginNode = document.createElement("div"); |
| 161 | + pluginNode.id = nodeContentId; |
| 162 | + config.render(pluginNode); |
| 163 | + pluginNode.style.display = "none"; // Default to hidden unless you're ifrst |
| 164 | + |
| 165 | + contents.appendChild(pluginNode); |
| 166 | + }); |
| 167 | + |
| 168 | + // Let first node be selected by default |
| 169 | + if (contents.childNodes.length) { |
| 170 | + contents.childNodes[0].style.display = "block"; |
| 171 | + } |
| 172 | + } |
| 173 | + }); |
| 174 | + }, |
| 175 | + |
| 176 | + /** Selectors for document (DOM) elements. Store identifier instead of immediate references in case they haven't loaded when Manager starts. */ |
| 177 | + selectors: DOM_SELECTORS, |
| 178 | + |
| 179 | + // Future API ideas |
| 180 | + // Fetch page's data in array, and cache so plugins could reuse it |
| 181 | + // Provide knowledge of what datasette JS or server-side via traditional console autocomplete |
| 182 | + // State helpers: URL params https://github.com/simonw/datasette/issues/1144 and localstorage |
| 183 | + // UI Hooks: command + k, tab manager hook |
| 184 | + // Should we notify plugins that have dependencies |
| 185 | + // when all dependencies were fulfilled? (leaflet, codemirror, etc) |
| 186 | + // https://github.com/simonw/datasette-leaflet -> this way |
| 187 | + // multiple plugins can all request the same copy of leaflet. |
| 188 | +}; |
| 189 | + |
| 190 | +const initializeDatasette = () => { |
| 191 | + // Hide the global behind __ prefix. Ideally they should be listening for the |
| 192 | + // DATASETTE_EVENTS.INIT event to avoid the habit of reading from the window. |
| 193 | + |
| 194 | + window.__DATASETTE__ = datasetteManager; |
| 195 | + console.debug("Datasette Manager Created!"); |
| 196 | + |
| 197 | + const initDatasetteEvent = new CustomEvent(DATASETTE_EVENTS.INIT, { |
| 198 | + detail: datasetteManager, |
| 199 | + }); |
| 200 | + |
| 201 | + document.dispatchEvent(initDatasetteEvent); |
| 202 | +}; |
| 203 | + |
| 204 | +/** |
| 205 | + * Main function |
| 206 | + * Fires AFTER the document has been parsed |
| 207 | + */ |
| 208 | +document.addEventListener("DOMContentLoaded", function () { |
| 209 | + initializeDatasette(); |
| 210 | +}); |
0 commit comments