myio-js-library 0.1.161 → 0.1.163
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +3138 -14
- package/dist/index.d.cts +962 -2
- package/dist/index.js +3116 -14
- package/dist/myio-js-library.umd.js +3113 -13
- package/dist/myio-js-library.umd.min.js +1 -1
- package/package.json +1 -1
|
@@ -650,6 +650,10 @@
|
|
|
650
650
|
}
|
|
651
651
|
|
|
652
652
|
// src/format/water.ts
|
|
653
|
+
function formatWater(value) {
|
|
654
|
+
const num = Number(value) || 0;
|
|
655
|
+
return `${num.toFixed(2)} m\xB3`;
|
|
656
|
+
}
|
|
653
657
|
function formatWaterVolumeM3(value, locale = "pt-BR") {
|
|
654
658
|
if (value === null || value === void 0 || isNaN(value)) {
|
|
655
659
|
return "-";
|
|
@@ -727,6 +731,76 @@
|
|
|
727
731
|
};
|
|
728
732
|
}
|
|
729
733
|
|
|
734
|
+
// src/format/time.ts
|
|
735
|
+
function formatRelativeTime(timestamp) {
|
|
736
|
+
if (!timestamp || timestamp <= 0) {
|
|
737
|
+
return "\u2014";
|
|
738
|
+
}
|
|
739
|
+
const now = Date.now();
|
|
740
|
+
const diffSeconds = Math.round((now - timestamp) / 1e3);
|
|
741
|
+
if (diffSeconds < 10) {
|
|
742
|
+
return "agora";
|
|
743
|
+
}
|
|
744
|
+
if (diffSeconds < 60) {
|
|
745
|
+
return `h\xE1 ${diffSeconds}s`;
|
|
746
|
+
}
|
|
747
|
+
const diffMinutes = Math.round(diffSeconds / 60);
|
|
748
|
+
if (diffMinutes === 1) {
|
|
749
|
+
return "h\xE1 1 min";
|
|
750
|
+
}
|
|
751
|
+
if (diffMinutes < 60) {
|
|
752
|
+
return `h\xE1 ${diffMinutes} mins`;
|
|
753
|
+
}
|
|
754
|
+
const diffHours = Math.round(diffMinutes / 60);
|
|
755
|
+
if (diffHours === 1) {
|
|
756
|
+
return "h\xE1 1 hora";
|
|
757
|
+
}
|
|
758
|
+
if (diffHours < 24) {
|
|
759
|
+
return `h\xE1 ${diffHours} horas`;
|
|
760
|
+
}
|
|
761
|
+
const diffDays = Math.round(diffHours / 24);
|
|
762
|
+
if (diffDays === 1) {
|
|
763
|
+
return "ontem";
|
|
764
|
+
}
|
|
765
|
+
if (diffDays <= 30) {
|
|
766
|
+
return `h\xE1 ${diffDays} dias`;
|
|
767
|
+
}
|
|
768
|
+
return new Date(timestamp).toLocaleDateString("pt-BR");
|
|
769
|
+
}
|
|
770
|
+
function formatarDuracao(ms) {
|
|
771
|
+
if (typeof ms !== "number" || ms < 0 || !isFinite(ms)) {
|
|
772
|
+
return "0s";
|
|
773
|
+
}
|
|
774
|
+
if (ms === 0) {
|
|
775
|
+
return "0s";
|
|
776
|
+
}
|
|
777
|
+
const segundos = Math.floor(ms / 1e3 % 60);
|
|
778
|
+
const minutos = Math.floor(ms / (1e3 * 60) % 60);
|
|
779
|
+
const horas = Math.floor(ms / (1e3 * 60 * 60) % 24);
|
|
780
|
+
const dias = Math.floor(ms / (1e3 * 60 * 60 * 24));
|
|
781
|
+
const parts = [];
|
|
782
|
+
if (dias > 0) {
|
|
783
|
+
parts.push(`${dias}d`);
|
|
784
|
+
if (horas > 0) {
|
|
785
|
+
parts.push(`${horas}h`);
|
|
786
|
+
}
|
|
787
|
+
} else if (horas > 0) {
|
|
788
|
+
parts.push(`${horas}h`);
|
|
789
|
+
if (minutos > 0) {
|
|
790
|
+
parts.push(`${minutos}m`);
|
|
791
|
+
}
|
|
792
|
+
} else if (minutos > 0) {
|
|
793
|
+
parts.push(`${minutos}m`);
|
|
794
|
+
if (segundos > 0) {
|
|
795
|
+
parts.push(`${segundos}s`);
|
|
796
|
+
}
|
|
797
|
+
} else {
|
|
798
|
+
parts.push(`${segundos}s`);
|
|
799
|
+
}
|
|
800
|
+
return parts.length > 0 ? parts.join(" ") : "0s";
|
|
801
|
+
}
|
|
802
|
+
var formatDuration = formatarDuracao;
|
|
803
|
+
|
|
730
804
|
// src/date/ymd.ts
|
|
731
805
|
function formatDateToYMD(date) {
|
|
732
806
|
if (!date) {
|
|
@@ -1235,6 +1309,11 @@
|
|
|
1235
1309
|
}
|
|
1236
1310
|
return getValueByDatakey(data, keyOrPath);
|
|
1237
1311
|
}
|
|
1312
|
+
function findValueWithDefault(values, key, defaultValue = null) {
|
|
1313
|
+
if (!Array.isArray(values)) return defaultValue;
|
|
1314
|
+
const found = values.find((v) => v.key === key || v.dataType === key);
|
|
1315
|
+
return found ? found.value : defaultValue;
|
|
1316
|
+
}
|
|
1238
1317
|
|
|
1239
1318
|
// src/utils/deviceStatus.js
|
|
1240
1319
|
var DeviceStatusType = {
|
|
@@ -1291,6 +1370,16 @@
|
|
|
1291
1370
|
}
|
|
1292
1371
|
return ConnectionStatusType.CONNECTED;
|
|
1293
1372
|
}
|
|
1373
|
+
function mapConnectionStatus(rawStatus) {
|
|
1374
|
+
const statusLower = String(rawStatus || "").toLowerCase().trim();
|
|
1375
|
+
if (statusLower === "online" || statusLower === "ok" || statusLower === "running") {
|
|
1376
|
+
return "online";
|
|
1377
|
+
}
|
|
1378
|
+
if (statusLower === "waiting" || statusLower === "connecting" || statusLower === "pending") {
|
|
1379
|
+
return "waiting";
|
|
1380
|
+
}
|
|
1381
|
+
return "offline";
|
|
1382
|
+
}
|
|
1294
1383
|
function mapDeviceStatusToCardStatus(deviceStatus) {
|
|
1295
1384
|
const statusMap = {
|
|
1296
1385
|
[DeviceStatusType.POWER_ON]: "ok",
|
|
@@ -7690,14 +7779,14 @@ ${rangeText}`;
|
|
|
7690
7779
|
return `${value.toFixed(config.decimals)} ${config.unit}`;
|
|
7691
7780
|
}
|
|
7692
7781
|
function initializeChart() {
|
|
7693
|
-
const
|
|
7694
|
-
if (!
|
|
7782
|
+
const Chart2 = window.Chart;
|
|
7783
|
+
if (!Chart2) {
|
|
7695
7784
|
console.warn("[RealTimeTelemetry] Chart.js not loaded");
|
|
7696
7785
|
return;
|
|
7697
7786
|
}
|
|
7698
7787
|
chartContainer.style.display = "block";
|
|
7699
7788
|
const config = TELEMETRY_CONFIG[selectedChartKey] || { label: selectedChartKey, unit: "" };
|
|
7700
|
-
chart = new
|
|
7789
|
+
chart = new Chart2(chartCanvas, {
|
|
7701
7790
|
type: "line",
|
|
7702
7791
|
data: {
|
|
7703
7792
|
datasets: [{
|
|
@@ -10562,8 +10651,8 @@ ${rangeText}`;
|
|
|
10562
10651
|
peakEl.textContent = `${strings.maximum}: ${peak.formattedValue} kW ${peak.key ? `(${peak.key}) ` : ""}${strings.at} ${dateStr}`;
|
|
10563
10652
|
peakEl.style.display = "block";
|
|
10564
10653
|
}
|
|
10565
|
-
const
|
|
10566
|
-
|
|
10654
|
+
const Chart2 = window.Chart;
|
|
10655
|
+
Chart2.register(window.ChartZoom);
|
|
10567
10656
|
if (chart) {
|
|
10568
10657
|
chart.data.datasets = chartData.series.map((series) => ({
|
|
10569
10658
|
label: series.label,
|
|
@@ -10615,7 +10704,7 @@ ${rangeText}`;
|
|
|
10615
10704
|
};
|
|
10616
10705
|
chart.update();
|
|
10617
10706
|
} else {
|
|
10618
|
-
chart = new
|
|
10707
|
+
chart = new Chart2(chartCanvas, {
|
|
10619
10708
|
type: "line",
|
|
10620
10709
|
data: {
|
|
10621
10710
|
datasets: chartData.series.map((series) => ({
|
|
@@ -18747,8 +18836,8 @@ ${rangeText}`;
|
|
|
18747
18836
|
<div class="myio-goals-progress-fill" style="width: ${Math.min(progress, 100)}%"></div>
|
|
18748
18837
|
</div>
|
|
18749
18838
|
<div class="myio-goals-progress-text">
|
|
18750
|
-
<span>${
|
|
18751
|
-
<span>${
|
|
18839
|
+
<span>${formatNumber3(monthlySum, locale)} ${annual.unit}</span>
|
|
18840
|
+
<span>${formatNumber3(annual.total, locale)} ${annual.unit}</span>
|
|
18752
18841
|
</div>
|
|
18753
18842
|
|
|
18754
18843
|
<!-- Monthly Grid -->
|
|
@@ -18822,7 +18911,7 @@ ${rangeText}`;
|
|
|
18822
18911
|
<span>${assetData.label || assetId}</span>
|
|
18823
18912
|
</div>
|
|
18824
18913
|
<div class="myio-goals-asset-total">
|
|
18825
|
-
${
|
|
18914
|
+
${formatNumber3(assetData.annual?.total || 0, locale)} ${assetData.annual?.unit || "kWh"}
|
|
18826
18915
|
</div>
|
|
18827
18916
|
<button class="myio-goals-btn-icon" data-action="delete-asset" data-asset-id="${assetId}" aria-label="${i18n.deleteAsset}">
|
|
18828
18917
|
<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
|
|
@@ -19137,7 +19226,7 @@ ${rangeText}`;
|
|
|
19137
19226
|
monthlySum += value;
|
|
19138
19227
|
}
|
|
19139
19228
|
if (monthlySum > annualTotal && annualTotal > 0) {
|
|
19140
|
-
errors.push(`${i18n.errorMonthlyExceedsAnnual} (${
|
|
19229
|
+
errors.push(`${i18n.errorMonthlyExceedsAnnual} (${formatNumber3(monthlySum, locale)} > ${formatNumber3(annualTotal, locale)})`);
|
|
19141
19230
|
}
|
|
19142
19231
|
}
|
|
19143
19232
|
return errors;
|
|
@@ -19228,8 +19317,8 @@ ${rangeText}`;
|
|
|
19228
19317
|
}
|
|
19229
19318
|
if (progressTexts.length === 2) {
|
|
19230
19319
|
const unit = document.getElementById("unit-select")?.value || "kWh";
|
|
19231
|
-
progressTexts[0].textContent = `${
|
|
19232
|
-
progressTexts[1].textContent = `${
|
|
19320
|
+
progressTexts[0].textContent = `${formatNumber3(monthlySum, locale)} ${unit}`;
|
|
19321
|
+
progressTexts[1].textContent = `${formatNumber3(annualTotal, locale)} ${unit}`;
|
|
19233
19322
|
}
|
|
19234
19323
|
}
|
|
19235
19324
|
function updateMonthlyUnits(unit) {
|
|
@@ -19327,7 +19416,7 @@ ${rangeText}`;
|
|
|
19327
19416
|
modal.addEventListener("keydown", handleTab);
|
|
19328
19417
|
firstElement.focus();
|
|
19329
19418
|
}
|
|
19330
|
-
function
|
|
19419
|
+
function formatNumber3(value, locale2) {
|
|
19331
19420
|
return new Intl.NumberFormat(locale2, {
|
|
19332
19421
|
minimumFractionDigits: 0,
|
|
19333
19422
|
maximumFractionDigits: 2
|
|
@@ -22278,10 +22367,3006 @@ ${rangeText}`;
|
|
|
22278
22367
|
return { destroy };
|
|
22279
22368
|
}
|
|
22280
22369
|
|
|
22370
|
+
// src/components/ModalHeader/index.ts
|
|
22371
|
+
var DEFAULT_BG_COLOR = "#3e1a7d";
|
|
22372
|
+
var DEFAULT_TEXT_COLOR = "white";
|
|
22373
|
+
var DEFAULT_BORDER_RADIUS = "10px 10px 0 0";
|
|
22374
|
+
var EXPORT_FORMAT_LABELS = {
|
|
22375
|
+
csv: "CSV",
|
|
22376
|
+
xls: "Excel (XLS)",
|
|
22377
|
+
pdf: "PDF"
|
|
22378
|
+
};
|
|
22379
|
+
var EXPORT_FORMAT_ICONS = {
|
|
22380
|
+
csv: "\u{1F4C4}",
|
|
22381
|
+
xls: "\u{1F4CA}",
|
|
22382
|
+
pdf: "\u{1F4D1}"
|
|
22383
|
+
};
|
|
22384
|
+
function createModalHeader(config) {
|
|
22385
|
+
let currentTheme = config.theme || "light";
|
|
22386
|
+
let currentIsMaximized = config.isMaximized || false;
|
|
22387
|
+
let currentTitle = config.title;
|
|
22388
|
+
let themeBtn = null;
|
|
22389
|
+
let maximizeBtn = null;
|
|
22390
|
+
let closeBtn = null;
|
|
22391
|
+
let exportBtn = null;
|
|
22392
|
+
let exportDropdown = null;
|
|
22393
|
+
const cleanupHandlers = [];
|
|
22394
|
+
const handleThemeClick = () => {
|
|
22395
|
+
currentTheme = currentTheme === "light" ? "dark" : "light";
|
|
22396
|
+
config.onThemeToggle?.(currentTheme);
|
|
22397
|
+
updateButtonIcons();
|
|
22398
|
+
};
|
|
22399
|
+
const handleMaximizeClick = () => {
|
|
22400
|
+
currentIsMaximized = !currentIsMaximized;
|
|
22401
|
+
config.onMaximize?.(currentIsMaximized);
|
|
22402
|
+
updateButtonIcons();
|
|
22403
|
+
};
|
|
22404
|
+
const handleCloseClick = () => {
|
|
22405
|
+
config.onClose?.();
|
|
22406
|
+
};
|
|
22407
|
+
const handleExportClick = (format) => {
|
|
22408
|
+
config.onExport?.(format);
|
|
22409
|
+
if (exportDropdown) {
|
|
22410
|
+
exportDropdown.style.display = "none";
|
|
22411
|
+
}
|
|
22412
|
+
};
|
|
22413
|
+
function updateButtonIcons() {
|
|
22414
|
+
if (themeBtn) {
|
|
22415
|
+
themeBtn.textContent = currentTheme === "dark" ? "\u2600\uFE0F" : "\u{1F319}";
|
|
22416
|
+
themeBtn.title = currentTheme === "dark" ? "Modo claro" : "Modo escuro";
|
|
22417
|
+
}
|
|
22418
|
+
if (maximizeBtn) {
|
|
22419
|
+
maximizeBtn.textContent = currentIsMaximized ? "\u{1F5D7}" : "\u{1F5D6}";
|
|
22420
|
+
maximizeBtn.title = currentIsMaximized ? "Restaurar" : "Maximizar";
|
|
22421
|
+
}
|
|
22422
|
+
}
|
|
22423
|
+
function getButtonStyle() {
|
|
22424
|
+
return `
|
|
22425
|
+
background: none;
|
|
22426
|
+
border: none;
|
|
22427
|
+
font-size: 16px;
|
|
22428
|
+
cursor: pointer;
|
|
22429
|
+
padding: 4px 8px;
|
|
22430
|
+
border-radius: 6px;
|
|
22431
|
+
color: rgba(255, 255, 255, 0.8);
|
|
22432
|
+
transition: background-color 0.2s, color 0.2s;
|
|
22433
|
+
`.replace(/\s+/g, " ").trim();
|
|
22434
|
+
}
|
|
22435
|
+
function renderExportDropdown() {
|
|
22436
|
+
const formats = config.exportFormats || [];
|
|
22437
|
+
if (formats.length === 0) return "";
|
|
22438
|
+
if (formats.length === 1) {
|
|
22439
|
+
return `
|
|
22440
|
+
<button id="${config.id}-export" title="Exportar ${EXPORT_FORMAT_LABELS[formats[0]]}" style="${getButtonStyle()}">
|
|
22441
|
+
\u{1F4E5}
|
|
22442
|
+
</button>
|
|
22443
|
+
`;
|
|
22444
|
+
}
|
|
22445
|
+
return `
|
|
22446
|
+
<div style="position: relative; display: inline-block;">
|
|
22447
|
+
<button id="${config.id}-export-btn" title="Exportar" style="${getButtonStyle()}">
|
|
22448
|
+
\u{1F4E5}
|
|
22449
|
+
</button>
|
|
22450
|
+
<div id="${config.id}-export-dropdown" style="
|
|
22451
|
+
display: none;
|
|
22452
|
+
position: absolute;
|
|
22453
|
+
top: 100%;
|
|
22454
|
+
right: 0;
|
|
22455
|
+
background: white;
|
|
22456
|
+
border-radius: 6px;
|
|
22457
|
+
box-shadow: 0 4px 12px rgba(0,0,0,0.15);
|
|
22458
|
+
min-width: 140px;
|
|
22459
|
+
z-index: 10001;
|
|
22460
|
+
margin-top: 4px;
|
|
22461
|
+
overflow: hidden;
|
|
22462
|
+
">
|
|
22463
|
+
${formats.map((format) => `
|
|
22464
|
+
<button
|
|
22465
|
+
id="${config.id}-export-${format}"
|
|
22466
|
+
class="myio-export-option"
|
|
22467
|
+
data-format="${format}"
|
|
22468
|
+
style="
|
|
22469
|
+
display: flex;
|
|
22470
|
+
align-items: center;
|
|
22471
|
+
gap: 8px;
|
|
22472
|
+
width: 100%;
|
|
22473
|
+
padding: 10px 14px;
|
|
22474
|
+
border: none;
|
|
22475
|
+
background: white;
|
|
22476
|
+
cursor: pointer;
|
|
22477
|
+
font-size: 13px;
|
|
22478
|
+
color: #333;
|
|
22479
|
+
text-align: left;
|
|
22480
|
+
transition: background-color 0.2s;
|
|
22481
|
+
"
|
|
22482
|
+
>
|
|
22483
|
+
${EXPORT_FORMAT_ICONS[format]} ${EXPORT_FORMAT_LABELS[format]}
|
|
22484
|
+
</button>
|
|
22485
|
+
`).join("")}
|
|
22486
|
+
</div>
|
|
22487
|
+
</div>
|
|
22488
|
+
`;
|
|
22489
|
+
}
|
|
22490
|
+
const instance = {
|
|
22491
|
+
render() {
|
|
22492
|
+
const bgColor = config.backgroundColor || DEFAULT_BG_COLOR;
|
|
22493
|
+
const textColor = config.textColor || DEFAULT_TEXT_COLOR;
|
|
22494
|
+
const borderRadius = currentIsMaximized ? "0" : config.borderRadius || DEFAULT_BORDER_RADIUS;
|
|
22495
|
+
const showTheme = config.showThemeToggle !== false;
|
|
22496
|
+
const showMax = config.showMaximize !== false;
|
|
22497
|
+
const showClose = config.showClose !== false;
|
|
22498
|
+
const showExport = config.exportFormats && config.exportFormats.length > 0;
|
|
22499
|
+
const iconHtml = config.icon ? `<span style="margin-right: 8px;">${config.icon}</span>` : "";
|
|
22500
|
+
const buttonStyle = getButtonStyle();
|
|
22501
|
+
return `
|
|
22502
|
+
<div class="myio-modal-header" style="
|
|
22503
|
+
padding: 4px 8px;
|
|
22504
|
+
display: flex;
|
|
22505
|
+
align-items: center;
|
|
22506
|
+
justify-content: space-between;
|
|
22507
|
+
background: ${bgColor};
|
|
22508
|
+
color: ${textColor};
|
|
22509
|
+
border-radius: ${borderRadius};
|
|
22510
|
+
min-height: 20px;
|
|
22511
|
+
font-family: 'Roboto', Arial, sans-serif;
|
|
22512
|
+
">
|
|
22513
|
+
<h2 id="${config.id}-header-title" style="
|
|
22514
|
+
margin: 6px;
|
|
22515
|
+
font-size: 18px;
|
|
22516
|
+
font-weight: 600;
|
|
22517
|
+
color: ${textColor};
|
|
22518
|
+
line-height: 2;
|
|
22519
|
+
display: flex;
|
|
22520
|
+
align-items: center;
|
|
22521
|
+
">
|
|
22522
|
+
${iconHtml}${currentTitle}
|
|
22523
|
+
</h2>
|
|
22524
|
+
<div style="display: flex; gap: 4px; align-items: center;">
|
|
22525
|
+
${showExport ? renderExportDropdown() : ""}
|
|
22526
|
+
${showTheme ? `
|
|
22527
|
+
<button id="${config.id}-theme-toggle" title="${currentTheme === "dark" ? "Modo claro" : "Modo escuro"}" style="${buttonStyle}">
|
|
22528
|
+
${currentTheme === "dark" ? "\u2600\uFE0F" : "\u{1F319}"}
|
|
22529
|
+
</button>
|
|
22530
|
+
` : ""}
|
|
22531
|
+
${showMax ? `
|
|
22532
|
+
<button id="${config.id}-maximize" title="${currentIsMaximized ? "Restaurar" : "Maximizar"}" style="${buttonStyle}">
|
|
22533
|
+
${currentIsMaximized ? "\u{1F5D7}" : "\u{1F5D6}"}
|
|
22534
|
+
</button>
|
|
22535
|
+
` : ""}
|
|
22536
|
+
${showClose ? `
|
|
22537
|
+
<button id="${config.id}-close" title="Fechar" style="${buttonStyle}; font-size: 20px;">
|
|
22538
|
+
\xD7
|
|
22539
|
+
</button>
|
|
22540
|
+
` : ""}
|
|
22541
|
+
</div>
|
|
22542
|
+
</div>
|
|
22543
|
+
`;
|
|
22544
|
+
},
|
|
22545
|
+
attachListeners() {
|
|
22546
|
+
themeBtn = document.getElementById(`${config.id}-theme-toggle`);
|
|
22547
|
+
maximizeBtn = document.getElementById(`${config.id}-maximize`);
|
|
22548
|
+
closeBtn = document.getElementById(`${config.id}-close`);
|
|
22549
|
+
const singleExportBtn = document.getElementById(`${config.id}-export`);
|
|
22550
|
+
if (singleExportBtn && config.exportFormats?.length === 1) {
|
|
22551
|
+
exportBtn = singleExportBtn;
|
|
22552
|
+
const format = config.exportFormats[0];
|
|
22553
|
+
const clickHandler = () => handleExportClick(format);
|
|
22554
|
+
exportBtn.addEventListener("click", clickHandler);
|
|
22555
|
+
cleanupHandlers.push(() => exportBtn?.removeEventListener("click", clickHandler));
|
|
22556
|
+
const enterHandler = () => {
|
|
22557
|
+
exportBtn.style.backgroundColor = "rgba(255, 255, 255, 0.2)";
|
|
22558
|
+
};
|
|
22559
|
+
const leaveHandler = () => {
|
|
22560
|
+
exportBtn.style.backgroundColor = "transparent";
|
|
22561
|
+
};
|
|
22562
|
+
exportBtn.addEventListener("mouseenter", enterHandler);
|
|
22563
|
+
exportBtn.addEventListener("mouseleave", leaveHandler);
|
|
22564
|
+
cleanupHandlers.push(() => {
|
|
22565
|
+
exportBtn?.removeEventListener("mouseenter", enterHandler);
|
|
22566
|
+
exportBtn?.removeEventListener("mouseleave", leaveHandler);
|
|
22567
|
+
});
|
|
22568
|
+
}
|
|
22569
|
+
const exportDropdownBtn = document.getElementById(`${config.id}-export-btn`);
|
|
22570
|
+
exportDropdown = document.getElementById(`${config.id}-export-dropdown`);
|
|
22571
|
+
if (exportDropdownBtn && exportDropdown) {
|
|
22572
|
+
exportBtn = exportDropdownBtn;
|
|
22573
|
+
const toggleHandler = (e) => {
|
|
22574
|
+
e.stopPropagation();
|
|
22575
|
+
if (exportDropdown) {
|
|
22576
|
+
exportDropdown.style.display = exportDropdown.style.display === "none" ? "block" : "none";
|
|
22577
|
+
}
|
|
22578
|
+
};
|
|
22579
|
+
exportDropdownBtn.addEventListener("click", toggleHandler);
|
|
22580
|
+
cleanupHandlers.push(() => exportDropdownBtn.removeEventListener("click", toggleHandler));
|
|
22581
|
+
const outsideClickHandler = (e) => {
|
|
22582
|
+
if (exportDropdown && !exportDropdown.contains(e.target) && e.target !== exportDropdownBtn) {
|
|
22583
|
+
exportDropdown.style.display = "none";
|
|
22584
|
+
}
|
|
22585
|
+
};
|
|
22586
|
+
document.addEventListener("click", outsideClickHandler);
|
|
22587
|
+
cleanupHandlers.push(() => document.removeEventListener("click", outsideClickHandler));
|
|
22588
|
+
config.exportFormats?.forEach((format) => {
|
|
22589
|
+
const btn = document.getElementById(`${config.id}-export-${format}`);
|
|
22590
|
+
if (btn) {
|
|
22591
|
+
const clickHandler = () => handleExportClick(format);
|
|
22592
|
+
btn.addEventListener("click", clickHandler);
|
|
22593
|
+
cleanupHandlers.push(() => btn.removeEventListener("click", clickHandler));
|
|
22594
|
+
const enterHandler2 = () => {
|
|
22595
|
+
btn.style.backgroundColor = "#f0f0f0";
|
|
22596
|
+
};
|
|
22597
|
+
const leaveHandler2 = () => {
|
|
22598
|
+
btn.style.backgroundColor = "white";
|
|
22599
|
+
};
|
|
22600
|
+
btn.addEventListener("mouseenter", enterHandler2);
|
|
22601
|
+
btn.addEventListener("mouseleave", leaveHandler2);
|
|
22602
|
+
cleanupHandlers.push(() => {
|
|
22603
|
+
btn.removeEventListener("mouseenter", enterHandler2);
|
|
22604
|
+
btn.removeEventListener("mouseleave", leaveHandler2);
|
|
22605
|
+
});
|
|
22606
|
+
}
|
|
22607
|
+
});
|
|
22608
|
+
const enterHandler = () => {
|
|
22609
|
+
exportDropdownBtn.style.backgroundColor = "rgba(255, 255, 255, 0.2)";
|
|
22610
|
+
};
|
|
22611
|
+
const leaveHandler = () => {
|
|
22612
|
+
exportDropdownBtn.style.backgroundColor = "transparent";
|
|
22613
|
+
};
|
|
22614
|
+
exportDropdownBtn.addEventListener("mouseenter", enterHandler);
|
|
22615
|
+
exportDropdownBtn.addEventListener("mouseleave", leaveHandler);
|
|
22616
|
+
cleanupHandlers.push(() => {
|
|
22617
|
+
exportDropdownBtn.removeEventListener("mouseenter", enterHandler);
|
|
22618
|
+
exportDropdownBtn.removeEventListener("mouseleave", leaveHandler);
|
|
22619
|
+
});
|
|
22620
|
+
}
|
|
22621
|
+
if (themeBtn && config.showThemeToggle !== false) {
|
|
22622
|
+
themeBtn.addEventListener("click", handleThemeClick);
|
|
22623
|
+
cleanupHandlers.push(() => themeBtn?.removeEventListener("click", handleThemeClick));
|
|
22624
|
+
const enterHandler = () => {
|
|
22625
|
+
themeBtn.style.backgroundColor = "rgba(255, 255, 255, 0.2)";
|
|
22626
|
+
};
|
|
22627
|
+
const leaveHandler = () => {
|
|
22628
|
+
themeBtn.style.backgroundColor = "transparent";
|
|
22629
|
+
};
|
|
22630
|
+
themeBtn.addEventListener("mouseenter", enterHandler);
|
|
22631
|
+
themeBtn.addEventListener("mouseleave", leaveHandler);
|
|
22632
|
+
cleanupHandlers.push(() => {
|
|
22633
|
+
themeBtn?.removeEventListener("mouseenter", enterHandler);
|
|
22634
|
+
themeBtn?.removeEventListener("mouseleave", leaveHandler);
|
|
22635
|
+
});
|
|
22636
|
+
}
|
|
22637
|
+
if (maximizeBtn && config.showMaximize !== false) {
|
|
22638
|
+
maximizeBtn.addEventListener("click", handleMaximizeClick);
|
|
22639
|
+
cleanupHandlers.push(() => maximizeBtn?.removeEventListener("click", handleMaximizeClick));
|
|
22640
|
+
const enterHandler = () => {
|
|
22641
|
+
maximizeBtn.style.backgroundColor = "rgba(255, 255, 255, 0.2)";
|
|
22642
|
+
};
|
|
22643
|
+
const leaveHandler = () => {
|
|
22644
|
+
maximizeBtn.style.backgroundColor = "transparent";
|
|
22645
|
+
};
|
|
22646
|
+
maximizeBtn.addEventListener("mouseenter", enterHandler);
|
|
22647
|
+
maximizeBtn.addEventListener("mouseleave", leaveHandler);
|
|
22648
|
+
cleanupHandlers.push(() => {
|
|
22649
|
+
maximizeBtn?.removeEventListener("mouseenter", enterHandler);
|
|
22650
|
+
maximizeBtn?.removeEventListener("mouseleave", leaveHandler);
|
|
22651
|
+
});
|
|
22652
|
+
}
|
|
22653
|
+
if (closeBtn && config.showClose !== false) {
|
|
22654
|
+
closeBtn.addEventListener("click", handleCloseClick);
|
|
22655
|
+
cleanupHandlers.push(() => closeBtn?.removeEventListener("click", handleCloseClick));
|
|
22656
|
+
const enterHandler = () => {
|
|
22657
|
+
closeBtn.style.backgroundColor = "rgba(255, 255, 255, 0.2)";
|
|
22658
|
+
};
|
|
22659
|
+
const leaveHandler = () => {
|
|
22660
|
+
closeBtn.style.backgroundColor = "transparent";
|
|
22661
|
+
};
|
|
22662
|
+
closeBtn.addEventListener("mouseenter", enterHandler);
|
|
22663
|
+
closeBtn.addEventListener("mouseleave", leaveHandler);
|
|
22664
|
+
cleanupHandlers.push(() => {
|
|
22665
|
+
closeBtn?.removeEventListener("mouseenter", enterHandler);
|
|
22666
|
+
closeBtn?.removeEventListener("mouseleave", leaveHandler);
|
|
22667
|
+
});
|
|
22668
|
+
}
|
|
22669
|
+
},
|
|
22670
|
+
update(updates) {
|
|
22671
|
+
if (updates.theme !== void 0) {
|
|
22672
|
+
currentTheme = updates.theme;
|
|
22673
|
+
updateButtonIcons();
|
|
22674
|
+
}
|
|
22675
|
+
if (updates.isMaximized !== void 0) {
|
|
22676
|
+
currentIsMaximized = updates.isMaximized;
|
|
22677
|
+
updateButtonIcons();
|
|
22678
|
+
}
|
|
22679
|
+
if (updates.title !== void 0) {
|
|
22680
|
+
currentTitle = updates.title;
|
|
22681
|
+
const titleEl = document.getElementById(`${config.id}-header-title`);
|
|
22682
|
+
if (titleEl) {
|
|
22683
|
+
const iconHtml = config.icon ? `<span style="margin-right: 8px;">${config.icon}</span>` : "";
|
|
22684
|
+
titleEl.innerHTML = `${iconHtml}${currentTitle}`;
|
|
22685
|
+
}
|
|
22686
|
+
}
|
|
22687
|
+
},
|
|
22688
|
+
getState() {
|
|
22689
|
+
return {
|
|
22690
|
+
theme: currentTheme,
|
|
22691
|
+
isMaximized: currentIsMaximized
|
|
22692
|
+
};
|
|
22693
|
+
},
|
|
22694
|
+
destroy() {
|
|
22695
|
+
cleanupHandlers.forEach((handler) => handler());
|
|
22696
|
+
cleanupHandlers.length = 0;
|
|
22697
|
+
themeBtn = null;
|
|
22698
|
+
maximizeBtn = null;
|
|
22699
|
+
closeBtn = null;
|
|
22700
|
+
exportBtn = null;
|
|
22701
|
+
exportDropdown = null;
|
|
22702
|
+
}
|
|
22703
|
+
};
|
|
22704
|
+
return instance;
|
|
22705
|
+
}
|
|
22706
|
+
function getModalHeaderStyles() {
|
|
22707
|
+
return `
|
|
22708
|
+
.myio-modal-header button:hover {
|
|
22709
|
+
background-color: rgba(255, 255, 255, 0.2) !important;
|
|
22710
|
+
}
|
|
22711
|
+
.myio-modal-header button:active {
|
|
22712
|
+
background-color: rgba(255, 255, 255, 0.3) !important;
|
|
22713
|
+
}
|
|
22714
|
+
.myio-export-option:hover {
|
|
22715
|
+
background-color: #f0f0f0 !important;
|
|
22716
|
+
}
|
|
22717
|
+
`;
|
|
22718
|
+
}
|
|
22719
|
+
|
|
22720
|
+
// src/components/Consumption7DaysChart/types.ts
|
|
22721
|
+
var DEFAULT_COLORS = {
|
|
22722
|
+
energy: {
|
|
22723
|
+
primary: "#2563eb",
|
|
22724
|
+
background: "rgba(37, 99, 235, 0.1)",
|
|
22725
|
+
gradient: ["#f0fdf4", "#dcfce7"],
|
|
22726
|
+
pointBackground: "#2563eb",
|
|
22727
|
+
pointBorder: "#ffffff"
|
|
22728
|
+
},
|
|
22729
|
+
water: {
|
|
22730
|
+
primary: "#0288d1",
|
|
22731
|
+
background: "rgba(2, 136, 209, 0.1)",
|
|
22732
|
+
gradient: ["#f0f9ff", "#bae6fd"],
|
|
22733
|
+
pointBackground: "#0288d1",
|
|
22734
|
+
pointBorder: "#ffffff"
|
|
22735
|
+
},
|
|
22736
|
+
gas: {
|
|
22737
|
+
primary: "#ea580c",
|
|
22738
|
+
background: "rgba(234, 88, 12, 0.1)",
|
|
22739
|
+
gradient: ["#fff7ed", "#fed7aa"],
|
|
22740
|
+
pointBackground: "#ea580c",
|
|
22741
|
+
pointBorder: "#ffffff"
|
|
22742
|
+
},
|
|
22743
|
+
temperature: {
|
|
22744
|
+
primary: "#dc2626",
|
|
22745
|
+
background: "rgba(220, 38, 38, 0.1)",
|
|
22746
|
+
gradient: ["#fef2f2", "#fecaca"],
|
|
22747
|
+
pointBackground: "#dc2626",
|
|
22748
|
+
pointBorder: "#ffffff"
|
|
22749
|
+
}
|
|
22750
|
+
};
|
|
22751
|
+
var THEME_COLORS = {
|
|
22752
|
+
light: {
|
|
22753
|
+
chartBackground: "#ffffff",
|
|
22754
|
+
text: "#1f2937",
|
|
22755
|
+
textMuted: "#6b7280",
|
|
22756
|
+
grid: "rgba(0, 0, 0, 0.1)",
|
|
22757
|
+
border: "#e5e7eb",
|
|
22758
|
+
tooltipBackground: "#ffffff",
|
|
22759
|
+
tooltipText: "#1f2937"
|
|
22760
|
+
},
|
|
22761
|
+
dark: {
|
|
22762
|
+
chartBackground: "#1f2937",
|
|
22763
|
+
text: "#f9fafb",
|
|
22764
|
+
textMuted: "#9ca3af",
|
|
22765
|
+
grid: "rgba(255, 255, 255, 0.1)",
|
|
22766
|
+
border: "#374151",
|
|
22767
|
+
tooltipBackground: "#374151",
|
|
22768
|
+
tooltipText: "#f9fafb"
|
|
22769
|
+
}
|
|
22770
|
+
};
|
|
22771
|
+
var DEFAULT_CONFIG = {
|
|
22772
|
+
defaultPeriod: 7,
|
|
22773
|
+
defaultChartType: "line",
|
|
22774
|
+
defaultVizMode: "total",
|
|
22775
|
+
defaultTheme: "light",
|
|
22776
|
+
cacheTTL: 3e5,
|
|
22777
|
+
// 5 minutes
|
|
22778
|
+
decimalPlaces: 1,
|
|
22779
|
+
lineTension: 0.4,
|
|
22780
|
+
pointRadius: 4,
|
|
22781
|
+
borderWidth: 2,
|
|
22782
|
+
fill: true,
|
|
22783
|
+
showLegend: false,
|
|
22784
|
+
enableExport: true
|
|
22785
|
+
};
|
|
22786
|
+
|
|
22787
|
+
// src/components/Consumption7DaysChart/createConsumption7DaysChart.ts
|
|
22788
|
+
function createConsumption7DaysChart(config) {
|
|
22789
|
+
let chartInstance = null;
|
|
22790
|
+
let cachedData = null;
|
|
22791
|
+
let currentPeriod = config.defaultPeriod ?? DEFAULT_CONFIG.defaultPeriod;
|
|
22792
|
+
let currentChartType = config.defaultChartType ?? DEFAULT_CONFIG.defaultChartType;
|
|
22793
|
+
let currentVizMode = config.defaultVizMode ?? DEFAULT_CONFIG.defaultVizMode;
|
|
22794
|
+
let currentTheme = config.theme ?? DEFAULT_CONFIG.defaultTheme;
|
|
22795
|
+
let currentIdealRange = config.idealRange ?? null;
|
|
22796
|
+
let isRendered = false;
|
|
22797
|
+
let autoRefreshTimer = null;
|
|
22798
|
+
const colors = {
|
|
22799
|
+
...DEFAULT_COLORS[config.domain] ?? DEFAULT_COLORS.energy,
|
|
22800
|
+
...config.colors
|
|
22801
|
+
};
|
|
22802
|
+
function $id(id) {
|
|
22803
|
+
if (config.$container && config.$container[0]) {
|
|
22804
|
+
return config.$container[0].querySelector(`#${id}`);
|
|
22805
|
+
}
|
|
22806
|
+
return document.getElementById(id);
|
|
22807
|
+
}
|
|
22808
|
+
function log(level, ...args) {
|
|
22809
|
+
const prefix = `[${config.domain.toUpperCase()}]`;
|
|
22810
|
+
console[level](prefix, ...args);
|
|
22811
|
+
}
|
|
22812
|
+
function calculateYAxisMax(values) {
|
|
22813
|
+
const maxValue = Math.max(...values, 0);
|
|
22814
|
+
if (config.domain === "temperature") {
|
|
22815
|
+
const tempConfig = config.temperatureConfig;
|
|
22816
|
+
if (tempConfig?.clampRange) {
|
|
22817
|
+
return tempConfig.clampRange.max;
|
|
22818
|
+
}
|
|
22819
|
+
const maxWithThreshold = Math.max(
|
|
22820
|
+
maxValue,
|
|
22821
|
+
tempConfig?.maxThreshold?.value ?? 0,
|
|
22822
|
+
tempConfig?.idealRange?.max ?? 0
|
|
22823
|
+
);
|
|
22824
|
+
return Math.ceil(maxWithThreshold + 5);
|
|
22825
|
+
}
|
|
22826
|
+
if (maxValue === 0) {
|
|
22827
|
+
return config.thresholdForLargeUnit ? config.thresholdForLargeUnit / 2 : 500;
|
|
22828
|
+
}
|
|
22829
|
+
let roundTo;
|
|
22830
|
+
if (config.thresholdForLargeUnit && maxValue >= config.thresholdForLargeUnit) {
|
|
22831
|
+
roundTo = config.thresholdForLargeUnit / 10;
|
|
22832
|
+
} else if (maxValue >= 1e3) {
|
|
22833
|
+
roundTo = 100;
|
|
22834
|
+
} else if (maxValue >= 100) {
|
|
22835
|
+
roundTo = 50;
|
|
22836
|
+
} else if (maxValue >= 10) {
|
|
22837
|
+
roundTo = 10;
|
|
22838
|
+
} else {
|
|
22839
|
+
roundTo = 5;
|
|
22840
|
+
}
|
|
22841
|
+
return Math.ceil(maxValue * 1.1 / roundTo) * roundTo;
|
|
22842
|
+
}
|
|
22843
|
+
function calculateYAxisMin(values) {
|
|
22844
|
+
if (config.domain !== "temperature") {
|
|
22845
|
+
return 0;
|
|
22846
|
+
}
|
|
22847
|
+
const tempConfig = config.temperatureConfig;
|
|
22848
|
+
if (tempConfig?.clampRange) {
|
|
22849
|
+
return tempConfig.clampRange.min;
|
|
22850
|
+
}
|
|
22851
|
+
const minValue = Math.min(...values);
|
|
22852
|
+
const minWithThreshold = Math.min(
|
|
22853
|
+
minValue,
|
|
22854
|
+
tempConfig?.minThreshold?.value ?? minValue,
|
|
22855
|
+
tempConfig?.idealRange?.min ?? minValue
|
|
22856
|
+
);
|
|
22857
|
+
return Math.floor(minWithThreshold - 5);
|
|
22858
|
+
}
|
|
22859
|
+
function buildTemperatureAnnotations() {
|
|
22860
|
+
const tempConfig = config.temperatureConfig;
|
|
22861
|
+
if (!tempConfig || config.domain !== "temperature") {
|
|
22862
|
+
return {};
|
|
22863
|
+
}
|
|
22864
|
+
const annotations = {};
|
|
22865
|
+
const createLineAnnotation = (line, id) => {
|
|
22866
|
+
const borderDash = line.lineStyle === "dashed" ? [6, 6] : line.lineStyle === "dotted" ? [2, 2] : [];
|
|
22867
|
+
return {
|
|
22868
|
+
type: "line",
|
|
22869
|
+
yMin: line.value,
|
|
22870
|
+
yMax: line.value,
|
|
22871
|
+
borderColor: line.color,
|
|
22872
|
+
borderWidth: line.lineWidth ?? 2,
|
|
22873
|
+
borderDash,
|
|
22874
|
+
label: {
|
|
22875
|
+
display: true,
|
|
22876
|
+
content: line.label,
|
|
22877
|
+
position: "end",
|
|
22878
|
+
backgroundColor: line.color,
|
|
22879
|
+
color: "#fff",
|
|
22880
|
+
font: { size: 10, weight: "bold" },
|
|
22881
|
+
padding: { x: 4, y: 2 }
|
|
22882
|
+
}
|
|
22883
|
+
};
|
|
22884
|
+
};
|
|
22885
|
+
if (tempConfig.minThreshold) {
|
|
22886
|
+
annotations["minThreshold"] = createLineAnnotation(tempConfig.minThreshold);
|
|
22887
|
+
}
|
|
22888
|
+
if (tempConfig.maxThreshold) {
|
|
22889
|
+
annotations["maxThreshold"] = createLineAnnotation(tempConfig.maxThreshold);
|
|
22890
|
+
}
|
|
22891
|
+
if (tempConfig.idealRange) {
|
|
22892
|
+
annotations["idealRange"] = {
|
|
22893
|
+
type: "box",
|
|
22894
|
+
yMin: tempConfig.idealRange.min,
|
|
22895
|
+
yMax: tempConfig.idealRange.max,
|
|
22896
|
+
backgroundColor: tempConfig.idealRange.color,
|
|
22897
|
+
borderWidth: 0,
|
|
22898
|
+
label: tempConfig.idealRange.label ? {
|
|
22899
|
+
display: true,
|
|
22900
|
+
content: tempConfig.idealRange.label,
|
|
22901
|
+
position: { x: "start", y: "center" },
|
|
22902
|
+
color: "#666",
|
|
22903
|
+
font: { size: 10 }
|
|
22904
|
+
} : void 0
|
|
22905
|
+
};
|
|
22906
|
+
}
|
|
22907
|
+
return annotations;
|
|
22908
|
+
}
|
|
22909
|
+
function buildIdealRangeAnnotation() {
|
|
22910
|
+
if (!currentIdealRange) {
|
|
22911
|
+
return {};
|
|
22912
|
+
}
|
|
22913
|
+
const { min, max, enabled = true } = currentIdealRange;
|
|
22914
|
+
if (!enabled || min === 0 && max === 0 || min >= max) {
|
|
22915
|
+
return {};
|
|
22916
|
+
}
|
|
22917
|
+
const defaultColors = {
|
|
22918
|
+
temperature: { bg: "rgba(34, 197, 94, 0.15)", border: "rgba(34, 197, 94, 0.4)" },
|
|
22919
|
+
energy: { bg: "rgba(37, 99, 235, 0.1)", border: "rgba(37, 99, 235, 0.3)" },
|
|
22920
|
+
water: { bg: "rgba(2, 136, 209, 0.1)", border: "rgba(2, 136, 209, 0.3)" },
|
|
22921
|
+
gas: { bg: "rgba(234, 88, 12, 0.1)", border: "rgba(234, 88, 12, 0.3)" }
|
|
22922
|
+
};
|
|
22923
|
+
const domainDefaults = defaultColors[config.domain] || defaultColors.energy;
|
|
22924
|
+
return {
|
|
22925
|
+
idealRangeBox: {
|
|
22926
|
+
type: "box",
|
|
22927
|
+
yMin: min,
|
|
22928
|
+
yMax: max,
|
|
22929
|
+
backgroundColor: currentIdealRange.color || domainDefaults.bg,
|
|
22930
|
+
borderColor: currentIdealRange.borderColor || domainDefaults.border,
|
|
22931
|
+
borderWidth: 1,
|
|
22932
|
+
label: currentIdealRange.label ? {
|
|
22933
|
+
display: true,
|
|
22934
|
+
content: currentIdealRange.label,
|
|
22935
|
+
position: { x: "start", y: "center" },
|
|
22936
|
+
color: "#666",
|
|
22937
|
+
font: { size: 10, style: "italic" },
|
|
22938
|
+
backgroundColor: "rgba(255, 255, 255, 0.8)",
|
|
22939
|
+
padding: { x: 4, y: 2 }
|
|
22940
|
+
} : void 0
|
|
22941
|
+
}
|
|
22942
|
+
};
|
|
22943
|
+
}
|
|
22944
|
+
function formatValue(value, includeUnit = true) {
|
|
22945
|
+
const decimals = config.decimalPlaces ?? DEFAULT_CONFIG.decimalPlaces;
|
|
22946
|
+
if (config.unitLarge && config.thresholdForLargeUnit && value >= config.thresholdForLargeUnit) {
|
|
22947
|
+
const converted = value / config.thresholdForLargeUnit;
|
|
22948
|
+
return includeUnit ? `${converted.toFixed(decimals)} ${config.unitLarge}` : converted.toFixed(decimals);
|
|
22949
|
+
}
|
|
22950
|
+
return includeUnit ? `${value.toFixed(decimals)} ${config.unit}` : value.toFixed(decimals);
|
|
22951
|
+
}
|
|
22952
|
+
function formatTickValue(value) {
|
|
22953
|
+
if (config.unitLarge && config.thresholdForLargeUnit && value >= config.thresholdForLargeUnit) {
|
|
22954
|
+
return `${(value / config.thresholdForLargeUnit).toFixed(1)}`;
|
|
22955
|
+
}
|
|
22956
|
+
return value.toFixed(0);
|
|
22957
|
+
}
|
|
22958
|
+
function buildChartConfig(data) {
|
|
22959
|
+
const yAxisMax = calculateYAxisMax(data.dailyTotals);
|
|
22960
|
+
const yAxisMin = calculateYAxisMin(data.dailyTotals);
|
|
22961
|
+
const tension = config.lineTension ?? DEFAULT_CONFIG.lineTension;
|
|
22962
|
+
const pointRadius = config.pointRadius ?? DEFAULT_CONFIG.pointRadius;
|
|
22963
|
+
const borderWidth = config.borderWidth ?? DEFAULT_CONFIG.borderWidth;
|
|
22964
|
+
const fill = config.fill ?? DEFAULT_CONFIG.fill;
|
|
22965
|
+
const showLegend = config.showLegend ?? DEFAULT_CONFIG.showLegend;
|
|
22966
|
+
const themeColors = THEME_COLORS[currentTheme];
|
|
22967
|
+
const isTemperature = config.domain === "temperature";
|
|
22968
|
+
let datasets;
|
|
22969
|
+
if (currentVizMode === "separate" && data.shoppingData && data.shoppingNames) {
|
|
22970
|
+
const shoppingColors = colors.shoppingColors || [
|
|
22971
|
+
"#2563eb",
|
|
22972
|
+
"#16a34a",
|
|
22973
|
+
"#ea580c",
|
|
22974
|
+
"#dc2626",
|
|
22975
|
+
"#8b5cf6",
|
|
22976
|
+
"#0891b2",
|
|
22977
|
+
"#65a30d",
|
|
22978
|
+
"#d97706",
|
|
22979
|
+
"#be185d",
|
|
22980
|
+
"#0d9488"
|
|
22981
|
+
];
|
|
22982
|
+
datasets = Object.entries(data.shoppingData).map(([shoppingId, values], index) => ({
|
|
22983
|
+
label: data.shoppingNames?.[shoppingId] || shoppingId,
|
|
22984
|
+
data: values,
|
|
22985
|
+
borderColor: shoppingColors[index % shoppingColors.length],
|
|
22986
|
+
backgroundColor: currentChartType === "line" ? `${shoppingColors[index % shoppingColors.length]}20` : shoppingColors[index % shoppingColors.length],
|
|
22987
|
+
fill: currentChartType === "line" && fill,
|
|
22988
|
+
tension,
|
|
22989
|
+
borderWidth,
|
|
22990
|
+
pointRadius: currentChartType === "line" ? pointRadius : 0,
|
|
22991
|
+
pointBackgroundColor: shoppingColors[index % shoppingColors.length],
|
|
22992
|
+
pointBorderColor: "#fff",
|
|
22993
|
+
pointBorderWidth: 2
|
|
22994
|
+
}));
|
|
22995
|
+
} else {
|
|
22996
|
+
const datasetLabel = isTemperature ? `Temperatura (${config.unit})` : `Consumo (${config.unit})`;
|
|
22997
|
+
datasets = [
|
|
22998
|
+
{
|
|
22999
|
+
label: datasetLabel,
|
|
23000
|
+
data: data.dailyTotals,
|
|
23001
|
+
borderColor: colors.primary,
|
|
23002
|
+
backgroundColor: currentChartType === "line" ? colors.background : colors.primary,
|
|
23003
|
+
fill: currentChartType === "line" && fill,
|
|
23004
|
+
tension,
|
|
23005
|
+
borderWidth,
|
|
23006
|
+
pointRadius: currentChartType === "line" ? pointRadius : 0,
|
|
23007
|
+
pointBackgroundColor: colors.pointBackground || colors.primary,
|
|
23008
|
+
pointBorderColor: colors.pointBorder || "#fff",
|
|
23009
|
+
pointBorderWidth: 2,
|
|
23010
|
+
borderRadius: currentChartType === "bar" ? 4 : 0
|
|
23011
|
+
}
|
|
23012
|
+
];
|
|
23013
|
+
}
|
|
23014
|
+
const temperatureAnnotations = buildTemperatureAnnotations();
|
|
23015
|
+
const idealRangeAnnotations = buildIdealRangeAnnotation();
|
|
23016
|
+
const allAnnotations = { ...temperatureAnnotations, ...idealRangeAnnotations };
|
|
23017
|
+
const yAxisLabel = config.unitLarge && config.thresholdForLargeUnit && yAxisMax >= config.thresholdForLargeUnit ? config.unitLarge : config.unit;
|
|
23018
|
+
return {
|
|
23019
|
+
type: currentChartType,
|
|
23020
|
+
data: {
|
|
23021
|
+
labels: data.labels,
|
|
23022
|
+
datasets
|
|
23023
|
+
},
|
|
23024
|
+
options: {
|
|
23025
|
+
responsive: true,
|
|
23026
|
+
maintainAspectRatio: false,
|
|
23027
|
+
animation: false,
|
|
23028
|
+
// CRITICAL: Prevents infinite growth bug
|
|
23029
|
+
plugins: {
|
|
23030
|
+
legend: {
|
|
23031
|
+
display: showLegend || currentVizMode === "separate",
|
|
23032
|
+
position: "bottom",
|
|
23033
|
+
labels: {
|
|
23034
|
+
color: themeColors.text
|
|
23035
|
+
}
|
|
23036
|
+
},
|
|
23037
|
+
tooltip: {
|
|
23038
|
+
backgroundColor: themeColors.tooltipBackground,
|
|
23039
|
+
titleColor: themeColors.tooltipText,
|
|
23040
|
+
bodyColor: themeColors.tooltipText,
|
|
23041
|
+
borderColor: themeColors.border,
|
|
23042
|
+
borderWidth: 1,
|
|
23043
|
+
callbacks: {
|
|
23044
|
+
label: function(context) {
|
|
23045
|
+
const value = context.parsed.y || 0;
|
|
23046
|
+
const label = context.dataset.label || "";
|
|
23047
|
+
return `${label}: ${formatValue(value)}`;
|
|
23048
|
+
}
|
|
23049
|
+
}
|
|
23050
|
+
},
|
|
23051
|
+
// Reference lines and ideal range (requires chartjs-plugin-annotation)
|
|
23052
|
+
annotation: Object.keys(allAnnotations).length > 0 ? {
|
|
23053
|
+
annotations: allAnnotations
|
|
23054
|
+
} : void 0
|
|
23055
|
+
},
|
|
23056
|
+
scales: {
|
|
23057
|
+
y: {
|
|
23058
|
+
beginAtZero: !isTemperature,
|
|
23059
|
+
// Temperature can have negative values
|
|
23060
|
+
min: yAxisMin,
|
|
23061
|
+
max: yAxisMax,
|
|
23062
|
+
// CRITICAL: Fixed max prevents animation loop
|
|
23063
|
+
grid: {
|
|
23064
|
+
color: themeColors.grid
|
|
23065
|
+
},
|
|
23066
|
+
title: {
|
|
23067
|
+
display: true,
|
|
23068
|
+
text: yAxisLabel,
|
|
23069
|
+
font: { size: 12 },
|
|
23070
|
+
color: themeColors.text
|
|
23071
|
+
},
|
|
23072
|
+
ticks: {
|
|
23073
|
+
font: { size: 11 },
|
|
23074
|
+
color: themeColors.textMuted,
|
|
23075
|
+
callback: function(value) {
|
|
23076
|
+
return formatTickValue(value);
|
|
23077
|
+
}
|
|
23078
|
+
}
|
|
23079
|
+
},
|
|
23080
|
+
x: {
|
|
23081
|
+
grid: {
|
|
23082
|
+
color: themeColors.grid
|
|
23083
|
+
},
|
|
23084
|
+
ticks: {
|
|
23085
|
+
font: { size: 11 },
|
|
23086
|
+
color: themeColors.textMuted
|
|
23087
|
+
}
|
|
23088
|
+
}
|
|
23089
|
+
}
|
|
23090
|
+
}
|
|
23091
|
+
};
|
|
23092
|
+
}
|
|
23093
|
+
function validateChartJs() {
|
|
23094
|
+
if (typeof Chart === "undefined") {
|
|
23095
|
+
log("error", "Chart.js not loaded. Cannot initialize chart.");
|
|
23096
|
+
config.onError?.(new Error("Chart.js not loaded"));
|
|
23097
|
+
return false;
|
|
23098
|
+
}
|
|
23099
|
+
return true;
|
|
23100
|
+
}
|
|
23101
|
+
function validateCanvas() {
|
|
23102
|
+
const canvas = $id(config.containerId);
|
|
23103
|
+
if (!canvas) {
|
|
23104
|
+
log("error", `Canvas #${config.containerId} not found`);
|
|
23105
|
+
config.onError?.(new Error(`Canvas #${config.containerId} not found`));
|
|
23106
|
+
return null;
|
|
23107
|
+
}
|
|
23108
|
+
return canvas;
|
|
23109
|
+
}
|
|
23110
|
+
function setupAutoRefresh() {
|
|
23111
|
+
if (config.autoRefreshInterval && config.autoRefreshInterval > 0) {
|
|
23112
|
+
if (autoRefreshTimer) {
|
|
23113
|
+
clearInterval(autoRefreshTimer);
|
|
23114
|
+
}
|
|
23115
|
+
autoRefreshTimer = setInterval(async () => {
|
|
23116
|
+
log("log", "Auto-refreshing data...");
|
|
23117
|
+
await instance.refresh(true);
|
|
23118
|
+
}, config.autoRefreshInterval);
|
|
23119
|
+
}
|
|
23120
|
+
}
|
|
23121
|
+
function cleanupAutoRefresh() {
|
|
23122
|
+
if (autoRefreshTimer) {
|
|
23123
|
+
clearInterval(autoRefreshTimer);
|
|
23124
|
+
autoRefreshTimer = null;
|
|
23125
|
+
}
|
|
23126
|
+
}
|
|
23127
|
+
function setupButtonHandlers() {
|
|
23128
|
+
if (config.settingsButtonId && config.onSettingsClick) {
|
|
23129
|
+
const settingsBtn = $id(config.settingsButtonId);
|
|
23130
|
+
if (settingsBtn) {
|
|
23131
|
+
settingsBtn.addEventListener("click", () => {
|
|
23132
|
+
log("log", "Settings button clicked");
|
|
23133
|
+
config.onSettingsClick?.();
|
|
23134
|
+
});
|
|
23135
|
+
log("log", "Settings button handler attached");
|
|
23136
|
+
}
|
|
23137
|
+
}
|
|
23138
|
+
if (config.maximizeButtonId && config.onMaximizeClick) {
|
|
23139
|
+
const maximizeBtn = $id(config.maximizeButtonId);
|
|
23140
|
+
if (maximizeBtn) {
|
|
23141
|
+
maximizeBtn.addEventListener("click", () => {
|
|
23142
|
+
log("log", "Maximize button clicked");
|
|
23143
|
+
config.onMaximizeClick?.();
|
|
23144
|
+
});
|
|
23145
|
+
log("log", "Maximize button handler attached");
|
|
23146
|
+
}
|
|
23147
|
+
}
|
|
23148
|
+
const enableExport = config.enableExport ?? DEFAULT_CONFIG.enableExport;
|
|
23149
|
+
if (enableExport && config.exportButtonId) {
|
|
23150
|
+
const exportBtn = $id(config.exportButtonId);
|
|
23151
|
+
if (exportBtn) {
|
|
23152
|
+
exportBtn.addEventListener("click", () => {
|
|
23153
|
+
log("log", "Export button clicked");
|
|
23154
|
+
if (config.onExportCSV && cachedData) {
|
|
23155
|
+
config.onExportCSV(cachedData);
|
|
23156
|
+
} else {
|
|
23157
|
+
instance.exportCSV();
|
|
23158
|
+
}
|
|
23159
|
+
});
|
|
23160
|
+
log("log", "Export button handler attached");
|
|
23161
|
+
}
|
|
23162
|
+
}
|
|
23163
|
+
}
|
|
23164
|
+
function generateCSVContent(data) {
|
|
23165
|
+
const rows = [];
|
|
23166
|
+
const decimals = config.decimalPlaces ?? DEFAULT_CONFIG.decimalPlaces;
|
|
23167
|
+
if (currentVizMode === "separate" && data.shoppingData && data.shoppingNames) {
|
|
23168
|
+
const shoppingHeaders = Object.keys(data.shoppingData).map(
|
|
23169
|
+
(id) => data.shoppingNames?.[id] || id
|
|
23170
|
+
);
|
|
23171
|
+
rows.push(["Data", ...shoppingHeaders, "Total"].join(";"));
|
|
23172
|
+
data.labels.forEach((label, index) => {
|
|
23173
|
+
const shoppingValues = Object.keys(data.shoppingData).map(
|
|
23174
|
+
(id) => data.shoppingData[id][index].toFixed(decimals)
|
|
23175
|
+
);
|
|
23176
|
+
rows.push([label, ...shoppingValues, data.dailyTotals[index].toFixed(decimals)].join(";"));
|
|
23177
|
+
});
|
|
23178
|
+
} else {
|
|
23179
|
+
rows.push(["Data", `Consumo (${config.unit})`].join(";"));
|
|
23180
|
+
data.labels.forEach((label, index) => {
|
|
23181
|
+
rows.push([label, data.dailyTotals[index].toFixed(decimals)].join(";"));
|
|
23182
|
+
});
|
|
23183
|
+
}
|
|
23184
|
+
const total = data.dailyTotals.reduce((sum, v) => sum + v, 0);
|
|
23185
|
+
const avg = total / data.dailyTotals.length;
|
|
23186
|
+
rows.push("");
|
|
23187
|
+
rows.push(["Total", total.toFixed(decimals)].join(";"));
|
|
23188
|
+
rows.push(["M\xE9dia", avg.toFixed(decimals)].join(";"));
|
|
23189
|
+
return rows.join("\n");
|
|
23190
|
+
}
|
|
23191
|
+
function downloadCSV(content, filename) {
|
|
23192
|
+
const BOM = "\uFEFF";
|
|
23193
|
+
const blob = new Blob([BOM + content], { type: "text/csv;charset=utf-8" });
|
|
23194
|
+
const url = URL.createObjectURL(blob);
|
|
23195
|
+
const link = document.createElement("a");
|
|
23196
|
+
link.href = url;
|
|
23197
|
+
link.download = `${filename}.csv`;
|
|
23198
|
+
document.body.appendChild(link);
|
|
23199
|
+
link.click();
|
|
23200
|
+
document.body.removeChild(link);
|
|
23201
|
+
URL.revokeObjectURL(url);
|
|
23202
|
+
log("log", `CSV exported: ${filename}.csv`);
|
|
23203
|
+
}
|
|
23204
|
+
function updateTitle() {
|
|
23205
|
+
if (config.titleElementId) {
|
|
23206
|
+
const titleEl = $id(config.titleElementId);
|
|
23207
|
+
if (titleEl) {
|
|
23208
|
+
if (currentPeriod === 0) {
|
|
23209
|
+
titleEl.textContent = `Consumo - Per\xEDodo Personalizado`;
|
|
23210
|
+
} else {
|
|
23211
|
+
titleEl.textContent = `Consumo dos \xFAltimos ${currentPeriod} dias`;
|
|
23212
|
+
}
|
|
23213
|
+
}
|
|
23214
|
+
}
|
|
23215
|
+
}
|
|
23216
|
+
const instance = {
|
|
23217
|
+
async render() {
|
|
23218
|
+
log("log", "Rendering chart...");
|
|
23219
|
+
if (!validateChartJs()) return;
|
|
23220
|
+
const canvas = validateCanvas();
|
|
23221
|
+
if (!canvas) return;
|
|
23222
|
+
try {
|
|
23223
|
+
log("log", `Fetching ${currentPeriod} days of data...`);
|
|
23224
|
+
cachedData = await config.fetchData(currentPeriod);
|
|
23225
|
+
cachedData.fetchTimestamp = Date.now();
|
|
23226
|
+
if (config.onBeforeRender) {
|
|
23227
|
+
cachedData = config.onBeforeRender(cachedData);
|
|
23228
|
+
}
|
|
23229
|
+
if (chartInstance) {
|
|
23230
|
+
chartInstance.destroy();
|
|
23231
|
+
chartInstance = null;
|
|
23232
|
+
}
|
|
23233
|
+
const ctx = canvas.getContext("2d");
|
|
23234
|
+
const chartConfig = buildChartConfig(cachedData);
|
|
23235
|
+
chartInstance = new Chart(ctx, chartConfig);
|
|
23236
|
+
isRendered = true;
|
|
23237
|
+
const yAxisMax = calculateYAxisMax(cachedData.dailyTotals);
|
|
23238
|
+
log("log", `Chart initialized with yAxisMax: ${yAxisMax}`);
|
|
23239
|
+
config.onDataLoaded?.(cachedData);
|
|
23240
|
+
config.onAfterRender?.(chartInstance);
|
|
23241
|
+
setupButtonHandlers();
|
|
23242
|
+
updateTitle();
|
|
23243
|
+
setupAutoRefresh();
|
|
23244
|
+
} catch (error) {
|
|
23245
|
+
log("error", "Failed to render chart:", error);
|
|
23246
|
+
config.onError?.(error instanceof Error ? error : new Error(String(error)));
|
|
23247
|
+
}
|
|
23248
|
+
},
|
|
23249
|
+
async update(data) {
|
|
23250
|
+
if (data) {
|
|
23251
|
+
cachedData = data;
|
|
23252
|
+
cachedData.fetchTimestamp = Date.now();
|
|
23253
|
+
}
|
|
23254
|
+
if (!chartInstance || !cachedData) {
|
|
23255
|
+
log("warn", "Cannot update: chart not initialized or no data");
|
|
23256
|
+
return;
|
|
23257
|
+
}
|
|
23258
|
+
let renderData = cachedData;
|
|
23259
|
+
if (config.onBeforeRender) {
|
|
23260
|
+
renderData = config.onBeforeRender(cachedData);
|
|
23261
|
+
}
|
|
23262
|
+
const chartConfig = buildChartConfig(renderData);
|
|
23263
|
+
chartInstance.data = chartConfig.data;
|
|
23264
|
+
chartInstance.options = chartConfig.options;
|
|
23265
|
+
chartInstance.update("none");
|
|
23266
|
+
log("log", "Chart updated");
|
|
23267
|
+
},
|
|
23268
|
+
setChartType(type) {
|
|
23269
|
+
if (currentChartType === type) return;
|
|
23270
|
+
log("log", `Changing chart type to: ${type}`);
|
|
23271
|
+
currentChartType = type;
|
|
23272
|
+
if (cachedData && chartInstance) {
|
|
23273
|
+
const canvas = validateCanvas();
|
|
23274
|
+
if (canvas) {
|
|
23275
|
+
chartInstance.destroy();
|
|
23276
|
+
const ctx = canvas.getContext("2d");
|
|
23277
|
+
chartInstance = new Chart(ctx, buildChartConfig(cachedData));
|
|
23278
|
+
}
|
|
23279
|
+
}
|
|
23280
|
+
},
|
|
23281
|
+
setVizMode(mode) {
|
|
23282
|
+
if (currentVizMode === mode) return;
|
|
23283
|
+
log("log", `Changing viz mode to: ${mode}`);
|
|
23284
|
+
currentVizMode = mode;
|
|
23285
|
+
if (cachedData) {
|
|
23286
|
+
this.update();
|
|
23287
|
+
}
|
|
23288
|
+
},
|
|
23289
|
+
async setPeriod(days) {
|
|
23290
|
+
if (currentPeriod === days) return;
|
|
23291
|
+
log("log", `Changing period to: ${days} days`);
|
|
23292
|
+
currentPeriod = days;
|
|
23293
|
+
updateTitle();
|
|
23294
|
+
await this.refresh(true);
|
|
23295
|
+
},
|
|
23296
|
+
async refresh(forceRefresh = false) {
|
|
23297
|
+
if (!forceRefresh && cachedData?.fetchTimestamp) {
|
|
23298
|
+
const age = Date.now() - cachedData.fetchTimestamp;
|
|
23299
|
+
const ttl = config.cacheTTL ?? DEFAULT_CONFIG.cacheTTL;
|
|
23300
|
+
if (age < ttl) {
|
|
23301
|
+
log("log", `Using cached data (age: ${Math.round(age / 1e3)}s)`);
|
|
23302
|
+
return;
|
|
23303
|
+
}
|
|
23304
|
+
}
|
|
23305
|
+
log("log", "Refreshing data...");
|
|
23306
|
+
await this.render();
|
|
23307
|
+
},
|
|
23308
|
+
destroy() {
|
|
23309
|
+
log("log", "Destroying chart...");
|
|
23310
|
+
cleanupAutoRefresh();
|
|
23311
|
+
if (chartInstance) {
|
|
23312
|
+
chartInstance.destroy();
|
|
23313
|
+
chartInstance = null;
|
|
23314
|
+
}
|
|
23315
|
+
cachedData = null;
|
|
23316
|
+
isRendered = false;
|
|
23317
|
+
},
|
|
23318
|
+
getChartInstance() {
|
|
23319
|
+
return chartInstance;
|
|
23320
|
+
},
|
|
23321
|
+
getCachedData() {
|
|
23322
|
+
return cachedData;
|
|
23323
|
+
},
|
|
23324
|
+
getState() {
|
|
23325
|
+
return {
|
|
23326
|
+
period: currentPeriod,
|
|
23327
|
+
chartType: currentChartType,
|
|
23328
|
+
vizMode: currentVizMode,
|
|
23329
|
+
theme: currentTheme,
|
|
23330
|
+
isRendered
|
|
23331
|
+
};
|
|
23332
|
+
},
|
|
23333
|
+
exportCSV(filename) {
|
|
23334
|
+
if (!cachedData) {
|
|
23335
|
+
log("warn", "Cannot export: no data available");
|
|
23336
|
+
return;
|
|
23337
|
+
}
|
|
23338
|
+
const defaultFilename = config.exportFilename || `${config.domain}-consumo-${(/* @__PURE__ */ new Date()).toISOString().slice(0, 10)}`;
|
|
23339
|
+
const csvContent = generateCSVContent(cachedData);
|
|
23340
|
+
downloadCSV(csvContent, filename || defaultFilename);
|
|
23341
|
+
},
|
|
23342
|
+
setTheme(theme) {
|
|
23343
|
+
if (currentTheme === theme) return;
|
|
23344
|
+
log("log", `Changing theme to: ${theme}`);
|
|
23345
|
+
currentTheme = theme;
|
|
23346
|
+
if (cachedData && chartInstance) {
|
|
23347
|
+
const canvas = validateCanvas();
|
|
23348
|
+
if (canvas) {
|
|
23349
|
+
chartInstance.destroy();
|
|
23350
|
+
const ctx = canvas.getContext("2d");
|
|
23351
|
+
chartInstance = new Chart(ctx, buildChartConfig(cachedData));
|
|
23352
|
+
}
|
|
23353
|
+
}
|
|
23354
|
+
},
|
|
23355
|
+
setIdealRange(range) {
|
|
23356
|
+
const rangeChanged = JSON.stringify(currentIdealRange) !== JSON.stringify(range);
|
|
23357
|
+
if (!rangeChanged) return;
|
|
23358
|
+
if (range) {
|
|
23359
|
+
log("log", `Setting ideal range: ${range.min} - ${range.max}`);
|
|
23360
|
+
} else {
|
|
23361
|
+
log("log", "Clearing ideal range");
|
|
23362
|
+
}
|
|
23363
|
+
currentIdealRange = range;
|
|
23364
|
+
if (cachedData && chartInstance) {
|
|
23365
|
+
const canvas = validateCanvas();
|
|
23366
|
+
if (canvas) {
|
|
23367
|
+
chartInstance.destroy();
|
|
23368
|
+
const ctx = canvas.getContext("2d");
|
|
23369
|
+
chartInstance = new Chart(ctx, buildChartConfig(cachedData));
|
|
23370
|
+
}
|
|
23371
|
+
}
|
|
23372
|
+
},
|
|
23373
|
+
getIdealRange() {
|
|
23374
|
+
return currentIdealRange;
|
|
23375
|
+
}
|
|
23376
|
+
};
|
|
23377
|
+
return instance;
|
|
23378
|
+
}
|
|
23379
|
+
|
|
23380
|
+
// src/components/Consumption7DaysChart/createConsumptionModal.ts
|
|
23381
|
+
var DOMAIN_CONFIG3 = {
|
|
23382
|
+
energy: { name: "Energia", icon: "\u26A1" },
|
|
23383
|
+
water: { name: "\xC1gua", icon: "\u{1F4A7}" },
|
|
23384
|
+
gas: { name: "G\xE1s", icon: "\u{1F525}" },
|
|
23385
|
+
temperature: { name: "Temperatura", icon: "\u{1F321}\uFE0F" }
|
|
23386
|
+
};
|
|
23387
|
+
function createConsumptionModal(config) {
|
|
23388
|
+
const modalId = `myio-consumption-modal-${Date.now()}`;
|
|
23389
|
+
let modalElement = null;
|
|
23390
|
+
let chartInstance = null;
|
|
23391
|
+
let headerInstance = null;
|
|
23392
|
+
let currentTheme = config.theme ?? "light";
|
|
23393
|
+
let currentChartType = config.defaultChartType ?? "line";
|
|
23394
|
+
let currentVizMode = config.defaultVizMode ?? "total";
|
|
23395
|
+
let isMaximized = false;
|
|
23396
|
+
const domainCfg = DOMAIN_CONFIG3[config.domain] || { name: config.domain, icon: "\u{1F4CA}" };
|
|
23397
|
+
const title = config.title || `${domainCfg.name} - Hist\xF3rico de Consumo`;
|
|
23398
|
+
function getThemeColors2() {
|
|
23399
|
+
return THEME_COLORS[currentTheme];
|
|
23400
|
+
}
|
|
23401
|
+
function renderModal4() {
|
|
23402
|
+
const colors = getThemeColors2();
|
|
23403
|
+
const exportFormats = config.exportFormats || ["csv"];
|
|
23404
|
+
headerInstance = createModalHeader({
|
|
23405
|
+
id: modalId,
|
|
23406
|
+
title,
|
|
23407
|
+
icon: domainCfg.icon,
|
|
23408
|
+
theme: currentTheme,
|
|
23409
|
+
isMaximized,
|
|
23410
|
+
exportFormats,
|
|
23411
|
+
onExport: (format) => {
|
|
23412
|
+
if (config.onExport) {
|
|
23413
|
+
config.onExport(format);
|
|
23414
|
+
} else {
|
|
23415
|
+
if (format === "csv") {
|
|
23416
|
+
chartInstance?.exportCSV();
|
|
23417
|
+
} else {
|
|
23418
|
+
console.warn(`[ConsumptionModal] Export format "${format}" requires custom onExport handler`);
|
|
23419
|
+
}
|
|
23420
|
+
}
|
|
23421
|
+
},
|
|
23422
|
+
onThemeToggle: (theme) => {
|
|
23423
|
+
currentTheme = theme;
|
|
23424
|
+
chartInstance?.setTheme(currentTheme);
|
|
23425
|
+
updateModal();
|
|
23426
|
+
},
|
|
23427
|
+
onMaximize: (maximized) => {
|
|
23428
|
+
isMaximized = maximized;
|
|
23429
|
+
updateModal();
|
|
23430
|
+
},
|
|
23431
|
+
onClose: () => {
|
|
23432
|
+
instance.close();
|
|
23433
|
+
}
|
|
23434
|
+
});
|
|
23435
|
+
return `
|
|
23436
|
+
<div class="myio-consumption-modal-overlay" style="
|
|
23437
|
+
position: fixed;
|
|
23438
|
+
top: 0;
|
|
23439
|
+
left: 0;
|
|
23440
|
+
width: 100%;
|
|
23441
|
+
height: 100%;
|
|
23442
|
+
background: rgba(0, 0, 0, 0.5);
|
|
23443
|
+
backdrop-filter: blur(2px);
|
|
23444
|
+
z-index: 99998;
|
|
23445
|
+
display: flex;
|
|
23446
|
+
justify-content: center;
|
|
23447
|
+
align-items: center;
|
|
23448
|
+
">
|
|
23449
|
+
<div class="myio-consumption-modal-content" style="
|
|
23450
|
+
background: ${colors.chartBackground};
|
|
23451
|
+
border-radius: ${isMaximized ? "0" : "10px"};
|
|
23452
|
+
width: ${isMaximized ? "100%" : "95%"};
|
|
23453
|
+
max-width: ${isMaximized ? "100%" : "1200px"};
|
|
23454
|
+
height: ${isMaximized ? "100%" : "85vh"};
|
|
23455
|
+
display: flex;
|
|
23456
|
+
flex-direction: column;
|
|
23457
|
+
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
|
|
23458
|
+
overflow: hidden;
|
|
23459
|
+
">
|
|
23460
|
+
<!-- MyIO Premium Header (using ModalHeader component) -->
|
|
23461
|
+
${headerInstance.render()}
|
|
23462
|
+
|
|
23463
|
+
<!-- Controls Bar -->
|
|
23464
|
+
<div class="myio-consumption-modal-controls" style="
|
|
23465
|
+
display: flex;
|
|
23466
|
+
gap: 16px;
|
|
23467
|
+
padding: 12px 16px;
|
|
23468
|
+
background: ${currentTheme === "dark" ? "#374151" : "#f7f7f7"};
|
|
23469
|
+
border-bottom: 1px solid ${colors.border};
|
|
23470
|
+
align-items: center;
|
|
23471
|
+
flex-wrap: wrap;
|
|
23472
|
+
">
|
|
23473
|
+
<!-- Viz Mode Tabs -->
|
|
23474
|
+
<div style="display: flex; gap: 2px; background: ${currentTheme === "dark" ? "#4b5563" : "#e5e7eb"}; border-radius: 8px; padding: 2px;">
|
|
23475
|
+
<button id="${modalId}-viz-total" style="
|
|
23476
|
+
padding: 6px 12px;
|
|
23477
|
+
border: none;
|
|
23478
|
+
border-radius: 6px;
|
|
23479
|
+
font-size: 13px;
|
|
23480
|
+
cursor: pointer;
|
|
23481
|
+
transition: all 0.2s;
|
|
23482
|
+
background: ${currentVizMode === "total" ? "#3e1a7d" : "transparent"};
|
|
23483
|
+
color: ${currentVizMode === "total" ? "white" : colors.text};
|
|
23484
|
+
">Consolidado</button>
|
|
23485
|
+
<button id="${modalId}-viz-separate" style="
|
|
23486
|
+
padding: 6px 12px;
|
|
23487
|
+
border: none;
|
|
23488
|
+
border-radius: 6px;
|
|
23489
|
+
font-size: 13px;
|
|
23490
|
+
cursor: pointer;
|
|
23491
|
+
transition: all 0.2s;
|
|
23492
|
+
background: ${currentVizMode === "separate" ? "#3e1a7d" : "transparent"};
|
|
23493
|
+
color: ${currentVizMode === "separate" ? "white" : colors.text};
|
|
23494
|
+
">Por Shopping</button>
|
|
23495
|
+
</div>
|
|
23496
|
+
|
|
23497
|
+
<!-- Chart Type Tabs -->
|
|
23498
|
+
<div style="display: flex; gap: 2px; background: ${currentTheme === "dark" ? "#4b5563" : "#e5e7eb"}; border-radius: 8px; padding: 2px;">
|
|
23499
|
+
<button id="${modalId}-type-line" style="
|
|
23500
|
+
padding: 6px 12px;
|
|
23501
|
+
border: none;
|
|
23502
|
+
border-radius: 6px;
|
|
23503
|
+
font-size: 13px;
|
|
23504
|
+
cursor: pointer;
|
|
23505
|
+
transition: all 0.2s;
|
|
23506
|
+
background: ${currentChartType === "line" ? "#3e1a7d" : "transparent"};
|
|
23507
|
+
color: ${currentChartType === "line" ? "white" : colors.text};
|
|
23508
|
+
">Linhas</button>
|
|
23509
|
+
<button id="${modalId}-type-bar" style="
|
|
23510
|
+
padding: 6px 12px;
|
|
23511
|
+
border: none;
|
|
23512
|
+
border-radius: 6px;
|
|
23513
|
+
font-size: 13px;
|
|
23514
|
+
cursor: pointer;
|
|
23515
|
+
transition: all 0.2s;
|
|
23516
|
+
background: ${currentChartType === "bar" ? "#3e1a7d" : "transparent"};
|
|
23517
|
+
color: ${currentChartType === "bar" ? "white" : colors.text};
|
|
23518
|
+
">Barras</button>
|
|
23519
|
+
</div>
|
|
23520
|
+
</div>
|
|
23521
|
+
|
|
23522
|
+
<!-- Chart Container -->
|
|
23523
|
+
<div style="
|
|
23524
|
+
flex: 1;
|
|
23525
|
+
padding: 16px;
|
|
23526
|
+
min-height: 0;
|
|
23527
|
+
position: relative;
|
|
23528
|
+
background: ${colors.chartBackground};
|
|
23529
|
+
">
|
|
23530
|
+
<canvas id="${modalId}-chart" style="width: 100%; height: 100%;"></canvas>
|
|
23531
|
+
</div>
|
|
23532
|
+
</div>
|
|
23533
|
+
</div>
|
|
23534
|
+
`;
|
|
23535
|
+
}
|
|
23536
|
+
function setupListeners() {
|
|
23537
|
+
if (!modalElement) return;
|
|
23538
|
+
headerInstance?.attachListeners();
|
|
23539
|
+
document.getElementById(`${modalId}-viz-total`)?.addEventListener("click", () => {
|
|
23540
|
+
currentVizMode = "total";
|
|
23541
|
+
chartInstance?.setVizMode("total");
|
|
23542
|
+
updateControlStyles();
|
|
23543
|
+
});
|
|
23544
|
+
document.getElementById(`${modalId}-viz-separate`)?.addEventListener("click", () => {
|
|
23545
|
+
currentVizMode = "separate";
|
|
23546
|
+
chartInstance?.setVizMode("separate");
|
|
23547
|
+
updateControlStyles();
|
|
23548
|
+
});
|
|
23549
|
+
document.getElementById(`${modalId}-type-line`)?.addEventListener("click", () => {
|
|
23550
|
+
currentChartType = "line";
|
|
23551
|
+
chartInstance?.setChartType("line");
|
|
23552
|
+
updateControlStyles();
|
|
23553
|
+
});
|
|
23554
|
+
document.getElementById(`${modalId}-type-bar`)?.addEventListener("click", () => {
|
|
23555
|
+
currentChartType = "bar";
|
|
23556
|
+
chartInstance?.setChartType("bar");
|
|
23557
|
+
updateControlStyles();
|
|
23558
|
+
});
|
|
23559
|
+
modalElement.querySelector(".myio-consumption-modal-overlay")?.addEventListener("click", (e) => {
|
|
23560
|
+
if (e.target.classList.contains("myio-consumption-modal-overlay")) {
|
|
23561
|
+
instance.close();
|
|
23562
|
+
}
|
|
23563
|
+
});
|
|
23564
|
+
const handleKeydown = (e) => {
|
|
23565
|
+
if (e.key === "Escape") {
|
|
23566
|
+
instance.close();
|
|
23567
|
+
}
|
|
23568
|
+
};
|
|
23569
|
+
document.addEventListener("keydown", handleKeydown);
|
|
23570
|
+
modalElement.__handleKeydown = handleKeydown;
|
|
23571
|
+
}
|
|
23572
|
+
function updateControlStyles() {
|
|
23573
|
+
const colors = getThemeColors2();
|
|
23574
|
+
const vizTotalBtn = document.getElementById(`${modalId}-viz-total`);
|
|
23575
|
+
const vizSeparateBtn = document.getElementById(`${modalId}-viz-separate`);
|
|
23576
|
+
if (vizTotalBtn) {
|
|
23577
|
+
vizTotalBtn.style.background = currentVizMode === "total" ? "#3e1a7d" : "transparent";
|
|
23578
|
+
vizTotalBtn.style.color = currentVizMode === "total" ? "white" : colors.text;
|
|
23579
|
+
}
|
|
23580
|
+
if (vizSeparateBtn) {
|
|
23581
|
+
vizSeparateBtn.style.background = currentVizMode === "separate" ? "#3e1a7d" : "transparent";
|
|
23582
|
+
vizSeparateBtn.style.color = currentVizMode === "separate" ? "white" : colors.text;
|
|
23583
|
+
}
|
|
23584
|
+
const typeLineBtn = document.getElementById(`${modalId}-type-line`);
|
|
23585
|
+
const typeBarBtn = document.getElementById(`${modalId}-type-bar`);
|
|
23586
|
+
if (typeLineBtn) {
|
|
23587
|
+
typeLineBtn.style.background = currentChartType === "line" ? "#3e1a7d" : "transparent";
|
|
23588
|
+
typeLineBtn.style.color = currentChartType === "line" ? "white" : colors.text;
|
|
23589
|
+
}
|
|
23590
|
+
if (typeBarBtn) {
|
|
23591
|
+
typeBarBtn.style.background = currentChartType === "bar" ? "#3e1a7d" : "transparent";
|
|
23592
|
+
typeBarBtn.style.color = currentChartType === "bar" ? "white" : colors.text;
|
|
23593
|
+
}
|
|
23594
|
+
}
|
|
23595
|
+
function updateModal() {
|
|
23596
|
+
if (!modalElement) return;
|
|
23597
|
+
const cachedData = chartInstance?.getCachedData();
|
|
23598
|
+
headerInstance?.destroy();
|
|
23599
|
+
chartInstance?.destroy();
|
|
23600
|
+
modalElement.innerHTML = renderModal4();
|
|
23601
|
+
setupListeners();
|
|
23602
|
+
if (cachedData) {
|
|
23603
|
+
chartInstance = createConsumption7DaysChart({
|
|
23604
|
+
...config,
|
|
23605
|
+
containerId: `${modalId}-chart`,
|
|
23606
|
+
theme: currentTheme,
|
|
23607
|
+
defaultChartType: currentChartType,
|
|
23608
|
+
defaultVizMode: currentVizMode
|
|
23609
|
+
});
|
|
23610
|
+
chartInstance.update(cachedData);
|
|
23611
|
+
}
|
|
23612
|
+
}
|
|
23613
|
+
const instance = {
|
|
23614
|
+
async open() {
|
|
23615
|
+
modalElement = document.createElement("div");
|
|
23616
|
+
modalElement.id = modalId;
|
|
23617
|
+
modalElement.innerHTML = renderModal4();
|
|
23618
|
+
const container = config.container || document.body;
|
|
23619
|
+
container.appendChild(modalElement);
|
|
23620
|
+
setupListeners();
|
|
23621
|
+
chartInstance = createConsumption7DaysChart({
|
|
23622
|
+
...config,
|
|
23623
|
+
containerId: `${modalId}-chart`,
|
|
23624
|
+
theme: currentTheme,
|
|
23625
|
+
defaultChartType: currentChartType,
|
|
23626
|
+
defaultVizMode: currentVizMode
|
|
23627
|
+
});
|
|
23628
|
+
await chartInstance.render();
|
|
23629
|
+
},
|
|
23630
|
+
close() {
|
|
23631
|
+
if (modalElement) {
|
|
23632
|
+
const handleKeydown = modalElement.__handleKeydown;
|
|
23633
|
+
if (handleKeydown) {
|
|
23634
|
+
document.removeEventListener("keydown", handleKeydown);
|
|
23635
|
+
}
|
|
23636
|
+
headerInstance?.destroy();
|
|
23637
|
+
headerInstance = null;
|
|
23638
|
+
chartInstance?.destroy();
|
|
23639
|
+
chartInstance = null;
|
|
23640
|
+
modalElement.remove();
|
|
23641
|
+
modalElement = null;
|
|
23642
|
+
config.onClose?.();
|
|
23643
|
+
}
|
|
23644
|
+
},
|
|
23645
|
+
getChart() {
|
|
23646
|
+
return chartInstance;
|
|
23647
|
+
},
|
|
23648
|
+
destroy() {
|
|
23649
|
+
instance.close();
|
|
23650
|
+
}
|
|
23651
|
+
};
|
|
23652
|
+
return instance;
|
|
23653
|
+
}
|
|
23654
|
+
|
|
23655
|
+
// src/components/Consumption7DaysChart/createConsumptionChartWidget.ts
|
|
23656
|
+
var DOMAIN_CONFIG4 = {
|
|
23657
|
+
energy: {
|
|
23658
|
+
name: "Energia",
|
|
23659
|
+
icon: "\u26A1",
|
|
23660
|
+
color: "#6c2fbf",
|
|
23661
|
+
colors: ["#2563eb", "#16a34a", "#8b5cf6", "#ea580c", "#dc2626"]
|
|
23662
|
+
},
|
|
23663
|
+
water: {
|
|
23664
|
+
name: "\xC1gua",
|
|
23665
|
+
icon: "\u{1F4A7}",
|
|
23666
|
+
color: "#0288d1",
|
|
23667
|
+
colors: ["#0288d1", "#06b6d4", "#0891b2", "#22d3ee", "#67e8f9"]
|
|
23668
|
+
},
|
|
23669
|
+
gas: {
|
|
23670
|
+
name: "G\xE1s",
|
|
23671
|
+
icon: "\u{1F525}",
|
|
23672
|
+
color: "#ea580c",
|
|
23673
|
+
colors: ["#ea580c", "#f97316", "#fb923c", "#fdba74", "#fed7aa"]
|
|
23674
|
+
},
|
|
23675
|
+
temperature: {
|
|
23676
|
+
name: "Temperatura",
|
|
23677
|
+
icon: "\u{1F321}\uFE0F",
|
|
23678
|
+
color: "#e65100",
|
|
23679
|
+
colors: ["#dc2626", "#059669", "#0ea5e9", "#f59e0b", "#8b5cf6"]
|
|
23680
|
+
}
|
|
23681
|
+
};
|
|
23682
|
+
function getWidgetStyles(theme, primaryColor) {
|
|
23683
|
+
const colors = THEME_COLORS[theme];
|
|
23684
|
+
return `
|
|
23685
|
+
.myio-chart-widget {
|
|
23686
|
+
font-family: Inter, system-ui, -apple-system, 'Segoe UI', Roboto, Arial, sans-serif;
|
|
23687
|
+
background: ${colors.chartBackground};
|
|
23688
|
+
border: 1px solid ${colors.border};
|
|
23689
|
+
border-radius: 16px;
|
|
23690
|
+
overflow: hidden;
|
|
23691
|
+
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
|
|
23692
|
+
}
|
|
23693
|
+
|
|
23694
|
+
.myio-chart-widget.dark {
|
|
23695
|
+
background: ${THEME_COLORS.dark.chartBackground};
|
|
23696
|
+
border-color: ${THEME_COLORS.dark.border};
|
|
23697
|
+
}
|
|
23698
|
+
|
|
23699
|
+
.myio-chart-widget-header {
|
|
23700
|
+
display: flex;
|
|
23701
|
+
justify-content: space-between;
|
|
23702
|
+
align-items: center;
|
|
23703
|
+
padding: 16px 20px;
|
|
23704
|
+
border-bottom: 1px solid ${colors.border};
|
|
23705
|
+
flex-wrap: wrap;
|
|
23706
|
+
gap: 12px;
|
|
23707
|
+
}
|
|
23708
|
+
|
|
23709
|
+
.myio-chart-widget-title-group {
|
|
23710
|
+
display: flex;
|
|
23711
|
+
align-items: center;
|
|
23712
|
+
gap: 10px;
|
|
23713
|
+
}
|
|
23714
|
+
|
|
23715
|
+
.myio-chart-widget-title {
|
|
23716
|
+
margin: 0;
|
|
23717
|
+
font-size: 16px;
|
|
23718
|
+
font-weight: 600;
|
|
23719
|
+
color: ${colors.text};
|
|
23720
|
+
}
|
|
23721
|
+
|
|
23722
|
+
.myio-chart-widget-controls {
|
|
23723
|
+
display: flex;
|
|
23724
|
+
align-items: center;
|
|
23725
|
+
gap: 12px;
|
|
23726
|
+
flex-wrap: wrap;
|
|
23727
|
+
}
|
|
23728
|
+
|
|
23729
|
+
.myio-chart-widget-tabs {
|
|
23730
|
+
display: flex;
|
|
23731
|
+
gap: 2px;
|
|
23732
|
+
background: ${theme === "dark" ? "#374151" : "#f3f4f6"};
|
|
23733
|
+
padding: 3px;
|
|
23734
|
+
border-radius: 8px;
|
|
23735
|
+
}
|
|
23736
|
+
|
|
23737
|
+
.myio-chart-widget-tab {
|
|
23738
|
+
padding: 6px 14px;
|
|
23739
|
+
font-size: 12px;
|
|
23740
|
+
font-weight: 500;
|
|
23741
|
+
border: none;
|
|
23742
|
+
background: transparent;
|
|
23743
|
+
color: ${colors.textMuted};
|
|
23744
|
+
cursor: pointer;
|
|
23745
|
+
border-radius: 6px;
|
|
23746
|
+
transition: all 0.2s;
|
|
23747
|
+
white-space: nowrap;
|
|
23748
|
+
}
|
|
23749
|
+
|
|
23750
|
+
.myio-chart-widget-tab:hover {
|
|
23751
|
+
color: ${colors.text};
|
|
23752
|
+
background: ${theme === "dark" ? "rgba(255,255,255,0.1)" : "rgba(0,0,0,0.05)"};
|
|
23753
|
+
}
|
|
23754
|
+
|
|
23755
|
+
.myio-chart-widget-tab.active {
|
|
23756
|
+
background: ${primaryColor};
|
|
23757
|
+
color: white;
|
|
23758
|
+
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
|
|
23759
|
+
}
|
|
23760
|
+
|
|
23761
|
+
.myio-chart-widget-btn {
|
|
23762
|
+
background: transparent;
|
|
23763
|
+
border: 1px solid ${colors.border};
|
|
23764
|
+
font-size: 16px;
|
|
23765
|
+
cursor: pointer;
|
|
23766
|
+
padding: 6px 10px;
|
|
23767
|
+
border-radius: 6px;
|
|
23768
|
+
transition: all 0.2s;
|
|
23769
|
+
color: ${colors.text};
|
|
23770
|
+
}
|
|
23771
|
+
|
|
23772
|
+
.myio-chart-widget-btn:hover {
|
|
23773
|
+
background: ${primaryColor};
|
|
23774
|
+
border-color: ${primaryColor};
|
|
23775
|
+
color: white;
|
|
23776
|
+
}
|
|
23777
|
+
|
|
23778
|
+
.myio-chart-widget-body {
|
|
23779
|
+
position: relative;
|
|
23780
|
+
padding: 16px 20px;
|
|
23781
|
+
}
|
|
23782
|
+
|
|
23783
|
+
.myio-chart-widget-canvas-container {
|
|
23784
|
+
position: relative;
|
|
23785
|
+
width: 100%;
|
|
23786
|
+
}
|
|
23787
|
+
|
|
23788
|
+
.myio-chart-widget-loading {
|
|
23789
|
+
position: absolute;
|
|
23790
|
+
top: 0;
|
|
23791
|
+
left: 0;
|
|
23792
|
+
right: 0;
|
|
23793
|
+
bottom: 0;
|
|
23794
|
+
background: rgba(255, 255, 255, 0.9);
|
|
23795
|
+
display: flex;
|
|
23796
|
+
align-items: center;
|
|
23797
|
+
justify-content: center;
|
|
23798
|
+
z-index: 10;
|
|
23799
|
+
border-radius: 8px;
|
|
23800
|
+
}
|
|
23801
|
+
|
|
23802
|
+
.myio-chart-widget.dark .myio-chart-widget-loading {
|
|
23803
|
+
background: rgba(31, 41, 55, 0.9);
|
|
23804
|
+
}
|
|
23805
|
+
|
|
23806
|
+
.myio-chart-widget-spinner {
|
|
23807
|
+
width: 32px;
|
|
23808
|
+
height: 32px;
|
|
23809
|
+
border: 3px solid ${colors.border};
|
|
23810
|
+
border-top-color: ${primaryColor};
|
|
23811
|
+
border-radius: 50%;
|
|
23812
|
+
animation: myio-spin 1s linear infinite;
|
|
23813
|
+
}
|
|
23814
|
+
|
|
23815
|
+
@keyframes myio-spin {
|
|
23816
|
+
to { transform: rotate(360deg); }
|
|
23817
|
+
}
|
|
23818
|
+
|
|
23819
|
+
.myio-chart-widget-footer {
|
|
23820
|
+
display: flex;
|
|
23821
|
+
justify-content: space-around;
|
|
23822
|
+
padding: 16px 20px;
|
|
23823
|
+
border-top: 1px solid ${colors.border};
|
|
23824
|
+
gap: 16px;
|
|
23825
|
+
flex-wrap: wrap;
|
|
23826
|
+
}
|
|
23827
|
+
|
|
23828
|
+
.myio-chart-widget-stat {
|
|
23829
|
+
display: flex;
|
|
23830
|
+
flex-direction: column;
|
|
23831
|
+
align-items: center;
|
|
23832
|
+
text-align: center;
|
|
23833
|
+
min-width: 100px;
|
|
23834
|
+
}
|
|
23835
|
+
|
|
23836
|
+
.myio-chart-widget-stat-label {
|
|
23837
|
+
font-size: 11px;
|
|
23838
|
+
font-weight: 500;
|
|
23839
|
+
color: ${colors.textMuted};
|
|
23840
|
+
text-transform: uppercase;
|
|
23841
|
+
letter-spacing: 0.5px;
|
|
23842
|
+
margin-bottom: 4px;
|
|
23843
|
+
}
|
|
23844
|
+
|
|
23845
|
+
.myio-chart-widget-stat-value {
|
|
23846
|
+
font-size: 20px;
|
|
23847
|
+
font-weight: 700;
|
|
23848
|
+
color: ${colors.text};
|
|
23849
|
+
}
|
|
23850
|
+
|
|
23851
|
+
.myio-chart-widget-stat-value.primary {
|
|
23852
|
+
color: ${primaryColor};
|
|
23853
|
+
}
|
|
23854
|
+
|
|
23855
|
+
.myio-chart-widget-stat-sub {
|
|
23856
|
+
font-size: 11px;
|
|
23857
|
+
color: ${colors.textMuted};
|
|
23858
|
+
margin-top: 2px;
|
|
23859
|
+
}
|
|
23860
|
+
|
|
23861
|
+
/* Settings Modal Overlay */
|
|
23862
|
+
.myio-settings-overlay {
|
|
23863
|
+
position: fixed;
|
|
23864
|
+
inset: 0;
|
|
23865
|
+
background: rgba(0, 0, 0, 0.6);
|
|
23866
|
+
display: flex;
|
|
23867
|
+
align-items: center;
|
|
23868
|
+
justify-content: center;
|
|
23869
|
+
z-index: 99999;
|
|
23870
|
+
backdrop-filter: blur(4px);
|
|
23871
|
+
}
|
|
23872
|
+
|
|
23873
|
+
.myio-settings-overlay.hidden {
|
|
23874
|
+
display: none;
|
|
23875
|
+
}
|
|
23876
|
+
|
|
23877
|
+
.myio-settings-card {
|
|
23878
|
+
background: ${colors.chartBackground};
|
|
23879
|
+
border-radius: 10px;
|
|
23880
|
+
width: 90%;
|
|
23881
|
+
max-width: 600px;
|
|
23882
|
+
max-height: 90vh;
|
|
23883
|
+
display: flex;
|
|
23884
|
+
flex-direction: column;
|
|
23885
|
+
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.2);
|
|
23886
|
+
overflow: hidden;
|
|
23887
|
+
}
|
|
23888
|
+
|
|
23889
|
+
.myio-settings-card .myio-modal-header {
|
|
23890
|
+
border-radius: 10px 10px 0 0;
|
|
23891
|
+
}
|
|
23892
|
+
|
|
23893
|
+
.myio-settings-body {
|
|
23894
|
+
padding: 20px;
|
|
23895
|
+
overflow-y: auto;
|
|
23896
|
+
display: flex;
|
|
23897
|
+
flex-direction: column;
|
|
23898
|
+
gap: 20px;
|
|
23899
|
+
}
|
|
23900
|
+
|
|
23901
|
+
.myio-settings-section {
|
|
23902
|
+
background: ${theme === "dark" ? "rgba(255,255,255,0.05)" : "#f8fafc"};
|
|
23903
|
+
border-radius: 10px;
|
|
23904
|
+
padding: 16px;
|
|
23905
|
+
border: 1px solid ${theme === "dark" ? "rgba(255,255,255,0.1)" : "#e2e8f0"};
|
|
23906
|
+
}
|
|
23907
|
+
|
|
23908
|
+
.myio-settings-section-label {
|
|
23909
|
+
font-size: 13px;
|
|
23910
|
+
font-weight: 600;
|
|
23911
|
+
color: ${colors.text};
|
|
23912
|
+
margin-bottom: 12px;
|
|
23913
|
+
display: flex;
|
|
23914
|
+
align-items: center;
|
|
23915
|
+
gap: 8px;
|
|
23916
|
+
}
|
|
23917
|
+
|
|
23918
|
+
.myio-settings-row {
|
|
23919
|
+
display: flex;
|
|
23920
|
+
gap: 16px;
|
|
23921
|
+
flex-wrap: wrap;
|
|
23922
|
+
align-items: flex-end;
|
|
23923
|
+
}
|
|
23924
|
+
|
|
23925
|
+
.myio-settings-field {
|
|
23926
|
+
display: flex;
|
|
23927
|
+
flex-direction: column;
|
|
23928
|
+
gap: 6px;
|
|
23929
|
+
flex: 1;
|
|
23930
|
+
min-width: 120px;
|
|
23931
|
+
}
|
|
23932
|
+
|
|
23933
|
+
.myio-settings-field-label {
|
|
23934
|
+
font-size: 12px;
|
|
23935
|
+
font-weight: 500;
|
|
23936
|
+
color: ${colors.textMuted};
|
|
23937
|
+
}
|
|
23938
|
+
|
|
23939
|
+
.myio-settings-input,
|
|
23940
|
+
.myio-settings-select {
|
|
23941
|
+
padding: 10px 14px;
|
|
23942
|
+
border: 1px solid ${colors.border};
|
|
23943
|
+
border-radius: 8px;
|
|
23944
|
+
font-size: 14px;
|
|
23945
|
+
background: ${colors.chartBackground};
|
|
23946
|
+
color: ${colors.text};
|
|
23947
|
+
width: 100%;
|
|
23948
|
+
}
|
|
23949
|
+
|
|
23950
|
+
.myio-settings-input:focus,
|
|
23951
|
+
.myio-settings-select:focus {
|
|
23952
|
+
outline: 2px solid ${primaryColor};
|
|
23953
|
+
outline-offset: 1px;
|
|
23954
|
+
}
|
|
23955
|
+
|
|
23956
|
+
.myio-settings-tabs {
|
|
23957
|
+
display: flex;
|
|
23958
|
+
gap: 2px;
|
|
23959
|
+
background: ${theme === "dark" ? "#374151" : "#e5e7eb"};
|
|
23960
|
+
padding: 3px;
|
|
23961
|
+
border-radius: 8px;
|
|
23962
|
+
}
|
|
23963
|
+
|
|
23964
|
+
.myio-settings-tab {
|
|
23965
|
+
flex: 1;
|
|
23966
|
+
padding: 8px 12px;
|
|
23967
|
+
font-size: 12px;
|
|
23968
|
+
font-weight: 500;
|
|
23969
|
+
border: none;
|
|
23970
|
+
background: transparent;
|
|
23971
|
+
color: ${colors.textMuted};
|
|
23972
|
+
cursor: pointer;
|
|
23973
|
+
border-radius: 6px;
|
|
23974
|
+
transition: all 0.2s;
|
|
23975
|
+
white-space: nowrap;
|
|
23976
|
+
}
|
|
23977
|
+
|
|
23978
|
+
.myio-settings-tab:hover {
|
|
23979
|
+
color: ${colors.text};
|
|
23980
|
+
background: ${theme === "dark" ? "rgba(255,255,255,0.1)" : "rgba(0,0,0,0.05)"};
|
|
23981
|
+
}
|
|
23982
|
+
|
|
23983
|
+
.myio-settings-tab.active {
|
|
23984
|
+
background: ${primaryColor};
|
|
23985
|
+
color: white;
|
|
23986
|
+
}
|
|
23987
|
+
|
|
23988
|
+
.myio-settings-footer {
|
|
23989
|
+
display: flex;
|
|
23990
|
+
gap: 12px;
|
|
23991
|
+
justify-content: flex-end;
|
|
23992
|
+
padding: 16px 20px;
|
|
23993
|
+
border-top: 1px solid ${colors.border};
|
|
23994
|
+
background: ${theme === "dark" ? "rgba(0,0,0,0.2)" : "#fafafa"};
|
|
23995
|
+
}
|
|
23996
|
+
|
|
23997
|
+
.myio-settings-btn {
|
|
23998
|
+
padding: 10px 20px;
|
|
23999
|
+
border-radius: 8px;
|
|
24000
|
+
font-size: 14px;
|
|
24001
|
+
font-weight: 500;
|
|
24002
|
+
cursor: pointer;
|
|
24003
|
+
transition: all 0.2s;
|
|
24004
|
+
}
|
|
24005
|
+
|
|
24006
|
+
.myio-settings-btn-secondary {
|
|
24007
|
+
background: transparent;
|
|
24008
|
+
border: 1px solid ${colors.border};
|
|
24009
|
+
color: ${colors.text};
|
|
24010
|
+
}
|
|
24011
|
+
|
|
24012
|
+
.myio-settings-btn-secondary:hover {
|
|
24013
|
+
background: ${theme === "dark" ? "rgba(255,255,255,0.1)" : "#f3f4f6"};
|
|
24014
|
+
}
|
|
24015
|
+
|
|
24016
|
+
.myio-settings-btn-primary {
|
|
24017
|
+
background: ${primaryColor};
|
|
24018
|
+
border: none;
|
|
24019
|
+
color: white;
|
|
24020
|
+
}
|
|
24021
|
+
|
|
24022
|
+
.myio-settings-btn-primary:hover {
|
|
24023
|
+
filter: brightness(1.1);
|
|
24024
|
+
}
|
|
24025
|
+
|
|
24026
|
+
.myio-settings-hint {
|
|
24027
|
+
font-size: 11px;
|
|
24028
|
+
color: ${colors.textMuted};
|
|
24029
|
+
font-weight: normal;
|
|
24030
|
+
}
|
|
24031
|
+
|
|
24032
|
+
.myio-settings-context-group {
|
|
24033
|
+
margin-bottom: 8px;
|
|
24034
|
+
}
|
|
24035
|
+
|
|
24036
|
+
.myio-settings-context-group:last-child {
|
|
24037
|
+
margin-bottom: 0;
|
|
24038
|
+
}
|
|
24039
|
+
|
|
24040
|
+
/* Dropdown styles */
|
|
24041
|
+
.myio-settings-dropdown-container {
|
|
24042
|
+
position: relative;
|
|
24043
|
+
}
|
|
24044
|
+
|
|
24045
|
+
.myio-settings-dropdown-btn {
|
|
24046
|
+
padding: 10px 14px;
|
|
24047
|
+
border: 1px solid ${colors.border};
|
|
24048
|
+
border-radius: 8px;
|
|
24049
|
+
font-size: 14px;
|
|
24050
|
+
background: ${colors.chartBackground};
|
|
24051
|
+
color: ${colors.text};
|
|
24052
|
+
cursor: pointer;
|
|
24053
|
+
min-width: 180px;
|
|
24054
|
+
display: flex;
|
|
24055
|
+
align-items: center;
|
|
24056
|
+
justify-content: space-between;
|
|
24057
|
+
gap: 8px;
|
|
24058
|
+
width: 100%;
|
|
24059
|
+
}
|
|
24060
|
+
|
|
24061
|
+
.myio-settings-dropdown-btn:hover {
|
|
24062
|
+
border-color: ${primaryColor};
|
|
24063
|
+
}
|
|
24064
|
+
|
|
24065
|
+
.myio-settings-dropdown-arrow {
|
|
24066
|
+
font-size: 10px;
|
|
24067
|
+
color: ${colors.textMuted};
|
|
24068
|
+
}
|
|
24069
|
+
|
|
24070
|
+
.myio-settings-dropdown {
|
|
24071
|
+
position: absolute;
|
|
24072
|
+
top: calc(100% + 4px);
|
|
24073
|
+
left: 0;
|
|
24074
|
+
z-index: 100001;
|
|
24075
|
+
background: ${colors.chartBackground};
|
|
24076
|
+
border: 1px solid ${colors.border};
|
|
24077
|
+
border-radius: 8px;
|
|
24078
|
+
box-shadow: 0 4px 16px rgba(0, 0, 0, 0.15);
|
|
24079
|
+
min-width: 220px;
|
|
24080
|
+
padding: 8px 0;
|
|
24081
|
+
}
|
|
24082
|
+
|
|
24083
|
+
.myio-settings-dropdown.hidden {
|
|
24084
|
+
display: none;
|
|
24085
|
+
}
|
|
24086
|
+
|
|
24087
|
+
.myio-settings-dropdown-option {
|
|
24088
|
+
display: flex;
|
|
24089
|
+
align-items: center;
|
|
24090
|
+
gap: 10px;
|
|
24091
|
+
padding: 10px 14px;
|
|
24092
|
+
cursor: pointer;
|
|
24093
|
+
font-size: 13px;
|
|
24094
|
+
color: ${colors.text};
|
|
24095
|
+
transition: background 0.15s;
|
|
24096
|
+
}
|
|
24097
|
+
|
|
24098
|
+
.myio-settings-dropdown-option:hover {
|
|
24099
|
+
background: ${theme === "dark" ? "rgba(255,255,255,0.1)" : "#f3f4f6"};
|
|
24100
|
+
}
|
|
24101
|
+
|
|
24102
|
+
.myio-settings-dropdown-option input {
|
|
24103
|
+
width: 16px;
|
|
24104
|
+
height: 16px;
|
|
24105
|
+
cursor: pointer;
|
|
24106
|
+
accent-color: ${primaryColor};
|
|
24107
|
+
}
|
|
24108
|
+
|
|
24109
|
+
.myio-settings-dropdown-actions {
|
|
24110
|
+
border-top: 1px solid ${colors.border};
|
|
24111
|
+
margin-top: 8px;
|
|
24112
|
+
padding: 8px;
|
|
24113
|
+
display: flex;
|
|
24114
|
+
flex-direction: column;
|
|
24115
|
+
gap: 6px;
|
|
24116
|
+
}
|
|
24117
|
+
|
|
24118
|
+
.myio-settings-dropdown-actions button {
|
|
24119
|
+
width: 100%;
|
|
24120
|
+
padding: 8px;
|
|
24121
|
+
background: ${theme === "dark" ? "rgba(255,255,255,0.1)" : "#f3f4f6"};
|
|
24122
|
+
border: none;
|
|
24123
|
+
border-radius: 6px;
|
|
24124
|
+
cursor: pointer;
|
|
24125
|
+
font-size: 12px;
|
|
24126
|
+
color: ${colors.text};
|
|
24127
|
+
transition: background 0.15s;
|
|
24128
|
+
}
|
|
24129
|
+
|
|
24130
|
+
.myio-settings-dropdown-actions button:hover {
|
|
24131
|
+
background: ${theme === "dark" ? "rgba(255,255,255,0.15)" : "#e5e7eb"};
|
|
24132
|
+
}
|
|
24133
|
+
|
|
24134
|
+
/* Suggestion icon styles */
|
|
24135
|
+
.myio-settings-section-label span[id$="-settings-suggestion"] {
|
|
24136
|
+
transition: opacity 0.2s, transform 0.2s;
|
|
24137
|
+
}
|
|
24138
|
+
|
|
24139
|
+
.myio-settings-section-label span[id$="-settings-suggestion"]:hover {
|
|
24140
|
+
opacity: 1 !important;
|
|
24141
|
+
transform: scale(1.2);
|
|
24142
|
+
}
|
|
24143
|
+
`;
|
|
24144
|
+
}
|
|
24145
|
+
function createConsumptionChartWidget(config) {
|
|
24146
|
+
const widgetId = `myio-widget-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
|
|
24147
|
+
let containerElement = null;
|
|
24148
|
+
let chartInstance = null;
|
|
24149
|
+
let styleElement = null;
|
|
24150
|
+
let settingsModalElement = null;
|
|
24151
|
+
let settingsHeaderInstance = null;
|
|
24152
|
+
let currentTheme = config.theme ?? "light";
|
|
24153
|
+
let currentChartType = config.defaultChartType ?? "line";
|
|
24154
|
+
let currentVizMode = config.defaultVizMode ?? "total";
|
|
24155
|
+
let currentPeriod = config.defaultPeriod ?? 7;
|
|
24156
|
+
let currentIdealRange = config.idealRange ?? null;
|
|
24157
|
+
let tempPeriod = currentPeriod;
|
|
24158
|
+
let tempChartType = currentChartType;
|
|
24159
|
+
let tempVizMode = currentVizMode;
|
|
24160
|
+
let tempTheme = currentTheme;
|
|
24161
|
+
let tempIdealRange = currentIdealRange;
|
|
24162
|
+
let currentSuggestion = null;
|
|
24163
|
+
const domainCfg = DOMAIN_CONFIG4[config.domain] || DOMAIN_CONFIG4.energy;
|
|
24164
|
+
const primaryColor = config.colors?.primary || domainCfg.color;
|
|
24165
|
+
const domainColors = config.colors?.shoppingColors || domainCfg.colors;
|
|
24166
|
+
const showSettingsButton = config.showSettingsButton ?? true;
|
|
24167
|
+
const showMaximizeButton = config.showMaximizeButton ?? true;
|
|
24168
|
+
const showVizModeTabs = config.showVizModeTabs ?? true;
|
|
24169
|
+
const showChartTypeTabs = config.showChartTypeTabs ?? true;
|
|
24170
|
+
const chartHeight = typeof config.chartHeight === "number" ? `${config.chartHeight}px` : config.chartHeight ?? "300px";
|
|
24171
|
+
function getTitle() {
|
|
24172
|
+
if (config.title) return config.title;
|
|
24173
|
+
const domainName = config.domain === "temperature" ? "Temperatura" : "Consumo";
|
|
24174
|
+
return `${domainName} dos \xFAltimos ${currentPeriod} dias`;
|
|
24175
|
+
}
|
|
24176
|
+
function renderHTML() {
|
|
24177
|
+
return `
|
|
24178
|
+
<div id="${widgetId}" class="myio-chart-widget ${currentTheme === "dark" ? "dark" : ""} ${config.className || ""}">
|
|
24179
|
+
<div class="myio-chart-widget-header">
|
|
24180
|
+
<div class="myio-chart-widget-title-group">
|
|
24181
|
+
${showSettingsButton ? `
|
|
24182
|
+
<button id="${widgetId}-settings-btn" class="myio-chart-widget-btn" title="Configura\xE7\xF5es">\u2699\uFE0F</button>
|
|
24183
|
+
` : ""}
|
|
24184
|
+
<h4 id="${widgetId}-title" class="myio-chart-widget-title">${getTitle()}</h4>
|
|
24185
|
+
</div>
|
|
24186
|
+
<div class="myio-chart-widget-controls">
|
|
24187
|
+
${showVizModeTabs ? `
|
|
24188
|
+
<div class="myio-chart-widget-tabs" id="${widgetId}-viz-tabs">
|
|
24189
|
+
<button class="myio-chart-widget-tab ${currentVizMode === "total" ? "active" : ""}" data-viz="total">Consolidado</button>
|
|
24190
|
+
<button class="myio-chart-widget-tab ${currentVizMode === "separate" ? "active" : ""}" data-viz="separate">Por Shopping</button>
|
|
24191
|
+
</div>
|
|
24192
|
+
` : ""}
|
|
24193
|
+
${showChartTypeTabs ? `
|
|
24194
|
+
<div class="myio-chart-widget-tabs" id="${widgetId}-type-tabs">
|
|
24195
|
+
<button class="myio-chart-widget-tab ${currentChartType === "line" ? "active" : ""}" data-type="line">Linhas</button>
|
|
24196
|
+
<button class="myio-chart-widget-tab ${currentChartType === "bar" ? "active" : ""}" data-type="bar">Barras</button>
|
|
24197
|
+
</div>
|
|
24198
|
+
` : ""}
|
|
24199
|
+
${showMaximizeButton ? `
|
|
24200
|
+
<button id="${widgetId}-maximize-btn" class="myio-chart-widget-btn" title="Maximizar">\u26F6</button>
|
|
24201
|
+
` : ""}
|
|
24202
|
+
</div>
|
|
24203
|
+
</div>
|
|
24204
|
+
<div class="myio-chart-widget-body">
|
|
24205
|
+
<div id="${widgetId}-loading" class="myio-chart-widget-loading" style="display: none;">
|
|
24206
|
+
<div class="myio-chart-widget-spinner"></div>
|
|
24207
|
+
</div>
|
|
24208
|
+
<div class="myio-chart-widget-canvas-container" style="height: ${chartHeight};">
|
|
24209
|
+
<canvas id="${widgetId}-canvas"></canvas>
|
|
24210
|
+
</div>
|
|
24211
|
+
</div>
|
|
24212
|
+
<div class="myio-chart-widget-footer" id="${widgetId}-footer">
|
|
24213
|
+
<div class="myio-chart-widget-stat">
|
|
24214
|
+
<span class="myio-chart-widget-stat-label">Total Per\xEDodo</span>
|
|
24215
|
+
<span id="${widgetId}-stat-total" class="myio-chart-widget-stat-value primary">--</span>
|
|
24216
|
+
</div>
|
|
24217
|
+
<div class="myio-chart-widget-stat">
|
|
24218
|
+
<span class="myio-chart-widget-stat-label">M\xE9dia Di\xE1ria</span>
|
|
24219
|
+
<span id="${widgetId}-stat-avg" class="myio-chart-widget-stat-value">--</span>
|
|
24220
|
+
</div>
|
|
24221
|
+
<div class="myio-chart-widget-stat">
|
|
24222
|
+
<span class="myio-chart-widget-stat-label">Dia de Pico</span>
|
|
24223
|
+
<span id="${widgetId}-stat-peak" class="myio-chart-widget-stat-value">--</span>
|
|
24224
|
+
<span id="${widgetId}-stat-peak-date" class="myio-chart-widget-stat-sub"></span>
|
|
24225
|
+
</div>
|
|
24226
|
+
</div>
|
|
24227
|
+
</div>
|
|
24228
|
+
`;
|
|
24229
|
+
}
|
|
24230
|
+
function renderSettingsModal() {
|
|
24231
|
+
const unit = config.unit ?? "";
|
|
24232
|
+
const isTemperature = config.domain === "temperature";
|
|
24233
|
+
settingsHeaderInstance = createModalHeader({
|
|
24234
|
+
id: `${widgetId}-settings`,
|
|
24235
|
+
title: "Configura\xE7\xF5es",
|
|
24236
|
+
icon: "\u2699\uFE0F",
|
|
24237
|
+
theme: tempTheme,
|
|
24238
|
+
backgroundColor: primaryColor,
|
|
24239
|
+
showThemeToggle: false,
|
|
24240
|
+
showMaximize: false,
|
|
24241
|
+
showClose: true,
|
|
24242
|
+
onClose: () => closeSettingsModal()
|
|
24243
|
+
});
|
|
24244
|
+
return `
|
|
24245
|
+
<div id="${widgetId}-settings-overlay" class="myio-settings-overlay hidden">
|
|
24246
|
+
<div class="myio-settings-card">
|
|
24247
|
+
${settingsHeaderInstance.render()}
|
|
24248
|
+
<div class="myio-settings-body">
|
|
24249
|
+
<!-- CONTEXT 1: Per\xEDodo e Dados -->
|
|
24250
|
+
<div class="myio-settings-context-group">
|
|
24251
|
+
<!-- Per\xEDodo -->
|
|
24252
|
+
<div class="myio-settings-section">
|
|
24253
|
+
<div class="myio-settings-section-label">\u{1F4C5} Per\xEDodo</div>
|
|
24254
|
+
<div class="myio-settings-row">
|
|
24255
|
+
<div class="myio-settings-field" style="flex: 1;">
|
|
24256
|
+
<select id="${widgetId}-settings-period" class="myio-settings-select">
|
|
24257
|
+
<option value="7" ${tempPeriod === 7 ? "selected" : ""}>\xDAltimos 7 dias</option>
|
|
24258
|
+
<option value="14" ${tempPeriod === 14 ? "selected" : ""}>\xDAltimos 14 dias</option>
|
|
24259
|
+
<option value="30" ${tempPeriod === 30 ? "selected" : ""}>\xDAltimos 30 dias</option>
|
|
24260
|
+
<option value="60" ${tempPeriod === 60 ? "selected" : ""}>\xDAltimos 60 dias</option>
|
|
24261
|
+
<option value="90" ${tempPeriod === 90 ? "selected" : ""}>\xDAltimos 90 dias</option>
|
|
24262
|
+
</select>
|
|
24263
|
+
</div>
|
|
24264
|
+
</div>
|
|
24265
|
+
</div>
|
|
24266
|
+
|
|
24267
|
+
<!-- Dados -->
|
|
24268
|
+
<div class="myio-settings-section">
|
|
24269
|
+
<div class="myio-settings-section-label">\u{1F4CA} Dados</div>
|
|
24270
|
+
<div class="myio-settings-row">
|
|
24271
|
+
<!-- Granularity Select -->
|
|
24272
|
+
<div class="myio-settings-field">
|
|
24273
|
+
<label class="myio-settings-field-label">Granularidade</label>
|
|
24274
|
+
<select id="${widgetId}-settings-granularity" class="myio-settings-select">
|
|
24275
|
+
<option value="1d" selected>\u{1F4C6} Por Dia</option>
|
|
24276
|
+
<option value="1h">\u{1F550} Por Hora</option>
|
|
24277
|
+
</select>
|
|
24278
|
+
</div>
|
|
24279
|
+
|
|
24280
|
+
<!-- Weekday Filter -->
|
|
24281
|
+
<div class="myio-settings-field">
|
|
24282
|
+
<label class="myio-settings-field-label">Dias da Semana</label>
|
|
24283
|
+
<div class="myio-settings-dropdown-container">
|
|
24284
|
+
<button type="button" id="${widgetId}-settings-weekday-btn" class="myio-settings-dropdown-btn">
|
|
24285
|
+
<span id="${widgetId}-settings-weekday-label">Todos os dias</span>
|
|
24286
|
+
<span class="myio-settings-dropdown-arrow">\u25BC</span>
|
|
24287
|
+
</button>
|
|
24288
|
+
<div id="${widgetId}-settings-weekday-dropdown" class="myio-settings-dropdown hidden">
|
|
24289
|
+
<label class="myio-settings-dropdown-option">
|
|
24290
|
+
<input type="checkbox" name="${widgetId}-weekday" value="dom" checked /> Domingo
|
|
24291
|
+
</label>
|
|
24292
|
+
<label class="myio-settings-dropdown-option">
|
|
24293
|
+
<input type="checkbox" name="${widgetId}-weekday" value="seg" checked /> Segunda-feira
|
|
24294
|
+
</label>
|
|
24295
|
+
<label class="myio-settings-dropdown-option">
|
|
24296
|
+
<input type="checkbox" name="${widgetId}-weekday" value="ter" checked /> Ter\xE7a-feira
|
|
24297
|
+
</label>
|
|
24298
|
+
<label class="myio-settings-dropdown-option">
|
|
24299
|
+
<input type="checkbox" name="${widgetId}-weekday" value="qua" checked /> Quarta-feira
|
|
24300
|
+
</label>
|
|
24301
|
+
<label class="myio-settings-dropdown-option">
|
|
24302
|
+
<input type="checkbox" name="${widgetId}-weekday" value="qui" checked /> Quinta-feira
|
|
24303
|
+
</label>
|
|
24304
|
+
<label class="myio-settings-dropdown-option">
|
|
24305
|
+
<input type="checkbox" name="${widgetId}-weekday" value="sex" checked /> Sexta-feira
|
|
24306
|
+
</label>
|
|
24307
|
+
<label class="myio-settings-dropdown-option">
|
|
24308
|
+
<input type="checkbox" name="${widgetId}-weekday" value="sab" checked /> S\xE1bado
|
|
24309
|
+
</label>
|
|
24310
|
+
<div class="myio-settings-dropdown-actions">
|
|
24311
|
+
<button type="button" id="${widgetId}-settings-weekday-all">Selecionar Todos</button>
|
|
24312
|
+
<button type="button" id="${widgetId}-settings-weekday-clear">Limpar</button>
|
|
24313
|
+
</div>
|
|
24314
|
+
</div>
|
|
24315
|
+
</div>
|
|
24316
|
+
</div>
|
|
24317
|
+
|
|
24318
|
+
<!-- Day Period Filter (only visible when hourly) -->
|
|
24319
|
+
<div class="myio-settings-field" id="${widgetId}-settings-dayperiod-field" style="display: none;">
|
|
24320
|
+
<label class="myio-settings-field-label">Per\xEDodos do Dia</label>
|
|
24321
|
+
<div class="myio-settings-dropdown-container">
|
|
24322
|
+
<button type="button" id="${widgetId}-settings-dayperiod-btn" class="myio-settings-dropdown-btn">
|
|
24323
|
+
<span id="${widgetId}-settings-dayperiod-label">Todos os per\xEDodos</span>
|
|
24324
|
+
<span class="myio-settings-dropdown-arrow">\u25BC</span>
|
|
24325
|
+
</button>
|
|
24326
|
+
<div id="${widgetId}-settings-dayperiod-dropdown" class="myio-settings-dropdown hidden">
|
|
24327
|
+
<label class="myio-settings-dropdown-option">
|
|
24328
|
+
<input type="checkbox" name="${widgetId}-dayperiod" value="madrugada" checked /> Madrugada (00h-06h)
|
|
24329
|
+
</label>
|
|
24330
|
+
<label class="myio-settings-dropdown-option">
|
|
24331
|
+
<input type="checkbox" name="${widgetId}-dayperiod" value="manha" checked /> Manh\xE3 (06h-12h)
|
|
24332
|
+
</label>
|
|
24333
|
+
<label class="myio-settings-dropdown-option">
|
|
24334
|
+
<input type="checkbox" name="${widgetId}-dayperiod" value="tarde" checked /> Tarde (12h-18h)
|
|
24335
|
+
</label>
|
|
24336
|
+
<label class="myio-settings-dropdown-option">
|
|
24337
|
+
<input type="checkbox" name="${widgetId}-dayperiod" value="noite" checked /> Noite (18h-24h)
|
|
24338
|
+
</label>
|
|
24339
|
+
<div class="myio-settings-dropdown-actions">
|
|
24340
|
+
<button type="button" id="${widgetId}-settings-dayperiod-all">Selecionar Todos</button>
|
|
24341
|
+
<button type="button" id="${widgetId}-settings-dayperiod-clear">Limpar</button>
|
|
24342
|
+
</div>
|
|
24343
|
+
</div>
|
|
24344
|
+
</div>
|
|
24345
|
+
</div>
|
|
24346
|
+
</div>
|
|
24347
|
+
</div>
|
|
24348
|
+
</div>
|
|
24349
|
+
|
|
24350
|
+
<!-- CONTEXT 2: Faixa Ideal -->
|
|
24351
|
+
<div class="myio-settings-context-group">
|
|
24352
|
+
<div class="myio-settings-section">
|
|
24353
|
+
<div class="myio-settings-section-label">
|
|
24354
|
+
\u{1F3AF} Faixa Ideal
|
|
24355
|
+
<span class="myio-settings-hint" id="${widgetId}-settings-range-hint">(opcional - deixe zerado para n\xE3o exibir)</span>
|
|
24356
|
+
<span
|
|
24357
|
+
id="${widgetId}-settings-suggestion"
|
|
24358
|
+
title=""
|
|
24359
|
+
style="cursor: pointer; font-size: 16px; opacity: 0.7; transition: opacity 0.2s; margin-left: 4px;"
|
|
24360
|
+
>\u{1F4A1}</span>
|
|
24361
|
+
</div>
|
|
24362
|
+
<div class="myio-settings-row">
|
|
24363
|
+
<div class="myio-settings-field" style="min-width: 100px;">
|
|
24364
|
+
<label class="myio-settings-field-label">M\xEDnimo (${unit})</label>
|
|
24365
|
+
<input type="number" id="${widgetId}-settings-range-min" class="myio-settings-input"
|
|
24366
|
+
value="${tempIdealRange?.min ?? ""}" placeholder="0" step="0.1">
|
|
24367
|
+
</div>
|
|
24368
|
+
<div class="myio-settings-field" style="min-width: 100px;">
|
|
24369
|
+
<label class="myio-settings-field-label">M\xE1ximo (${unit})</label>
|
|
24370
|
+
<input type="number" id="${widgetId}-settings-range-max" class="myio-settings-input"
|
|
24371
|
+
value="${tempIdealRange?.max ?? ""}" placeholder="0" step="0.1">
|
|
24372
|
+
</div>
|
|
24373
|
+
<div class="myio-settings-field" style="flex: 1;">
|
|
24374
|
+
<label class="myio-settings-field-label">R\xF3tulo</label>
|
|
24375
|
+
<input type="text" id="${widgetId}-settings-range-label" class="myio-settings-input"
|
|
24376
|
+
value="${tempIdealRange?.label ?? ""}" placeholder="${isTemperature ? "Faixa Ideal" : "Meta de Consumo"}">
|
|
24377
|
+
</div>
|
|
24378
|
+
</div>
|
|
24379
|
+
</div>
|
|
24380
|
+
</div>
|
|
24381
|
+
|
|
24382
|
+
<!-- CONTEXT 3: Visualiza\xE7\xE3o -->
|
|
24383
|
+
<div class="myio-settings-context-group">
|
|
24384
|
+
<div class="myio-settings-section">
|
|
24385
|
+
<div class="myio-settings-section-label">\u{1F3A8} Visualiza\xE7\xE3o</div>
|
|
24386
|
+
<div class="myio-settings-row" style="gap: 20px; flex-wrap: wrap;">
|
|
24387
|
+
<!-- Chart Type -->
|
|
24388
|
+
<div class="myio-settings-field" style="flex: 1; min-width: 180px;">
|
|
24389
|
+
<label class="myio-settings-field-label">Tipo de Gr\xE1fico</label>
|
|
24390
|
+
<div class="myio-settings-tabs" id="${widgetId}-settings-chart-type">
|
|
24391
|
+
<button class="myio-settings-tab ${tempChartType === "line" ? "active" : ""}" data-type="line">\u{1F4C8} Linhas</button>
|
|
24392
|
+
<button class="myio-settings-tab ${tempChartType === "bar" ? "active" : ""}" data-type="bar">\u{1F4CA} Barras</button>
|
|
24393
|
+
</div>
|
|
24394
|
+
</div>
|
|
24395
|
+
|
|
24396
|
+
<!-- Viz Mode -->
|
|
24397
|
+
<div class="myio-settings-field" style="flex: 1; min-width: 200px;">
|
|
24398
|
+
<label class="myio-settings-field-label">Agrupamento</label>
|
|
24399
|
+
<div class="myio-settings-tabs" id="${widgetId}-settings-viz-mode">
|
|
24400
|
+
<button class="myio-settings-tab ${tempVizMode === "total" ? "active" : ""}" data-viz="total">\u{1F517} Consolidado</button>
|
|
24401
|
+
<button class="myio-settings-tab ${tempVizMode === "separate" ? "active" : ""}" data-viz="separate">\u{1F3EC} Por Shopping</button>
|
|
24402
|
+
</div>
|
|
24403
|
+
</div>
|
|
24404
|
+
|
|
24405
|
+
<!-- Theme -->
|
|
24406
|
+
<div class="myio-settings-field" style="flex: 1; min-width: 160px;">
|
|
24407
|
+
<label class="myio-settings-field-label">Tema</label>
|
|
24408
|
+
<div class="myio-settings-tabs" id="${widgetId}-settings-theme">
|
|
24409
|
+
<button class="myio-settings-tab ${tempTheme === "light" ? "active" : ""}" data-theme="light">\u2600\uFE0F Light</button>
|
|
24410
|
+
<button class="myio-settings-tab ${tempTheme === "dark" ? "active" : ""}" data-theme="dark">\u{1F319} Dark</button>
|
|
24411
|
+
</div>
|
|
24412
|
+
</div>
|
|
24413
|
+
</div>
|
|
24414
|
+
</div>
|
|
24415
|
+
</div>
|
|
24416
|
+
</div>
|
|
24417
|
+
<div class="myio-settings-footer">
|
|
24418
|
+
<button id="${widgetId}-settings-reset" class="myio-settings-btn myio-settings-btn-secondary">Resetar</button>
|
|
24419
|
+
<button id="${widgetId}-settings-apply" class="myio-settings-btn myio-settings-btn-primary">Carregar</button>
|
|
24420
|
+
</div>
|
|
24421
|
+
</div>
|
|
24422
|
+
</div>
|
|
24423
|
+
`;
|
|
24424
|
+
}
|
|
24425
|
+
function injectStyles() {
|
|
24426
|
+
if (styleElement) return;
|
|
24427
|
+
styleElement = document.createElement("style");
|
|
24428
|
+
styleElement.id = `${widgetId}-styles`;
|
|
24429
|
+
styleElement.textContent = getWidgetStyles(currentTheme, primaryColor);
|
|
24430
|
+
document.head.appendChild(styleElement);
|
|
24431
|
+
}
|
|
24432
|
+
function updateStyles() {
|
|
24433
|
+
if (styleElement) {
|
|
24434
|
+
styleElement.textContent = getWidgetStyles(currentTheme, primaryColor);
|
|
24435
|
+
}
|
|
24436
|
+
}
|
|
24437
|
+
function setupListeners() {
|
|
24438
|
+
if (showSettingsButton) {
|
|
24439
|
+
document.getElementById(`${widgetId}-settings-btn`)?.addEventListener("click", () => {
|
|
24440
|
+
openSettingsModal();
|
|
24441
|
+
config.onSettingsClick?.();
|
|
24442
|
+
});
|
|
24443
|
+
}
|
|
24444
|
+
if (showMaximizeButton && config.onMaximizeClick) {
|
|
24445
|
+
document.getElementById(`${widgetId}-maximize-btn`)?.addEventListener("click", () => {
|
|
24446
|
+
config.onMaximizeClick?.();
|
|
24447
|
+
});
|
|
24448
|
+
}
|
|
24449
|
+
if (showVizModeTabs) {
|
|
24450
|
+
document.getElementById(`${widgetId}-viz-tabs`)?.addEventListener("click", (e) => {
|
|
24451
|
+
const target = e.target;
|
|
24452
|
+
if (target.classList.contains("myio-chart-widget-tab")) {
|
|
24453
|
+
const mode = target.dataset.viz;
|
|
24454
|
+
if (mode) {
|
|
24455
|
+
instance.setVizMode(mode);
|
|
24456
|
+
}
|
|
24457
|
+
}
|
|
24458
|
+
});
|
|
24459
|
+
}
|
|
24460
|
+
if (showChartTypeTabs) {
|
|
24461
|
+
document.getElementById(`${widgetId}-type-tabs`)?.addEventListener("click", (e) => {
|
|
24462
|
+
const target = e.target;
|
|
24463
|
+
if (target.classList.contains("myio-chart-widget-tab")) {
|
|
24464
|
+
const type = target.dataset.type;
|
|
24465
|
+
if (type) {
|
|
24466
|
+
instance.setChartType(type);
|
|
24467
|
+
}
|
|
24468
|
+
}
|
|
24469
|
+
});
|
|
24470
|
+
}
|
|
24471
|
+
}
|
|
24472
|
+
function updateTabStates() {
|
|
24473
|
+
document.querySelectorAll(`#${widgetId}-viz-tabs .myio-chart-widget-tab`).forEach((tab) => {
|
|
24474
|
+
const btn = tab;
|
|
24475
|
+
btn.classList.toggle("active", btn.dataset.viz === currentVizMode);
|
|
24476
|
+
});
|
|
24477
|
+
document.querySelectorAll(`#${widgetId}-type-tabs .myio-chart-widget-tab`).forEach((tab) => {
|
|
24478
|
+
const btn = tab;
|
|
24479
|
+
btn.classList.toggle("active", btn.dataset.type === currentChartType);
|
|
24480
|
+
});
|
|
24481
|
+
}
|
|
24482
|
+
function updateTitle() {
|
|
24483
|
+
const titleEl = document.getElementById(`${widgetId}-title`);
|
|
24484
|
+
if (titleEl) {
|
|
24485
|
+
titleEl.textContent = getTitle();
|
|
24486
|
+
}
|
|
24487
|
+
}
|
|
24488
|
+
function setLoading(loading) {
|
|
24489
|
+
const loadingEl = document.getElementById(`${widgetId}-loading`);
|
|
24490
|
+
if (loadingEl) {
|
|
24491
|
+
loadingEl.style.display = loading ? "flex" : "none";
|
|
24492
|
+
}
|
|
24493
|
+
}
|
|
24494
|
+
function formatValue(value) {
|
|
24495
|
+
const unit = config.unit ?? "";
|
|
24496
|
+
const unitLarge = config.unitLarge;
|
|
24497
|
+
const threshold = config.thresholdForLargeUnit ?? 1e3;
|
|
24498
|
+
if (unitLarge && Math.abs(value) >= threshold) {
|
|
24499
|
+
return `${(value / threshold).toFixed(2)} ${unitLarge}`;
|
|
24500
|
+
}
|
|
24501
|
+
return `${value.toFixed(2)} ${unit}`;
|
|
24502
|
+
}
|
|
24503
|
+
function updateFooterStats(data) {
|
|
24504
|
+
const totalEl = document.getElementById(`${widgetId}-stat-total`);
|
|
24505
|
+
const avgEl = document.getElementById(`${widgetId}-stat-avg`);
|
|
24506
|
+
const peakEl = document.getElementById(`${widgetId}-stat-peak`);
|
|
24507
|
+
const peakDateEl = document.getElementById(`${widgetId}-stat-peak-date`);
|
|
24508
|
+
if (!data.dailyTotals || data.dailyTotals.length === 0) {
|
|
24509
|
+
if (totalEl) totalEl.textContent = "--";
|
|
24510
|
+
if (avgEl) avgEl.textContent = "--";
|
|
24511
|
+
if (peakEl) peakEl.textContent = "--";
|
|
24512
|
+
if (peakDateEl) peakDateEl.textContent = "";
|
|
24513
|
+
return;
|
|
24514
|
+
}
|
|
24515
|
+
const isTemperature = config.domain === "temperature";
|
|
24516
|
+
const totals = data.dailyTotals;
|
|
24517
|
+
const labels = data.labels ?? [];
|
|
24518
|
+
const total = totals.reduce((a, b) => a + b, 0);
|
|
24519
|
+
const avg = total / totals.length;
|
|
24520
|
+
const peakValue = Math.max(...totals);
|
|
24521
|
+
const peakIndex = totals.indexOf(peakValue);
|
|
24522
|
+
const peakDate = labels[peakIndex] ?? "";
|
|
24523
|
+
if (totalEl) {
|
|
24524
|
+
if (isTemperature) {
|
|
24525
|
+
totalEl.textContent = formatValue(avg);
|
|
24526
|
+
const labelEl = totalEl.previousElementSibling;
|
|
24527
|
+
if (labelEl) labelEl.textContent = "M\xE9dia Per\xEDodo";
|
|
24528
|
+
} else {
|
|
24529
|
+
totalEl.textContent = formatValue(total);
|
|
24530
|
+
}
|
|
24531
|
+
}
|
|
24532
|
+
if (avgEl) {
|
|
24533
|
+
avgEl.textContent = formatValue(avg);
|
|
24534
|
+
}
|
|
24535
|
+
if (peakEl) {
|
|
24536
|
+
peakEl.textContent = formatValue(peakValue);
|
|
24537
|
+
}
|
|
24538
|
+
if (peakDateEl) {
|
|
24539
|
+
peakDateEl.textContent = peakDate;
|
|
24540
|
+
}
|
|
24541
|
+
}
|
|
24542
|
+
function openSettingsModal() {
|
|
24543
|
+
tempPeriod = currentPeriod;
|
|
24544
|
+
tempChartType = currentChartType;
|
|
24545
|
+
tempVizMode = currentVizMode;
|
|
24546
|
+
tempTheme = currentTheme;
|
|
24547
|
+
tempIdealRange = currentIdealRange ? { ...currentIdealRange } : null;
|
|
24548
|
+
if (!settingsModalElement) {
|
|
24549
|
+
settingsModalElement = document.createElement("div");
|
|
24550
|
+
settingsModalElement.innerHTML = renderSettingsModal();
|
|
24551
|
+
document.body.appendChild(settingsModalElement.firstElementChild);
|
|
24552
|
+
settingsModalElement = document.getElementById(`${widgetId}-settings-overlay`);
|
|
24553
|
+
setupSettingsModalListeners();
|
|
24554
|
+
}
|
|
24555
|
+
updateSettingsModalValues();
|
|
24556
|
+
updateIdealRangeSuggestionTooltip();
|
|
24557
|
+
settingsModalElement?.classList.remove("hidden");
|
|
24558
|
+
}
|
|
24559
|
+
function closeSettingsModal() {
|
|
24560
|
+
settingsModalElement?.classList.add("hidden");
|
|
24561
|
+
}
|
|
24562
|
+
function updateSettingsModalValues() {
|
|
24563
|
+
const periodSelect = document.getElementById(`${widgetId}-settings-period`);
|
|
24564
|
+
if (periodSelect) periodSelect.value = String(tempPeriod);
|
|
24565
|
+
const minInput = document.getElementById(`${widgetId}-settings-range-min`);
|
|
24566
|
+
const maxInput = document.getElementById(`${widgetId}-settings-range-max`);
|
|
24567
|
+
const labelInput = document.getElementById(`${widgetId}-settings-range-label`);
|
|
24568
|
+
if (minInput) minInput.value = tempIdealRange?.min?.toString() ?? "";
|
|
24569
|
+
if (maxInput) maxInput.value = tempIdealRange?.max?.toString() ?? "";
|
|
24570
|
+
if (labelInput) labelInput.value = tempIdealRange?.label ?? "";
|
|
24571
|
+
updateSettingsModalTabs();
|
|
24572
|
+
}
|
|
24573
|
+
function updateSettingsModalTabs() {
|
|
24574
|
+
document.querySelectorAll(`#${widgetId}-settings-chart-type .myio-settings-tab`).forEach((tab) => {
|
|
24575
|
+
const btn = tab;
|
|
24576
|
+
btn.classList.toggle("active", btn.dataset.type === tempChartType);
|
|
24577
|
+
});
|
|
24578
|
+
document.querySelectorAll(`#${widgetId}-settings-viz-mode .myio-settings-tab`).forEach((tab) => {
|
|
24579
|
+
const btn = tab;
|
|
24580
|
+
btn.classList.toggle("active", btn.dataset.viz === tempVizMode);
|
|
24581
|
+
});
|
|
24582
|
+
document.querySelectorAll(`#${widgetId}-settings-theme .myio-settings-tab`).forEach((tab) => {
|
|
24583
|
+
const btn = tab;
|
|
24584
|
+
btn.classList.toggle("active", btn.dataset.theme === tempTheme);
|
|
24585
|
+
});
|
|
24586
|
+
}
|
|
24587
|
+
function updateWeekdayLabel() {
|
|
24588
|
+
const checkboxes = document.querySelectorAll(`input[name="${widgetId}-weekday"]`);
|
|
24589
|
+
const checked = Array.from(checkboxes).filter((cb) => cb.checked);
|
|
24590
|
+
const label = document.getElementById(`${widgetId}-settings-weekday-label`);
|
|
24591
|
+
if (label) {
|
|
24592
|
+
if (checked.length === 0) {
|
|
24593
|
+
label.textContent = "Nenhum dia";
|
|
24594
|
+
} else if (checked.length === checkboxes.length) {
|
|
24595
|
+
label.textContent = "Todos os dias";
|
|
24596
|
+
} else {
|
|
24597
|
+
label.textContent = `${checked.length} dias selecionados`;
|
|
24598
|
+
}
|
|
24599
|
+
}
|
|
24600
|
+
}
|
|
24601
|
+
function updateDayPeriodLabel() {
|
|
24602
|
+
const checkboxes = document.querySelectorAll(`input[name="${widgetId}-dayperiod"]`);
|
|
24603
|
+
const checked = Array.from(checkboxes).filter((cb) => cb.checked);
|
|
24604
|
+
const label = document.getElementById(`${widgetId}-settings-dayperiod-label`);
|
|
24605
|
+
if (label) {
|
|
24606
|
+
if (checked.length === 0) {
|
|
24607
|
+
label.textContent = "Nenhum per\xEDodo";
|
|
24608
|
+
} else if (checked.length === checkboxes.length) {
|
|
24609
|
+
label.textContent = "Todos os per\xEDodos";
|
|
24610
|
+
} else {
|
|
24611
|
+
label.textContent = `${checked.length} per\xEDodos selecionados`;
|
|
24612
|
+
}
|
|
24613
|
+
}
|
|
24614
|
+
}
|
|
24615
|
+
function calculateIdealRangeSuggestion() {
|
|
24616
|
+
const data = chartInstance?.getCachedData();
|
|
24617
|
+
if (!data || !data.dailyTotals || data.dailyTotals.length === 0) {
|
|
24618
|
+
return { min: 0, max: 0, avg: 0 };
|
|
24619
|
+
}
|
|
24620
|
+
const total = data.dailyTotals.reduce((a, b) => a + b, 0);
|
|
24621
|
+
const avg = total / data.dailyTotals.length;
|
|
24622
|
+
const min = avg * 0.85;
|
|
24623
|
+
const max = avg * 1.15;
|
|
24624
|
+
return {
|
|
24625
|
+
min: Math.round(min * 10) / 10,
|
|
24626
|
+
max: Math.round(max * 10) / 10,
|
|
24627
|
+
avg: Math.round(avg * 10) / 10
|
|
24628
|
+
};
|
|
24629
|
+
}
|
|
24630
|
+
function updateIdealRangeSuggestionTooltip() {
|
|
24631
|
+
const suggestion = calculateIdealRangeSuggestion();
|
|
24632
|
+
const suggestionEl = document.getElementById(`${widgetId}-settings-suggestion`);
|
|
24633
|
+
const hintEl = document.getElementById(`${widgetId}-settings-range-hint`);
|
|
24634
|
+
const unit = config.unit ?? "";
|
|
24635
|
+
const isTemperature = config.domain === "temperature";
|
|
24636
|
+
currentSuggestion = suggestion;
|
|
24637
|
+
if (hintEl) {
|
|
24638
|
+
if (isTemperature) {
|
|
24639
|
+
hintEl.textContent = "(valores carregados do cliente)";
|
|
24640
|
+
} else {
|
|
24641
|
+
hintEl.textContent = "(opcional - deixe zerado para n\xE3o exibir)";
|
|
24642
|
+
}
|
|
24643
|
+
}
|
|
24644
|
+
if (suggestionEl) {
|
|
24645
|
+
if (suggestion.avg > 0) {
|
|
24646
|
+
const tooltipText = `Sugest\xE3o: ${suggestion.min} - ${suggestion.max} ${unit} (m\xE9dia \xB115%). Clique para aplicar.`;
|
|
24647
|
+
suggestionEl.title = tooltipText;
|
|
24648
|
+
suggestionEl.style.display = "inline";
|
|
24649
|
+
} else {
|
|
24650
|
+
suggestionEl.style.display = "none";
|
|
24651
|
+
}
|
|
24652
|
+
}
|
|
24653
|
+
}
|
|
24654
|
+
function applyIdealRangeSuggestion() {
|
|
24655
|
+
if (!currentSuggestion || currentSuggestion.min === 0 && currentSuggestion.max === 0) {
|
|
24656
|
+
return;
|
|
24657
|
+
}
|
|
24658
|
+
const minInput = document.getElementById(`${widgetId}-settings-range-min`);
|
|
24659
|
+
const maxInput = document.getElementById(`${widgetId}-settings-range-max`);
|
|
24660
|
+
const labelInput = document.getElementById(`${widgetId}-settings-range-label`);
|
|
24661
|
+
if (minInput) minInput.value = String(currentSuggestion.min);
|
|
24662
|
+
if (maxInput) maxInput.value = String(currentSuggestion.max);
|
|
24663
|
+
if (labelInput) labelInput.value = "Faixa Sugerida";
|
|
24664
|
+
tempIdealRange = {
|
|
24665
|
+
min: currentSuggestion.min,
|
|
24666
|
+
max: currentSuggestion.max,
|
|
24667
|
+
label: "Faixa Sugerida"
|
|
24668
|
+
};
|
|
24669
|
+
}
|
|
24670
|
+
function setupSettingsModalListeners() {
|
|
24671
|
+
settingsHeaderInstance?.attachListeners();
|
|
24672
|
+
document.getElementById(`${widgetId}-settings-overlay`)?.addEventListener("click", (e) => {
|
|
24673
|
+
if (e.target.classList.contains("myio-settings-overlay")) {
|
|
24674
|
+
closeSettingsModal();
|
|
24675
|
+
}
|
|
24676
|
+
});
|
|
24677
|
+
document.getElementById(`${widgetId}-settings-granularity`)?.addEventListener("change", (e) => {
|
|
24678
|
+
const select = e.target;
|
|
24679
|
+
const dayPeriodField = document.getElementById(`${widgetId}-settings-dayperiod-field`);
|
|
24680
|
+
if (dayPeriodField) {
|
|
24681
|
+
dayPeriodField.style.display = select.value === "1h" ? "block" : "none";
|
|
24682
|
+
}
|
|
24683
|
+
});
|
|
24684
|
+
document.getElementById(`${widgetId}-settings-suggestion`)?.addEventListener("click", () => {
|
|
24685
|
+
applyIdealRangeSuggestion();
|
|
24686
|
+
});
|
|
24687
|
+
document.getElementById(`${widgetId}-settings-weekday-btn`)?.addEventListener("click", (e) => {
|
|
24688
|
+
e.stopPropagation();
|
|
24689
|
+
const dropdown = document.getElementById(`${widgetId}-settings-weekday-dropdown`);
|
|
24690
|
+
dropdown?.classList.toggle("hidden");
|
|
24691
|
+
});
|
|
24692
|
+
document.querySelectorAll(`input[name="${widgetId}-weekday"]`).forEach((cb) => {
|
|
24693
|
+
cb.addEventListener("change", updateWeekdayLabel);
|
|
24694
|
+
});
|
|
24695
|
+
document.getElementById(`${widgetId}-settings-weekday-all`)?.addEventListener("click", () => {
|
|
24696
|
+
document.querySelectorAll(`input[name="${widgetId}-weekday"]`).forEach((cb) => {
|
|
24697
|
+
cb.checked = true;
|
|
24698
|
+
});
|
|
24699
|
+
updateWeekdayLabel();
|
|
24700
|
+
});
|
|
24701
|
+
document.getElementById(`${widgetId}-settings-weekday-clear`)?.addEventListener("click", () => {
|
|
24702
|
+
document.querySelectorAll(`input[name="${widgetId}-weekday"]`).forEach((cb) => {
|
|
24703
|
+
cb.checked = false;
|
|
24704
|
+
});
|
|
24705
|
+
updateWeekdayLabel();
|
|
24706
|
+
});
|
|
24707
|
+
document.getElementById(`${widgetId}-settings-dayperiod-btn`)?.addEventListener("click", (e) => {
|
|
24708
|
+
e.stopPropagation();
|
|
24709
|
+
const dropdown = document.getElementById(`${widgetId}-settings-dayperiod-dropdown`);
|
|
24710
|
+
dropdown?.classList.toggle("hidden");
|
|
24711
|
+
});
|
|
24712
|
+
document.querySelectorAll(`input[name="${widgetId}-dayperiod"]`).forEach((cb) => {
|
|
24713
|
+
cb.addEventListener("change", updateDayPeriodLabel);
|
|
24714
|
+
});
|
|
24715
|
+
document.getElementById(`${widgetId}-settings-dayperiod-all`)?.addEventListener("click", () => {
|
|
24716
|
+
document.querySelectorAll(`input[name="${widgetId}-dayperiod"]`).forEach((cb) => {
|
|
24717
|
+
cb.checked = true;
|
|
24718
|
+
});
|
|
24719
|
+
updateDayPeriodLabel();
|
|
24720
|
+
});
|
|
24721
|
+
document.getElementById(`${widgetId}-settings-dayperiod-clear`)?.addEventListener("click", () => {
|
|
24722
|
+
document.querySelectorAll(`input[name="${widgetId}-dayperiod"]`).forEach((cb) => {
|
|
24723
|
+
cb.checked = false;
|
|
24724
|
+
});
|
|
24725
|
+
updateDayPeriodLabel();
|
|
24726
|
+
});
|
|
24727
|
+
document.addEventListener("click", (e) => {
|
|
24728
|
+
const target = e.target;
|
|
24729
|
+
const weekdayDropdown = document.getElementById(`${widgetId}-settings-weekday-dropdown`);
|
|
24730
|
+
const dayperiodDropdown = document.getElementById(`${widgetId}-settings-dayperiod-dropdown`);
|
|
24731
|
+
if (weekdayDropdown && !target.closest(`#${widgetId}-settings-weekday-btn`) && !target.closest(`#${widgetId}-settings-weekday-dropdown`)) {
|
|
24732
|
+
weekdayDropdown.classList.add("hidden");
|
|
24733
|
+
}
|
|
24734
|
+
if (dayperiodDropdown && !target.closest(`#${widgetId}-settings-dayperiod-btn`) && !target.closest(`#${widgetId}-settings-dayperiod-dropdown`)) {
|
|
24735
|
+
dayperiodDropdown.classList.add("hidden");
|
|
24736
|
+
}
|
|
24737
|
+
});
|
|
24738
|
+
document.getElementById(`${widgetId}-settings-chart-type`)?.addEventListener("click", (e) => {
|
|
24739
|
+
const target = e.target;
|
|
24740
|
+
if (target.classList.contains("myio-settings-tab")) {
|
|
24741
|
+
tempChartType = target.dataset.type;
|
|
24742
|
+
updateSettingsModalTabs();
|
|
24743
|
+
}
|
|
24744
|
+
});
|
|
24745
|
+
document.getElementById(`${widgetId}-settings-viz-mode`)?.addEventListener("click", (e) => {
|
|
24746
|
+
const target = e.target;
|
|
24747
|
+
if (target.classList.contains("myio-settings-tab")) {
|
|
24748
|
+
tempVizMode = target.dataset.viz;
|
|
24749
|
+
updateSettingsModalTabs();
|
|
24750
|
+
}
|
|
24751
|
+
});
|
|
24752
|
+
document.getElementById(`${widgetId}-settings-theme`)?.addEventListener("click", (e) => {
|
|
24753
|
+
const target = e.target;
|
|
24754
|
+
if (target.classList.contains("myio-settings-tab")) {
|
|
24755
|
+
tempTheme = target.dataset.theme;
|
|
24756
|
+
updateSettingsModalTabs();
|
|
24757
|
+
}
|
|
24758
|
+
});
|
|
24759
|
+
document.getElementById(`${widgetId}-settings-reset`)?.addEventListener("click", () => {
|
|
24760
|
+
tempPeriod = config.defaultPeriod ?? 7;
|
|
24761
|
+
tempChartType = config.defaultChartType ?? "line";
|
|
24762
|
+
tempVizMode = config.defaultVizMode ?? "total";
|
|
24763
|
+
tempTheme = config.theme ?? "light";
|
|
24764
|
+
tempIdealRange = config.idealRange ?? null;
|
|
24765
|
+
const granularitySelect = document.getElementById(`${widgetId}-settings-granularity`);
|
|
24766
|
+
if (granularitySelect) granularitySelect.value = "1d";
|
|
24767
|
+
const dayPeriodField = document.getElementById(`${widgetId}-settings-dayperiod-field`);
|
|
24768
|
+
if (dayPeriodField) dayPeriodField.style.display = "none";
|
|
24769
|
+
document.querySelectorAll(`input[name="${widgetId}-weekday"]`).forEach((cb) => {
|
|
24770
|
+
cb.checked = true;
|
|
24771
|
+
});
|
|
24772
|
+
updateWeekdayLabel();
|
|
24773
|
+
document.querySelectorAll(`input[name="${widgetId}-dayperiod"]`).forEach((cb) => {
|
|
24774
|
+
cb.checked = true;
|
|
24775
|
+
});
|
|
24776
|
+
updateDayPeriodLabel();
|
|
24777
|
+
updateSettingsModalValues();
|
|
24778
|
+
});
|
|
24779
|
+
document.getElementById(`${widgetId}-settings-apply`)?.addEventListener("click", async () => {
|
|
24780
|
+
const minInput = document.getElementById(`${widgetId}-settings-range-min`);
|
|
24781
|
+
const maxInput = document.getElementById(`${widgetId}-settings-range-max`);
|
|
24782
|
+
const labelInput = document.getElementById(`${widgetId}-settings-range-label`);
|
|
24783
|
+
const periodSelect = document.getElementById(`${widgetId}-settings-period`);
|
|
24784
|
+
const min = parseFloat(minInput?.value || "0");
|
|
24785
|
+
const max = parseFloat(maxInput?.value || "0");
|
|
24786
|
+
const label = labelInput?.value || "";
|
|
24787
|
+
tempPeriod = parseInt(periodSelect?.value || "7", 10);
|
|
24788
|
+
if (min > 0 || max > 0) {
|
|
24789
|
+
tempIdealRange = { min, max, label };
|
|
24790
|
+
} else {
|
|
24791
|
+
tempIdealRange = null;
|
|
24792
|
+
}
|
|
24793
|
+
closeSettingsModal();
|
|
24794
|
+
if (tempTheme !== currentTheme) {
|
|
24795
|
+
instance.setTheme(tempTheme);
|
|
24796
|
+
}
|
|
24797
|
+
if (tempChartType !== currentChartType) {
|
|
24798
|
+
instance.setChartType(tempChartType);
|
|
24799
|
+
}
|
|
24800
|
+
if (tempVizMode !== currentVizMode) {
|
|
24801
|
+
instance.setVizMode(tempVizMode);
|
|
24802
|
+
}
|
|
24803
|
+
if (JSON.stringify(tempIdealRange) !== JSON.stringify(currentIdealRange)) {
|
|
24804
|
+
instance.setIdealRange(tempIdealRange);
|
|
24805
|
+
}
|
|
24806
|
+
if (tempPeriod !== currentPeriod) {
|
|
24807
|
+
await instance.setPeriod(tempPeriod);
|
|
24808
|
+
}
|
|
24809
|
+
});
|
|
24810
|
+
}
|
|
24811
|
+
const instance = {
|
|
24812
|
+
async render() {
|
|
24813
|
+
containerElement = document.getElementById(config.containerId);
|
|
24814
|
+
if (!containerElement) {
|
|
24815
|
+
console.error(`[ConsumptionWidget] Container #${config.containerId} not found`);
|
|
24816
|
+
return;
|
|
24817
|
+
}
|
|
24818
|
+
injectStyles();
|
|
24819
|
+
containerElement.innerHTML = renderHTML();
|
|
24820
|
+
setupListeners();
|
|
24821
|
+
setLoading(true);
|
|
24822
|
+
chartInstance = createConsumption7DaysChart({
|
|
24823
|
+
...config,
|
|
24824
|
+
containerId: `${widgetId}-canvas`,
|
|
24825
|
+
theme: currentTheme,
|
|
24826
|
+
defaultChartType: currentChartType,
|
|
24827
|
+
defaultVizMode: currentVizMode,
|
|
24828
|
+
defaultPeriod: currentPeriod,
|
|
24829
|
+
idealRange: currentIdealRange,
|
|
24830
|
+
colors: {
|
|
24831
|
+
primary: primaryColor,
|
|
24832
|
+
background: `${primaryColor}20`,
|
|
24833
|
+
shoppingColors: domainColors,
|
|
24834
|
+
...config.colors
|
|
24835
|
+
},
|
|
24836
|
+
onDataLoaded: (data) => {
|
|
24837
|
+
setLoading(false);
|
|
24838
|
+
updateFooterStats(data);
|
|
24839
|
+
config.onDataLoaded?.(data);
|
|
24840
|
+
},
|
|
24841
|
+
onError: (error) => {
|
|
24842
|
+
setLoading(false);
|
|
24843
|
+
config.onError?.(error);
|
|
24844
|
+
}
|
|
24845
|
+
});
|
|
24846
|
+
await chartInstance.render();
|
|
24847
|
+
setLoading(false);
|
|
24848
|
+
},
|
|
24849
|
+
async refresh(forceRefresh = false) {
|
|
24850
|
+
if (!chartInstance) return;
|
|
24851
|
+
setLoading(true);
|
|
24852
|
+
await chartInstance.refresh(forceRefresh);
|
|
24853
|
+
setLoading(false);
|
|
24854
|
+
},
|
|
24855
|
+
setChartType(type) {
|
|
24856
|
+
if (currentChartType === type) return;
|
|
24857
|
+
currentChartType = type;
|
|
24858
|
+
chartInstance?.setChartType(type);
|
|
24859
|
+
updateTabStates();
|
|
24860
|
+
},
|
|
24861
|
+
setVizMode(mode) {
|
|
24862
|
+
if (currentVizMode === mode) return;
|
|
24863
|
+
currentVizMode = mode;
|
|
24864
|
+
chartInstance?.setVizMode(mode);
|
|
24865
|
+
updateTabStates();
|
|
24866
|
+
},
|
|
24867
|
+
setTheme(theme) {
|
|
24868
|
+
if (currentTheme === theme) return;
|
|
24869
|
+
currentTheme = theme;
|
|
24870
|
+
chartInstance?.setTheme(theme);
|
|
24871
|
+
const widget = document.getElementById(widgetId);
|
|
24872
|
+
if (widget) {
|
|
24873
|
+
widget.classList.toggle("dark", theme === "dark");
|
|
24874
|
+
}
|
|
24875
|
+
updateStyles();
|
|
24876
|
+
},
|
|
24877
|
+
async setPeriod(days) {
|
|
24878
|
+
if (currentPeriod === days) return;
|
|
24879
|
+
currentPeriod = days;
|
|
24880
|
+
updateTitle();
|
|
24881
|
+
setLoading(true);
|
|
24882
|
+
await chartInstance?.setPeriod(days);
|
|
24883
|
+
setLoading(false);
|
|
24884
|
+
},
|
|
24885
|
+
setIdealRange(range) {
|
|
24886
|
+
currentIdealRange = range;
|
|
24887
|
+
chartInstance?.setIdealRange(range);
|
|
24888
|
+
},
|
|
24889
|
+
getChart() {
|
|
24890
|
+
return chartInstance;
|
|
24891
|
+
},
|
|
24892
|
+
getCachedData() {
|
|
24893
|
+
return chartInstance?.getCachedData() ?? null;
|
|
24894
|
+
},
|
|
24895
|
+
exportCSV(filename) {
|
|
24896
|
+
chartInstance?.exportCSV(filename);
|
|
24897
|
+
},
|
|
24898
|
+
destroy() {
|
|
24899
|
+
chartInstance?.destroy();
|
|
24900
|
+
chartInstance = null;
|
|
24901
|
+
if (styleElement) {
|
|
24902
|
+
styleElement.remove();
|
|
24903
|
+
styleElement = null;
|
|
24904
|
+
}
|
|
24905
|
+
settingsHeaderInstance?.destroy();
|
|
24906
|
+
settingsHeaderInstance = null;
|
|
24907
|
+
if (settingsModalElement) {
|
|
24908
|
+
settingsModalElement.remove();
|
|
24909
|
+
settingsModalElement = null;
|
|
24910
|
+
}
|
|
24911
|
+
if (containerElement) {
|
|
24912
|
+
containerElement.innerHTML = "";
|
|
24913
|
+
containerElement = null;
|
|
24914
|
+
}
|
|
24915
|
+
}
|
|
24916
|
+
};
|
|
24917
|
+
return instance;
|
|
24918
|
+
}
|
|
24919
|
+
|
|
24920
|
+
// src/components/ExportData/index.ts
|
|
24921
|
+
var DEFAULT_COLORS4 = {
|
|
24922
|
+
primary: "#3e1a7d",
|
|
24923
|
+
// MyIO purple
|
|
24924
|
+
secondary: "#6b4c9a",
|
|
24925
|
+
// Light purple
|
|
24926
|
+
accent: "#00bcd4",
|
|
24927
|
+
// Cyan accent
|
|
24928
|
+
background: "#ffffff",
|
|
24929
|
+
// White
|
|
24930
|
+
text: "#333333",
|
|
24931
|
+
// Dark gray
|
|
24932
|
+
chartColors: ["#3e1a7d", "#00bcd4", "#4caf50", "#ff9800", "#e91e63", "#9c27b0"]
|
|
24933
|
+
};
|
|
24934
|
+
var DOMAIN_ICONS = {
|
|
24935
|
+
energy: "\u26A1",
|
|
24936
|
+
// Lightning bolt
|
|
24937
|
+
water: "\u{1F4A7}",
|
|
24938
|
+
// Water drop
|
|
24939
|
+
temperature: "\u{1F321}\uFE0F"
|
|
24940
|
+
// Thermometer
|
|
24941
|
+
};
|
|
24942
|
+
var DOMAIN_LABELS = {
|
|
24943
|
+
energy: "Energia",
|
|
24944
|
+
water: "\xC1gua",
|
|
24945
|
+
temperature: "Temperatura"
|
|
24946
|
+
};
|
|
24947
|
+
var DOMAIN_LABELS_EN = {
|
|
24948
|
+
energy: "Energy",
|
|
24949
|
+
water: "Water",
|
|
24950
|
+
temperature: "Temperature"
|
|
24951
|
+
};
|
|
24952
|
+
var DOMAIN_UNITS = {
|
|
24953
|
+
energy: "kWh",
|
|
24954
|
+
water: "m\xB3",
|
|
24955
|
+
temperature: "\xB0C"
|
|
24956
|
+
};
|
|
24957
|
+
var CSV_SEPARATORS = {
|
|
24958
|
+
"pt-BR": ";",
|
|
24959
|
+
"en-US": ",",
|
|
24960
|
+
"default": ";"
|
|
24961
|
+
};
|
|
24962
|
+
function formatDateForFilename(date) {
|
|
24963
|
+
const pad = (n) => n.toString().padStart(2, "0");
|
|
24964
|
+
return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}-${pad(date.getHours())}-${pad(date.getMinutes())}-${pad(date.getSeconds())}`;
|
|
24965
|
+
}
|
|
24966
|
+
function sanitizeFilename(str) {
|
|
24967
|
+
return str.replace(/[<>:"/\\|?*]/g, "").replace(/\s+/g, "_").substring(0, 50);
|
|
24968
|
+
}
|
|
24969
|
+
function generateFilename(data, config) {
|
|
24970
|
+
const timestamp = formatDateForFilename(/* @__PURE__ */ new Date());
|
|
24971
|
+
const domainLabel = DOMAIN_LABELS_EN[config.domain].toUpperCase();
|
|
24972
|
+
const ext = config.formatExport;
|
|
24973
|
+
let baseName = "export";
|
|
24974
|
+
if ("device" in data && data.device) {
|
|
24975
|
+
const device = data.device;
|
|
24976
|
+
const label = device.label || device.name || "device";
|
|
24977
|
+
const identifier = device.identifier ? `-${device.identifier}` : "";
|
|
24978
|
+
baseName = `${sanitizeFilename(label)}${identifier}`;
|
|
24979
|
+
} else if ("customer" in data && data.customer?.customerName) {
|
|
24980
|
+
baseName = sanitizeFilename(data.customer.customerName);
|
|
24981
|
+
} else if ("groupName" in data) {
|
|
24982
|
+
baseName = sanitizeFilename(data.groupName);
|
|
24983
|
+
}
|
|
24984
|
+
return `${baseName}-${domainLabel}-${timestamp}.${ext}`;
|
|
24985
|
+
}
|
|
24986
|
+
function normalizeTimestamp(ts) {
|
|
24987
|
+
if (ts instanceof Date) return ts;
|
|
24988
|
+
if (typeof ts === "number") return new Date(ts);
|
|
24989
|
+
return new Date(ts);
|
|
24990
|
+
}
|
|
24991
|
+
function calculateStats2(dataPoints) {
|
|
24992
|
+
if (dataPoints.length === 0) {
|
|
24993
|
+
return { min: 0, max: 0, average: 0, sum: 0, count: 0 };
|
|
24994
|
+
}
|
|
24995
|
+
const values = dataPoints.map((d) => d.value);
|
|
24996
|
+
const sum = values.reduce((a, b) => a + b, 0);
|
|
24997
|
+
return {
|
|
24998
|
+
min: Math.min(...values),
|
|
24999
|
+
max: Math.max(...values),
|
|
25000
|
+
average: sum / values.length,
|
|
25001
|
+
sum,
|
|
25002
|
+
count: values.length
|
|
25003
|
+
};
|
|
25004
|
+
}
|
|
25005
|
+
function formatNumber2(value, locale, decimals = 2) {
|
|
25006
|
+
return new Intl.NumberFormat(locale, {
|
|
25007
|
+
minimumFractionDigits: decimals,
|
|
25008
|
+
maximumFractionDigits: decimals
|
|
25009
|
+
}).format(value);
|
|
25010
|
+
}
|
|
25011
|
+
function formatDate3(date, locale) {
|
|
25012
|
+
return new Intl.DateTimeFormat(locale, {
|
|
25013
|
+
year: "numeric",
|
|
25014
|
+
month: "2-digit",
|
|
25015
|
+
day: "2-digit",
|
|
25016
|
+
hour: "2-digit",
|
|
25017
|
+
minute: "2-digit"
|
|
25018
|
+
}).format(date);
|
|
25019
|
+
}
|
|
25020
|
+
function generateCSV(data, config) {
|
|
25021
|
+
const sep = CSV_SEPARATORS[config.locale] || CSV_SEPARATORS["default"];
|
|
25022
|
+
const rows = [];
|
|
25023
|
+
const escapeCSV = (val) => {
|
|
25024
|
+
const str = String(val ?? "");
|
|
25025
|
+
if (str.includes(sep) || str.includes('"') || str.includes("\n")) {
|
|
25026
|
+
return `"${str.replace(/"/g, '""')}"`;
|
|
25027
|
+
}
|
|
25028
|
+
return str;
|
|
25029
|
+
};
|
|
25030
|
+
const formatNumCSV = (val) => {
|
|
25031
|
+
return formatNumber2(val, config.locale);
|
|
25032
|
+
};
|
|
25033
|
+
if ("device" in data && "data" in data && Array.isArray(data.data)) {
|
|
25034
|
+
const deviceData = data;
|
|
25035
|
+
rows.push(["Timestamp", config.domainLabel, `Unit (${config.domainUnit})`]);
|
|
25036
|
+
for (const point of deviceData.data) {
|
|
25037
|
+
const ts = normalizeTimestamp(point.timestamp);
|
|
25038
|
+
rows.push([
|
|
25039
|
+
formatDate3(ts, config.locale),
|
|
25040
|
+
formatNumCSV(point.value),
|
|
25041
|
+
point.unit || config.domainUnit
|
|
25042
|
+
]);
|
|
25043
|
+
}
|
|
25044
|
+
if (config.includeStats) {
|
|
25045
|
+
const stats = calculateStats2(deviceData.data);
|
|
25046
|
+
rows.push([]);
|
|
25047
|
+
rows.push(["Statistics", "", ""]);
|
|
25048
|
+
rows.push(["Minimum", formatNumCSV(stats.min), config.domainUnit]);
|
|
25049
|
+
rows.push(["Maximum", formatNumCSV(stats.max), config.domainUnit]);
|
|
25050
|
+
rows.push(["Average", formatNumCSV(stats.average), config.domainUnit]);
|
|
25051
|
+
rows.push(["Total", formatNumCSV(stats.sum), config.domainUnit]);
|
|
25052
|
+
rows.push(["Count", String(stats.count), "points"]);
|
|
25053
|
+
}
|
|
25054
|
+
} else if ("devices" in data && Array.isArray(data.devices)) {
|
|
25055
|
+
const compData = data;
|
|
25056
|
+
const deviceHeaders = compData.devices.map(
|
|
25057
|
+
(d) => d.device.label || d.device.name || "Device"
|
|
25058
|
+
);
|
|
25059
|
+
rows.push(["Timestamp", ...deviceHeaders]);
|
|
25060
|
+
const allTimestamps = /* @__PURE__ */ new Set();
|
|
25061
|
+
compData.devices.forEach((d) => {
|
|
25062
|
+
d.data.forEach((point) => {
|
|
25063
|
+
allTimestamps.add(normalizeTimestamp(point.timestamp).getTime());
|
|
25064
|
+
});
|
|
25065
|
+
});
|
|
25066
|
+
const sortedTimestamps = Array.from(allTimestamps).sort((a, b) => a - b);
|
|
25067
|
+
for (const ts of sortedTimestamps) {
|
|
25068
|
+
const row = [formatDate3(new Date(ts), config.locale)];
|
|
25069
|
+
for (const device of compData.devices) {
|
|
25070
|
+
const point = device.data.find(
|
|
25071
|
+
(p) => normalizeTimestamp(p.timestamp).getTime() === ts
|
|
25072
|
+
);
|
|
25073
|
+
row.push(point ? formatNumCSV(point.value) : "");
|
|
25074
|
+
}
|
|
25075
|
+
rows.push(row);
|
|
25076
|
+
}
|
|
25077
|
+
}
|
|
25078
|
+
return rows.map((row) => row.map(escapeCSV).join(sep)).join("\r\n");
|
|
25079
|
+
}
|
|
25080
|
+
function generateXLSX(data, config) {
|
|
25081
|
+
return generateCSV(data, config);
|
|
25082
|
+
}
|
|
25083
|
+
function generatePDFContent(data, config) {
|
|
25084
|
+
const { colors, domainIcon, domainLabel, domainUnit, locale, includeStats, includeChart } = config;
|
|
25085
|
+
let deviceLabel = "Export";
|
|
25086
|
+
let customerName = "";
|
|
25087
|
+
let identifier = "";
|
|
25088
|
+
let dataPoints = [];
|
|
25089
|
+
if ("device" in data && "data" in data) {
|
|
25090
|
+
const deviceData = data;
|
|
25091
|
+
deviceLabel = deviceData.device.label || deviceData.device.name || "Device";
|
|
25092
|
+
identifier = deviceData.device.identifier || "";
|
|
25093
|
+
customerName = deviceData.customer?.customerName || "";
|
|
25094
|
+
dataPoints = deviceData.data;
|
|
25095
|
+
}
|
|
25096
|
+
const stats = calculateStats2(dataPoints);
|
|
25097
|
+
const tableRows = dataPoints.slice(0, 100).map((point) => {
|
|
25098
|
+
const ts = normalizeTimestamp(point.timestamp);
|
|
25099
|
+
return `
|
|
25100
|
+
<tr>
|
|
25101
|
+
<td style="padding: 8px; border-bottom: 1px solid #eee;">${formatDate3(ts, locale)}</td>
|
|
25102
|
+
<td style="padding: 8px; border-bottom: 1px solid #eee; text-align: right;">${formatNumber2(point.value, locale)}</td>
|
|
25103
|
+
<td style="padding: 8px; border-bottom: 1px solid #eee;">${point.unit || domainUnit}</td>
|
|
25104
|
+
</tr>
|
|
25105
|
+
`;
|
|
25106
|
+
}).join("");
|
|
25107
|
+
const statsSection = includeStats ? `
|
|
25108
|
+
<div style="margin-top: 24px; padding: 16px; background: #f5f5f5; border-radius: 8px;">
|
|
25109
|
+
<h3 style="margin: 0 0 12px 0; color: ${colors.primary};">Statistics</h3>
|
|
25110
|
+
<table style="width: 100%;">
|
|
25111
|
+
<tr>
|
|
25112
|
+
<td><strong>Minimum:</strong></td>
|
|
25113
|
+
<td>${formatNumber2(stats.min, locale)} ${domainUnit}</td>
|
|
25114
|
+
<td><strong>Maximum:</strong></td>
|
|
25115
|
+
<td>${formatNumber2(stats.max, locale)} ${domainUnit}</td>
|
|
25116
|
+
</tr>
|
|
25117
|
+
<tr>
|
|
25118
|
+
<td><strong>Average:</strong></td>
|
|
25119
|
+
<td>${formatNumber2(stats.average, locale)} ${domainUnit}</td>
|
|
25120
|
+
<td><strong>Total:</strong></td>
|
|
25121
|
+
<td>${formatNumber2(stats.sum, locale)} ${domainUnit}</td>
|
|
25122
|
+
</tr>
|
|
25123
|
+
</table>
|
|
25124
|
+
</div>
|
|
25125
|
+
` : "";
|
|
25126
|
+
return `
|
|
25127
|
+
<!DOCTYPE html>
|
|
25128
|
+
<html>
|
|
25129
|
+
<head>
|
|
25130
|
+
<meta charset="UTF-8">
|
|
25131
|
+
<title>${deviceLabel} - ${domainLabel} Report</title>
|
|
25132
|
+
<style>
|
|
25133
|
+
body {
|
|
25134
|
+
font-family: 'Roboto', Arial, sans-serif;
|
|
25135
|
+
margin: 0;
|
|
25136
|
+
padding: 24px;
|
|
25137
|
+
color: ${colors.text};
|
|
25138
|
+
background: ${colors.background};
|
|
25139
|
+
}
|
|
25140
|
+
.header {
|
|
25141
|
+
background: ${colors.primary};
|
|
25142
|
+
color: white;
|
|
25143
|
+
padding: 20px;
|
|
25144
|
+
border-radius: 8px;
|
|
25145
|
+
margin-bottom: 24px;
|
|
25146
|
+
}
|
|
25147
|
+
.header h1 {
|
|
25148
|
+
margin: 0;
|
|
25149
|
+
font-size: 24px;
|
|
25150
|
+
}
|
|
25151
|
+
.header .subtitle {
|
|
25152
|
+
opacity: 0.9;
|
|
25153
|
+
margin-top: 8px;
|
|
25154
|
+
}
|
|
25155
|
+
.device-info {
|
|
25156
|
+
display: flex;
|
|
25157
|
+
gap: 16px;
|
|
25158
|
+
margin-bottom: 16px;
|
|
25159
|
+
padding: 12px;
|
|
25160
|
+
background: #f5f5f5;
|
|
25161
|
+
border-radius: 8px;
|
|
25162
|
+
}
|
|
25163
|
+
.device-info span {
|
|
25164
|
+
padding: 4px 12px;
|
|
25165
|
+
background: ${colors.secondary};
|
|
25166
|
+
color: white;
|
|
25167
|
+
border-radius: 4px;
|
|
25168
|
+
font-size: 14px;
|
|
25169
|
+
}
|
|
25170
|
+
table {
|
|
25171
|
+
width: 100%;
|
|
25172
|
+
border-collapse: collapse;
|
|
25173
|
+
}
|
|
25174
|
+
th {
|
|
25175
|
+
background: ${colors.primary};
|
|
25176
|
+
color: white;
|
|
25177
|
+
padding: 12px 8px;
|
|
25178
|
+
text-align: left;
|
|
25179
|
+
}
|
|
25180
|
+
th:nth-child(2) {
|
|
25181
|
+
text-align: right;
|
|
25182
|
+
}
|
|
25183
|
+
.footer {
|
|
25184
|
+
margin-top: 24px;
|
|
25185
|
+
padding-top: 16px;
|
|
25186
|
+
border-top: 1px solid #eee;
|
|
25187
|
+
text-align: center;
|
|
25188
|
+
font-size: 12px;
|
|
25189
|
+
color: #999;
|
|
25190
|
+
}
|
|
25191
|
+
@media print {
|
|
25192
|
+
body { padding: 0; }
|
|
25193
|
+
.header { border-radius: 0; }
|
|
25194
|
+
}
|
|
25195
|
+
</style>
|
|
25196
|
+
</head>
|
|
25197
|
+
<body>
|
|
25198
|
+
<div class="header">
|
|
25199
|
+
<h1>${domainIcon} ${deviceLabel}</h1>
|
|
25200
|
+
<div class="subtitle">${domainLabel} Report - Generated ${formatDate3(/* @__PURE__ */ new Date(), locale)}</div>
|
|
25201
|
+
</div>
|
|
25202
|
+
|
|
25203
|
+
${customerName ? `<div class="customer-name" style="margin-bottom: 16px; font-size: 18px;"><strong>Customer:</strong> ${customerName}</div>` : ""}
|
|
25204
|
+
|
|
25205
|
+
${identifier ? `
|
|
25206
|
+
<div class="device-info">
|
|
25207
|
+
<span>ID: ${identifier}</span>
|
|
25208
|
+
<span>Domain: ${domainLabel}</span>
|
|
25209
|
+
<span>Unit: ${domainUnit}</span>
|
|
25210
|
+
</div>
|
|
25211
|
+
` : ""}
|
|
25212
|
+
|
|
25213
|
+
<table>
|
|
25214
|
+
<thead>
|
|
25215
|
+
<tr>
|
|
25216
|
+
<th>Timestamp</th>
|
|
25217
|
+
<th>${domainLabel} (${domainUnit})</th>
|
|
25218
|
+
<th>Unit</th>
|
|
25219
|
+
</tr>
|
|
25220
|
+
</thead>
|
|
25221
|
+
<tbody>
|
|
25222
|
+
${tableRows}
|
|
25223
|
+
${dataPoints.length > 100 ? `<tr><td colspan="3" style="text-align: center; padding: 16px; color: #999;">... and ${dataPoints.length - 100} more rows</td></tr>` : ""}
|
|
25224
|
+
</tbody>
|
|
25225
|
+
</table>
|
|
25226
|
+
|
|
25227
|
+
${statsSection}
|
|
25228
|
+
|
|
25229
|
+
<div class="footer">
|
|
25230
|
+
<p>${config.footerText || "Generated by MyIO Platform"}</p>
|
|
25231
|
+
</div>
|
|
25232
|
+
</body>
|
|
25233
|
+
</html>
|
|
25234
|
+
`;
|
|
25235
|
+
}
|
|
25236
|
+
function buildTemplateExport(params) {
|
|
25237
|
+
const {
|
|
25238
|
+
domain,
|
|
25239
|
+
formatExport,
|
|
25240
|
+
typeExport,
|
|
25241
|
+
colorsPallet,
|
|
25242
|
+
locale = "pt-BR",
|
|
25243
|
+
includeChart = formatExport === "pdf",
|
|
25244
|
+
includeStats = true,
|
|
25245
|
+
headerText,
|
|
25246
|
+
footerText
|
|
25247
|
+
} = params;
|
|
25248
|
+
const colors = {
|
|
25249
|
+
...DEFAULT_COLORS4,
|
|
25250
|
+
...colorsPallet,
|
|
25251
|
+
chartColors: colorsPallet?.chartColors || DEFAULT_COLORS4.chartColors
|
|
25252
|
+
};
|
|
25253
|
+
return {
|
|
25254
|
+
domain,
|
|
25255
|
+
formatExport,
|
|
25256
|
+
typeExport,
|
|
25257
|
+
colors,
|
|
25258
|
+
locale,
|
|
25259
|
+
includeChart,
|
|
25260
|
+
includeStats,
|
|
25261
|
+
headerText: headerText || `${DOMAIN_LABELS[domain]} Report`,
|
|
25262
|
+
footerText: footerText || "Generated by MyIO Platform",
|
|
25263
|
+
domainIcon: DOMAIN_ICONS[domain],
|
|
25264
|
+
domainLabel: DOMAIN_LABELS[domain],
|
|
25265
|
+
domainUnit: DOMAIN_UNITS[domain]
|
|
25266
|
+
};
|
|
25267
|
+
}
|
|
25268
|
+
function myioExportData(data, config, options) {
|
|
25269
|
+
const filename = generateFilename(data, config);
|
|
25270
|
+
let allDataPoints = [];
|
|
25271
|
+
if ("data" in data && Array.isArray(data.data)) {
|
|
25272
|
+
allDataPoints = data.data;
|
|
25273
|
+
} else if ("devices" in data && Array.isArray(data.devices)) {
|
|
25274
|
+
allDataPoints = data.devices.flatMap((d) => d.data);
|
|
25275
|
+
}
|
|
25276
|
+
const stats = calculateStats2(allDataPoints);
|
|
25277
|
+
const instance = {
|
|
25278
|
+
async export() {
|
|
25279
|
+
try {
|
|
25280
|
+
options?.onProgress?.(10, "Generating content...");
|
|
25281
|
+
let content;
|
|
25282
|
+
let mimeType;
|
|
25283
|
+
let finalFilename = filename;
|
|
25284
|
+
switch (config.formatExport) {
|
|
25285
|
+
case "csv":
|
|
25286
|
+
content = generateCSV(data, config);
|
|
25287
|
+
mimeType = "text/csv;charset=utf-8;";
|
|
25288
|
+
break;
|
|
25289
|
+
case "xlsx":
|
|
25290
|
+
content = generateXLSX(data, config);
|
|
25291
|
+
mimeType = "text/csv;charset=utf-8;";
|
|
25292
|
+
finalFilename = filename.replace(".xlsx", ".csv");
|
|
25293
|
+
break;
|
|
25294
|
+
case "pdf":
|
|
25295
|
+
content = generatePDFContent(data, config);
|
|
25296
|
+
mimeType = "text/html;charset=utf-8;";
|
|
25297
|
+
finalFilename = filename.replace(".pdf", ".html");
|
|
25298
|
+
break;
|
|
25299
|
+
default:
|
|
25300
|
+
throw new Error(`Unsupported format: ${config.formatExport}`);
|
|
25301
|
+
}
|
|
25302
|
+
options?.onProgress?.(80, "Creating file...");
|
|
25303
|
+
const bom = config.formatExport === "csv" ? "\uFEFF" : "";
|
|
25304
|
+
const blob = new Blob([bom + content], { type: mimeType });
|
|
25305
|
+
options?.onProgress?.(100, "Export complete");
|
|
25306
|
+
return {
|
|
25307
|
+
success: true,
|
|
25308
|
+
filename: finalFilename,
|
|
25309
|
+
blob,
|
|
25310
|
+
dataUrl: URL.createObjectURL(blob)
|
|
25311
|
+
};
|
|
25312
|
+
} catch (error) {
|
|
25313
|
+
return {
|
|
25314
|
+
success: false,
|
|
25315
|
+
filename,
|
|
25316
|
+
error: error instanceof Error ? error.message : "Unknown error"
|
|
25317
|
+
};
|
|
25318
|
+
}
|
|
25319
|
+
},
|
|
25320
|
+
async download() {
|
|
25321
|
+
const result = await this.export();
|
|
25322
|
+
if (!result.success || !result.blob) {
|
|
25323
|
+
console.error("Export failed:", result.error);
|
|
25324
|
+
return;
|
|
25325
|
+
}
|
|
25326
|
+
const link = document.createElement("a");
|
|
25327
|
+
link.href = URL.createObjectURL(result.blob);
|
|
25328
|
+
link.download = result.filename;
|
|
25329
|
+
link.style.display = "none";
|
|
25330
|
+
document.body.appendChild(link);
|
|
25331
|
+
link.click();
|
|
25332
|
+
document.body.removeChild(link);
|
|
25333
|
+
URL.revokeObjectURL(link.href);
|
|
25334
|
+
},
|
|
25335
|
+
async preview() {
|
|
25336
|
+
if (config.formatExport !== "pdf") {
|
|
25337
|
+
return null;
|
|
25338
|
+
}
|
|
25339
|
+
const result = await this.export();
|
|
25340
|
+
return result.dataUrl || null;
|
|
25341
|
+
},
|
|
25342
|
+
getStats() {
|
|
25343
|
+
return stats;
|
|
25344
|
+
},
|
|
25345
|
+
getFilename() {
|
|
25346
|
+
return filename;
|
|
25347
|
+
}
|
|
25348
|
+
};
|
|
25349
|
+
if (options?.autoDownload) {
|
|
25350
|
+
instance.download();
|
|
25351
|
+
}
|
|
25352
|
+
return instance;
|
|
25353
|
+
}
|
|
25354
|
+
var EXPORT_DEFAULT_COLORS = DEFAULT_COLORS4;
|
|
25355
|
+
var EXPORT_DOMAIN_ICONS = DOMAIN_ICONS;
|
|
25356
|
+
var EXPORT_DOMAIN_LABELS = DOMAIN_LABELS;
|
|
25357
|
+
var EXPORT_DOMAIN_UNITS = DOMAIN_UNITS;
|
|
25358
|
+
|
|
22281
25359
|
exports.CHART_COLORS = CHART_COLORS;
|
|
25360
|
+
exports.CONSUMPTION_CHART_COLORS = DEFAULT_COLORS;
|
|
25361
|
+
exports.CONSUMPTION_CHART_DEFAULTS = DEFAULT_CONFIG;
|
|
25362
|
+
exports.CONSUMPTION_THEME_COLORS = THEME_COLORS;
|
|
22282
25363
|
exports.ConnectionStatusType = ConnectionStatusType;
|
|
22283
25364
|
exports.DEFAULT_CLAMP_RANGE = DEFAULT_CLAMP_RANGE;
|
|
22284
25365
|
exports.DeviceStatusType = DeviceStatusType;
|
|
25366
|
+
exports.EXPORT_DEFAULT_COLORS = EXPORT_DEFAULT_COLORS;
|
|
25367
|
+
exports.EXPORT_DOMAIN_ICONS = EXPORT_DOMAIN_ICONS;
|
|
25368
|
+
exports.EXPORT_DOMAIN_LABELS = EXPORT_DOMAIN_LABELS;
|
|
25369
|
+
exports.EXPORT_DOMAIN_UNITS = EXPORT_DOMAIN_UNITS;
|
|
22285
25370
|
exports.MyIOChartModal = MyIOChartModal;
|
|
22286
25371
|
exports.MyIODraggableCard = MyIODraggableCard;
|
|
22287
25372
|
exports.MyIOSelectionStoreClass = MyIOSelectionStoreClass;
|
|
@@ -22292,11 +25377,13 @@ ${rangeText}`;
|
|
|
22292
25377
|
exports.averageByDay = averageByDay;
|
|
22293
25378
|
exports.buildListItemsThingsboardByUniqueDatasource = buildListItemsThingsboardByUniqueDatasource;
|
|
22294
25379
|
exports.buildMyioIngestionAuth = buildMyioIngestionAuth;
|
|
25380
|
+
exports.buildTemplateExport = buildTemplateExport;
|
|
22295
25381
|
exports.buildWaterReportCSV = buildWaterReportCSV;
|
|
22296
25382
|
exports.buildWaterStoresCSV = buildWaterStoresCSV;
|
|
22297
25383
|
exports.calcDeltaPercent = calcDeltaPercent;
|
|
22298
25384
|
exports.calculateDeviceStatus = calculateDeviceStatus;
|
|
22299
25385
|
exports.calculateDeviceStatusWithRanges = calculateDeviceStatusWithRanges;
|
|
25386
|
+
exports.calculateExportStats = calculateStats2;
|
|
22300
25387
|
exports.calculateStats = calculateStats;
|
|
22301
25388
|
exports.clampTemperature = clampTemperature;
|
|
22302
25389
|
exports.classify = classify;
|
|
@@ -22304,8 +25391,12 @@ ${rangeText}`;
|
|
|
22304
25391
|
exports.classifyWaterLabels = classifyWaterLabels;
|
|
22305
25392
|
exports.clearAllAuthCaches = clearAllAuthCaches;
|
|
22306
25393
|
exports.connectionStatusIcons = connectionStatusIcons;
|
|
25394
|
+
exports.createConsumption7DaysChart = createConsumption7DaysChart;
|
|
25395
|
+
exports.createConsumptionChartWidget = createConsumptionChartWidget;
|
|
25396
|
+
exports.createConsumptionModal = createConsumptionModal;
|
|
22307
25397
|
exports.createDateRangePicker = createDateRangePicker2;
|
|
22308
25398
|
exports.createInputDateRangePickerInsideDIV = createInputDateRangePickerInsideDIV;
|
|
25399
|
+
exports.createModalHeader = createModalHeader;
|
|
22309
25400
|
exports.decodePayload = decodePayload;
|
|
22310
25401
|
exports.decodePayloadBase64Xor = decodePayloadBase64Xor;
|
|
22311
25402
|
exports.detectDeviceType = detectDeviceType;
|
|
@@ -22319,6 +25410,7 @@ ${rangeText}`;
|
|
|
22319
25410
|
exports.fetchThingsboardCustomerAttrsFromStorage = fetchThingsboardCustomerAttrsFromStorage;
|
|
22320
25411
|
exports.fetchThingsboardCustomerServerScopeAttrs = fetchThingsboardCustomerServerScopeAttrs;
|
|
22321
25412
|
exports.findValue = findValue;
|
|
25413
|
+
exports.findValueWithDefault = findValueWithDefault;
|
|
22322
25414
|
exports.fmtPerc = fmtPerc;
|
|
22323
25415
|
exports.fmtPercLegacy = fmtPerc2;
|
|
22324
25416
|
exports.formatAllInSameUnit = formatAllInSameUnit;
|
|
@@ -22326,18 +25418,24 @@ ${rangeText}`;
|
|
|
22326
25418
|
exports.formatDateForInput = formatDateForInput;
|
|
22327
25419
|
exports.formatDateToYMD = formatDateToYMD;
|
|
22328
25420
|
exports.formatDateWithTimezoneOffset = formatDateWithTimezoneOffset;
|
|
25421
|
+
exports.formatDuration = formatDuration;
|
|
22329
25422
|
exports.formatEnergy = formatEnergy;
|
|
22330
25423
|
exports.formatNumberReadable = formatNumberReadable;
|
|
25424
|
+
exports.formatRelativeTime = formatRelativeTime;
|
|
22331
25425
|
exports.formatTankHeadFromCm = formatTankHeadFromCm;
|
|
22332
25426
|
exports.formatTemperature = formatTemperature2;
|
|
25427
|
+
exports.formatWater = formatWater;
|
|
22333
25428
|
exports.formatWaterByGroup = formatWaterByGroup;
|
|
22334
25429
|
exports.formatWaterVolumeM3 = formatWaterVolumeM3;
|
|
25430
|
+
exports.formatarDuracao = formatarDuracao;
|
|
25431
|
+
exports.generateExportFilename = generateFilename;
|
|
22335
25432
|
exports.getAuthCacheStats = getAuthCacheStats;
|
|
22336
25433
|
exports.getAvailableContexts = getAvailableContexts;
|
|
22337
25434
|
exports.getConnectionStatusIcon = getConnectionStatusIcon;
|
|
22338
25435
|
exports.getDateRangeArray = getDateRangeArray;
|
|
22339
25436
|
exports.getDeviceStatusIcon = getDeviceStatusIcon;
|
|
22340
25437
|
exports.getDeviceStatusInfo = getDeviceStatusInfo;
|
|
25438
|
+
exports.getModalHeaderStyles = getModalHeaderStyles;
|
|
22341
25439
|
exports.getSaoPauloISOString = getSaoPauloISOString;
|
|
22342
25440
|
exports.getSaoPauloISOStringFixed = getSaoPauloISOStringFixed;
|
|
22343
25441
|
exports.getValueByDatakey = getValueByDatakey;
|
|
@@ -22349,8 +25447,10 @@ ${rangeText}`;
|
|
|
22349
25447
|
exports.isValidConnectionStatus = isValidConnectionStatus;
|
|
22350
25448
|
exports.isValidDeviceStatus = isValidDeviceStatus;
|
|
22351
25449
|
exports.isWaterCategory = isWaterCategory;
|
|
25450
|
+
exports.mapConnectionStatus = mapConnectionStatus;
|
|
22352
25451
|
exports.mapDeviceStatusToCardStatus = mapDeviceStatusToCardStatus;
|
|
22353
25452
|
exports.mapDeviceToConnectionStatus = mapDeviceToConnectionStatus;
|
|
25453
|
+
exports.myioExportData = myioExportData;
|
|
22354
25454
|
exports.normalizeRecipients = normalizeRecipients;
|
|
22355
25455
|
exports.numbers = numbers_exports;
|
|
22356
25456
|
exports.openDashboardPopup = openDashboardPopup;
|