-
Notifications
You must be signed in to change notification settings - Fork 8
server/tailsql: add history and saved queries #44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+213
−8
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,27 @@ | ||
| import { Params, Area, Cycle, Loop } from './sprite.js'; | ||
|
|
||
| (() => { | ||
| const query = document.getElementById('query'); | ||
| const qButton = document.getElementById('send-query'); | ||
| const dlButton = document.getElementById("dl-button"); | ||
| const qform = document.getElementById('qform'); | ||
| const output = document.getElementById('output'); | ||
| const base = document.location.origin + document.location.pathname; | ||
| const sources = document.getElementById('sources'); | ||
| const body = document.getElementById('tsql'); | ||
| const query = document.getElementById('query'); | ||
| const qButton = document.getElementById('send-query'); | ||
| const dlButton = document.getElementById("dl-button"); | ||
| const saveButton = document.getElementById('save-query'); | ||
| const qform = document.getElementById('qform'); | ||
| const output = document.getElementById('output'); | ||
| const base = document.location.origin + document.location.pathname; | ||
| const sources = document.getElementById('sources'); | ||
| const body = document.getElementById('tsql'); | ||
| const historyList = document.getElementById('history-list'); | ||
| const clearHistory = document.getElementById('clear-history'); | ||
| const savedList = document.getElementById('saved-query-list'); | ||
|
|
||
| const nuts = /a?corn|\bnut\b|seed|squirrel|tailsql/i; | ||
| const velo = 5, delay = 60, runChance = 0.03; | ||
| let hasRun = false; | ||
|
|
||
| const LOCALSTORAGE_KEY_HISTORY = 'tailsql-history'; | ||
| const LOCALSTORAGE_KEY_SAVED = 'tailsql-saved'; | ||
| const MAX_HISTORY_ENTRIES = 20; | ||
|
|
||
| const param = new Params(256, 256, 8, 8); | ||
| const aRunRight = new Loop(velo, 0, 5, [5,1,2,3]); | ||
| const aRunLeft = new Loop(-velo, 0, 6, [5,1,2,3]); | ||
|
|
@@ -105,10 +113,131 @@ import { Params, Area, Cycle, Loop } from './sprite.js'; | |
| performDownload('query.csv', href); | ||
| }); | ||
|
|
||
| function getHistory() { | ||
| try { return JSON.parse(localStorage.getItem(LOCALSTORAGE_KEY_HISTORY)) || []; } | ||
| catch { return []; } | ||
| } | ||
|
|
||
| function setHistory(h) { | ||
| localStorage.setItem(LOCALSTORAGE_KEY_HISTORY, JSON.stringify(h)); | ||
| } | ||
|
|
||
| function getSavedQueries() { | ||
| try { return JSON.parse(localStorage.getItem(LOCALSTORAGE_KEY_SAVED)) || []; } | ||
| catch { return []; } | ||
| } | ||
|
|
||
| function setSavedQueries(s) { | ||
| localStorage.setItem(LOCALSTORAGE_KEY_SAVED, JSON.stringify(s)); | ||
| } | ||
|
|
||
| function renderHistory() { | ||
| historyList.innerHTML = ''; | ||
| const h = getHistory(); | ||
| for (const entry of h) { | ||
| const li = document.createElement('li'); | ||
| const span = document.createElement('span'); | ||
| span.className = 'query-text'; | ||
| span.textContent = entry.query; | ||
| span.title = entry.query; | ||
| li.appendChild(span); | ||
| li.addEventListener('click', () => { | ||
| query.value = entry.query; | ||
| if (sources && entry.source) { | ||
| for (const opt of sources.options) { | ||
| if (opt.value === entry.source) { | ||
| opt.selected = true; | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| query.focus(); | ||
| }); | ||
| historyList.appendChild(li); | ||
| } | ||
| } | ||
|
|
||
| function renderSavedQueries() { | ||
| savedList.innerHTML = ''; | ||
| const s = getSavedQueries(); | ||
| for (let i = 0; i < s.length; i++) { | ||
| const entry = s[i]; | ||
| const li = document.createElement('li'); | ||
| const name = document.createElement('span'); | ||
| name.className = 'query-name'; | ||
| name.textContent = entry.name + ':'; | ||
| const span = document.createElement('span'); | ||
| span.className = 'query-text'; | ||
| span.textContent = entry.query; | ||
| span.title = entry.query; | ||
| const del = document.createElement('button'); | ||
| del.className = 'delete-btn'; | ||
| del.textContent = '\u00d7'; | ||
| del.title = 'Delete saved query'; | ||
| del.addEventListener('click', (evt) => { | ||
| evt.stopPropagation(); | ||
| const cur = getSavedQueries(); | ||
| cur.splice(i, 1); | ||
| setSavedQueries(cur); | ||
| renderSavedQueries(); | ||
| }); | ||
| li.appendChild(name); | ||
| li.appendChild(span); | ||
| li.appendChild(del); | ||
| li.addEventListener('click', () => { | ||
| query.value = entry.query; | ||
| query.focus(); | ||
| }); | ||
| savedList.appendChild(li); | ||
| } | ||
| } | ||
|
|
||
| function recordHistory() { | ||
| const errorDiv = document.getElementById('error'); | ||
| const q = query.value.trim(); | ||
| if (!q || errorDiv || !output) return; | ||
|
|
||
| const h = getHistory().filter(e => e.query !== q); | ||
| h.unshift({ query: q, source: sources.value, ts: Date.now() }); | ||
| if (h.length > MAX_HISTORY_ENTRIES) h.length = MAX_HISTORY_ENTRIES; | ||
| setHistory(h); | ||
| } | ||
|
|
||
| saveButton.addEventListener('click', (evt) => { | ||
| evt.preventDefault(); | ||
| const q = query.value.trim(); | ||
| if (!q) return; | ||
| const name = prompt('Name for this query:'); | ||
| if (!name) return; | ||
| const s = getSavedQueries(); | ||
| s.push({ query: q, name: name.trim() }); | ||
| setSavedQueries(s); | ||
| renderSavedQueries(); | ||
| }); | ||
|
|
||
| clearHistory.addEventListener('click', (evt) => { | ||
| evt.stopPropagation(); | ||
| localStorage.removeItem(LOCALSTORAGE_KEY_HISTORY); | ||
| renderHistory(); | ||
| }); | ||
|
|
||
| // Disable the download button when there are no query results. | ||
| window.addEventListener("load", disableIfNoOutput(dlButton)); | ||
| // Initially focus the query input. | ||
| window.addEventListener("load", (evt) => { query.focus(); }); | ||
| // Refresh when the input source changes. | ||
| sources.addEventListener('change', (evt) => { qform.submit() }); | ||
|
|
||
| // Persist open/closed state of query panels across page reloads. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice |
||
| document.querySelectorAll('#query-panels details').forEach((d) => { | ||
| if (localStorage.getItem(d.id) === 'open') d.open = true; | ||
| d.addEventListener('toggle', () => { | ||
| if (d.open) localStorage.setItem(d.id, 'open'); | ||
| else localStorage.removeItem(d.id); | ||
| }); | ||
| }); | ||
|
|
||
| recordHistory(); | ||
| renderHistory(); | ||
| renderSavedQueries(); | ||
| })() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd almost be tempted to make this a
<template>we could pull out and instantiate, but this isn't so hairy yet that it feels urgent 😅