File size: 4,692 Bytes
502688e
 
 
c9d2448
502688e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c9d2448
502688e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c9d2448
502688e
 
c9d2448
502688e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c9d2448
 
502688e
 
 
 
 
 
 
 
c9d2448
 
502688e
c9d2448
 
502688e
 
 
 
 
 
 
 
c9d2448
502688e
 
 
 
 
 
 
 
 
 
 
 
 
c9d2448
 
502688e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c9d2448
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
(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; } };
})();