diff --git a/crypto_monitor_binance/templates/index.html b/crypto_monitor_binance/templates/index.html index eaf0ee3..90d5b9f 100644 --- a/crypto_monitor_binance/templates/index.html +++ b/crypto_monitor_binance/templates/index.html @@ -851,7 +851,7 @@ - + - + - + - + - + {% include 'embed_boot_scripts.html' %} - + diff --git a/static/instance_embed.js b/static/instance_embed.js index 2fb63f0..7e90b8e 100644 --- a/static/instance_embed.js +++ b/static/instance_embed.js @@ -76,6 +76,10 @@ if (typeof global.loadReviews === "function") global.loadReviews(); if (typeof global.toggleReviewMode === "function") global.toggleReviewMode(); } + if (tab === "stats") { + if (typeof global.initStatsSegmentFromUrl === "function") global.initStatsSegmentFromUrl(); + if (typeof global.initInstanceStatsCalendar === "function") global.initInstanceStatsCalendar(); + } if (typeof global.refreshPriceSnapshotConditional === "function") { global.refreshPriceSnapshotConditional(); } diff --git a/static/trade_stats_calendar.js b/static/trade_stats_calendar.js index 866478c..1c352dc 100644 --- a/static/trade_stats_calendar.js +++ b/static/trade_stats_calendar.js @@ -71,7 +71,7 @@ TradeStatsCalendar.prototype.render = function () { if (!this.gridEl || !this.titleEl) return; - this.ensureMonth(new Date()); + if (this.year <= 0 || this.month <= 0) this.ensureMonth(new Date()); this.titleEl.textContent = monthLabel(this.year, this.month); var first = new Date(this.year, this.month - 1, 1); var lastDay = new Date(this.year, this.month, 0).getDate(); @@ -149,6 +149,7 @@ TradeStatsCalendar.prototype.load = async function () { this.ensureMonth(new Date()); + this.render(); var q = this.buildQuery(this.year, this.month); if (!q.has("year")) q.set("year", String(this.year)); if (!q.has("month")) q.set("month", String(this.month)); @@ -160,6 +161,10 @@ var resp = await fetch(this.apiUrl + "?" + q.toString(), { credentials: "same-origin", }); + if (!resp.ok) { + console.warn("[trade calendar] api", resp.status); + return; + } data = await resp.json(); } this.days = this.parseResponse(data) || {}; @@ -167,6 +172,7 @@ if (this.onMonthChange) this.onMonthChange(this.year, this.month, this.days); } catch (e) { console.warn("[trade calendar]", e); + this.render(); } }; @@ -200,4 +206,36 @@ }; global.TradeStatsCalendar = TradeStatsCalendar; + + global.statsCalendarWidget = null; + + global.initInstanceStatsCalendar = function () { + var grid = document.getElementById("stats-calendar"); + if (!grid || !global.TradeStatsCalendar) return null; + global.statsCalendarWidget = new TradeStatsCalendar({ + gridEl: grid, + titleEl: document.getElementById("stats-cal-title"), + prevBtn: document.getElementById("stats-cal-prev"), + nextBtn: document.getElementById("stats-cal-next"), + apiUrl: "/api/stats/calendar", + showSick: false, + buildQuery: function (year, month) { + var q = new URLSearchParams(); + q.set("year", String(year)); + q.set("month", String(month)); + var sel = document.getElementById("stats-segment-select"); + if (sel) q.set("segment", sel.value || "all"); + return q; + }, + parseResponse: function (data) { + if (data && data.ok === false) return {}; + return (data && data.days) || {}; + }, + }); + global.statsCalendarWidget.render(); + void global.statsCalendarWidget.load(); + return global.statsCalendarWidget; + }; + + global.initStatsCalendarWidget = global.initInstanceStatsCalendar; })(window);