@datalabz_/dashboard-kit 6.0.3 → 6.0.5

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.
Files changed (5) hide show
  1. package/README.md +36 -8
  2. package/package.json +1 -1
  3. package/v4.js +1 -1
  4. package/v5.js +1 -1
  5. package/v6.js +215 -18
package/README.md CHANGED
@@ -53,27 +53,55 @@ A major's files are one served bundle; the repo's per-file line budget
53
53
  (AGENTS.md #2) does not apply to it — it cannot be split without changing the
54
54
  URLs already pinned by live dashboards.
55
55
 
56
+ ## Filters from the parent
57
+
58
+ Any key in the `filters` of `CUSTOM_DASHBOARDS_PARAMS` other than `dateFrom`, `dateTo`, `timezone` and
59
+ `hierarchyId` is a header filter of the dashboard. At its first boot the kit writes the value into the
60
+ control with that `id` (for a radio group, the `name` its radios share), reads it back, and locks it. Later
61
+ messages never touch it. `null`, `undefined` and an empty string are errors, and so is a value the control
62
+ does not keep, such as an option that is not there, a date in the wrong format or text in a number field.
63
+ An error, or a key with no control, means the dashboard does not load: it says why and posts
64
+ `{ type: 'DK_FILTERS_REFUSED', message }` to its parent. It never opens unfiltered.
65
+
66
+ A select with no option but the empty one at boot gets its options from a query that runs later, so the
67
+ value cannot be checked against them. It goes in as an option of its own, selected, and stays selected
68
+ while the dashboard fills in the rest. A static select keeps the check.
69
+
56
70
  ## Asking a dashboard for its filters
57
71
 
58
72
  A parent may post `{ type: 'DK_GET_FILTERS_INFO' }` to a dashboard's frame; the kit answers the parent with
59
73
  `{ type: 'DK_FILTERS_INFO', filters: [...] }`. It needs no token or period, so a parent can ask as soon as
60
- the frame has loaded. The list is the controls in the dashboard's header — what it can be opened
61
- with — one entry each:
74
+ the frame has loaded. The list is the header controls the dashboard can be opened with, which is
75
+ what the kit can write into a header control at its first boot, one entry each:
62
76
 
63
77
  ```js
64
78
  { id: 'playerUsername', label: 'Player username', type: 'text', // type: the input's type, 'select' or 'textarea'
65
- required: true, value: '',
79
+ required: true, defaultValue: '',
66
80
  placeholder, min, max, // only when the control sets them
67
81
  disabled: true, // only when it is disabled
68
82
  multiple: false, optionsTotal: 2, // select only
69
83
  options: [{ value: 'BRL', label: 'Real' }] } // select only: the first 100 options
70
84
  ```
71
85
 
72
- The whole header is read, so a control the dashboard adds to it after `init` is listed too. Left
73
- out: a control with no `id`, a second control with an `id` already listed, a password field, and
74
- radio, file, hidden and button controls, which the kit cannot fill. A select's options are those
75
- it has when asked: one the dashboard fills by a query later has none yet, and `optionsTotal` says how
76
- many there were. The answer always goes to the dashboard's parent, whoever asked.
86
+ The list follows the fill, so it holds only what a filter can actually be given:
87
+
88
+ - Left out: a control with no `id`, a second control with an `id` already listed, and file, hidden,
89
+ password and button controls, which the kit refuses to fill.
90
+ - A radio group is one filter, keyed by the `name` its radios share (a radio with no name is left out): it
91
+ is listed once, labelled by its `<fieldset><legend>`, with the radios as `options`; filling it checks
92
+ the radio with that value and locks the group, and a value that is none of them is refused.
93
+ - A select lists only options with a value, since an empty value is refused, and `optionsTotal` counts
94
+ those. A select with none at boot is dynamic: its options come from a query that runs later, so it is
95
+ listed with `dynamicOptions: true` and no options, and takes any value (below).
96
+ - What exists when the dashboard boots is what counts. Once it has booted, the answer is the list taken
97
+ at that moment, so options a section loads later by query, and controls added later, are not in it, as
98
+ they were not there for the fill. Nothing is read again, so `defaultValue` is the value the dashboard starts with.
99
+ - A dynamic select cannot be checked against options that are not there yet. The value goes in as an
100
+ option of its own, selected, and stays selected while the dashboard fills in the rest: whatever
101
+ replaces the options, the filter is still the one asked for, so the dashboard never opens unfiltered.
102
+ A static select keeps the check: a value that is not one of its options is refused.
103
+
104
+ The answer always goes to the dashboard's parent, whoever asked.
77
105
 
78
106
  ## Rules
79
107
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@datalabz_/dashboard-kit",
3
- "version": "6.0.3",
3
+ "version": "6.0.5",
4
4
  "description": "DashboardKit runtime + styles, served from jsDelivr",
5
5
  "license": "UNLICENSED",
6
6
  "files": [
package/v4.js CHANGED
@@ -20,7 +20,7 @@
20
20
  let _activeTabId = '';
21
21
  let _sections = []; // { id, label, load, loaded, errors }
22
22
  let _title = '';
23
- let _gitVer = '8ed9062'; // replaced at deploy: sed -i "s/8ed9062/$(git rev-parse --short HEAD)/g" v4.js
23
+ let _gitVer = 'b7798b2'; // replaced at deploy: sed -i "s/b7798b2/$(git rev-parse --short HEAD)/g" v4.js
24
24
  let _calls = []; // System Info call log
25
25
  let _sectionErrors = {}; // sectionId → error count
26
26
  let _generation = 0;
package/v5.js CHANGED
@@ -20,7 +20,7 @@
20
20
  let _activeTabId = '';
21
21
  let _sections = []; // { id, label, load, loaded, errors }
22
22
  let _title = '';
23
- let _gitVer = '8ed9062'; // replaced at deploy: sed -i "s/8ed9062/$(git rev-parse --short HEAD)/g" v4.js
23
+ let _gitVer = 'b7798b2'; // replaced at deploy: sed -i "s/b7798b2/$(git rev-parse --short HEAD)/g" v4.js
24
24
  let _calls = []; // System Info call log
25
25
  let _sectionErrors = {}; // sectionId → error count
26
26
  let _generation = 0;
package/v6.js CHANGED
@@ -20,11 +20,13 @@
20
20
  let _activeTabId = '';
21
21
  let _sections = []; // { id, label, load, loaded, errors }
22
22
  let _title = '';
23
- let _gitVer = '8ed9062'; // replaced at deploy: sed -i "s/8ed9062/$(git rev-parse --short HEAD)/g" v6.js
23
+ let _gitVer = 'b7798b2'; // replaced at deploy: sed -i "s/b7798b2/$(git rev-parse --short HEAD)/g" v6.js
24
24
  let _calls = []; // System Info call log
25
25
  let _sectionErrors = {}; // sectionId → error count
26
26
  let _generation = 0;
27
27
  let _ttEl = null;
28
+ let _pendingFilters = {}; // header filters a parent sent before boot, applied at boot
29
+ let _filtersRefused = false;
28
30
 
29
31
  // ── Inject CSS ───────────────────────────────────────────────────────────────
30
32
  // The kit carries its own CSS: Claude artifacts may load scripts from jsDelivr but not
@@ -932,12 +934,18 @@ details.dk-call summary::-webkit-details-marker {
932
934
  }
933
935
 
934
936
  // ── Filters a parent can ask for ──────────────────────────────────────────────
935
- // DK_GET_FILTERS_INFO from the parent is answered with DK_FILTERS_INFO: the controls a dashboard keeps in
936
- // its header, which are what it can be opened with. It needs no token or period, so a parent can
937
- // ask as soon as the frame has loaded. Left out: controls the kit cannot fill (radio, file,
938
- // hidden, buttons), a password field, a control with no id, and a second control with an id
939
- // already listed. The whole header is read, so a control a dashboard adds to it after init is listed.
940
- const FILTER_SKIP_TYPES = ['radio', 'file', 'hidden', 'button', 'submit', 'reset', 'image', 'password'];
937
+ // DK_GET_FILTERS_INFO from the parent is answered with DK_FILTERS_INFO: the header controls a
938
+ // dashboard can be opened with, which is what the kit can write into a header control at its first boot. So
939
+ // the list follows that flow and nothing else: a control it cannot fill is not listed (radio,
940
+ // file, hidden, password, buttons; no id; a second control with an id already listed), a select
941
+ // lists only options a value may name, since an empty value is refused, and a select with none
942
+ // is dynamic: its options come later, so it is listed with dynamicOptions: true and takes any value.
943
+ // What exists when the dashboard boots is what counts: once it has booted, the list is the one
944
+ // taken at that moment, so options a section loads later by query are not in it, as they were not
945
+ // there for the fill. Nothing is read again. It needs no token or
946
+ // period, so a parent can ask as soon as the frame has loaded.
947
+ const UNFILLABLE_TYPES = ['file', 'hidden', 'password', 'button', 'submit', 'reset', 'image'];
948
+ let _bootFilters = null; // the list as it was when the dashboard booted
941
949
  const FILTER_MAX_OPTIONS = 100;
942
950
  const FILTER_MAX_TEXT = 500;
943
951
 
@@ -957,15 +965,57 @@ details.dk-call summary::-webkit-details-marker {
957
965
  return _clip(el.getAttribute('aria-label') || el.getAttribute('placeholder') || '', 200);
958
966
  }
959
967
 
968
+ // A radio group is one filter, keyed by its name: the radios share the name and each has its own
969
+ // value. Filling it checks the radio with the value; listing it is one entry with the radios as options.
970
+ function _radioGroup(header, name) {
971
+ return Array.from(header.querySelectorAll('input[type="radio"]')).filter(function (r) { return r.name === name; });
972
+ }
973
+
974
+ function _radioInfo(header, name) {
975
+ const group = _radioGroup(header, name);
976
+ const legend = group[0].closest('fieldset') && group[0].closest('fieldset').querySelector('legend');
977
+ const checked = group.find(function (r) { return r.checked; });
978
+ const f = {
979
+ id: name,
980
+ label: _clip((legend && legend.textContent.replace(/\s+/g, ' ').trim()) || name, 200),
981
+ type: 'radio',
982
+ required: group.some(function (r) { return r.required; }),
983
+ optionsTotal: group.length,
984
+ options: group.slice(0, FILTER_MAX_OPTIONS).map(function (r) {
985
+ return { value: _clip(r.value, 200), label: _filterLabel(r) || _clip(r.value, 200) };
986
+ }),
987
+ defaultValue: checked ? checked.value : '',
988
+ };
989
+ if (group.every(function (r) { return r.disabled; })) f.disabled = true;
990
+ return f;
991
+ }
992
+
993
+ function _controlValue(el) {
994
+ if (el.tagName === 'SELECT') {
995
+ return el.multiple
996
+ ? Array.from(el.selectedOptions).slice(0, FILTER_MAX_OPTIONS).map(function (o) { return _clip(o.value, 200); })
997
+ : _clip(el.value, 200);
998
+ }
999
+ return el.type === 'checkbox' ? el.checked : _clip(el.value, FILTER_MAX_TEXT);
1000
+ }
1001
+
960
1002
  function _dashboardFilters() {
961
1003
  const header = document.querySelector('[data-dk-header]');
962
1004
  const out = [];
963
1005
  if (!header) return out;
964
- const seen = {};
1006
+ const seen = new Set();
1007
+ const seenRadio = new Set();
965
1008
  header.querySelectorAll('input, select, textarea').forEach(function (el) {
966
- if (!el.id || seen[el.id] || FILTER_SKIP_TYPES.indexOf(el.type) >= 0) return;
967
- seen[el.id] = true;
1009
+ if (el.type === 'radio') {
1010
+ if (!el.name || seenRadio.has(el.name)) return;
1011
+ seenRadio.add(el.name);
1012
+ out.push(_radioInfo(header, el.name));
1013
+ return;
1014
+ }
1015
+ if (!el.id || seen.has(el.id) || UNFILLABLE_TYPES.indexOf(el.type) >= 0) return;
968
1016
  const isSelect = el.tagName === 'SELECT';
1017
+ const named = isSelect ? Array.from(el.options).filter(function (o) { return o.value !== ''; }) : null;
1018
+ seen.add(el.id);
969
1019
  const f = {
970
1020
  id: el.id,
971
1021
  label: _filterLabel(el),
@@ -980,29 +1030,46 @@ details.dk-call summary::-webkit-details-marker {
980
1030
  }
981
1031
  if (isSelect) {
982
1032
  f.multiple = el.multiple;
983
- f.optionsTotal = el.options.length;
984
- f.options = Array.from(el.options).slice(0, FILTER_MAX_OPTIONS).map(function (o) {
985
- return { value: _clip(o.value, 200), label: _clip(o.textContent.trim(), 200) };
986
- });
987
- f.value = el.multiple ? Array.from(el.selectedOptions).map(function (o) { return o.value; }) : el.value;
988
- } else {
989
- f.value = el.type === 'checkbox' ? el.checked : _clip(el.value, FILTER_MAX_TEXT);
1033
+ if (named.length) {
1034
+ f.optionsTotal = named.length;
1035
+ f.options = named.slice(0, FILTER_MAX_OPTIONS).map(function (o) {
1036
+ return { value: _clip(o.value, 200), label: _clip(o.textContent.trim(), 200) };
1037
+ });
1038
+ } else {
1039
+ // Its options come later and differ from one time to the next, so none are listed: the
1040
+ // select takes any value.
1041
+ f.dynamicOptions = true;
1042
+ }
990
1043
  }
1044
+ f.defaultValue = _controlValue(el);
991
1045
  out.push(f);
992
1046
  });
993
1047
  return out;
994
1048
  }
995
1049
 
1050
+ // What DK_FILTERS_INFO carries: the dashboard as it starts, not as it is now. Once it has booted
1051
+ // that is the list taken then, before the fill wrote or locked anything, so what a reader typed
1052
+ // since is never in it, and what a host stores with the dashboard does not depend on who asked.
1053
+ function _filtersInfo() {
1054
+ return (_bootFilters || _dashboardFilters()).map(function (f) { return Object.assign({}, f); });
1055
+ }
996
1056
 
997
1057
  function _onMessage(e) {
998
1058
  if (!e.data || typeof e.data !== 'object') return;
999
1059
  if (e.data.type === 'DK_GET_FILTERS_INFO') {
1000
- window.parent.postMessage({ type: 'DK_FILTERS_INFO', filters: _dashboardFilters() }, '*');
1060
+ let reply;
1061
+ try { reply = { type: 'DK_FILTERS_INFO', filters: _filtersInfo() }; }
1062
+ catch (err) {
1063
+ console.error('[DashboardKit] could not describe the filters:', err);
1064
+ reply = { type: 'DK_FILTERS_INFO', filters: [], error: String((err && err.message) || err) };
1065
+ }
1066
+ window.parent.postMessage(reply, '*');
1001
1067
  return;
1002
1068
  }
1003
1069
  if (e.data.type === 'RK_SNAPSHOT') { _snapshot(); return; }
1004
1070
  if (e.data.type === 'CHAT_MODE') { document.documentElement.dataset.chatMode = 'true'; return; }
1005
1071
  if (e.data.type !== 'CUSTOM_DASHBOARDS_PARAMS') return;
1072
+ if (_filtersRefused) return;
1006
1073
 
1007
1074
  if (typeof e.data.token === 'string') _authToken = e.data.token;
1008
1075
 
@@ -1024,9 +1091,19 @@ details.dk-call summary::-webkit-details-marker {
1024
1091
  if (typeof f.timezone === 'string') _timezone = f.timezone;
1025
1092
  if ('hierarchyId' in f)
1026
1093
  _hierarchyId = f.hierarchyId == null ? null : Number(f.hierarchyId);
1094
+ // Any other key is a header filter. It is applied once, at boot, never by a later message.
1095
+ if (!_hasBooted) Object.keys(f).forEach(function (k) {
1096
+ if (CONTEXT_KEYS.indexOf(k) < 0) _pendingFilters[k] = f[k];
1097
+ });
1027
1098
  }
1028
1099
 
1029
1100
  if (!_hasBooted && _authToken && _dateFromIso && _dateToIso && _timezone) {
1101
+ // Before the fill locks anything and before a section runs. A fault in describing the filters
1102
+ // must never keep the dashboard from booting, so it only costs the list.
1103
+ try { _bootFilters = _dashboardFilters(); }
1104
+ catch (err) { console.error('[DashboardKit] could not describe the filters:', err); _bootFilters = null; }
1105
+ const refusal = _fillHeaderFilters(_pendingFilters);
1106
+ if (refusal) { _refuseFilters(refusal); return; }
1030
1107
  _hasBooted = true;
1031
1108
  const wait = document.getElementById('dk-wait');
1032
1109
  if (wait) wait.hidden = true;
@@ -1038,6 +1115,126 @@ details.dk-call summary::-webkit-details-marker {
1038
1115
  }
1039
1116
  }
1040
1117
 
1118
+ // ── Filters a parent passes ───────────────────────────────────────────────────
1119
+ // dateFrom, dateTo, timezone and hierarchyId are context the host owns. Any other key in
1120
+ // `filters` is a header filter: written into the control with that id, read back, and locked.
1121
+ // What the control kept was valid; what the browser dropped (an <option> that is not there, a
1122
+ // date in the wrong format, text in a number field) was not. Anything else is a refusal and the
1123
+ // dashboard does not boot — it never opens unfiltered. No input or change event is fired:
1124
+ // a section reads its filters from the controls when it loads.
1125
+ const CONTEXT_KEYS = ['dateFrom', 'dateTo', 'timezone', 'hierarchyId'];
1126
+
1127
+ function _show(v) {
1128
+ let s;
1129
+ try { s = JSON.stringify(v); } catch { s = String(v); }
1130
+ if (s === undefined) s = 'undefined';
1131
+ return s.length > 60 ? s.slice(0, 57) + '…' : s;
1132
+ }
1133
+
1134
+ // null when the control took the value, otherwise why not.
1135
+ // A select with no option but the empty one gets its options from a query that runs after the
1136
+ // boot, so nothing can be checked against them. The value goes in as an option of its own, selected,
1137
+ // and stays selected while the dashboard fills in the rest: whatever replaces the options, the
1138
+ // filter is still the one asked for, so the dashboard never opens unfiltered.
1139
+ function _fillLateSelect(el, v) {
1140
+ if (el.multiple ? (!Array.isArray(v) || !v.length) : (typeof v !== 'string' && typeof v !== 'number'))
1141
+ return el.multiple ? 'a multiple select takes a non-empty array' : 'a filter takes a string or a number';
1142
+ const values = (el.multiple ? v : [v]).map(String);
1143
+ if (values.indexOf('') >= 0) return 'there is no value';
1144
+ function apply() {
1145
+ values.forEach(function (s) {
1146
+ if (!Array.from(el.options).some(function (o) { return o.value === s; })) el.appendChild(new Option(s, s));
1147
+ });
1148
+ Array.from(el.options).forEach(function (o) { o.selected = values.indexOf(o.value) >= 0; });
1149
+ }
1150
+ apply();
1151
+ const watch = { childList: true };
1152
+ let repairs = 0;
1153
+ const keep = new MutationObserver(function () {
1154
+ const got = Array.from(el.selectedOptions).map(function (o) { return o.value; });
1155
+ if (got.length === values.length && values.every(function (s) { return got.indexOf(s) >= 0; })) return;
1156
+ // A dashboard that rebuilds the options each time it sees them change would trade blows with
1157
+ // this forever and freeze the page; past this many repairs the kit stops.
1158
+ // Then it says so and shows no data: a filter it could not keep must not leave figures on screen.
1159
+ if (++repairs > 100) {
1160
+ keep.disconnect();
1161
+ _refuseFilters('The filter "' + el.id + '" keeps being reset by the dashboard.');
1162
+ document.querySelectorAll('.dk-panel, nav.dk-tabs').forEach(function (n) { n.hidden = true; });
1163
+ return;
1164
+ }
1165
+ keep.disconnect();
1166
+ apply();
1167
+ keep.takeRecords();
1168
+ keep.observe(el, watch);
1169
+ });
1170
+ keep.observe(el, watch);
1171
+ return null;
1172
+ }
1173
+
1174
+ function _writeControl(el, v) {
1175
+ if (v === null || v === undefined || v === '') return 'there is no value';
1176
+ if (el.tagName === 'SELECT' && !Array.from(el.options).some(function (o) { return o.value !== ''; }))
1177
+ return _fillLateSelect(el, v);
1178
+ if (el.tagName === 'SELECT' && el.multiple) {
1179
+ if (!Array.isArray(v) || !v.length) return 'a multiple select takes a non-empty array';
1180
+ const want = v.map(String);
1181
+ Array.from(el.options).forEach(function (o) { o.selected = want.indexOf(o.value) >= 0; });
1182
+ const got = Array.from(el.selectedOptions).map(function (o) { return o.value; });
1183
+ return got.length === want.length ? null : 'not every value is one of its options';
1184
+ }
1185
+ if (el.type === 'checkbox') {
1186
+ if (typeof v !== 'boolean') return 'a checkbox takes true or false';
1187
+ el.checked = v;
1188
+ return null;
1189
+ }
1190
+ if (el.type === 'radio') return 'a radio group is filled by the name its radios share';
1191
+ if (UNFILLABLE_TYPES.indexOf(el.type) >= 0)
1192
+ return 'this kind of control cannot be filled';
1193
+ if (typeof v !== 'string' && typeof v !== 'number') return 'a filter takes a string or a number';
1194
+ const s = String(v);
1195
+ el.value = s;
1196
+ return el.value === s ? null : 'the control does not accept it';
1197
+ }
1198
+
1199
+ // null when every key found its control and was accepted, otherwise the refusal in words.
1200
+ function _fillHeaderFilters(values) {
1201
+ const keys = Object.keys(values);
1202
+ if (!keys.length) return null;
1203
+ const header = document.querySelector('[data-dk-header]');
1204
+ for (let i = 0; i < keys.length; i++) {
1205
+ const k = keys[i];
1206
+ const el = header ? header.querySelector('#' + CSS.escape(k)) : null;
1207
+ // A radio group is named, not identified: the key is the name the radios share.
1208
+ const group = header && !(el && el.type !== 'radio') ? _radioGroup(header, k) : [];
1209
+ if (group.length) {
1210
+ const v = values[k];
1211
+ if (v === null || v === undefined || v === '') return 'The filter "' + k + '" cannot take ' + _show(v) + ': there is no value.';
1212
+ const hit = (typeof v === 'string' || typeof v === 'number') ? group.find(function (r) { return r.value === String(v); }) : null;
1213
+ if (!hit) return 'The filter "' + k + '" cannot take ' + _show(v) + ': it is not one of its options.';
1214
+ hit.checked = true;
1215
+ group.forEach(function (r) { r.disabled = true; });
1216
+ continue;
1217
+ }
1218
+ if (!el || !/^(INPUT|SELECT|TEXTAREA)$/.test(el.tagName)) return 'The dashboard has no filter "' + k + '".';
1219
+ const why = _writeControl(el, values[k]);
1220
+ if (why) return 'The filter "' + k + '" cannot take ' + _show(values[k]) + ': ' + why + '.';
1221
+ el.disabled = true;
1222
+ }
1223
+ return null;
1224
+ }
1225
+
1226
+ function _refuseFilters(message) {
1227
+ _filtersRefused = true;
1228
+ const wait = document.getElementById('dk-wait');
1229
+ if (wait) {
1230
+ wait.hidden = false;
1231
+ wait.textContent = message;
1232
+ wait.style.color = 'var(--err)';
1233
+ wait.style.fontStyle = 'normal';
1234
+ }
1235
+ window.parent.postMessage({ type: 'DK_FILTERS_REFUSED', message: message }, '*');
1236
+ }
1237
+
1041
1238
  // ── Tab state machine ─────────────────────────────────────────────────────────
1042
1239
  function _buildTabNav() {
1043
1240
  const nav = document.getElementById('dk-tabs');