tobogganSe connecter

library

Prebuilt pebble components: drop-in custom elements, no compiler needed

Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.

Dernier runAucun
0Étoiles
0Forks
2Issues
47 lignes · 1667 octetsBrut · Historique
1// A chart's state: which series its legend hides, and which item is
2// active (hovered, or reached with the arrow keys), whose tooltip shows.
3// Every change is an action through the reducer.
4//
5// Lifted unchanged from saison's report charts (server/ts-src/elements/chart/
6// state.ts, also in toboggan and times), the first piece of the library's
7// chart core. It lives in this package until a second chart needs it: a
8// package's types must stand alone (scripts/declarations.ts).
9
10export interface ChartState {
11 hidden: ReadonlySet<string>;
12 active: number | null;
13}
14
15export type Step = "next" | "prev" | "first" | "last";
16
17export type ChartAction =
18 | { type: "toggle"; key: string }
19 | { type: "activate"; index: number | null }
20 | { type: "step"; by: Step; count: number };
21
22export const initialChartState: ChartState = { hidden: new Set(), active: null };
23
24export function chartReducer(state: ChartState, action: ChartAction): ChartState {
25 switch (action.type) {
26 case "toggle": {
27 const hidden = new Set(state.hidden);
28 if (!hidden.delete(action.key)) hidden.add(action.key);
29 return { ...state, hidden };
30 }
31 case "activate":
32 return state.active === action.index ? state : { ...state, active: action.index };
33 case "step": {
34 const { count, by } = action;
35 if (count <= 0) return state;
36 const last = count - 1;
37 const at = state.active;
38 const active =
39 by === "first" ? 0
40 : by === "last" ? last
41 : at === null ? (by === "next" ? 0 : last)
42 : by === "next" ? Math.min(last, at + 1)
43 : Math.max(0, at - 1);
44 return { ...state, active };
45 }
46 }
47}