quantvat / src /static /js /dd_notes.js
heisbuba's picture
Update src/static/js/dd_notes.js
c9d2448 verified
Raw History Blame
4.69 kB
(function () {
'use strict';
var state = { coinId: null, symbol: '', note: '', tags: [], vocabulary: [], storeError: false };
function byId(id) { return document.getElementById(id); }
async function post(url, body) {
var res = await fetch(url, {
method: 'POST',
headers: { 'Content-Type': 'application/json', 'X-CSRFToken': window.QV.csrf },
body: JSON.stringify(body)
});
var json = await res.json().catch(function () { return null; });
if (!res.ok || !json || json.ok === false) {
throw new Error((json && json.message) || 'Could not save. Please try again.');
}
return json;
}
function message(text, tone) {
var el = byId('nm-msg');
el.textContent = text || '';
el.className = 'nm-msg' + (text && tone ? ' ' + tone : '');
}
function renderButton() {
var has = !!(state.note || state.tags.length);
var btn = byId('btn-note');
btn.classList.toggle('has-note', has);
btn.title = has ? 'Edit note' : 'Add note';
byId('note-text').textContent = has ? 'Edit note' : 'Note';
}
function isOpen() { return !byId('note-modal').hidden; }
function close() {
byId('note-modal').hidden = true;
byId('btn-note').focus();
}
function open() {
if (!state.coinId) return;
byId('nm-coin').textContent = state.symbol ? state.symbol.toUpperCase() : state.coinId;
byId('nm-note').value = state.note;
byId('nm-setup').value = state.tags.join(', ');
var list = byId('nm-tags');
list.textContent = '';
state.vocabulary.forEach(function (tag) {
var option = document.createElement('option');
option.value = tag;
list.appendChild(option);
});
byId('nm-save').disabled = false;
message(state.storeError ? 'Notes are unavailable right now. Changes will not be saved.' : '',
state.storeError ? 'warn' : '');
byId('note-modal').hidden = false;
byId('nm-note').focus();
}
async function save() {
var coinId = state.coinId;
if (!coinId) return;
var note = byId('nm-note').value.trim();
var typed = byId('nm-setup').value.trim();
var tags = typed.split(',').map(function (t) { return t.trim().toLowerCase(); }).filter(Boolean);
var saveBtn = byId('nm-save');
saveBtn.disabled = true;
message('');
try {
if (note !== state.note) {
var saved = await post('/screener/notes', { coin_id: coinId, note: note });
if (state.coinId === coinId) state.note = saved.note || '';
}
if (tags.join(',') !== state.tags.join(',')) {
var tagged = await post('/screener/setups', { coin_id: coinId, tags: tags });
if (state.coinId === coinId) {
state.tags = tagged.tags || [];
state.vocabulary = tagged.setup_tags || state.vocabulary;
}
}
if (state.coinId === coinId) {
renderButton();
close();
}
} catch (err) {
if (state.coinId === coinId) {
renderButton();
message(err.message, 'error');
}
} finally {
saveBtn.disabled = false;
}
}
// Called by deep_diver.js each time a coin finishes loading.
function load(d, coinId) {
state = {
coinId: coinId,
symbol: (d && d.vitals && d.vitals.symbol) || '',
note: (d && d.note) || '',
tags: (d && d.tags) || [],
vocabulary: (d && d.setup_tags) || [],
storeError: !!(d && d.store_error)
};
if (isOpen()) byId('note-modal').hidden = true;
renderButton();
}
byId('btn-note').addEventListener('click', open);
byId('nm-save').addEventListener('click', save);
byId('nm-cancel').addEventListener('click', close);
byId('nm-close').addEventListener('click', close);
byId('note-modal').addEventListener('click', function (event) {
if (event.target === byId('note-modal')) close();
});
document.addEventListener('keydown', function (event) {
if (event.key === 'Escape' && isOpen()) close();
});
byId('nm-setup').addEventListener('keydown', function (event) {
if (event.key === 'Enter') { event.preventDefault(); save(); }
});
window.DDNotes = { load: load, open: open, close: close, save: save, state: function () { return state; } };
})();