-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeco-o.planning-forecast-deltas.user.js
More file actions
247 lines (206 loc) · 8.74 KB
/
geco-o.planning-forecast-deltas.user.js
File metadata and controls
247 lines (206 loc) · 8.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// ==UserScript==
// @name GECO-O - Planning Forecast Deltas
// @namespace https://geco.reply.com/
// @version 2.2.3
// @description Show deltas for forecasts
// @author Roman Allenstein <r.allenstein@reply.de>
// @match https://geco.reply.com/*
// @match https://geco.reply.eu/*
// @run-at document-idle
// @grant none
// @downloadURL https://github.com/vanilla-reply/geco-toolbox/raw/refs/heads/main/geco-o.planning-forecast-deltas.user.js
// @updateURL https://github.com/vanilla-reply/geco-toolbox/raw/refs/heads/main/geco-o.planning-forecast-deltas.user.js
// ==/UserScript==
// == Changelog ========================================================================================================
// 1.0 Initial release
// 2.0.0 Fix selectors to work with both .forecast wrapped and direct inputs in month cells
// 2.1.0 Remove @noframes to run inside iframes (SPA version)
// 2.2.0 Fix layout shift by using absolute positioning for delta overlays
// 2.2.1 Fix Font size
// 2.2.2 Fix delta formatting
// 2.2.3 Fix delta formatting for negative values
(function() {
'use strict';
const DEBUG = false;
function dbg(...args) {
if (DEBUG) console.log('[ForecastDeltas]', ...args);
}
/** Inject styles **/
function ensureStyles() {
if (document.getElementById('tm-forecast-diff-style')) return;
const style = document.createElement('style');
style.id = 'tm-forecast-diff-style';
style.textContent = `
/* Cells with deltas */
.table__cell.tm-forecast-has-diff,
.forecast.table__subcell.tm-forecast-has-diff {
overflow: visible;
position: relative;
}
.tm-forecast-diff {
position: absolute;
left: 10px;
top: 50%;
transform: translateY(-50%);
font-size: 1em !important;
font-weight: bold;
white-space: nowrap;
display: none; /* Wird nur gezeigt wenn != 0 */
pointer-events: none;
z-index: 1;
text-shadow: 0 0 2px white, 0 0 2px white, 0 0 2px white;
}
/* Footer total delta */
.tm-forecast-diff-total {
position: absolute;
left: 10px;
top: 50%;
transform: translateY(-50%);
font-size: 1em !important;
font-weight: bold;
white-space: nowrap;
display: none; /* Wird nur gezeigt wenn != 0 */
pointer-events: none;
z-index: 1;
text-shadow: 0 0 2px white, 0 0 2px white, 0 0 2px white;
}
`;
document.head.appendChild(style);
dbg("Styles injected");
}
/** Parsing helper **/
function parseDE(str) {
if (!str) return 0;
str = str.replace(/\./g, '').replace(',', '.').trim();
const n = parseFloat(str);
return isNaN(n) ? 0 : n;
}
/** Delta format **/
function formatDelta(num) {
const sign = num > 0 ? "+" : "";
let formatted = Math.abs(num).toFixed(3).replace('.', ',');
formatted = formatted.replace(/(,\d*?)0+$/, "$1").replace(/,$/, ",0");
return sign + formatted;
}
/** Init one planning table **/
function initPlanningTable(table) {
if (table.dataset.tmForecastDone === "1") return;
table.dataset.tmForecastDone = "1";
ensureStyles();
dbg("Init planning table:", table);
// Find ALL inputs with data-init-value in month cells (both .forecast wrapped and direct)
const inputs = table.querySelectorAll(
'.table__body .table__cell[data-month] input.value[data-init-value]'
);
dbg("Forecast inputs found:", inputs.length);
if (!inputs.length) {
dbg("Alt check - all inputs in table:", table.querySelectorAll('input').length);
dbg("Alt check - input.value:", table.querySelectorAll('input.value').length);
dbg("Alt check - [data-init-value]:", table.querySelectorAll('[data-init-value]').length);
}
inputs.forEach(input => {
if (input.dataset.tmInit) return;
input.dataset.tmInit = "1";
input.dataset.tmBase = input.getAttribute("data-init-value") || input.value || "0";
// Container can be .forecast subcell or .table__cell directly
const container = input.closest('.forecast') || input.closest('.table__cell');
container.classList.add("tm-forecast-has-diff");
let diffSpan = container.querySelector(".tm-forecast-diff");
if (!diffSpan) {
diffSpan = document.createElement("span");
diffSpan.className = "tm-forecast-diff";
container.insertBefore(diffSpan, input);
}
const handler = () => updateCellDelta(input, table);
input.addEventListener("input", handler);
input.addEventListener("change", handler);
updateCellDelta(input, table);
});
updateAllTotals(table);
}
/** Update a single cell **/
function updateCellDelta(input, table) {
const base = parseDE(input.dataset.tmBase || "0");
const curr = parseDE(input.value);
const delta = curr - base;
dbg("Delta cell:", input, "=", delta);
const container = input.closest('.forecast') || input.closest('.table__cell');
const span = container?.querySelector(".tm-forecast-diff");
if (!span) return;
if (Math.abs(delta) < 1e-9) {
span.style.display = "none";
} else {
span.textContent = formatDelta(delta);
span.style.color = delta > 0 ? "#008800" : "#bb0000";
span.style.display = "inline";
}
const cell = input.closest('.table__cell[data-month]');
if (cell) {
updateColumnTotal(table, cell.getAttribute("data-month"));
}
}
/** Update total for one month **/
function updateColumnTotal(table, month) {
// Find all inputs with init values in this month (both .forecast wrapped and direct)
const inputs = table.querySelectorAll(
`.table__body .table__cell[data-month="${month}"] input.value[data-init-value]`
);
let total = 0;
inputs.forEach(inp => {
const base = parseDE(inp.dataset.tmBase || "0");
const curr = parseDE(inp.value);
total += (curr - base);
});
dbg(`Month ${month} total delta =`, total);
// Footer cell can have .forecast subcell or contain .value directly
const footerCell = table.querySelector(
`.table__foot .table__row--totals .table__cell[data-month="${month}"]`
);
if (!footerCell) return;
const footerContainer = footerCell.querySelector('.forecast') || footerCell;
const valueEl = footerContainer.querySelector(".value");
if (!valueEl) return;
let span = footerContainer.querySelector('.tm-forecast-diff-total');
if (!span) {
span = document.createElement("span");
span.className = "tm-forecast-diff-total";
footerContainer.insertBefore(span, valueEl);
}
if (Math.abs(total) < 1e-9) {
span.style.display = "none";
} else {
span.textContent = formatDelta(total);
span.style.color = total > 0 ? "#008800" : "#bb0000";
span.style.display = "inline";
}
}
function updateAllTotals(table) {
const months = table.querySelectorAll(
'.table__foot .table__row--totals .table__cell[data-month]'
);
months.forEach(cell => {
updateColumnTotal(table, cell.getAttribute("data-month"));
});
}
/** Initialization watcher **/
function tryInit() {
const tables = document.querySelectorAll('.table.table--planning.table--scrolling');
dbg("tryInit - tables found:", tables.length);
if (!tables.length) {
// Try alternative selectors for debugging
dbg("Alt check - .table--planning:", document.querySelectorAll('.table--planning').length);
dbg("Alt check - .table--scrolling:", document.querySelectorAll('.table--scrolling').length);
return false;
}
tables.forEach(initPlanningTable);
return true;
}
if (!tryInit()) {
const observer = new MutationObserver(tryInit);
observer.observe(document.documentElement, {
childList: true,
subtree: true
});
window.addEventListener("hashchange", () => setTimeout(tryInit, 200));
}
})();