Make stats DSL primary with highlighted editor, completion and live diagnostics
This commit is contained in:
+1
-24
@@ -3,32 +3,9 @@
|
||||
const root = document.getElementById('stats-studio');
|
||||
if (!root) return;
|
||||
const form = document.getElementById('explorer-form');
|
||||
const editor = document.getElementById('query-editor');
|
||||
const input = document.getElementById('query-input');
|
||||
const toggle = document.getElementById('query-toggle');
|
||||
const status = document.getElementById('studio-status');
|
||||
const value = name => form.elements.namedItem(name).value;
|
||||
function serialize() {
|
||||
const lines = ['runs', ` during last ${value('weeks')} weeks`];
|
||||
if (value('end')) lines.push(` ending ${value('end')}`);
|
||||
lines.push(` where distance >= ${value('minimum') || 0} km`);
|
||||
[['maximum', 'distance <=', 'km'], ['hr_min', 'heart_rate >=', 'bpm'], ['hr_max', 'heart_rate <=', 'bpm']].forEach(([key, clause, unit]) => {
|
||||
if (value(key)) lines.push(` and ${clause} ${value(key)} ${unit}`);
|
||||
});
|
||||
lines.push(` summarize ${value('metric')}${value('secondary') === 'none' ? '' : ', ' + value('secondary')}`, ` by ${value('group')}`, ` compare ${value('compare') === 'yes' ? 'previous period' : 'none'}`);
|
||||
return lines.join('\n');
|
||||
}
|
||||
form.addEventListener('change', event => { if (event.target !== input) input.value = serialize(); });
|
||||
toggle.addEventListener('click', () => {
|
||||
if (!editor.hidden) {
|
||||
// Apply edited DSL on the server before returning to synchronized guided controls.
|
||||
if (input.value.trim() !== serialize().trim()) { input.name = 'query'; form.submit(); return; }
|
||||
}
|
||||
editor.hidden = !editor.hidden;
|
||||
toggle.setAttribute('aria-expanded', String(!editor.hidden));
|
||||
toggle.textContent = editor.hidden ? '〈/〉 Show query' : '〈/〉 Hide query';
|
||||
});
|
||||
form.addEventListener('submit', () => { if (!editor.hidden) input.name = 'query'; });
|
||||
const storageKey = `stride.stats.views.${root.dataset.owner}`;
|
||||
const views = document.getElementById('saved-views');
|
||||
let saved = [];
|
||||
@@ -48,7 +25,7 @@
|
||||
document.getElementById('save-view').onclick = () => {
|
||||
const name = window.prompt('Name this view', `${value('metric').replaceAll('_', ' ')} · last ${value('weeks')} weeks`);
|
||||
if (!name || !name.trim()) return;
|
||||
const query = editor.hidden ? serialize() : input.value;
|
||||
const query = window.strideQueryEditor ? window.strideQueryEditor.getValue() : input.value;
|
||||
saved = [{name: name.trim().slice(0, 80), query}, ...saved.filter(v => v.query !== query)].slice(0, 12);
|
||||
persist(); renderSaved();
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user