@yuneta/gobj-ui 7.26.52 → 7.26.54

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.
@@ -9677,169 +9677,22 @@ function yui_icons_list() {
9677
9677
  return list;
9678
9678
  }
9679
9679
  //#endregion
9680
- //#region src/yui_time.js
9680
+ //#region src/yui_date_text.js
9681
9681
  /***********************************************************************
9682
- * yui_time.js
9683
- *
9684
- * Time, without a DOM: epochs, and the algebra of PERIODS.
9685
- *
9686
- * Two things live here, and they are the two halves of every date
9687
- * UI the projects keep rewriting:
9688
- *
9689
- * 1) EPOCH <-> the local wall clock. A timestamp travels in the
9690
- * producer's own unit — seconds, or milliseconds when a topic
9691
- * says so — and every conversion crosses that `ms` flag. Getting
9692
- * it wrong puts the same instant on two different clocks in one
9693
- * screen (it did: a picker asking "from 18:55" local returned
9694
- * rows a table labelled 16:55).
9695
- *
9696
- * 2) The PERIOD. A period is not an enum of five names: it is a
9697
- * BUCKET, and a bucket is `count` of a `unit`:
9698
- *
9699
- * {id: "day", unit: "day", count: 1}
9700
- * {id: "week", unit: "week", count: 1} ISO, Mon-first
9701
- * {id: "bimester", unit: "month", count: 2}
9702
- * {id: "quarter", unit: "month", count: 3}
9703
- * {id: "semester", unit: "month", count: 6}
9704
- * {id: "decade", unit: "year", count: 10}
9705
- * {id: "15min", unit: "minute", count: 15}
9706
- *
9707
- * Given that pair, ONE implementation answers every question a
9708
- * date navigator asks — where does the bucket holding this
9709
- * instant start and end, what is the previous/next one, what do
9710
- * I call it — so an app that wants quarters declares a quarter
9711
- * instead of asking for a new component.
9712
- *
9713
- * Buckets are ALIGNED, never "count back from now": months align to
9714
- * the year (which is why 2, 3, 4, 6 and 12 fall on clean calendar
9715
- * boundaries), weeks to Monday, hours to local midnight. "Last 7
9716
- * days" is a different animal — a rolling window — and it is not a
9717
- * period: see `rolling_bounds()`.
9682
+ * yui_date_text.js
9718
9683
  *
9719
- * Everything here is LOCAL time (the user reads a local clock) and
9720
- * DST-safe: it never adds 86400000 ms to cross a day, it builds the
9721
- * next date from calendar fields and lets the platform do it.
9684
+ * A moment as a VIEW shows it as text, and the locale to show it
9685
+ * in. A module of its own, with no i18n key, on purpose: the views
9686
+ * that show a time (C_YUI_FORM, C_YUI_TREEDB_TOPIC_WITH_FORM,
9687
+ * C_YUI_JSON, C_YUI_UPLOT) import it, and validate-locales follows
9688
+ * the modules an app mounts -- importing yui_time.js for this made
9689
+ * every app that shows a time owe the keys of the period picker
9690
+ * (gobj-ui 7.26.52). yui_time.js re-exports both.
9722
9691
  *
9723
9692
  * Copyright (c) 2026, ArtGins.
9724
9693
  * All Rights Reserved.
9725
9694
  ***********************************************************************/
9726
9695
  /***************************************************************
9727
- * Constants
9728
- ***************************************************************/
9729
- var MS_DAY = 864e5;
9730
- var PERIOD_UNITS = [
9731
- "minute",
9732
- "hour",
9733
- "day",
9734
- "week",
9735
- "month",
9736
- "year"
9737
- ];
9738
- var YUI_PERIODS = {
9739
- minute: {
9740
- id: "minute",
9741
- unit: "minute",
9742
- count: 1
9743
- },
9744
- "5min": {
9745
- id: "5min",
9746
- unit: "minute",
9747
- count: 5
9748
- },
9749
- "15min": {
9750
- id: "15min",
9751
- unit: "minute",
9752
- count: 15
9753
- },
9754
- hour: {
9755
- id: "hour",
9756
- unit: "hour",
9757
- count: 1
9758
- },
9759
- day: {
9760
- id: "day",
9761
- unit: "day",
9762
- count: 1
9763
- },
9764
- week: {
9765
- id: "week",
9766
- unit: "week",
9767
- count: 1
9768
- },
9769
- fortnight: {
9770
- id: "fortnight",
9771
- unit: "week",
9772
- count: 2
9773
- },
9774
- month: {
9775
- id: "month",
9776
- unit: "month",
9777
- count: 1
9778
- },
9779
- bimester: {
9780
- id: "bimester",
9781
- unit: "month",
9782
- count: 2
9783
- },
9784
- quarter: {
9785
- id: "quarter",
9786
- unit: "month",
9787
- count: 3
9788
- },
9789
- semester: {
9790
- id: "semester",
9791
- unit: "month",
9792
- count: 6
9793
- },
9794
- year: {
9795
- id: "year",
9796
- unit: "year",
9797
- count: 1
9798
- },
9799
- decade: {
9800
- id: "decade",
9801
- unit: "year",
9802
- count: 10
9803
- }
9804
- };
9805
- var YUI_PERIODS_DEFAULT = [
9806
- "hour",
9807
- "day",
9808
- "week",
9809
- "month",
9810
- "year"
9811
- ];
9812
- var YUI_ROLLING = {
9813
- "1h": {
9814
- id: "1h",
9815
- secs: 3600,
9816
- label: "last hour"
9817
- },
9818
- "6h": {
9819
- id: "6h",
9820
- secs: 21600,
9821
- label: "last 6h"
9822
- },
9823
- "24h": {
9824
- id: "24h",
9825
- secs: 86400,
9826
- label: "last 24h"
9827
- },
9828
- "7d": {
9829
- id: "7d",
9830
- secs: 604800,
9831
- label: "last 7 days"
9832
- },
9833
- "30d": {
9834
- id: "30d",
9835
- secs: 2592e3,
9836
- label: "last 30 days"
9837
- }
9838
- };
9839
- /***************************
9840
- * Locale / i18n
9841
- ***************************/
9842
- /***************************************************************
9843
9696
  * The locale to format with.
9844
9697
  *
9845
9698
  * NEVER at module top level, and never `navigator.language` raw:
@@ -9854,78 +9707,6 @@ function safe_locale(locale) {
9854
9707
  if (typeof nav === "string" && nav && nav !== "undefined") return nav;
9855
9708
  return "en";
9856
9709
  }
9857
- /***************************************************************
9858
- * Translate through the APP's translator when it gave us one, and
9859
- * degrade to the key itself otherwise — interpolating `{{x}}` by hand,
9860
- * so a label never renders raw mustaches at a caller that has no i18n.
9861
- ***************************************************************/
9862
- function tr(t, key, params) {
9863
- if (typeof t === "function") return t(key, params);
9864
- let s = key;
9865
- for (let k in params || {}) s = s.replaceAll(`{{${k}}}`, String(params[k]));
9866
- return s;
9867
- }
9868
- /***************************************************************
9869
- * Intl formatters, built PER CALL (see safe_locale). They are cheap
9870
- * next to a repaint, and caching them would need a locale-change hook
9871
- * the library has no business owning.
9872
- ***************************************************************/
9873
- function fmt_intl(date, locale, opts) {
9874
- try {
9875
- return new Intl.DateTimeFormat(safe_locale(locale), opts).format(date);
9876
- } catch (e) {
9877
- return date.toDateString();
9878
- }
9879
- }
9880
- /***************************
9881
- * Epoch <-> clock
9882
- ***************************/
9883
- /***************************************************************
9884
- * Epoch in the producer's unit -> milliseconds, and back.
9885
- * `ms` true means the value already IS milliseconds.
9886
- ***************************************************************/
9887
- function epoch_to_ms(value, ms) {
9888
- if (!value) return 0;
9889
- return ms ? value : value * 1e3;
9890
- }
9891
- function ms_to_epoch(value_ms, ms) {
9892
- if (!value_ms) return 0;
9893
- return ms ? value_ms : Math.floor(value_ms / 1e3);
9894
- }
9895
- /***************************************************************
9896
- * The LOCAL wall-clock string an `<input type="datetime-local">` takes
9897
- * ("YYYY-MM-DDTHH:MM:SS", which needs step=1 to keep the seconds), and
9898
- * the parse back. Empty / unparseable -> 0, which is exactly how a
9899
- * match condition reads an absent bound.
9900
- ***************************************************************/
9901
- function epoch_to_local_input(value, ms) {
9902
- if (!value) return "";
9903
- let d = new Date(epoch_to_ms(value, ms));
9904
- let pad = (n) => String(n).padStart(2, "0");
9905
- return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}T${pad(d.getHours())}:${pad(d.getMinutes())}:${pad(d.getSeconds())}`;
9906
- }
9907
- function local_input_to_epoch(v, ms) {
9908
- if (!v) return 0;
9909
- let parsed = Date.parse(v);
9910
- if (Number.isNaN(parsed)) return 0;
9911
- return ms_to_epoch(parsed, ms);
9912
- }
9913
- /***************************************************************
9914
- * A timestamp for a table cell: the local wall clock, space-separated,
9915
- * keeping the milliseconds when the producer bothered to send them (a
9916
- * topic that sets sf_t_ms usually appends several records inside the
9917
- * same second, and a column that hides that shows a column of ties).
9918
- ***************************************************************/
9919
- function fmt_epoch(value, ms) {
9920
- if (!value) return "";
9921
- try {
9922
- let s = epoch_to_local_input(value, ms).replace("T", " ");
9923
- if (ms) s += `.${String(value % 1e3).padStart(3, "0")}`;
9924
- return s;
9925
- } catch (e) {
9926
- return String(value);
9927
- }
9928
- }
9929
9710
  var DATE_TIME_OPTIONS = {
9930
9711
  day: "numeric",
9931
9712
  month: "short",
@@ -9956,439 +9737,105 @@ function fmt_date_time(date, locale) {
9956
9737
  try {
9957
9738
  return new Intl.DateTimeFormat(safe_locale(locale), DATE_TIME_OPTIONS).format(date);
9958
9739
  } catch (e) {
9959
- return epoch_to_local_input(date.getTime(), true).replace("T", " ");
9740
+ let pad = (n) => String(n).padStart(2, "0");
9741
+ return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())} ${pad(date.getHours())}:${pad(date.getMinutes())}:${pad(date.getSeconds())}`;
9960
9742
  }
9961
9743
  }
9962
- /***************************
9963
- * Calendar atoms
9964
- ***************************/
9965
- /***************************************************************
9966
- * The day number of a date, counting local calendar days from
9967
- * 1970-01-01. Built from the FIELDS (Date.UTC of y/m/d), never from
9968
- * the timestamp: a DST day is 23 or 25 hours long, so dividing a local
9969
- * epoch by 86400000 drifts a day twice a year.
9970
- ***************************************************************/
9971
- function day_number(d) {
9972
- return Math.floor(Date.UTC(d.getFullYear(), d.getMonth(), d.getDate()) / MS_DAY);
9973
- }
9974
- function date_from_day_number(n) {
9975
- let d = /* @__PURE__ */ new Date(n * MS_DAY);
9976
- return new Date(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate());
9977
- }
9978
- /***************************************************************
9979
- * The Monday of the week a date falls in (ISO: the week starts on
9980
- * Monday, and Sunday closes it).
9981
- ***************************************************************/
9982
- function start_of_iso_week(d) {
9983
- let day = d.getDay();
9984
- let back = day === 0 ? 6 : day - 1;
9985
- return new Date(d.getFullYear(), d.getMonth(), d.getDate() - back);
9986
- }
9987
- /***************************************************************
9988
- * The ISO week a date belongs to: {week, year}. The YEAR is the ISO
9989
- * week-year, not the calendar year — 2026-12-31 lives in week 1 of
9990
- * 2027, and a label that said "week 1 2026" would name a week six
9991
- * months away.
9992
- ***************************************************************/
9993
- function iso_week(d) {
9994
- let monday = start_of_iso_week(d);
9995
- let thursday = new Date(monday.getFullYear(), monday.getMonth(), monday.getDate() + 3);
9996
- let jan1 = new Date(thursday.getFullYear(), 0, 1);
9997
- return {
9998
- week: Math.floor((day_number(thursday) - day_number(jan1)) / 7) + 1,
9999
- year: thursday.getFullYear()
10000
- };
10001
- }
10002
- /***************************************************************
10003
- * The index of the ISO week a date falls in, counting weeks from the
10004
- * Monday of the epoch week. It is what aligns a MULTI-week bucket (a
10005
- * fortnight) to a Monday instead of to whatever day 1970-01-01 was.
10006
- ***************************************************************/
10007
- function week_index(d) {
10008
- return Math.floor((day_number(start_of_iso_week(d)) + 3) / 7);
10009
- }
10010
- /***************************
10011
- * Period algebra
10012
- ***************************/
10013
- /***************************************************************
10014
- * Normalize whatever a caller hands us into a spec: an id of the
10015
- * catalog, or a spec of its own. Returns null for anything that is not
10016
- * a bucket (a "span" / "custom" mode of a navigator has no algebra).
10017
- ***************************************************************/
10018
- function period_spec(period) {
10019
- if (!period) return null;
10020
- let spec = typeof period === "string" ? YUI_PERIODS[period] : period;
10021
- if (!spec || !spec.unit) return null;
10022
- if (PERIOD_UNITS.indexOf(spec.unit) < 0) return null;
10023
- let count = parseInt(spec.count, 10);
10024
- if (!(count > 0)) count = 1;
10025
- return {
10026
- id: spec.id || spec.unit,
10027
- unit: spec.unit,
10028
- count
10029
- };
9744
+ /************************************************************
9745
+ * JSON type discriminator (viewer vocabulary)
9746
+ ************************************************************/
9747
+ function json_type(value) {
9748
+ if (value === null) return "null";
9749
+ if (Array.isArray(value)) return "array";
9750
+ switch (typeof value) {
9751
+ case "string": return "string";
9752
+ case "number": return "number";
9753
+ case "boolean": return "boolean";
9754
+ case "object": return "object";
9755
+ default: return "unknown";
9756
+ }
10030
9757
  }
10031
- /***************************************************************
10032
- * The START of the bucket that holds `anchor_ms`, as a local Date.
9758
+ /************************************************************
9759
+ * Detect a kw_collapse() sentinel.
10033
9760
  *
10034
- * Alignment is what makes a bucket a bucket. Each unit floors against
10035
- * the natural origin of the unit above it, so the boundaries are the
10036
- * ones a human already has in their head:
9761
+ * The kernel replaces an over-limit dict with:
9762
+ * { "__collapsed__": { "path": ..., "size": N } }
9763
+ * and an over-limit array with:
9764
+ * [ { "__collapsed__": { "path": ..., "size": N } } ]
10037
9765
  *
10038
- * minute -> the hour (a 15min bucket starts at :00/:15/:30/:45)
10039
- * hour -> local midnight (a 6h bucket starts at 00/06/12/18)
10040
- * day -> 1970-01-01 (count=1 is the plain day; a 10-day
10041
- * bucket has no calendar origin to
10042
- * align to, so it aligns to the epoch)
10043
- * week -> the epoch week, Monday-first (ISO)
10044
- * month -> January (which is why 2/3/4/6/12 give calendar
10045
- * bimesters, quarters and semesters)
10046
- * year -> year 0 (a decade starts at 2020, not 2021)
10047
- ***************************************************************/
10048
- function period_start(period, anchor_ms) {
10049
- let spec = period_spec(period);
10050
- if (!spec) return null;
10051
- let d = new Date(anchor_ms);
10052
- let n = spec.count;
10053
- switch (spec.unit) {
10054
- case "minute": {
10055
- let m = Math.floor(d.getMinutes() / n) * n;
10056
- return new Date(d.getFullYear(), d.getMonth(), d.getDate(), d.getHours(), m);
10057
- }
10058
- case "hour": {
10059
- let h = Math.floor(d.getHours() / n) * n;
10060
- return new Date(d.getFullYear(), d.getMonth(), d.getDate(), h);
10061
- }
10062
- case "day": return date_from_day_number(Math.floor(day_number(d) / n) * n);
10063
- case "week": return date_from_day_number(Math.floor(week_index(d) / n) * n * 7 - 3);
10064
- case "month": {
10065
- let mi = d.getFullYear() * 12 + d.getMonth();
10066
- let s = Math.floor(mi / n) * n;
10067
- return new Date(Math.floor(s / 12), s % 12, 1);
9766
+ * Returns { size, path, is_array } or null.
9767
+ ************************************************************/
9768
+ function is_collapsed(value) {
9769
+ if (value && typeof value === "object" && !Array.isArray(value)) {
9770
+ let keys = Object.keys(value);
9771
+ if (keys.length === 1 && keys[0] === "__collapsed__") {
9772
+ let c = value.__collapsed__ || {};
9773
+ return {
9774
+ size: c.size,
9775
+ path: c.path,
9776
+ is_array: false
9777
+ };
10068
9778
  }
10069
- case "year": {
10070
- let y = Math.floor(d.getFullYear() / n) * n;
10071
- return new Date(y, 0, 1);
9779
+ return null;
9780
+ }
9781
+ if (Array.isArray(value) && value.length === 1) {
9782
+ let e = value[0];
9783
+ if (e && typeof e === "object" && !Array.isArray(e) && Object.keys(e).length === 1 && e.__collapsed__) {
9784
+ let c = e.__collapsed__ || {};
9785
+ return {
9786
+ size: c.size,
9787
+ path: c.path,
9788
+ is_array: true
9789
+ };
10072
9790
  }
10073
- default: return null;
10074
9791
  }
9792
+ return null;
10075
9793
  }
10076
- /***************************************************************
10077
- * The start of the bucket `delta` buckets away (delta may be negative).
10078
- * Calendar arithmetic, never millisecond arithmetic: `new Date(y, m+3, 1)`
10079
- * overflows the year by itself, and stepping a day across a DST change
10080
- * keeps landing on midnight.
10081
- ***************************************************************/
10082
- function period_shift(period, anchor_ms, delta) {
10083
- let spec = period_spec(period);
10084
- if (!spec) return anchor_ms;
10085
- let s = period_start(spec, anchor_ms);
10086
- let step = spec.count * (delta || 0);
10087
- switch (spec.unit) {
10088
- case "minute": return new Date(s.getFullYear(), s.getMonth(), s.getDate(), s.getHours(), s.getMinutes() + step).getTime();
10089
- case "hour": return new Date(s.getFullYear(), s.getMonth(), s.getDate(), s.getHours() + step).getTime();
10090
- case "day": return new Date(s.getFullYear(), s.getMonth(), s.getDate() + step).getTime();
10091
- case "week": return new Date(s.getFullYear(), s.getMonth(), s.getDate() + step * 7).getTime();
10092
- case "month": return new Date(s.getFullYear(), s.getMonth() + step, 1).getTime();
10093
- case "year": return new Date(s.getFullYear() + step, 0, 1).getTime();
10094
- default: return anchor_ms;
10095
- }
9794
+ /************************************************************
9795
+ * Join / split absolute path segments (kernel convention)
9796
+ ************************************************************/
9797
+ function seg_join(segments) {
9798
+ return segments.map(function(s) {
9799
+ return String(s);
9800
+ }).join("`");
10096
9801
  }
10097
- /***************************************************************
10098
- * The bucket holding `anchor_ms`, as {from, to} in MILLISECONDS.
10099
- *
10100
- * `to` is INCLUSIVE — the last millisecond of the bucket, not the first
10101
- * of the next one. Both ends of a match condition are inclusive, and an
10102
- * exclusive end handed to one silently swallows the record that landed
10103
- * exactly on the boundary.
10104
- ***************************************************************/
10105
- function period_bounds(period, anchor_ms) {
10106
- let spec = period_spec(period);
10107
- if (!spec) return {
10108
- from: 0,
10109
- to: 0
10110
- };
10111
- return {
10112
- from: period_start(spec, anchor_ms).getTime(),
10113
- to: period_shift(spec, anchor_ms, 1) - 1
10114
- };
9802
+ function seg_split(path) {
9803
+ if (path === "" || path === null || path === void 0) return [];
9804
+ return path.split("`");
10115
9805
  }
10116
- /***************************************************************
10117
- * The same bucket, in the unit the CONSUMER speaks (a tranger topic
10118
- * keeps its timestamps in seconds, unless its system_flag says
10119
- * milliseconds). This is the function a query builder calls.
10120
- ***************************************************************/
10121
- function period_bounds_epoch(period, anchor_ms, ms) {
10122
- let b = period_bounds(period, anchor_ms);
10123
- if (!b.from) return {
10124
- from: 0,
10125
- to: 0
10126
- };
10127
- return {
10128
- from: ms_to_epoch(b.from, ms),
10129
- to: ms_to_epoch(b.to, ms)
10130
- };
9806
+ /************************************************************
9807
+ * Walk a JSON tree by absolute segments; undefined if the
9808
+ * path does not resolve.
9809
+ ************************************************************/
9810
+ function get_by_segments(root, segments) {
9811
+ let v = root;
9812
+ for (let seg of segments) {
9813
+ if (v === null || v === void 0) return;
9814
+ if (Array.isArray(v)) v = v[Number(seg)];
9815
+ else if (typeof v === "object") v = v[seg];
9816
+ else return;
9817
+ }
9818
+ return v;
10131
9819
  }
10132
- /***************************************************************
10133
- * A ROLLING window: `secs` back from now. Not a bucket — it has no
10134
- * previous and no next, it just ends at `now`.
9820
+ /************************************************************
9821
+ * Set a value at absolute segments (mutates root).
10135
9822
  *
10136
- * The `to` end is left OPEN (0). An iterator with no upper bound keeps
10137
- * matching the records that land while the card is on screen; pinning
10138
- * it to "now" would freeze the window at the instant the user clicked.
10139
- ***************************************************************/
10140
- function rolling_bounds(rolling, ms, now_ms) {
10141
- let r = typeof rolling === "string" ? YUI_ROLLING[rolling] : rolling;
10142
- if (!r || !r.secs) return {
10143
- from: 0,
10144
- to: 0
10145
- };
10146
- return {
10147
- from: ms_to_epoch((now_ms === void 0 ? Date.now() : now_ms) - r.secs * 1e3, ms),
10148
- to: 0
10149
- };
10150
- }
10151
- /***************************************************************
10152
- * Is this the LAST bucket — the one `now` falls in? It is what greys
10153
- * out the "next" arrow, and what tells the navigator it is already home.
10154
- ***************************************************************/
10155
- function is_current_period(period, anchor_ms, now_ms) {
10156
- let spec = period_spec(period);
10157
- if (!spec) return false;
10158
- let now = now_ms === void 0 ? Date.now() : now_ms;
10159
- let a = period_start(spec, anchor_ms);
10160
- let b = period_start(spec, now);
10161
- return !!a && !!b && a.getTime() === b.getTime();
9823
+ * segments == [] means "replace the whole root" — the caller
9824
+ * must use the RETURNED value in that case (a primitive/array
9825
+ * root cannot be mutated in place).
9826
+ ************************************************************/
9827
+ function set_by_segments(root, segments, new_value) {
9828
+ if (segments.length === 0) return new_value;
9829
+ let parent = get_by_segments(root, segments.slice(0, -1));
9830
+ if (parent === null || parent === void 0 || typeof parent !== "object") return root;
9831
+ let last = segments[segments.length - 1];
9832
+ if (Array.isArray(parent)) parent[Number(last)] = new_value;
9833
+ else parent[last] = new_value;
9834
+ return root;
10162
9835
  }
10163
- /***************************************************************
10164
- * Recognize the bucket a {from, to} pair came from, so a range that
10165
- * travelled through a URL, a saved view or a backend answer comes back
10166
- * as the period the user picked instead of as a hand-typed range.
10167
- *
10168
- * `from`/`to` are in the CONSUMER's unit (`ms`), and so is the
10169
- * comparison — that is the whole trap: a bucket ends on the last
10170
- * MILLISECOND (…23:59:59.999), and a consumer that keeps seconds stored
10171
- * it truncated (…23:59:59). Comparing in milliseconds, a week saved by a
10172
- * seconds-based topic came back as "no bucket at all", every time.
10173
- *
10174
- * Only an EXACT match counts: both ends must land on the bucket's own
10175
- * boundaries, as that consumer would have written them. `candidates` are
10176
- * ids/specs, tried in order. Returns {period, anchor} (anchor in ms) or
10177
- * null.
10178
- ***************************************************************/
10179
- function infer_period(from, to, candidates, ms) {
10180
- if (!from || !to || to <= from) return null;
10181
- let from_ms = epoch_to_ms(from, ms);
10182
- for (let candidate of candidates || Object.keys(YUI_PERIODS)) {
10183
- let spec = period_spec(candidate);
10184
- if (!spec) continue;
10185
- let b = period_bounds_epoch(spec, from_ms, ms);
10186
- if (b.from === from && b.to === to) return {
10187
- period: spec,
10188
- anchor: from_ms
10189
- };
10190
- }
10191
- return null;
10192
- }
10193
- /***************************
10194
- * Period labels
10195
- ***************************/
10196
- /***************************************************************
10197
- * The name of a GRANULARITY (what the segmented control shows):
10198
- * the spec's own id as an i18n key, so an app that declares
10199
- * {id: "quarter"} gets "Trimestre" the moment it adds the key.
10200
- ***************************************************************/
10201
- function period_name(period, t) {
10202
- let spec = period_spec(period);
10203
- if (!spec) return tr(t, String(period && period.id || period || ""));
10204
- return tr(t, spec.id);
10205
- }
10206
- /***************************************************************
10207
- * The name of the bucket a navigator is PARKED on: what goes between
10208
- * the two arrows.
10209
- *
10210
- * day -> "Today" / "Yesterday" / "13 jul 2026"
10211
- * week -> "Week 27" (+ year when it is not this one)
10212
- * month -> "July" (+ year when it is not this one)
10213
- * quarter -> "Q3 2026" (unit month, count 3)
10214
- * semester -> "H2 2026" (unit month, count 6)
10215
- * year -> "2026"
10216
- * decade -> "2020 – 2029"
10217
- * any other -> the bucket's own edges: "1 jul – 31 aug 2026"
10218
- *
10219
- * The named ones are named because a range reads worse than a name;
10220
- * everything an app invents falls back to the range, which is always
10221
- * true and never wrong.
10222
- ***************************************************************/
10223
- function period_label(period, anchor_ms, t, locale) {
10224
- let spec = period_spec(period);
10225
- if (!spec) return "";
10226
- let loc = safe_locale(locale);
10227
- let start = period_start(spec, anchor_ms);
10228
- let end = new Date(period_bounds(spec, anchor_ms).to);
10229
- let now = /* @__PURE__ */ new Date();
10230
- let this_year = start.getFullYear() === now.getFullYear();
10231
- if (spec.unit === "day" && spec.count === 1) {
10232
- let diff = day_number(start) - day_number(now);
10233
- if (diff === 0) return tr(t, "today");
10234
- if (diff === -1) return tr(t, "yesterday");
10235
- if (diff === 1) return tr(t, "tomorrow");
10236
- return fmt_intl(start, loc, this_year ? {
10237
- day: "numeric",
10238
- month: "short"
10239
- } : {
10240
- day: "numeric",
10241
- month: "short",
10242
- year: "numeric"
10243
- });
10244
- }
10245
- if (spec.unit === "hour" && spec.count === 1) {
10246
- let hh = fmt_intl(start, loc, {
10247
- hour: "2-digit",
10248
- minute: "2-digit"
10249
- });
10250
- if (day_number(start) === day_number(now)) return hh;
10251
- return `${hh} · ${fmt_intl(start, loc, {
10252
- day: "numeric",
10253
- month: "short"
10254
- })}`;
10255
- }
10256
- if (spec.unit === "week" && spec.count === 1) {
10257
- let away = week_index(start) - week_index(now);
10258
- if (away === 0) return tr(t, "this week");
10259
- if (away === -1) return tr(t, "last week");
10260
- let w = iso_week(start);
10261
- return w.year === now.getFullYear() ? tr(t, "week {{n}}", { n: w.week }) : tr(t, "week {{n}} {{y}}", {
10262
- n: w.week,
10263
- y: w.year
10264
- });
10265
- }
10266
- if (spec.unit === "month" && spec.count === 1) return fmt_intl(start, loc, this_year ? { month: "long" } : {
10267
- month: "long",
10268
- year: "numeric"
10269
- });
10270
- if (spec.unit === "month" && (spec.count === 3 || spec.count === 6)) {
10271
- let n = Math.floor(start.getMonth() / spec.count) + 1;
10272
- return tr(t, spec.count === 3 ? "quarter {{n}} {{y}}" : "semester {{n}} {{y}}", {
10273
- n,
10274
- y: start.getFullYear()
10275
- });
10276
- }
10277
- if (spec.unit === "year") {
10278
- if (spec.count === 1) return String(start.getFullYear());
10279
- return `${start.getFullYear()} – ${end.getFullYear()}`;
10280
- }
10281
- let opts = spec.unit === "minute" || spec.unit === "hour" ? {
10282
- day: "numeric",
10283
- month: "short",
10284
- hour: "2-digit",
10285
- minute: "2-digit"
10286
- } : {
10287
- day: "numeric",
10288
- month: "short"
10289
- };
10290
- if (start.getFullYear() !== end.getFullYear()) {
10291
- let y_opts = Object.assign({ year: "numeric" }, opts);
10292
- return `${fmt_intl(start, loc, y_opts)} – ${fmt_intl(end, loc, y_opts)}`;
10293
- }
10294
- let span = `${fmt_intl(start, loc, opts)} – ${fmt_intl(end, loc, opts)}`;
10295
- return this_year ? span : `${span} ${start.getFullYear()}`;
10296
- }
10297
- /************************************************************
10298
- * JSON type discriminator (viewer vocabulary)
10299
- ************************************************************/
10300
- function json_type(value) {
10301
- if (value === null) return "null";
10302
- if (Array.isArray(value)) return "array";
10303
- switch (typeof value) {
10304
- case "string": return "string";
10305
- case "number": return "number";
10306
- case "boolean": return "boolean";
10307
- case "object": return "object";
10308
- default: return "unknown";
10309
- }
10310
- }
10311
- /************************************************************
10312
- * Detect a kw_collapse() sentinel.
10313
- *
10314
- * The kernel replaces an over-limit dict with:
10315
- * { "__collapsed__": { "path": ..., "size": N } }
10316
- * and an over-limit array with:
10317
- * [ { "__collapsed__": { "path": ..., "size": N } } ]
10318
- *
10319
- * Returns { size, path, is_array } or null.
10320
- ************************************************************/
10321
- function is_collapsed(value) {
10322
- if (value && typeof value === "object" && !Array.isArray(value)) {
10323
- let keys = Object.keys(value);
10324
- if (keys.length === 1 && keys[0] === "__collapsed__") {
10325
- let c = value.__collapsed__ || {};
10326
- return {
10327
- size: c.size,
10328
- path: c.path,
10329
- is_array: false
10330
- };
10331
- }
10332
- return null;
10333
- }
10334
- if (Array.isArray(value) && value.length === 1) {
10335
- let e = value[0];
10336
- if (e && typeof e === "object" && !Array.isArray(e) && Object.keys(e).length === 1 && e.__collapsed__) {
10337
- let c = e.__collapsed__ || {};
10338
- return {
10339
- size: c.size,
10340
- path: c.path,
10341
- is_array: true
10342
- };
10343
- }
10344
- }
10345
- return null;
10346
- }
10347
- /************************************************************
10348
- * Join / split absolute path segments (kernel convention)
10349
- ************************************************************/
10350
- function seg_join(segments) {
10351
- return segments.map(function(s) {
10352
- return String(s);
10353
- }).join("`");
10354
- }
10355
- function seg_split(path) {
10356
- if (path === "" || path === null || path === void 0) return [];
10357
- return path.split("`");
10358
- }
10359
- /************************************************************
10360
- * Walk a JSON tree by absolute segments; undefined if the
10361
- * path does not resolve.
10362
- ************************************************************/
10363
- function get_by_segments(root, segments) {
10364
- let v = root;
10365
- for (let seg of segments) {
10366
- if (v === null || v === void 0) return;
10367
- if (Array.isArray(v)) v = v[Number(seg)];
10368
- else if (typeof v === "object") v = v[seg];
10369
- else return;
10370
- }
10371
- return v;
10372
- }
10373
- /************************************************************
10374
- * Set a value at absolute segments (mutates root).
10375
- *
10376
- * segments == [] means "replace the whole root" — the caller
10377
- * must use the RETURNED value in that case (a primitive/array
10378
- * root cannot be mutated in place).
10379
- ************************************************************/
10380
- function set_by_segments(root, segments, new_value) {
10381
- if (segments.length === 0) return new_value;
10382
- let parent = get_by_segments(root, segments.slice(0, -1));
10383
- if (parent === null || parent === void 0 || typeof parent !== "object") return root;
10384
- let last = segments[segments.length - 1];
10385
- if (Array.isArray(parent)) parent[Number(last)] = new_value;
10386
- else parent[last] = new_value;
10387
- return root;
10388
- }
10389
- /************************************************************
10390
- * Does `value` (or any loaded descendant / its own key)
10391
- * contain the lower-cased search term?
9836
+ /************************************************************
9837
+ * Does `value` (or any loaded descendant / its own key)
9838
+ * contain the lower-cased search term?
10392
9839
  *
10393
9840
  * Collapsed (not-yet-loaded) subtrees can't be searched, so
10394
9841
  * they never match on content — only their key can match,
@@ -10870,6 +10317,67 @@ function form_time_cell_editor(real_type) {
10870
10317
  };
10871
10318
  }
10872
10319
  //#endregion
10320
+ //#region src/yui_tabulator_edit.js
10321
+ /****************************************************************************
10322
+ * yui_tabulator_edit.js
10323
+ *
10324
+ * An open cell editor survives a FORCED redraw of its table.
10325
+ *
10326
+ * `table.redraw(true)` builds every row's cells again, the one
10327
+ * being edited too, and leaves Tabulator's edit pointing at the
10328
+ * cell it threw away (it ends the edit before a data refresh,
10329
+ * not before a redraw). Tabulator issues one itself, from
10330
+ * ResizeTable's IntersectionObserver, the first time a table
10331
+ * built out of sight comes into view -- and a click on a cell
10332
+ * of a table below the fold scrolls it in AND opens the editor
10333
+ * in the same turn. Chromium blurs the box it takes out, the
10334
+ * editor's blur ends the edit on the dead cell, and Tabulator
10335
+ * throws in Validate.editorClear ("reading 'remove'"); Firefox
10336
+ * does not blur, and throws at the next edit. Any editor, ours
10337
+ * (form_time_cell_editor.js) or Tabulator's own.
10338
+ *
10339
+ * So the edit is ended BEFORE the forced redraw, while its cell
10340
+ * is alive, and opened again on the new cell of the same row
10341
+ * and column after it. What was typed in the box is lost; the
10342
+ * editor stays open under the reader.
10343
+ *
10344
+ * Copyright (c) 2026, ArtGins.
10345
+ * All Rights Reserved.
10346
+ ****************************************************************************/
10347
+ /************************************************************
10348
+ * Wrap `tabulator.redraw` so that a forced redraw ends the
10349
+ * open edit first and opens it again after.
10350
+ ************************************************************/
10351
+ function yui_tabulator_keep_edit_on_redraw(tabulator) {
10352
+ let editing = null;
10353
+ tabulator.on("cellEditing", function(cell) {
10354
+ editing = cell;
10355
+ });
10356
+ tabulator.on("cellEdited", function() {
10357
+ editing = null;
10358
+ });
10359
+ tabulator.on("cellEditCancelled", function() {
10360
+ editing = null;
10361
+ });
10362
+ let redraw = tabulator.redraw.bind(tabulator);
10363
+ tabulator.redraw = function(force) {
10364
+ let cell = editing;
10365
+ let $cell = cell ? cell.getElement() : null;
10366
+ if (!force || !$cell || !$cell.classList.contains("tabulator-editing")) return redraw(force);
10367
+ let row = cell.getRow();
10368
+ let field = cell.getField();
10369
+ cell.cancelEdit();
10370
+ let ret = redraw(force);
10371
+ let again = row.getCell(field);
10372
+ if (!again) {
10373
+ log_warning(`yui_tabulator_keep_edit_on_redraw: no cell '${field}' after the redraw`);
10374
+ return ret;
10375
+ }
10376
+ again.edit(true);
10377
+ return ret;
10378
+ };
10379
+ }
10380
+ //#endregion
10873
10381
  //#region src/form_json_field.js
10874
10382
  /***********************************************************************
10875
10383
  * form_json_field.js
@@ -13179,6 +12687,7 @@ function create_tabulator$1(gobj, $extend, name, template, rows) {
13179
12687
  visible: gobj_read_bool_attr(gobj, "selectable")
13180
12688
  };
13181
12689
  let tabulator = new TabulatorFull($extend, tabulator_settings);
12690
+ yui_tabulator_keep_edit_on_redraw(tabulator);
13182
12691
  tabulator.on("dataChanged", function() {
13183
12692
  if (gobj.priv.loading_rows > 0) return;
13184
12693
  gobj_send_event(gobj, "EV_RECORD_CHANGED", {}, gobj);
@@ -33571,6 +33080,7 @@ function create_tabulator(gobj) {
33571
33080
  return;
33572
33081
  }
33573
33082
  let tabulator = new TabulatorFull($table_el, Object.assign({}, tabulator_settings, yui_tabulator_lang(t), { placeholder: t("no data available", { defaultValue: "No data available" }) }));
33083
+ yui_tabulator_keep_edit_on_redraw(tabulator);
33574
33084
  if (with_checkbox) $table_el.classList.add("yui-no-row-hover");
33575
33085
  function update_rowcount(explicit_count) {
33576
33086
  let $rc = tabulator.element && tabulator.element.querySelector(".yui-tabulator-rowcount");
@@ -83540,6 +83050,576 @@ function register_c_yui_schema_editor() {
83540
83050
  return create_gclass$1(GCLASS_NAME$1);
83541
83051
  }
83542
83052
  //#endregion
83053
+ //#region src/yui_time.js
83054
+ /***********************************************************************
83055
+ * yui_time.js
83056
+ *
83057
+ * Time, without a DOM: epochs, and the algebra of PERIODS.
83058
+ *
83059
+ * Two things live here, and they are the two halves of every date
83060
+ * UI the projects keep rewriting:
83061
+ *
83062
+ * 1) EPOCH <-> the local wall clock. A timestamp travels in the
83063
+ * producer's own unit — seconds, or milliseconds when a topic
83064
+ * says so — and every conversion crosses that `ms` flag. Getting
83065
+ * it wrong puts the same instant on two different clocks in one
83066
+ * screen (it did: a picker asking "from 18:55" local returned
83067
+ * rows a table labelled 16:55).
83068
+ *
83069
+ * 2) The PERIOD. A period is not an enum of five names: it is a
83070
+ * BUCKET, and a bucket is `count` of a `unit`:
83071
+ *
83072
+ * {id: "day", unit: "day", count: 1}
83073
+ * {id: "week", unit: "week", count: 1} ISO, Mon-first
83074
+ * {id: "bimester", unit: "month", count: 2}
83075
+ * {id: "quarter", unit: "month", count: 3}
83076
+ * {id: "semester", unit: "month", count: 6}
83077
+ * {id: "decade", unit: "year", count: 10}
83078
+ * {id: "15min", unit: "minute", count: 15}
83079
+ *
83080
+ * Given that pair, ONE implementation answers every question a
83081
+ * date navigator asks — where does the bucket holding this
83082
+ * instant start and end, what is the previous/next one, what do
83083
+ * I call it — so an app that wants quarters declares a quarter
83084
+ * instead of asking for a new component.
83085
+ *
83086
+ * Buckets are ALIGNED, never "count back from now": months align to
83087
+ * the year (which is why 2, 3, 4, 6 and 12 fall on clean calendar
83088
+ * boundaries), weeks to Monday, hours to local midnight. "Last 7
83089
+ * days" is a different animal — a rolling window — and it is not a
83090
+ * period: see `rolling_bounds()`.
83091
+ *
83092
+ * Everything here is LOCAL time (the user reads a local clock) and
83093
+ * DST-safe: it never adds 86400000 ms to cross a day, it builds the
83094
+ * next date from calendar fields and lets the platform do it.
83095
+ *
83096
+ * Copyright (c) 2026, ArtGins.
83097
+ * All Rights Reserved.
83098
+ ***********************************************************************/
83099
+ /***************************************************************
83100
+ * Constants
83101
+ ***************************************************************/
83102
+ var MS_DAY = 864e5;
83103
+ var PERIOD_UNITS = [
83104
+ "minute",
83105
+ "hour",
83106
+ "day",
83107
+ "week",
83108
+ "month",
83109
+ "year"
83110
+ ];
83111
+ var YUI_PERIODS = {
83112
+ minute: {
83113
+ id: "minute",
83114
+ unit: "minute",
83115
+ count: 1
83116
+ },
83117
+ "5min": {
83118
+ id: "5min",
83119
+ unit: "minute",
83120
+ count: 5
83121
+ },
83122
+ "15min": {
83123
+ id: "15min",
83124
+ unit: "minute",
83125
+ count: 15
83126
+ },
83127
+ hour: {
83128
+ id: "hour",
83129
+ unit: "hour",
83130
+ count: 1
83131
+ },
83132
+ day: {
83133
+ id: "day",
83134
+ unit: "day",
83135
+ count: 1
83136
+ },
83137
+ week: {
83138
+ id: "week",
83139
+ unit: "week",
83140
+ count: 1
83141
+ },
83142
+ fortnight: {
83143
+ id: "fortnight",
83144
+ unit: "week",
83145
+ count: 2
83146
+ },
83147
+ month: {
83148
+ id: "month",
83149
+ unit: "month",
83150
+ count: 1
83151
+ },
83152
+ bimester: {
83153
+ id: "bimester",
83154
+ unit: "month",
83155
+ count: 2
83156
+ },
83157
+ quarter: {
83158
+ id: "quarter",
83159
+ unit: "month",
83160
+ count: 3
83161
+ },
83162
+ semester: {
83163
+ id: "semester",
83164
+ unit: "month",
83165
+ count: 6
83166
+ },
83167
+ year: {
83168
+ id: "year",
83169
+ unit: "year",
83170
+ count: 1
83171
+ },
83172
+ decade: {
83173
+ id: "decade",
83174
+ unit: "year",
83175
+ count: 10
83176
+ }
83177
+ };
83178
+ var YUI_PERIODS_DEFAULT = [
83179
+ "hour",
83180
+ "day",
83181
+ "week",
83182
+ "month",
83183
+ "year"
83184
+ ];
83185
+ var YUI_ROLLING = {
83186
+ "1h": {
83187
+ id: "1h",
83188
+ secs: 3600,
83189
+ label: "last hour"
83190
+ },
83191
+ "6h": {
83192
+ id: "6h",
83193
+ secs: 21600,
83194
+ label: "last 6h"
83195
+ },
83196
+ "24h": {
83197
+ id: "24h",
83198
+ secs: 86400,
83199
+ label: "last 24h"
83200
+ },
83201
+ "7d": {
83202
+ id: "7d",
83203
+ secs: 604800,
83204
+ label: "last 7 days"
83205
+ },
83206
+ "30d": {
83207
+ id: "30d",
83208
+ secs: 2592e3,
83209
+ label: "last 30 days"
83210
+ }
83211
+ };
83212
+ /***************************
83213
+ * Locale / i18n
83214
+ ***************************/
83215
+ /***************************************************************
83216
+ * Translate through the APP's translator when it gave us one, and
83217
+ * degrade to the key itself otherwise — interpolating `{{x}}` by hand,
83218
+ * so a label never renders raw mustaches at a caller that has no i18n.
83219
+ ***************************************************************/
83220
+ function tr(t, key, params) {
83221
+ if (typeof t === "function") return t(key, params);
83222
+ let s = key;
83223
+ for (let k in params || {}) s = s.replaceAll(`{{${k}}}`, String(params[k]));
83224
+ return s;
83225
+ }
83226
+ /***************************************************************
83227
+ * Intl formatters, built PER CALL (see safe_locale). They are cheap
83228
+ * next to a repaint, and caching them would need a locale-change hook
83229
+ * the library has no business owning.
83230
+ ***************************************************************/
83231
+ function fmt_intl(date, locale, opts) {
83232
+ try {
83233
+ return new Intl.DateTimeFormat(safe_locale(locale), opts).format(date);
83234
+ } catch (e) {
83235
+ return date.toDateString();
83236
+ }
83237
+ }
83238
+ /***************************
83239
+ * Epoch <-> clock
83240
+ ***************************/
83241
+ /***************************************************************
83242
+ * Epoch in the producer's unit -> milliseconds, and back.
83243
+ * `ms` true means the value already IS milliseconds.
83244
+ ***************************************************************/
83245
+ function epoch_to_ms(value, ms) {
83246
+ if (!value) return 0;
83247
+ return ms ? value : value * 1e3;
83248
+ }
83249
+ function ms_to_epoch(value_ms, ms) {
83250
+ if (!value_ms) return 0;
83251
+ return ms ? value_ms : Math.floor(value_ms / 1e3);
83252
+ }
83253
+ /***************************************************************
83254
+ * The LOCAL wall-clock string an `<input type="datetime-local">` takes
83255
+ * ("YYYY-MM-DDTHH:MM:SS", which needs step=1 to keep the seconds), and
83256
+ * the parse back. Empty / unparseable -> 0, which is exactly how a
83257
+ * match condition reads an absent bound.
83258
+ ***************************************************************/
83259
+ function epoch_to_local_input(value, ms) {
83260
+ if (!value) return "";
83261
+ let d = new Date(epoch_to_ms(value, ms));
83262
+ let pad = (n) => String(n).padStart(2, "0");
83263
+ return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}T${pad(d.getHours())}:${pad(d.getMinutes())}:${pad(d.getSeconds())}`;
83264
+ }
83265
+ function local_input_to_epoch(v, ms) {
83266
+ if (!v) return 0;
83267
+ let parsed = Date.parse(v);
83268
+ if (Number.isNaN(parsed)) return 0;
83269
+ return ms_to_epoch(parsed, ms);
83270
+ }
83271
+ /***************************************************************
83272
+ * A timestamp for a table cell: the local wall clock, space-separated,
83273
+ * keeping the milliseconds when the producer bothered to send them (a
83274
+ * topic that sets sf_t_ms usually appends several records inside the
83275
+ * same second, and a column that hides that shows a column of ties).
83276
+ ***************************************************************/
83277
+ function fmt_epoch(value, ms) {
83278
+ if (!value) return "";
83279
+ try {
83280
+ let s = epoch_to_local_input(value, ms).replace("T", " ");
83281
+ if (ms) s += `.${String(value % 1e3).padStart(3, "0")}`;
83282
+ return s;
83283
+ } catch (e) {
83284
+ return String(value);
83285
+ }
83286
+ }
83287
+ /***************************
83288
+ * Calendar atoms
83289
+ ***************************/
83290
+ /***************************************************************
83291
+ * The day number of a date, counting local calendar days from
83292
+ * 1970-01-01. Built from the FIELDS (Date.UTC of y/m/d), never from
83293
+ * the timestamp: a DST day is 23 or 25 hours long, so dividing a local
83294
+ * epoch by 86400000 drifts a day twice a year.
83295
+ ***************************************************************/
83296
+ function day_number(d) {
83297
+ return Math.floor(Date.UTC(d.getFullYear(), d.getMonth(), d.getDate()) / MS_DAY);
83298
+ }
83299
+ function date_from_day_number(n) {
83300
+ let d = /* @__PURE__ */ new Date(n * MS_DAY);
83301
+ return new Date(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate());
83302
+ }
83303
+ /***************************************************************
83304
+ * The Monday of the week a date falls in (ISO: the week starts on
83305
+ * Monday, and Sunday closes it).
83306
+ ***************************************************************/
83307
+ function start_of_iso_week(d) {
83308
+ let day = d.getDay();
83309
+ let back = day === 0 ? 6 : day - 1;
83310
+ return new Date(d.getFullYear(), d.getMonth(), d.getDate() - back);
83311
+ }
83312
+ /***************************************************************
83313
+ * The ISO week a date belongs to: {week, year}. The YEAR is the ISO
83314
+ * week-year, not the calendar year — 2026-12-31 lives in week 1 of
83315
+ * 2027, and a label that said "week 1 2026" would name a week six
83316
+ * months away.
83317
+ ***************************************************************/
83318
+ function iso_week(d) {
83319
+ let monday = start_of_iso_week(d);
83320
+ let thursday = new Date(monday.getFullYear(), monday.getMonth(), monday.getDate() + 3);
83321
+ let jan1 = new Date(thursday.getFullYear(), 0, 1);
83322
+ return {
83323
+ week: Math.floor((day_number(thursday) - day_number(jan1)) / 7) + 1,
83324
+ year: thursday.getFullYear()
83325
+ };
83326
+ }
83327
+ /***************************************************************
83328
+ * The index of the ISO week a date falls in, counting weeks from the
83329
+ * Monday of the epoch week. It is what aligns a MULTI-week bucket (a
83330
+ * fortnight) to a Monday instead of to whatever day 1970-01-01 was.
83331
+ ***************************************************************/
83332
+ function week_index(d) {
83333
+ return Math.floor((day_number(start_of_iso_week(d)) + 3) / 7);
83334
+ }
83335
+ /***************************
83336
+ * Period algebra
83337
+ ***************************/
83338
+ /***************************************************************
83339
+ * Normalize whatever a caller hands us into a spec: an id of the
83340
+ * catalog, or a spec of its own. Returns null for anything that is not
83341
+ * a bucket (a "span" / "custom" mode of a navigator has no algebra).
83342
+ ***************************************************************/
83343
+ function period_spec(period) {
83344
+ if (!period) return null;
83345
+ let spec = typeof period === "string" ? YUI_PERIODS[period] : period;
83346
+ if (!spec || !spec.unit) return null;
83347
+ if (PERIOD_UNITS.indexOf(spec.unit) < 0) return null;
83348
+ let count = parseInt(spec.count, 10);
83349
+ if (!(count > 0)) count = 1;
83350
+ return {
83351
+ id: spec.id || spec.unit,
83352
+ unit: spec.unit,
83353
+ count
83354
+ };
83355
+ }
83356
+ /***************************************************************
83357
+ * The START of the bucket that holds `anchor_ms`, as a local Date.
83358
+ *
83359
+ * Alignment is what makes a bucket a bucket. Each unit floors against
83360
+ * the natural origin of the unit above it, so the boundaries are the
83361
+ * ones a human already has in their head:
83362
+ *
83363
+ * minute -> the hour (a 15min bucket starts at :00/:15/:30/:45)
83364
+ * hour -> local midnight (a 6h bucket starts at 00/06/12/18)
83365
+ * day -> 1970-01-01 (count=1 is the plain day; a 10-day
83366
+ * bucket has no calendar origin to
83367
+ * align to, so it aligns to the epoch)
83368
+ * week -> the epoch week, Monday-first (ISO)
83369
+ * month -> January (which is why 2/3/4/6/12 give calendar
83370
+ * bimesters, quarters and semesters)
83371
+ * year -> year 0 (a decade starts at 2020, not 2021)
83372
+ ***************************************************************/
83373
+ function period_start(period, anchor_ms) {
83374
+ let spec = period_spec(period);
83375
+ if (!spec) return null;
83376
+ let d = new Date(anchor_ms);
83377
+ let n = spec.count;
83378
+ switch (spec.unit) {
83379
+ case "minute": {
83380
+ let m = Math.floor(d.getMinutes() / n) * n;
83381
+ return new Date(d.getFullYear(), d.getMonth(), d.getDate(), d.getHours(), m);
83382
+ }
83383
+ case "hour": {
83384
+ let h = Math.floor(d.getHours() / n) * n;
83385
+ return new Date(d.getFullYear(), d.getMonth(), d.getDate(), h);
83386
+ }
83387
+ case "day": return date_from_day_number(Math.floor(day_number(d) / n) * n);
83388
+ case "week": return date_from_day_number(Math.floor(week_index(d) / n) * n * 7 - 3);
83389
+ case "month": {
83390
+ let mi = d.getFullYear() * 12 + d.getMonth();
83391
+ let s = Math.floor(mi / n) * n;
83392
+ return new Date(Math.floor(s / 12), s % 12, 1);
83393
+ }
83394
+ case "year": {
83395
+ let y = Math.floor(d.getFullYear() / n) * n;
83396
+ return new Date(y, 0, 1);
83397
+ }
83398
+ default: return null;
83399
+ }
83400
+ }
83401
+ /***************************************************************
83402
+ * The start of the bucket `delta` buckets away (delta may be negative).
83403
+ * Calendar arithmetic, never millisecond arithmetic: `new Date(y, m+3, 1)`
83404
+ * overflows the year by itself, and stepping a day across a DST change
83405
+ * keeps landing on midnight.
83406
+ ***************************************************************/
83407
+ function period_shift(period, anchor_ms, delta) {
83408
+ let spec = period_spec(period);
83409
+ if (!spec) return anchor_ms;
83410
+ let s = period_start(spec, anchor_ms);
83411
+ let step = spec.count * (delta || 0);
83412
+ switch (spec.unit) {
83413
+ case "minute": return new Date(s.getFullYear(), s.getMonth(), s.getDate(), s.getHours(), s.getMinutes() + step).getTime();
83414
+ case "hour": return new Date(s.getFullYear(), s.getMonth(), s.getDate(), s.getHours() + step).getTime();
83415
+ case "day": return new Date(s.getFullYear(), s.getMonth(), s.getDate() + step).getTime();
83416
+ case "week": return new Date(s.getFullYear(), s.getMonth(), s.getDate() + step * 7).getTime();
83417
+ case "month": return new Date(s.getFullYear(), s.getMonth() + step, 1).getTime();
83418
+ case "year": return new Date(s.getFullYear() + step, 0, 1).getTime();
83419
+ default: return anchor_ms;
83420
+ }
83421
+ }
83422
+ /***************************************************************
83423
+ * The bucket holding `anchor_ms`, as {from, to} in MILLISECONDS.
83424
+ *
83425
+ * `to` is INCLUSIVE — the last millisecond of the bucket, not the first
83426
+ * of the next one. Both ends of a match condition are inclusive, and an
83427
+ * exclusive end handed to one silently swallows the record that landed
83428
+ * exactly on the boundary.
83429
+ ***************************************************************/
83430
+ function period_bounds(period, anchor_ms) {
83431
+ let spec = period_spec(period);
83432
+ if (!spec) return {
83433
+ from: 0,
83434
+ to: 0
83435
+ };
83436
+ return {
83437
+ from: period_start(spec, anchor_ms).getTime(),
83438
+ to: period_shift(spec, anchor_ms, 1) - 1
83439
+ };
83440
+ }
83441
+ /***************************************************************
83442
+ * The same bucket, in the unit the CONSUMER speaks (a tranger topic
83443
+ * keeps its timestamps in seconds, unless its system_flag says
83444
+ * milliseconds). This is the function a query builder calls.
83445
+ ***************************************************************/
83446
+ function period_bounds_epoch(period, anchor_ms, ms) {
83447
+ let b = period_bounds(period, anchor_ms);
83448
+ if (!b.from) return {
83449
+ from: 0,
83450
+ to: 0
83451
+ };
83452
+ return {
83453
+ from: ms_to_epoch(b.from, ms),
83454
+ to: ms_to_epoch(b.to, ms)
83455
+ };
83456
+ }
83457
+ /***************************************************************
83458
+ * A ROLLING window: `secs` back from now. Not a bucket — it has no
83459
+ * previous and no next, it just ends at `now`.
83460
+ *
83461
+ * The `to` end is left OPEN (0). An iterator with no upper bound keeps
83462
+ * matching the records that land while the card is on screen; pinning
83463
+ * it to "now" would freeze the window at the instant the user clicked.
83464
+ ***************************************************************/
83465
+ function rolling_bounds(rolling, ms, now_ms) {
83466
+ let r = typeof rolling === "string" ? YUI_ROLLING[rolling] : rolling;
83467
+ if (!r || !r.secs) return {
83468
+ from: 0,
83469
+ to: 0
83470
+ };
83471
+ return {
83472
+ from: ms_to_epoch((now_ms === void 0 ? Date.now() : now_ms) - r.secs * 1e3, ms),
83473
+ to: 0
83474
+ };
83475
+ }
83476
+ /***************************************************************
83477
+ * Is this the LAST bucket — the one `now` falls in? It is what greys
83478
+ * out the "next" arrow, and what tells the navigator it is already home.
83479
+ ***************************************************************/
83480
+ function is_current_period(period, anchor_ms, now_ms) {
83481
+ let spec = period_spec(period);
83482
+ if (!spec) return false;
83483
+ let now = now_ms === void 0 ? Date.now() : now_ms;
83484
+ let a = period_start(spec, anchor_ms);
83485
+ let b = period_start(spec, now);
83486
+ return !!a && !!b && a.getTime() === b.getTime();
83487
+ }
83488
+ /***************************************************************
83489
+ * Recognize the bucket a {from, to} pair came from, so a range that
83490
+ * travelled through a URL, a saved view or a backend answer comes back
83491
+ * as the period the user picked instead of as a hand-typed range.
83492
+ *
83493
+ * `from`/`to` are in the CONSUMER's unit (`ms`), and so is the
83494
+ * comparison — that is the whole trap: a bucket ends on the last
83495
+ * MILLISECOND (…23:59:59.999), and a consumer that keeps seconds stored
83496
+ * it truncated (…23:59:59). Comparing in milliseconds, a week saved by a
83497
+ * seconds-based topic came back as "no bucket at all", every time.
83498
+ *
83499
+ * Only an EXACT match counts: both ends must land on the bucket's own
83500
+ * boundaries, as that consumer would have written them. `candidates` are
83501
+ * ids/specs, tried in order. Returns {period, anchor} (anchor in ms) or
83502
+ * null.
83503
+ ***************************************************************/
83504
+ function infer_period(from, to, candidates, ms) {
83505
+ if (!from || !to || to <= from) return null;
83506
+ let from_ms = epoch_to_ms(from, ms);
83507
+ for (let candidate of candidates || Object.keys(YUI_PERIODS)) {
83508
+ let spec = period_spec(candidate);
83509
+ if (!spec) continue;
83510
+ let b = period_bounds_epoch(spec, from_ms, ms);
83511
+ if (b.from === from && b.to === to) return {
83512
+ period: spec,
83513
+ anchor: from_ms
83514
+ };
83515
+ }
83516
+ return null;
83517
+ }
83518
+ /***************************
83519
+ * Period labels
83520
+ ***************************/
83521
+ /***************************************************************
83522
+ * The name of a GRANULARITY (what the segmented control shows):
83523
+ * the spec's own id as an i18n key, so an app that declares
83524
+ * {id: "quarter"} gets "Trimestre" the moment it adds the key.
83525
+ ***************************************************************/
83526
+ function period_name(period, t) {
83527
+ let spec = period_spec(period);
83528
+ if (!spec) return tr(t, String(period && period.id || period || ""));
83529
+ return tr(t, spec.id);
83530
+ }
83531
+ /***************************************************************
83532
+ * The name of the bucket a navigator is PARKED on: what goes between
83533
+ * the two arrows.
83534
+ *
83535
+ * day -> "Today" / "Yesterday" / "13 jul 2026"
83536
+ * week -> "Week 27" (+ year when it is not this one)
83537
+ * month -> "July" (+ year when it is not this one)
83538
+ * quarter -> "Q3 2026" (unit month, count 3)
83539
+ * semester -> "H2 2026" (unit month, count 6)
83540
+ * year -> "2026"
83541
+ * decade -> "2020 – 2029"
83542
+ * any other -> the bucket's own edges: "1 jul – 31 aug 2026"
83543
+ *
83544
+ * The named ones are named because a range reads worse than a name;
83545
+ * everything an app invents falls back to the range, which is always
83546
+ * true and never wrong.
83547
+ ***************************************************************/
83548
+ function period_label(period, anchor_ms, t, locale) {
83549
+ let spec = period_spec(period);
83550
+ if (!spec) return "";
83551
+ let loc = safe_locale(locale);
83552
+ let start = period_start(spec, anchor_ms);
83553
+ let end = new Date(period_bounds(spec, anchor_ms).to);
83554
+ let now = /* @__PURE__ */ new Date();
83555
+ let this_year = start.getFullYear() === now.getFullYear();
83556
+ if (spec.unit === "day" && spec.count === 1) {
83557
+ let diff = day_number(start) - day_number(now);
83558
+ if (diff === 0) return tr(t, "today");
83559
+ if (diff === -1) return tr(t, "yesterday");
83560
+ if (diff === 1) return tr(t, "tomorrow");
83561
+ return fmt_intl(start, loc, this_year ? {
83562
+ day: "numeric",
83563
+ month: "short"
83564
+ } : {
83565
+ day: "numeric",
83566
+ month: "short",
83567
+ year: "numeric"
83568
+ });
83569
+ }
83570
+ if (spec.unit === "hour" && spec.count === 1) {
83571
+ let hh = fmt_intl(start, loc, {
83572
+ hour: "2-digit",
83573
+ minute: "2-digit"
83574
+ });
83575
+ if (day_number(start) === day_number(now)) return hh;
83576
+ return `${hh} · ${fmt_intl(start, loc, {
83577
+ day: "numeric",
83578
+ month: "short"
83579
+ })}`;
83580
+ }
83581
+ if (spec.unit === "week" && spec.count === 1) {
83582
+ let away = week_index(start) - week_index(now);
83583
+ if (away === 0) return tr(t, "this week");
83584
+ if (away === -1) return tr(t, "last week");
83585
+ let w = iso_week(start);
83586
+ return w.year === now.getFullYear() ? tr(t, "week {{n}}", { n: w.week }) : tr(t, "week {{n}} {{y}}", {
83587
+ n: w.week,
83588
+ y: w.year
83589
+ });
83590
+ }
83591
+ if (spec.unit === "month" && spec.count === 1) return fmt_intl(start, loc, this_year ? { month: "long" } : {
83592
+ month: "long",
83593
+ year: "numeric"
83594
+ });
83595
+ if (spec.unit === "month" && (spec.count === 3 || spec.count === 6)) {
83596
+ let n = Math.floor(start.getMonth() / spec.count) + 1;
83597
+ return tr(t, spec.count === 3 ? "quarter {{n}} {{y}}" : "semester {{n}} {{y}}", {
83598
+ n,
83599
+ y: start.getFullYear()
83600
+ });
83601
+ }
83602
+ if (spec.unit === "year") {
83603
+ if (spec.count === 1) return String(start.getFullYear());
83604
+ return `${start.getFullYear()} – ${end.getFullYear()}`;
83605
+ }
83606
+ let opts = spec.unit === "minute" || spec.unit === "hour" ? {
83607
+ day: "numeric",
83608
+ month: "short",
83609
+ hour: "2-digit",
83610
+ minute: "2-digit"
83611
+ } : {
83612
+ day: "numeric",
83613
+ month: "short"
83614
+ };
83615
+ if (start.getFullYear() !== end.getFullYear()) {
83616
+ let y_opts = Object.assign({ year: "numeric" }, opts);
83617
+ return `${fmt_intl(start, loc, y_opts)} – ${fmt_intl(end, loc, y_opts)}`;
83618
+ }
83619
+ let span = `${fmt_intl(start, loc, opts)} – ${fmt_intl(end, loc, opts)}`;
83620
+ return this_year ? span : `${span} ${start.getFullYear()}`;
83621
+ }
83622
+ //#endregion
83543
83623
  //#region src/c_yui_period.js
83544
83624
  /***********************************************************************
83545
83625
  * c_yui_period.js