everlights 0.1.1 → 1.0.0
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.
- checksums.yaml +4 -4
- data/README.md +43 -7
- data/config/everlights.example.yml +105 -0
- data/exe/everlights +75 -26
- data/lib/everlights/cloud.rb +113 -0
- data/lib/everlights/sync.rb +93 -10
- data/lib/everlights/web.html +869 -0
- data/lib/everlights/web.rb +214 -0
- data/lib/everlights.rb +81 -10
- data/skills/everlights/SKILL.md +8 -5
- metadata +11 -5
|
@@ -0,0 +1,869 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="utf-8">
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
6
|
+
<title>EverLights schedule</title>
|
|
7
|
+
<link rel="icon" href="data:image/svg+xml,%3Csvg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 16 16%22%3E%3Ccircle cx=%228%22 cy=%228%22 r=%226%22 fill=%22%23ffb547%22/%3E%3C/svg%3E">
|
|
8
|
+
<link rel="preconnect" href="https://fonts.googleapis.com">
|
|
9
|
+
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
|
|
10
|
+
<link href="https://fonts.googleapis.com/css2?family=Bricolage+Grotesque:opsz,wght@12..96,400;12..96,500;12..96,700&display=swap" rel="stylesheet">
|
|
11
|
+
<style>
|
|
12
|
+
:root {
|
|
13
|
+
--dusk: #1c2340; /* page: the sky when shows start */
|
|
14
|
+
--night: #141a30; /* panels */
|
|
15
|
+
--wire: #3b4373; /* lines, unlit bulbs */
|
|
16
|
+
--frost: #ecebf6; /* text */
|
|
17
|
+
--haze: #a3a6cc; /* secondary text */
|
|
18
|
+
--ember: #ffb547; /* the one action color: apply */
|
|
19
|
+
--alarm: #ff8a80;
|
|
20
|
+
color-scheme: dark;
|
|
21
|
+
}
|
|
22
|
+
* { box-sizing: border-box; }
|
|
23
|
+
[hidden] { display: none !important; }
|
|
24
|
+
body {
|
|
25
|
+
margin: 0; background: var(--dusk); color: var(--frost);
|
|
26
|
+
font: 15px/1.45 "Bricolage Grotesque", system-ui, sans-serif; font-variant-numeric: tabular-nums;
|
|
27
|
+
}
|
|
28
|
+
button, input, select { font: inherit; color: inherit; }
|
|
29
|
+
:focus-visible { outline: 2px solid var(--ember); outline-offset: 2px; }
|
|
30
|
+
.muted { color: var(--haze); }
|
|
31
|
+
|
|
32
|
+
header {
|
|
33
|
+
position: sticky; top: 0; z-index: 5; display: flex; flex-wrap: wrap; gap: 12px 24px; align-items: center;
|
|
34
|
+
padding: 14px clamp(16px, 4vw, 40px); background: color-mix(in srgb, var(--dusk) 88%, transparent);
|
|
35
|
+
backdrop-filter: blur(8px); border-bottom: 1px solid var(--wire);
|
|
36
|
+
}
|
|
37
|
+
h1 { font-size: 22px; font-weight: 700; margin: 0; letter-spacing: -.01em; }
|
|
38
|
+
h1 small { font-weight: 400; font-size: 14px; color: var(--haze); margin-left: 8px; }
|
|
39
|
+
.status { margin-left: auto; display: flex; gap: 12px; align-items: center; flex-wrap: wrap; }
|
|
40
|
+
.status .saved { color: var(--haze); font-size: 13px; }
|
|
41
|
+
.btn { border: 1px solid var(--wire); background: none; border-radius: 6px; padding: 6px 12px; cursor: pointer; }
|
|
42
|
+
.btn:hover { border-color: var(--haze); }
|
|
43
|
+
.previewing { display: flex; gap: 10px; align-items: center; font-size: 14px; }
|
|
44
|
+
.previewing::before { content: ""; width: 9px; height: 9px; border-radius: 50%; background: var(--ember); box-shadow: 0 0 8px var(--ember); }
|
|
45
|
+
.apply { background: var(--ember); color: #2a1a00; border-color: var(--ember); font-weight: 700; }
|
|
46
|
+
.apply:disabled { background: none; color: var(--haze); border-color: var(--wire); font-weight: 400; cursor: default; }
|
|
47
|
+
.review { width: 100%; margin: 0; padding: 10px 0 0; list-style: none; font-size: 14px; display: grid; gap: 2px; }
|
|
48
|
+
.review b { display: inline-block; width: 64px; font-weight: 500; }
|
|
49
|
+
.op-create b { color: #9be89b; } .op-delete b { color: var(--alarm); } .op-update b { color: var(--ember); }
|
|
50
|
+
.problems { width: 100%; color: var(--alarm); margin: 0; padding-left: 18px; font-size: 14px; }
|
|
51
|
+
|
|
52
|
+
main { padding: 24px clamp(16px, 4vw, 40px) 80px; max-width: 1400px; margin: 0 auto; }
|
|
53
|
+
h2 { font-size: 17px; font-weight: 500; margin: 0 0 4px; }
|
|
54
|
+
h2 + p { margin: 0 0 14px; color: var(--haze); font-size: 14px; }
|
|
55
|
+
|
|
56
|
+
/* The calendar: each month is a light string, each night a bulb lit with that night's sequence. */
|
|
57
|
+
.strings { overflow-x: auto; padding-bottom: 4px; }
|
|
58
|
+
.string { display: grid; grid-template-columns: 76px repeat(31, 22px); align-items: center; height: 30px; min-width: max-content; position: relative; }
|
|
59
|
+
.string::before { content: ""; position: absolute; left: 76px; right: 0; top: 50%; border-top: 1px solid var(--wire); }
|
|
60
|
+
.string .m { color: var(--haze); font-size: 13px; }
|
|
61
|
+
.bulb {
|
|
62
|
+
width: 13px; height: 13px; border-radius: 50%; justify-self: center; position: relative; z-index: 1;
|
|
63
|
+
border: 1px solid var(--wire); background: var(--dusk); cursor: pointer; transition: opacity .15s;
|
|
64
|
+
}
|
|
65
|
+
.bulb.lit { border: 0; box-shadow: 0 0 9px -1px var(--glow); }
|
|
66
|
+
.bulb.note { background: repeating-linear-gradient(45deg, var(--wire) 0 2px, var(--dusk) 2px 4px); }
|
|
67
|
+
.bulb.today { outline: 1px solid var(--frost); outline-offset: 3px; }
|
|
68
|
+
.bulb.weekend { width: 15px; height: 15px; }
|
|
69
|
+
.strings.focus .bulb:not(.hit) { opacity: .18; }
|
|
70
|
+
.readout { min-height: 22px; font-size: 14px; margin: 8px 0 0; }
|
|
71
|
+
|
|
72
|
+
.cols { display: grid; grid-template-columns: minmax(0, 3fr) minmax(0, 2fr); gap: 40px; margin-top: 36px; }
|
|
73
|
+
@media (max-width: 900px) { .cols { grid-template-columns: 1fr; } }
|
|
74
|
+
.toolbar { display: flex; gap: 8px; align-items: baseline; margin-bottom: 12px; }
|
|
75
|
+
.toolbar h2 { margin-right: auto; }
|
|
76
|
+
|
|
77
|
+
/* Rules */
|
|
78
|
+
.rules { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
|
|
79
|
+
.rule { background: var(--night); border: 1px solid transparent; border-radius: 8px; }
|
|
80
|
+
.rule.drop-before { box-shadow: 0 -3px 0 var(--ember); }
|
|
81
|
+
.rule.drop-after { box-shadow: 0 3px 0 var(--ember); }
|
|
82
|
+
.rule.dragging { opacity: .4; }
|
|
83
|
+
.rule.open { border-color: var(--wire); }
|
|
84
|
+
.rule-head { display: grid; grid-template-columns: 24px 1fr auto; gap: 8px; align-items: center; padding: 8px 10px 8px 4px; }
|
|
85
|
+
.grip { cursor: grab; border: 0; background: none; color: var(--haze); padding: 4px; line-height: 1; letter-spacing: -2px; }
|
|
86
|
+
.rule-title { border: 0; background: none; text-align: left; padding: 0; cursor: pointer; }
|
|
87
|
+
.rule-title strong { font-weight: 500; }
|
|
88
|
+
.rule-title span { color: var(--haze); font-size: 13px; margin-left: 8px; }
|
|
89
|
+
.rule-body { padding: 4px 14px 14px 36px; display: grid; gap: 10px; }
|
|
90
|
+
.chips { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; min-height: 30px; padding: 0 10px 8px 36px; }
|
|
91
|
+
.rule.open .chips { padding-bottom: 0; }
|
|
92
|
+
.rule.over { border-color: var(--ember); border-style: dashed; }
|
|
93
|
+
.chip {
|
|
94
|
+
display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--wire); border-radius: 999px;
|
|
95
|
+
padding: 2px 4px 2px 3px; font-size: 13px; cursor: grab; background: var(--dusk);
|
|
96
|
+
}
|
|
97
|
+
.chip .sw { width: 22px; height: 14px; border-radius: 999px; }
|
|
98
|
+
.chip input { width: 3.2em; border: 0; background: none; color: var(--haze); text-align: right; padding: 0; }
|
|
99
|
+
.chip .x { border: 0; background: none; color: var(--haze); cursor: pointer; padding: 0 4px; }
|
|
100
|
+
.add-seq { border: 1px dashed var(--wire); border-radius: 999px; background: none; padding: 2px 10px; width: 150px; font-size: 13px; }
|
|
101
|
+
.field { display: grid; grid-template-columns: 70px 1fr; gap: 8px; align-items: center; font-size: 14px; }
|
|
102
|
+
.field > span { color: var(--haze); }
|
|
103
|
+
.field input[type=text], .field select, .seq-name {
|
|
104
|
+
background: var(--dusk); border: 1px solid var(--wire); border-radius: 6px; padding: 5px 8px; width: 100%;
|
|
105
|
+
}
|
|
106
|
+
.pair { display: flex; gap: 8px; align-items: center; }
|
|
107
|
+
.days { display: flex; gap: 4px; flex-wrap: wrap; }
|
|
108
|
+
.days label { position: relative; }
|
|
109
|
+
.days input { position: absolute; opacity: 0; }
|
|
110
|
+
.days span { display: inline-block; padding: 3px 8px; border: 1px solid var(--wire); border-radius: 6px; font-size: 13px; cursor: pointer; }
|
|
111
|
+
.days input:checked + span { background: var(--wire); }
|
|
112
|
+
.days input:focus-visible + span { outline: 2px solid var(--ember); }
|
|
113
|
+
.remove { justify-self: start; border: 0; background: none; color: var(--alarm); cursor: pointer; padding: 0; font-size: 13px; }
|
|
114
|
+
|
|
115
|
+
/* Sequences */
|
|
116
|
+
.seqs { display: grid; gap: 10px; }
|
|
117
|
+
.seq { background: var(--night); border-radius: 8px; padding: 12px; display: grid; gap: 10px; }
|
|
118
|
+
.seq-top { display: flex; gap: 8px; align-items: center; }
|
|
119
|
+
.seq-name { font-weight: 500; }
|
|
120
|
+
.bulbs { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
|
|
121
|
+
.swatch { position: relative; }
|
|
122
|
+
.swatch input { width: 28px; height: 28px; padding: 0; border: 0; border-radius: 50%; background: none; cursor: pointer; }
|
|
123
|
+
.swatch input::-webkit-color-swatch-wrapper { padding: 0; }
|
|
124
|
+
.swatch input::-webkit-color-swatch { border: 0; border-radius: 50%; }
|
|
125
|
+
.swatch input::-moz-color-swatch { border: 0; border-radius: 50%; }
|
|
126
|
+
.swatch .x { position: absolute; top: -6px; right: -6px; width: 16px; height: 16px; border-radius: 50%; border: 0; padding: 0;
|
|
127
|
+
font-size: 11px; line-height: 16px; background: var(--wire); cursor: pointer; display: none; }
|
|
128
|
+
.swatch:hover .x, .swatch:focus-within .x { display: block; }
|
|
129
|
+
.plus { width: 28px; height: 28px; border-radius: 50%; border: 1px dashed var(--wire); background: none; cursor: pointer; color: var(--haze); }
|
|
130
|
+
.fx { display: grid; grid-template-columns: 120px 1fr 34px 20px; gap: 8px; align-items: center; font-size: 14px; }
|
|
131
|
+
.fx select { background: var(--dusk); border: 1px solid var(--wire); border-radius: 6px; padding: 3px 6px; }
|
|
132
|
+
.fx input[type=range] { accent-color: var(--ember); }
|
|
133
|
+
.fx .x { border: 0; background: none; color: var(--haze); cursor: pointer; }
|
|
134
|
+
.seqs.cards { grid-template-columns: repeat(auto-fill, minmax(340px, 1fr)); align-items: start; }
|
|
135
|
+
.seq-actions { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
|
|
136
|
+
.seq-actions .btn { padding: 4px 10px; font-size: 14px; }
|
|
137
|
+
.btn.save { border-color: var(--ember); color: var(--ember); }
|
|
138
|
+
.btn:disabled { opacity: .45; cursor: default; }
|
|
139
|
+
.dirty { color: var(--ember); font-size: 13px; }
|
|
140
|
+
.readonly .bulbs span { width: 22px; height: 22px; border-radius: 50%; }
|
|
141
|
+
.palette { margin-top: 28px; }
|
|
142
|
+
.palette input[type=search] { background: var(--night); border: 1px solid var(--wire); border-radius: 6px; padding: 5px 8px; width: 100%; margin-bottom: 10px; }
|
|
143
|
+
.palette .chips { padding: 0; }
|
|
144
|
+
aside .chips { max-height: 60vh; overflow-y: auto; }
|
|
145
|
+
.chip.on { border-color: var(--ember); }
|
|
146
|
+
|
|
147
|
+
/* Views */
|
|
148
|
+
.tabs { display: flex; gap: 4px; }
|
|
149
|
+
.tabs a { color: var(--haze); text-decoration: none; padding: 5px 12px; border-radius: 6px; }
|
|
150
|
+
.tabs a[aria-current=page] { color: var(--frost); background: var(--night); }
|
|
151
|
+
body:not([data-view=sequences]) #view-sequences, body:not([data-view=bridge]) #view-bridge,
|
|
152
|
+
body:not([data-view=calendar]) #view-calendar { display: none; }
|
|
153
|
+
|
|
154
|
+
/* Bridge */
|
|
155
|
+
.checks { list-style: none; margin: 0 0 32px; padding: 0; display: grid; gap: 6px; }
|
|
156
|
+
.checks li { display: grid; grid-template-columns: 14px 170px 1fr; gap: 10px; align-items: baseline; }
|
|
157
|
+
.checks li::before { content: ""; width: 9px; height: 9px; border-radius: 50%; border: 1px solid var(--wire); align-self: center; }
|
|
158
|
+
.checks .ok::before { background: #9be89b; border: 0; box-shadow: 0 0 7px #9be89b; }
|
|
159
|
+
.checks .warn::before { background: var(--alarm); border: 0; box-shadow: 0 0 7px var(--alarm); }
|
|
160
|
+
.checks .warn { color: var(--alarm); }
|
|
161
|
+
.panels { display: grid; grid-template-columns: repeat(auto-fill, minmax(340px, 1fr)); gap: 16px; align-items: start; }
|
|
162
|
+
.panel { background: var(--night); border-radius: 8px; padding: 14px 16px; }
|
|
163
|
+
.panel h3 { font-size: 15px; font-weight: 500; margin: 0 0 10px; }
|
|
164
|
+
.panel dl { display: grid; grid-template-columns: 130px 1fr; gap: 4px 12px; margin: 0; font-size: 14px; }
|
|
165
|
+
.panel dt { color: var(--haze); }
|
|
166
|
+
.panel dd { margin: 0; }
|
|
167
|
+
.actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
|
|
168
|
+
.btn.danger { border-color: var(--alarm); color: var(--alarm); }
|
|
169
|
+
details { margin-top: 24px; font-size: 13px; }
|
|
170
|
+
details pre { background: var(--night); padding: 12px; border-radius: 8px; overflow-x: auto; }
|
|
171
|
+
|
|
172
|
+
/* Hover card for any sequence chip */
|
|
173
|
+
.pop {
|
|
174
|
+
position: absolute; z-index: 10; width: min(380px, calc(100vw - 32px)); background: var(--night);
|
|
175
|
+
border: 1px solid var(--wire); border-radius: 10px; box-shadow: 0 12px 32px rgb(5 8 20 / .55);
|
|
176
|
+
}
|
|
177
|
+
.pop .seq { background: none; }
|
|
178
|
+
.error { color: var(--alarm); }
|
|
179
|
+
@media (prefers-reduced-motion: reduce) { * { transition: none !important; } }
|
|
180
|
+
</style>
|
|
181
|
+
</head>
|
|
182
|
+
<body>
|
|
183
|
+
<header>
|
|
184
|
+
<h1>EverLights <small id="where"></small></h1>
|
|
185
|
+
<nav class="tabs" aria-label="Views"><a href="#calendar" id="tab-calendar">Calendar</a><a href="#sequences" id="tab-sequences">Sequences</a><a href="#bridge" id="tab-bridge">Bridge</a></nav>
|
|
186
|
+
<div class="status">
|
|
187
|
+
<span class="previewing" id="previewing" hidden><span id="preview-name"></span> is on the lights <button class="btn" id="stop-preview">Stop preview</button></span>
|
|
188
|
+
<span class="saved" id="saved" aria-live="polite">Reading the bridge…</span>
|
|
189
|
+
<button class="btn" id="review-btn" aria-expanded="false" hidden>Review</button>
|
|
190
|
+
<button class="btn apply" id="apply" disabled>Apply</button>
|
|
191
|
+
</div>
|
|
192
|
+
<ul class="review" id="review" hidden></ul>
|
|
193
|
+
<ul class="problems" id="problems" hidden></ul>
|
|
194
|
+
</header>
|
|
195
|
+
|
|
196
|
+
<main>
|
|
197
|
+
<div id="view-calendar">
|
|
198
|
+
<section aria-labelledby="cal-h">
|
|
199
|
+
<h2 id="cal-h">Nights ahead</h2>
|
|
200
|
+
<p>Each bulb is one night, lit with the sequence that plays. Striped bulbs have no show; hover for why.</p>
|
|
201
|
+
<div class="strings" id="strings"></div>
|
|
202
|
+
<p class="readout" id="readout" aria-live="polite"></p>
|
|
203
|
+
</section>
|
|
204
|
+
|
|
205
|
+
<div class="cols">
|
|
206
|
+
<section aria-labelledby="rules-h">
|
|
207
|
+
<div class="toolbar"><h2 id="rules-h">Schedule</h2><button class="btn" id="new-rule">New rule</button></div>
|
|
208
|
+
<p class="muted" style="margin:-6px 0 14px;font-size:14px">Higher rules win shared nights. Drag the handle to reorder, or focus it and use the arrow keys. Drag sequences onto a rule to add them; hover one to see, edit or preview it.</p>
|
|
209
|
+
<ol class="rules" id="rules"></ol>
|
|
210
|
+
</section>
|
|
211
|
+
<aside class="palette" style="margin-top:0" aria-labelledby="side-h">
|
|
212
|
+
<h2 id="side-h">Sequences</h2>
|
|
213
|
+
<p>Yours first, then the bridge's.</p>
|
|
214
|
+
<input type="search" id="side-filter" placeholder="Filter" aria-label="Filter sequences">
|
|
215
|
+
<div class="chips" id="side"></div>
|
|
216
|
+
</aside>
|
|
217
|
+
</div>
|
|
218
|
+
</div>
|
|
219
|
+
|
|
220
|
+
<div id="view-sequences">
|
|
221
|
+
<div class="toolbar"><h2>Your sequences</h2><button class="btn" id="new-seq">New sequence</button></div>
|
|
222
|
+
<div class="seqs cards" id="seqs"></div>
|
|
223
|
+
<div class="palette">
|
|
224
|
+
<h2>On the bridge</h2>
|
|
225
|
+
<p>Built-ins and app-made sequences. Hover one to preview it or copy it into yours.</p>
|
|
226
|
+
<input type="search" id="filter" placeholder="Filter" aria-label="Filter bridge sequences">
|
|
227
|
+
<div class="chips" id="palette"></div>
|
|
228
|
+
</div>
|
|
229
|
+
</div>
|
|
230
|
+
|
|
231
|
+
<div id="view-bridge">
|
|
232
|
+
<div class="toolbar">
|
|
233
|
+
<h2>Health</h2>
|
|
234
|
+
<div class="actions">
|
|
235
|
+
<span class="muted" id="bridge-at" aria-live="polite"></span>
|
|
236
|
+
<button class="btn" id="bridge-refresh">Refresh</button>
|
|
237
|
+
<button class="btn" id="bridge-time" title="The bridge normally keeps time over NTP">Set clock to now</button>
|
|
238
|
+
<button class="btn danger" id="bridge-reboot">Reboot</button>
|
|
239
|
+
</div>
|
|
240
|
+
</div>
|
|
241
|
+
<ul class="checks" id="checks"></ul>
|
|
242
|
+
<div class="panels" id="panels"></div>
|
|
243
|
+
<details><summary class="muted">Everything the bridge reports</summary><pre id="raw"></pre></details>
|
|
244
|
+
</div>
|
|
245
|
+
</main>
|
|
246
|
+
<div class="pop" id="pop" role="dialog" aria-label="Sequence" hidden></div>
|
|
247
|
+
<datalist id="seq-names"></datalist>
|
|
248
|
+
|
|
249
|
+
<script>
|
|
250
|
+
const $ = (id) => document.getElementById(id);
|
|
251
|
+
const DAYS = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
|
|
252
|
+
let S = null; // last state from the server
|
|
253
|
+
let cfg = null; // the config being edited
|
|
254
|
+
let open = new Set(); // expanded rule names
|
|
255
|
+
|
|
256
|
+
function el(tag, attrs = {}, ...kids) {
|
|
257
|
+
const n = document.createElement(tag);
|
|
258
|
+
for (const [k, v] of Object.entries(attrs)) {
|
|
259
|
+
if (v == null || v === false) continue;
|
|
260
|
+
if (k.startsWith("on")) n.addEventListener(k.slice(2), v);
|
|
261
|
+
else if (k === "style") n.style.cssText = v;
|
|
262
|
+
else if (k in n && k !== "list") n[k] = v;
|
|
263
|
+
else n.setAttribute(k, v === true ? "" : v);
|
|
264
|
+
}
|
|
265
|
+
n.append(...kids.flat().filter((k) => k != null && k !== false));
|
|
266
|
+
return n;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
// --- sequences and colors ---
|
|
270
|
+
const parseRef = (ref) => { const m = String(ref).match(/^(.*?):(\d+)$/); return m ? [m[1].trim(), m[2]] : [String(ref).trim(), ""]; };
|
|
271
|
+
function colorsOf(name) {
|
|
272
|
+
const lower = name.toLowerCase();
|
|
273
|
+
const own = Object.entries(cfg.sequences || {}).find(([n]) => n.toLowerCase() === lower);
|
|
274
|
+
if (own) return (own[1]?.colors || []).map(String);
|
|
275
|
+
return S.bridge_sequences.find((s) => s.name.toLowerCase() === lower)?.colors || [];
|
|
276
|
+
}
|
|
277
|
+
const hex = (c) => "#" + String(c).replace(/^#/, "");
|
|
278
|
+
// the same count of colors, evenly from the first to the last (as "FIRST..LAST:N" does in the config)
|
|
279
|
+
function blend(colors) {
|
|
280
|
+
const [a, b] = [colors[0], colors.at(-1)].map((c) => [1, 3, 5].map((i) => parseInt(hex(c).slice(i, i + 2), 16)));
|
|
281
|
+
return colors.map((_, i) => a.map((v, k) => Math.round(v + (b[k] - v) * i / (colors.length - 1)).toString(16).padStart(2, "0")).join("").toUpperCase());
|
|
282
|
+
}
|
|
283
|
+
function stripe(colors) {
|
|
284
|
+
if (!colors.length) return "transparent";
|
|
285
|
+
if (colors.length === 1) return hex(colors[0]);
|
|
286
|
+
const w = 100 / colors.length;
|
|
287
|
+
return `linear-gradient(90deg, ${colors.map((c, i) => `${hex(c)} ${i * w}% ${(i + 1) * w}%`).join(", ")})`;
|
|
288
|
+
}
|
|
289
|
+
function wheel(colors) {
|
|
290
|
+
if (colors.length < 2) return hex(colors[0] || "000000");
|
|
291
|
+
const w = 360 / colors.length;
|
|
292
|
+
return `conic-gradient(${colors.map((c, i) => `${hex(c)} ${i * w}deg ${(i + 1) * w}deg`).join(", ")})`;
|
|
293
|
+
}
|
|
294
|
+
const ruleColors = (rule) => colorsOf(parseRef((rule?.sequences || [])[0] || "")[0]);
|
|
295
|
+
|
|
296
|
+
// --- the calendar ---
|
|
297
|
+
const fmtDay = (d) => d.toLocaleDateString(undefined, { weekday: "short", month: "short", day: "numeric" });
|
|
298
|
+
function renderStrings() {
|
|
299
|
+
const box = $("strings"); box.replaceChildren();
|
|
300
|
+
const today = new Date(S.today + "T12:00");
|
|
301
|
+
const last = new Date(today); last.setDate(last.getDate() + S.horizon);
|
|
302
|
+
const rules = Object.fromEntries((cfg.schedule || []).map((r) => [r.name, r]));
|
|
303
|
+
for (let m = new Date(today.getFullYear(), today.getMonth(), 1); m <= last; m.setMonth(m.getMonth() + 1)) {
|
|
304
|
+
const row = el("div", { className: "string" }, el("span", { className: "m" }, m.toLocaleDateString(undefined, m.getMonth() === 0 || +m === +new Date(today.getFullYear(), today.getMonth(), 1) ? { month: "short", year: "numeric" } : { month: "short" })));
|
|
305
|
+
const days = new Date(m.getFullYear(), m.getMonth() + 1, 0).getDate();
|
|
306
|
+
for (let d = 1; d <= days; d++) {
|
|
307
|
+
const date = new Date(m.getFullYear(), m.getMonth(), d, 12);
|
|
308
|
+
const iso = `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, "0")}-${String(d).padStart(2, "0")}`;
|
|
309
|
+
if (date < today || date > last) { row.append(el("span")); continue; }
|
|
310
|
+
const n = S.nights?.[iso];
|
|
311
|
+
const colors = n && !n.note ? ruleColors(rules[n.rule]) : [];
|
|
312
|
+
const text = !n ? `${fmtDay(date)}: no rule` : n.note ? `${fmtDay(date)}: ${n.rule}, no show (${n.note})`
|
|
313
|
+
: `${fmtDay(date)}: ${n.rule}, ${n.start}–${n.end}${rules[n.rule] ? ", " + rules[n.rule].sequences.map((s) => parseRef(s)[0]).join(", ") : ""}`;
|
|
314
|
+
const bulb = el("span", {
|
|
315
|
+
className: `bulb${colors.length ? " lit" : ""}${n?.note ? " note" : ""}${iso === S.today ? " today" : ""}${[0, 6].includes(date.getDay()) ? " weekend" : ""}`,
|
|
316
|
+
title: text, "data-rule": n?.rule,
|
|
317
|
+
onmouseenter: () => ($("readout").textContent = text),
|
|
318
|
+
onclick: () => n && focusRule(n.rule),
|
|
319
|
+
});
|
|
320
|
+
if (colors.length) { bulb.style.background = wheel(colors); bulb.style.setProperty("--glow", hex(colors[0])); }
|
|
321
|
+
row.append(bulb);
|
|
322
|
+
}
|
|
323
|
+
box.append(row);
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
function highlight(name) {
|
|
327
|
+
$("strings").classList.toggle("focus", !!name);
|
|
328
|
+
for (const b of $("strings").querySelectorAll(".bulb")) b.classList.toggle("hit", b.dataset.rule === name);
|
|
329
|
+
}
|
|
330
|
+
function focusRule(name) {
|
|
331
|
+
open.add(name); renderRules();
|
|
332
|
+
document.querySelector(`[data-name="${CSS.escape(name)}"]`)?.scrollIntoView({ behavior: "smooth", block: "center" });
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
// --- rules ---
|
|
336
|
+
function when(r) {
|
|
337
|
+
if (r.ics != null) return r.match ? `calendar feed, "${r.match}"` : "calendar feed";
|
|
338
|
+
if (r.on != null) return String(r.on);
|
|
339
|
+
const span = r.from != null ? `${r.from} to ${r.to}` : "all year";
|
|
340
|
+
const ev = r.every ? [].concat(r.every) : null;
|
|
341
|
+
return ev && ev.length < 7 ? `${ev.join(", ")}, ${span}` : `nightly, ${span}`;
|
|
342
|
+
}
|
|
343
|
+
function times(r) {
|
|
344
|
+
const d = cfg.defaults || {};
|
|
345
|
+
return `${r.start ?? d.start ?? "dusk"}–${r.end ?? d.end ?? "23:00"}`;
|
|
346
|
+
}
|
|
347
|
+
let drag = null; // {type: "rule", i} | {type: "seq", name, from?: [ruleIndex, chipIndex]}
|
|
348
|
+
|
|
349
|
+
function chip(ref, ri, ci) {
|
|
350
|
+
const [name, secs] = parseRef(ref);
|
|
351
|
+
const r = cfg.schedule[ri];
|
|
352
|
+
return el("span", {
|
|
353
|
+
className: "chip", draggable: true, "data-ci": ci, "data-pop": name, tabindex: 0,
|
|
354
|
+
// r.sequences[ci], not ref: the seconds may have changed since this chip was drawn
|
|
355
|
+
ondragstart: (e) => { drag = { type: "seq", name: r.sequences[ci], from: [ri, ci] }; e.dataTransfer.setData("text/plain", name); e.stopPropagation(); closePop(); },
|
|
356
|
+
},
|
|
357
|
+
el("span", { className: "sw", style: `background:${stripe(colorsOf(name))}` }),
|
|
358
|
+
name,
|
|
359
|
+
el("input", { type: "number", min: 1, value: secs, placeholder: String(cfg.defaults?.seconds ?? 20), "aria-label": `${name} seconds`,
|
|
360
|
+
title: "Seconds per pass; blank uses the default",
|
|
361
|
+
onchange: (e) => { r.sequences[ci] = e.target.value ? `${name}:${e.target.value}` : name; save(); } }),
|
|
362
|
+
el("button", { className: "x", "aria-label": `Remove ${name} from ${r.name}`, onclick: () => { r.sequences.splice(ci, 1); save(true); } }, "×"),
|
|
363
|
+
);
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
function chipZone(r, ri) {
|
|
367
|
+
return el("div", { className: "chips" }, (r.sequences || []).map((ref, ci) => chip(ref, ri, ci)),
|
|
368
|
+
el("input", { className: "add-seq", list: "seq-names", placeholder: "Add sequence", "aria-label": `Add a sequence to ${r.name}`,
|
|
369
|
+
onchange: (e) => { const v = e.target.value.trim(); if (v) { (r.sequences ||= []).push(v); save(true); } } }));
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
// a sequence dropped anywhere on a rule: before the chip it lands on, else at the end
|
|
373
|
+
function dropSeq(e, r, ri) {
|
|
374
|
+
const target = e.target.closest(".chip[data-ci]");
|
|
375
|
+
let at = target ? +target.dataset.ci : (r.sequences || []).length;
|
|
376
|
+
if (drag.from) {
|
|
377
|
+
const [fr, fc] = drag.from;
|
|
378
|
+
cfg.schedule[fr].sequences.splice(fc, 1);
|
|
379
|
+
if (fr === ri && fc < at) at--;
|
|
380
|
+
}
|
|
381
|
+
(r.sequences ||= []).splice(at, 0, drag.name);
|
|
382
|
+
drag = null; save(true);
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
function field(label, ...kids) { return el("label", { className: "field" }, el("span", {}, label), ...kids); }
|
|
386
|
+
function text(obj, key, attrs = {}) {
|
|
387
|
+
return el("input", { type: "text", value: obj[key] ?? "", ...attrs,
|
|
388
|
+
onchange: (e) => { const v = e.target.value.trim(); v ? (obj[key] = v) : delete obj[key]; save(); } });
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
function ruleBody(r, ri) {
|
|
392
|
+
const kind = r.ics != null ? "ics" : r.on != null ? "on" : "span";
|
|
393
|
+
const every = r.every ? [].concat(r.every).map((d) => DAYS.find((x) => x.toLowerCase() === String(d).slice(0, 3).toLowerCase())) : DAYS;
|
|
394
|
+
const setDays = (days) => {
|
|
395
|
+
if (days.length === 7 && r.from != null) delete r.every; else r.every = days;
|
|
396
|
+
save();
|
|
397
|
+
};
|
|
398
|
+
const d = cfg.defaults || {};
|
|
399
|
+
return el("div", { className: "rule-body" },
|
|
400
|
+
field("Name", el("input", { type: "text", value: r.name, onchange: (e) => {
|
|
401
|
+
const was = r.name; r.name = e.target.value.trim() || was;
|
|
402
|
+
if (open.delete(was)) open.add(r.name);
|
|
403
|
+
save(true);
|
|
404
|
+
} })),
|
|
405
|
+
field("When", el("select", { onchange: (e) => {
|
|
406
|
+
for (const k of ["on", "from", "to", "every", "ics", "match"]) delete r[k];
|
|
407
|
+
if (e.target.value === "on") r.on = "Dec 25";
|
|
408
|
+
else if (e.target.value === "ics") r.ics = "";
|
|
409
|
+
else r.every = DAYS.slice();
|
|
410
|
+
save(true);
|
|
411
|
+
} }, el("option", { value: "on", selected: kind === "on" }, "One night a year"), el("option", { value: "span", selected: kind === "span" }, "Several nights"),
|
|
412
|
+
el("option", { value: "ics", selected: kind === "ics" }, "Days in a calendar feed"))),
|
|
413
|
+
kind === "on" ? field("Date", text(r, "on", { placeholder: "Dec 25, Easter-2, 4th Thursday of November" }))
|
|
414
|
+
: kind === "ics" ? [
|
|
415
|
+
field("Feed", text(r, "ics", { placeholder: "https://… .ics (webcal:// works too)" })),
|
|
416
|
+
field("Title has", text(r, "match", { placeholder: "e.g. Bobcats; blank for every event" })),
|
|
417
|
+
]
|
|
418
|
+
: [
|
|
419
|
+
field("From", el("div", { className: "pair" },
|
|
420
|
+
text(r, "from", { placeholder: "Dec 1", "aria-label": "From" }), el("span", { className: "muted" }, "to"),
|
|
421
|
+
text(r, "to", { placeholder: "Jan 6", "aria-label": "To" }))),
|
|
422
|
+
el("div", { className: "field" }, el("span", {}, "Nights"), el("div", { className: "days" }, DAYS.map((day) =>
|
|
423
|
+
el("label", {}, el("input", { type: "checkbox", checked: every.includes(day),
|
|
424
|
+
// read the boxes, not `every`: a save that keeps focus doesn't redraw this rule
|
|
425
|
+
onchange: (e) => { const boxes = e.target.closest(".days").querySelectorAll("input"); setDays(DAYS.filter((_, i) => boxes[i].checked)); } }), el("span", {}, day))))),
|
|
426
|
+
],
|
|
427
|
+
field("Starts", text(r, "start", { placeholder: `${d.start ?? "dusk"} (default); dusk+15 or 18:30` })),
|
|
428
|
+
field("Ends", text(r, "end", { placeholder: `${d.end ?? "23:00"} (default)` })),
|
|
429
|
+
el("button", { className: "remove", onclick: () => { cfg.schedule.splice(ri, 1); save(true); } }, `Remove ${r.name}`),
|
|
430
|
+
);
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
function moveRule(from, to) {
|
|
434
|
+
const [r] = cfg.schedule.splice(from, 1);
|
|
435
|
+
cfg.schedule.splice(to, 0, r);
|
|
436
|
+
save(true);
|
|
437
|
+
requestAnimationFrame(() => document.querySelector(`[data-name="${CSS.escape(r.name)}"] .grip`)?.focus());
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
function renderRules() {
|
|
441
|
+
const list = $("rules");
|
|
442
|
+
list.replaceChildren(...(cfg.schedule || []).map((r, ri) => {
|
|
443
|
+
const isOpen = open.has(r.name);
|
|
444
|
+
const li = el("li", { className: `rule${isOpen ? " open" : ""}`, "data-name": r.name,
|
|
445
|
+
onmouseenter: () => highlight(r.name), onmouseleave: () => highlight(null),
|
|
446
|
+
ondragover: (e) => {
|
|
447
|
+
if (drag?.type === "seq") { e.preventDefault(); li.classList.add("over"); return; }
|
|
448
|
+
if (drag?.type !== "rule") return;
|
|
449
|
+
e.preventDefault();
|
|
450
|
+
const box = li.getBoundingClientRect(), after = e.clientY > box.top + box.height / 2;
|
|
451
|
+
li.classList.toggle("drop-after", after); li.classList.toggle("drop-before", !after);
|
|
452
|
+
},
|
|
453
|
+
ondragleave: (e) => { if (!li.contains(e.relatedTarget)) li.classList.remove("drop-before", "drop-after", "over"); },
|
|
454
|
+
ondrop: (e) => {
|
|
455
|
+
li.classList.remove("over");
|
|
456
|
+
if (drag?.type === "seq") { e.preventDefault(); return dropSeq(e, r, ri); }
|
|
457
|
+
if (drag?.type !== "rule") return;
|
|
458
|
+
e.preventDefault();
|
|
459
|
+
const after = li.classList.contains("drop-after");
|
|
460
|
+
li.classList.remove("drop-before", "drop-after");
|
|
461
|
+
let to = ri + (after ? 1 : 0);
|
|
462
|
+
if (drag.i < to) to--;
|
|
463
|
+
if (to !== drag.i) moveRule(drag.i, to);
|
|
464
|
+
drag = null;
|
|
465
|
+
},
|
|
466
|
+
},
|
|
467
|
+
el("div", { className: "rule-head" },
|
|
468
|
+
el("button", { className: "grip", draggable: true, "aria-label": `Move ${r.name}; arrow keys reorder`, title: "Drag to reorder",
|
|
469
|
+
ondragstart: (e) => { drag = { type: "rule", i: ri }; e.dataTransfer.setData("text/plain", r.name); li.classList.add("dragging"); },
|
|
470
|
+
ondragend: () => li.classList.remove("dragging"),
|
|
471
|
+
onkeydown: (e) => {
|
|
472
|
+
if (e.key === "ArrowUp" && ri > 0) { e.preventDefault(); moveRule(ri, ri - 1); }
|
|
473
|
+
if (e.key === "ArrowDown" && ri < cfg.schedule.length - 1) { e.preventDefault(); moveRule(ri, ri + 1); }
|
|
474
|
+
} }, "⋮⋮"),
|
|
475
|
+
el("button", { className: "rule-title", "aria-expanded": String(isOpen),
|
|
476
|
+
onclick: () => { isOpen ? open.delete(r.name) : open.add(r.name); renderRules(); } },
|
|
477
|
+
el("strong", {}, r.name), el("span", {}, `${when(r)}, ${times(r)}`)),
|
|
478
|
+
el("span", { className: "sw", "aria-hidden": "true", style: `width:22px;height:14px;border-radius:999px;background:${stripe(ruleColors(r))}` }),
|
|
479
|
+
),
|
|
480
|
+
chipZone(r, ri),
|
|
481
|
+
isOpen && ruleBody(r, ri),
|
|
482
|
+
);
|
|
483
|
+
return li;
|
|
484
|
+
}));
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
// --- sequences: one editor for the cards and the hover card. Edits are a draft until Save. ---
|
|
488
|
+
const drafts = new Map(); // name -> {name, colors, effects}
|
|
489
|
+
const own = (name) => Object.keys(cfg.sequences).find((n) => n.toLowerCase() === name.toLowerCase());
|
|
490
|
+
const bridgeSeq = (name) => S.bridge_sequences.find((s) => s.name.toLowerCase() === name.toLowerCase());
|
|
491
|
+
const asSpec = (d) => ({ colors: d.colors, ...(Object.keys(d.effects || {}).length ? { effects: d.effects } : {}) });
|
|
492
|
+
const savedDraft = (name) => { const s = cfg.sequences[name] || {}; return { name, colors: [...(s.colors || [])], effects: { ...(s.effects || {}) } }; }; // `Name:` with no value is null
|
|
493
|
+
const draftOf = (name) => drafts.get(name) || savedDraft(name);
|
|
494
|
+
const isDirty = (name) => drafts.has(name) && JSON.stringify(drafts.get(name)) !== JSON.stringify(savedDraft(name));
|
|
495
|
+
|
|
496
|
+
function renameSeq(old, name) {
|
|
497
|
+
if (!name || name === old) return;
|
|
498
|
+
cfg.sequences = Object.fromEntries(Object.entries(cfg.sequences).map(([k, v]) => [k === old ? name : k, v]));
|
|
499
|
+
for (const r of cfg.schedule || []) {
|
|
500
|
+
r.sequences = (r.sequences || []).map((ref) => {
|
|
501
|
+
const [n, secs] = parseRef(ref);
|
|
502
|
+
return n.toLowerCase() === old.toLowerCase() ? (secs ? `${name}:${secs}` : name) : ref;
|
|
503
|
+
});
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
function saveSeq(name) {
|
|
508
|
+
const d = drafts.get(name);
|
|
509
|
+
if (!d) return;
|
|
510
|
+
const to = d.name.trim() || name;
|
|
511
|
+
if (to !== name && own(to)) return renderStatus(`There's already a sequence called ${to}`);
|
|
512
|
+
cfg.sequences[name] = asSpec(d);
|
|
513
|
+
renameSeq(name, to);
|
|
514
|
+
drafts.delete(name);
|
|
515
|
+
if (preview?.name === name) preview.name = to;
|
|
516
|
+
if (pop.name === name) pop.name = to;
|
|
517
|
+
save(true);
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
function previewButton(name, spec, isOwn) {
|
|
521
|
+
const on = preview?.name === name;
|
|
522
|
+
return el("button", { className: "btn", "aria-pressed": String(on), onclick: () => (on ? stopPreview() : startPreview(name, spec, isOwn)) },
|
|
523
|
+
on ? "Stop preview" : "Preview");
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
// an own sequence; redraws itself in place as the draft changes
|
|
527
|
+
function seqEditor(name, { card = false } = {}) {
|
|
528
|
+
const d = draftOf(name);
|
|
529
|
+
const box = el("div", { className: "seq", "data-seq": name });
|
|
530
|
+
const changed = (redraw = true) => {
|
|
531
|
+
drafts.set(name, d);
|
|
532
|
+
if (preview?.name === name && preview.sent !== JSON.stringify(asSpec(d))) startPreview(name, asSpec(d)); // edits show on the lights
|
|
533
|
+
if (redraw) box.replaceWith(seqEditor(name, { card }));
|
|
534
|
+
};
|
|
535
|
+
const fx = Object.entries(d.effects);
|
|
536
|
+
const setFx = () => { d.effects = Object.fromEntries(fx); changed(); };
|
|
537
|
+
const dirty = isDirty(name);
|
|
538
|
+
box.append(...[
|
|
539
|
+
el("div", { className: "seq-top" },
|
|
540
|
+
el("span", { className: "chip", draggable: true, title: "Drag onto a rule", "aria-hidden": "true",
|
|
541
|
+
ondragstart: (e) => { drag = { type: "seq", name }; e.dataTransfer.setData("text/plain", name); closePop(); } },
|
|
542
|
+
el("span", { className: "sw", style: `background:${stripe(d.colors)}` })),
|
|
543
|
+
el("input", { className: "seq-name", type: "text", value: d.name, "aria-label": "Sequence name",
|
|
544
|
+
onchange: (e) => { d.name = e.target.value; changed(); } }),
|
|
545
|
+
),
|
|
546
|
+
el("div", { className: "bulbs" },
|
|
547
|
+
d.colors.map((c, i) => el("span", { className: "swatch" },
|
|
548
|
+
el("input", { type: "color", value: hex(c).toLowerCase(), "aria-label": `${name} color ${i + 1}`,
|
|
549
|
+
oninput: (e) => { d.colors[i] = e.target.value.slice(1).toUpperCase(); box.querySelector(".sw").style.background = stripe(d.colors); },
|
|
550
|
+
onchange: (e) => { d.colors[i] = e.target.value.slice(1).toUpperCase(); changed(); } }),
|
|
551
|
+
d.colors.length > 1 && el("button", { className: "x", "aria-label": `Remove ${name} color ${i + 1}`,
|
|
552
|
+
onclick: () => { d.colors.splice(i, 1); changed(); } }, "×"))),
|
|
553
|
+
el("button", { className: "plus", "aria-label": `Add a color to ${name}`, title: "Add a color",
|
|
554
|
+
onclick: () => { d.colors.push(d.colors.at(-1) || "FFFFFF"); changed(); } }, "+"),
|
|
555
|
+
d.colors.length > 2 && el("button", { className: "plus", "aria-label": `Blend ${name} from its first color to its last`,
|
|
556
|
+
title: "Blend: fill the colors between the first and last evenly", onclick: () => { d.colors = blend(d.colors); changed(); } }, "↔"),
|
|
557
|
+
),
|
|
558
|
+
fx.map(([type, value], i) => el("div", { className: "fx" },
|
|
559
|
+
el("select", { "aria-label": "Effect", onchange: (e) => { fx[i][0] = e.target.value; setFx(); } },
|
|
560
|
+
S.effects.map((t) => el("option", { value: t, selected: t === type }, t))),
|
|
561
|
+
el("input", { type: "range", min: 0, max: 255, value, "aria-label": `${type} strength`,
|
|
562
|
+
oninput: (e) => (e.target.nextSibling.textContent = e.target.value),
|
|
563
|
+
onchange: (e) => { fx[i][1] = +e.target.value; setFx(); } }),
|
|
564
|
+
el("span", { className: "muted" }, String(value)),
|
|
565
|
+
el("button", { className: "x", "aria-label": `Remove ${type}`, onclick: () => { fx.splice(i, 1); setFx(); } }, "×"),
|
|
566
|
+
)),
|
|
567
|
+
el("button", { className: "remove", style: "color:var(--haze)",
|
|
568
|
+
onclick: () => { const t = S.effects.find((t) => !fx.some(([x]) => x === t)); if (t) { fx.push([t, 128]); setFx(); } } }, "Add effect"),
|
|
569
|
+
el("div", { className: "seq-actions" },
|
|
570
|
+
el("button", { className: "btn save", disabled: !dirty, onclick: () => saveSeq(name) }, "Save"),
|
|
571
|
+
dirty && el("button", { className: "btn", onclick: () => { drafts.delete(name); box.replaceWith(seqEditor(name, { card })); } }, "Revert"),
|
|
572
|
+
previewButton(name, asSpec(d), true),
|
|
573
|
+
dirty && el("span", { className: "dirty" }, "Not saved"),
|
|
574
|
+
card && el("button", { className: "btn", style: "margin-left:auto", onclick: () => { delete cfg.sequences[name]; drafts.delete(name); save(true); } }, "Delete"),
|
|
575
|
+
),
|
|
576
|
+
].flat());
|
|
577
|
+
return box;
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
// a bridge sequence: look, preview, or copy into yours
|
|
581
|
+
function bridgeCard(s) {
|
|
582
|
+
const effects = Object.fromEntries((s.effects || []).map((e) => [e.effectType, e.value]));
|
|
583
|
+
return el("div", { className: "seq readonly" },
|
|
584
|
+
el("div", { className: "seq-top" }, el("strong", {}, s.name), el("span", { className: "muted" }, s.builtin ? "built in" : "made in the app")),
|
|
585
|
+
el("div", { className: "bulbs" }, s.colors.map((c) => el("span", { style: `background:${hex(c)}` }))),
|
|
586
|
+
Object.keys(effects).length ? el("div", { className: "muted" }, Object.entries(effects).map(([t, v]) => `${t} ${v}`).join(", ")) : null,
|
|
587
|
+
el("div", { className: "seq-actions" },
|
|
588
|
+
previewButton(s.name, { colors: s.colors, effects: s.effects }, false),
|
|
589
|
+
el("button", { className: "btn", onclick: () => {
|
|
590
|
+
let name = `${s.name} (mine)`, n = 2;
|
|
591
|
+
while (own(name)) name = `${s.name} (mine ${n++})`;
|
|
592
|
+
cfg.sequences[name] = asSpec({ colors: [...s.colors], effects });
|
|
593
|
+
save(true); openPop(name, pop.anchor, true);
|
|
594
|
+
} }, "Copy to mine")),
|
|
595
|
+
);
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
function renderSeqs() {
|
|
599
|
+
$("seqs").replaceChildren(...Object.keys(cfg.sequences).map((name) => seqEditor(name, { card: true })));
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
// chip for the sidebar and bridge list: drag onto a rule, hover or click for its card
|
|
603
|
+
function seqChip(name, colors) {
|
|
604
|
+
return el("span", { className: `chip${preview?.name === name ? " on" : ""}`, draggable: true, tabindex: 0, "data-pop": name,
|
|
605
|
+
ondragstart: (e) => { drag = { type: "seq", name }; e.dataTransfer.setData("text/plain", name); closePop(); } },
|
|
606
|
+
el("span", { className: "sw", style: `background:${stripe(colors)}` }), name);
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
function renderPalette() {
|
|
610
|
+
const q = $("filter").value.toLowerCase(), sq = $("side-filter").value.toLowerCase();
|
|
611
|
+
const bridge = S.bridge_sequences;
|
|
612
|
+
$("palette").replaceChildren(...bridge.filter((s) => s.name.toLowerCase().includes(q)).map((s) => seqChip(s.name, s.colors)));
|
|
613
|
+
$("side").replaceChildren(
|
|
614
|
+
...Object.keys(cfg.sequences).filter((n) => n.toLowerCase().includes(sq)).map((n) => seqChip(n, draftOf(n).colors)),
|
|
615
|
+
...bridge.filter((s) => s.name.toLowerCase().includes(sq)).map((s) => seqChip(s.name, s.colors)));
|
|
616
|
+
$("seq-names").replaceChildren(...[...Object.keys(cfg.sequences), ...bridge.map((s) => s.name)].map((n) => el("option", { value: n })));
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
// --- the hover card: opens on hover (or click / Enter) over any [data-pop] chip; stays while you work in it ---
|
|
620
|
+
const pop = { el: $("pop"), name: null, anchor: null, pinned: false, timer: null };
|
|
621
|
+
function openPop(name, anchor, pinned = false) {
|
|
622
|
+
clearTimeout(pop.timer);
|
|
623
|
+
const mine = own(name), b = !mine && bridgeSeq(name);
|
|
624
|
+
if (!mine && !b) return;
|
|
625
|
+
pop.pinned = pinned || (pop.pinned && pop.name === (mine || name)); // a pinned card unpins when it moves to another sequence
|
|
626
|
+
pop.name = mine || name; pop.anchor = anchor;
|
|
627
|
+
pop.el.replaceChildren(mine ? seqEditor(mine) : bridgeCard(b));
|
|
628
|
+
pop.el.hidden = false;
|
|
629
|
+
if (anchor?.isConnected) {
|
|
630
|
+
const a = anchor.getBoundingClientRect(), w = pop.el.offsetWidth;
|
|
631
|
+
pop.el.style.left = `${Math.max(16, Math.min(a.left, innerWidth - w - 16)) + scrollX}px`;
|
|
632
|
+
pop.el.style.top = `${a.bottom + 6 + scrollY}px`;
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
function closePop(force = false) {
|
|
636
|
+
clearTimeout(pop.timer);
|
|
637
|
+
if (!force && (pop.pinned || pop.el.contains(document.activeElement) || (pop.name && isDirty(pop.name)))) return;
|
|
638
|
+
// an unsaved draft stays (the Sequences view shares it); Revert or Save drop it
|
|
639
|
+
pop.el.hidden = true; pop.name = null; pop.pinned = false;
|
|
640
|
+
}
|
|
641
|
+
const later = (fn, ms) => { clearTimeout(pop.timer); pop.timer = setTimeout(fn, ms); };
|
|
642
|
+
document.addEventListener("mouseover", (e) => {
|
|
643
|
+
const chip = e.target.closest("[data-pop]");
|
|
644
|
+
if (chip && !drag) later(() => openPop(chip.dataset.pop, chip), pop.el.hidden ? 350 : 120);
|
|
645
|
+
else if (pop.el.contains(e.target)) clearTimeout(pop.timer);
|
|
646
|
+
else if (!pop.el.hidden) later(() => closePop(), 300);
|
|
647
|
+
});
|
|
648
|
+
document.addEventListener("click", (e) => {
|
|
649
|
+
const chip = e.target.closest("[data-pop]");
|
|
650
|
+
if (chip && !e.target.closest("input, button")) openPop(chip.dataset.pop, chip, true);
|
|
651
|
+
else if (!pop.el.hidden && !pop.el.contains(e.target) && e.target.isConnected) closePop(true);
|
|
652
|
+
});
|
|
653
|
+
document.addEventListener("keydown", (e) => {
|
|
654
|
+
if (e.key === "Escape" && !pop.el.hidden) { const back = pop.anchor; closePop(true); back?.focus?.(); }
|
|
655
|
+
const chip = e.target.closest?.("[data-pop]");
|
|
656
|
+
if (chip && e.target === chip && (e.key === "Enter" || e.key === " ")) {
|
|
657
|
+
e.preventDefault(); openPop(chip.dataset.pop, chip, true); pop.el.querySelector("input, button")?.focus();
|
|
658
|
+
}
|
|
659
|
+
});
|
|
660
|
+
function refreshPop() {
|
|
661
|
+
if (pop.el.hidden || !pop.name) return;
|
|
662
|
+
const anchor = pop.anchor?.isConnected ? pop.anchor : document.querySelector(`[data-pop="${CSS.escape(pop.name)}"]`);
|
|
663
|
+
openPop(pop.name, anchor);
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
// --- status, saving, applying ---
|
|
667
|
+
function renderStatus(msg) {
|
|
668
|
+
const plan = S.plan || [];
|
|
669
|
+
$("where").textContent = S.host || "";
|
|
670
|
+
$("saved").textContent = msg ?? (S.error ? "" : plan.length ? `${plan.length} change${plan.length === 1 ? "" : "s"} to send to the bridge` : "The bridge matches your schedule");
|
|
671
|
+
$("apply").disabled = !!S.error || !plan.length;
|
|
672
|
+
$("apply").textContent = plan.length ? `Apply ${plan.length}` : "Apply";
|
|
673
|
+
$("review-btn").hidden = !plan.length;
|
|
674
|
+
$("review").replaceChildren(...plan.map((a) => el("li", { className: `op-${a.op}` }, el("b", {}, a.op), `${a.kind === "event" ? "" : "sequence "}${a.name}`, a.detail ? el("span", { className: "muted" }, `: ${a.detail}`) : null)));
|
|
675
|
+
if (!plan.length) { $("review").hidden = true; $("review-btn").setAttribute("aria-expanded", "false"); }
|
|
676
|
+
const problems = S.error ? S.error.replace(/^config problems, nothing applied:\n/, "").split("\n").map((l) => l.replace(/^\s*- /, "")) : [];
|
|
677
|
+
$("problems").hidden = !problems.length;
|
|
678
|
+
$("problems").replaceChildren(...problems.map((p) => el("li", {}, p)));
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
// keep the last good calendar, bridge list and dates while there are problems
|
|
682
|
+
const merge = (state) => {
|
|
683
|
+
for (const k of ["nights", "bridge_sequences", "today", "horizon", "host"]) if (!(k in state)) state[k] = S?.[k];
|
|
684
|
+
state.bridge_sequences ||= [];
|
|
685
|
+
return state;
|
|
686
|
+
};
|
|
687
|
+
function useConfig(c) {
|
|
688
|
+
cfg = c; cfg.sequences ||= {}; cfg.schedule ||= [];
|
|
689
|
+
for (const spec of Object.values(cfg.sequences)) if (spec) spec.colors = (spec.colors || []).map(String);
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
let version = null; // of everlights.yml as last read; a save is refused if the file changed since
|
|
693
|
+
function take(state, msg) {
|
|
694
|
+
S = merge(state);
|
|
695
|
+
if (S.version) version = S.version;
|
|
696
|
+
if (S.config) useConfig(S.config);
|
|
697
|
+
if (!cfg) return renderStatus();
|
|
698
|
+
renderStrings(); renderRules(); renderSeqs(); renderPalette(); renderStatus(msg);
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
let saving = Promise.resolve(), saves = 0;
|
|
702
|
+
function save(structural = false) {
|
|
703
|
+
if (structural) { renderRules(); renderSeqs(); renderPalette(); refreshPop(); }
|
|
704
|
+
$("saved").textContent = "Saving…";
|
|
705
|
+
const body = JSON.stringify(cfg), mine = ++saves;
|
|
706
|
+
saving = saving.then(() => fetch("/api/config", { method: "PUT", headers: { "Content-Type": "application/json", ...(version && { "If-Match": version }) }, body }))
|
|
707
|
+
.then(async (r) => {
|
|
708
|
+
const state = await r.json(), latest = mine === saves;
|
|
709
|
+
if (r.status === 412) { // edited outside the page: show the file; saves queued on the old copy get 412 too
|
|
710
|
+
if (latest) take(state, "everlights.yml changed outside this page, so it was reloaded. Redo your last change.");
|
|
711
|
+
return;
|
|
712
|
+
}
|
|
713
|
+
if (!r.ok) throw new Error(state.error || r.statusText);
|
|
714
|
+
version = state.version;
|
|
715
|
+
if (!latest) return; // a newer save is queued; drawing this answer would undo it on screen
|
|
716
|
+
const focused = document.activeElement;
|
|
717
|
+
const keep = focused && focused !== document.body && !structural;
|
|
718
|
+
S = merge(state);
|
|
719
|
+
if (state.config && !keep) useConfig(state.config); // don't redraw fields under the cursor
|
|
720
|
+
renderStrings(); renderStatus(state.error ? "Saved, with problems to fix" : undefined);
|
|
721
|
+
if (!keep) { renderRules(); renderSeqs(); renderPalette(); refreshPop(); }
|
|
722
|
+
else document.querySelectorAll(".rule").forEach((li, i) => (li.querySelector(".rule-title span").textContent = `${when(cfg.schedule[i])}, ${times(cfg.schedule[i])}`));
|
|
723
|
+
})
|
|
724
|
+
.catch((e) => renderStatus(`Couldn't save: ${e.message}`));
|
|
725
|
+
}
|
|
726
|
+
|
|
727
|
+
// --- preview on the lights; the server clears it if these pings stop (tab closed, laptop asleep) ---
|
|
728
|
+
let preview = null, pinger = null;
|
|
729
|
+
const post = (path, body, opts = {}) => fetch(path, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(body), ...opts })
|
|
730
|
+
.then(async (r) => { const j = await r.json(); if (j.error) throw new Error(j.error); return j; });
|
|
731
|
+
|
|
732
|
+
async function startPreview(name, spec, own = true) {
|
|
733
|
+
try {
|
|
734
|
+
await post("/api/preview", { colors: spec.colors, effects: spec.effects });
|
|
735
|
+
preview = { name, own, sent: JSON.stringify(spec) };
|
|
736
|
+
clearInterval(pinger);
|
|
737
|
+
pinger = setInterval(() => post("/api/preview", { ping: true }).then((j) => { if (!j.previewing) previewEnded(); }).catch(() => {}), 5000);
|
|
738
|
+
renderPreview();
|
|
739
|
+
} catch (e) { renderStatus(`Couldn't preview: ${e.message}`); }
|
|
740
|
+
}
|
|
741
|
+
async function stopPreview() {
|
|
742
|
+
try { await post("/api/preview", { stop: true }); previewEnded(); }
|
|
743
|
+
catch (e) { renderStatus(`Couldn't stop the preview: ${e.message}`); }
|
|
744
|
+
}
|
|
745
|
+
function previewEnded() { preview = null; clearInterval(pinger); renderPreview(); }
|
|
746
|
+
function renderPreview() {
|
|
747
|
+
$("previewing").hidden = !preview;
|
|
748
|
+
$("preview-name").textContent = preview?.name || "";
|
|
749
|
+
renderSeqs(); renderPalette(); refreshPop();
|
|
750
|
+
}
|
|
751
|
+
$("stop-preview").onclick = stopPreview;
|
|
752
|
+
addEventListener("pagehide", () => preview && post("/api/preview", { stop: true }, { keepalive: true }));
|
|
753
|
+
|
|
754
|
+
$("apply").onclick = async () => {
|
|
755
|
+
$("apply").disabled = true; $("apply").textContent = "Applying…";
|
|
756
|
+
await saving; // apply what's on screen, not the file as of an edit ago
|
|
757
|
+
try {
|
|
758
|
+
const r = await fetch("/api/apply", { method: "POST", headers: { "Content-Type": "application/json" }, body: "{}" }), state = await r.json();
|
|
759
|
+
if (!r.ok) throw new Error(state.error || r.statusText);
|
|
760
|
+
take(state, state.error ? undefined : `Applied ${state.applied} change${state.applied === 1 ? "" : "s"}`);
|
|
761
|
+
} catch (e) {
|
|
762
|
+
// part of the plan may have gone through before the error: show where things stand now
|
|
763
|
+
const msg = `Couldn't apply: ${e.message}`, state = await fetch("/api/state").then((r) => r.json()).catch(() => null);
|
|
764
|
+
state?.config ? take(state, msg) : renderStatus(msg);
|
|
765
|
+
}
|
|
766
|
+
};
|
|
767
|
+
$("review-btn").onclick = (e) => {
|
|
768
|
+
const show = $("review").hidden;
|
|
769
|
+
$("review").hidden = !show; e.target.setAttribute("aria-expanded", String(show));
|
|
770
|
+
};
|
|
771
|
+
$("new-rule").onclick = () => {
|
|
772
|
+
let name = "New rule", n = 2;
|
|
773
|
+
while (cfg.schedule.some((r) => r.name === name)) name = `New rule ${n++}`;
|
|
774
|
+
cfg.schedule.unshift({ name, on: "Dec 25", sequences: [] });
|
|
775
|
+
open.add(name); save(true);
|
|
776
|
+
};
|
|
777
|
+
$("new-seq").onclick = () => {
|
|
778
|
+
let name = "New sequence", n = 2;
|
|
779
|
+
while (cfg.sequences[name]) name = `New sequence ${n++}`;
|
|
780
|
+
cfg.sequences[name] = { colors: ["FFB547"] };
|
|
781
|
+
save(true);
|
|
782
|
+
requestAnimationFrame(() => document.querySelector(`#seqs [data-seq="${CSS.escape(name)}"] .seq-name`)?.select());
|
|
783
|
+
};
|
|
784
|
+
$("filter").oninput = $("side-filter").oninput = renderPalette;
|
|
785
|
+
|
|
786
|
+
// --- bridge info ---
|
|
787
|
+
const ago = (iso) => {
|
|
788
|
+
if (!iso || iso.startsWith("1970")) return "never";
|
|
789
|
+
const m = Math.round((Date.now() - new Date(iso)) / 60000);
|
|
790
|
+
return m < 1 ? "just now" : m < 120 ? `${m} min ago` : m < 2880 ? `${Math.round(m / 60)} h ago` : `${Math.round(m / 1440)} days ago`;
|
|
791
|
+
};
|
|
792
|
+
const yes = (v, y = "yes", n = "no") => (v ? y : n);
|
|
793
|
+
function panel(title, rows) {
|
|
794
|
+
return el("section", { className: "panel" }, el("h3", {}, title),
|
|
795
|
+
el("dl", {}, rows.filter(([, v]) => v != null && v !== "").flatMap(([k, v]) => [el("dt", {}, k), el("dd", {}, String(v))])));
|
|
796
|
+
}
|
|
797
|
+
async function loadBridge() {
|
|
798
|
+
$("bridge-at").textContent = "Reading the bridge…";
|
|
799
|
+
try {
|
|
800
|
+
const r = await fetch("/api/bridge"), j = await r.json();
|
|
801
|
+
if (j.error) throw new Error(j.error);
|
|
802
|
+
const b = j.status, net = b.network || {};
|
|
803
|
+
$("checks").replaceChildren(...j.checks.map((c) => el("li", { className: c.ok === true ? "ok" : c.ok === false ? "warn" : "info" },
|
|
804
|
+
el("span", {}, c.name), el("span", {}, c.text))));
|
|
805
|
+
$("panels").replaceChildren(
|
|
806
|
+
panel(`Bridge ${b.serial}`, [
|
|
807
|
+
["Firmware", `${b.firmwareVersion}, radio ${b.radioFirmwareVersion}`],
|
|
808
|
+
["Network", `${net.interface || "?"}, ${b.localIp}${b.ssid ? `, ${b.ssid}` : ""}`],
|
|
809
|
+
["Clock", `${yes(net.ntp, "NTP on", "NTP off")}${b.timeOffset ? `, offset ${b.timeOffset}` : ""}`],
|
|
810
|
+
["Cloud", `${yes(b.serverReachable, "reachable", "unreachable")}, checked in ${ago(b.lastServerCheckin)}`],
|
|
811
|
+
["Memory", b.minFreeHeap != null && `lowest free since boot ${b.minFreeHeap.toLocaleString()} bytes`],
|
|
812
|
+
]),
|
|
813
|
+
panel("Reboots and links", [
|
|
814
|
+
["Reboots", b.rebootCount],
|
|
815
|
+
["Last reset", b.resetReason != null && `reason ${b.resetReason}`],
|
|
816
|
+
["Supervisor", b.supervisorRebootCount != null && `${b.supervisorRebootCount} reboots`],
|
|
817
|
+
["Radio", `${b.radioRebootCount} reboots, ${b.radioLoopsPerSecond ?? "?"} loops/s, answered ${ago(b.lastRadioResponse)}`],
|
|
818
|
+
["Wi-Fi retries", b.wifiReconnectAttempts],
|
|
819
|
+
["MQTT", b.mqttOutageMs != null && `${b.mqttOutageMs} ms outage, last ack ${Math.round((b.mqttBrokerAckAgeMs || 0) / 1000)} s ago`],
|
|
820
|
+
]),
|
|
821
|
+
...(b.zones || []).map((z) => panel(`Zone ${z.serial}`, [
|
|
822
|
+
["State", yes(z.active, "active", "inactive")],
|
|
823
|
+
["Lights", `${z.configuredLength} pixels, ${z.colorOrder}`],
|
|
824
|
+
["Firmware", `${z.firmwareVersion}, hardware ${z.hardwareVersion}`],
|
|
825
|
+
["Signal", `RSSI ${z.rssi}, SNR ${z.snr}`],
|
|
826
|
+
["Sensors", `temperature ${z.temperature}, voltage ${z.voltage}, current ${z.current} (raw)`],
|
|
827
|
+
["Reboots", z.rebootCount],
|
|
828
|
+
["Last answer", ago(z.lastResponseDate)],
|
|
829
|
+
])),
|
|
830
|
+
);
|
|
831
|
+
$("raw").textContent = JSON.stringify(b, null, 2);
|
|
832
|
+
$("bridge-at").textContent = `Read ${new Date().toLocaleTimeString([], { hour: "numeric", minute: "2-digit" })}`;
|
|
833
|
+
} catch (e) { $("bridge-at").textContent = `Couldn't read the bridge: ${e.message}`; }
|
|
834
|
+
}
|
|
835
|
+
async function bridgeAction(action, done) {
|
|
836
|
+
try { await post("/api/bridge", { action }); $("bridge-at").textContent = done; }
|
|
837
|
+
catch (e) { $("bridge-at").textContent = `Couldn't ${action === "time" ? "set the clock" : "reboot"}: ${e.message}`; }
|
|
838
|
+
}
|
|
839
|
+
$("bridge-refresh").onclick = loadBridge;
|
|
840
|
+
$("bridge-time").onclick = () => bridgeAction("time", "Bridge clock set to now").then(loadBridge);
|
|
841
|
+
// two steps instead of a confirm() dialog
|
|
842
|
+
$("bridge-reboot").onclick = (e) => {
|
|
843
|
+
const btn = e.currentTarget;
|
|
844
|
+
if (btn.dataset.armed) {
|
|
845
|
+
delete btn.dataset.armed; btn.textContent = "Reboot";
|
|
846
|
+
bridgeAction("reboot", "Rebooting; the lights may blink. Refresh in a minute.");
|
|
847
|
+
} else {
|
|
848
|
+
btn.dataset.armed = "1"; btn.textContent = "Reboot now? Click again";
|
|
849
|
+
setTimeout(() => { if (btn.dataset.armed) { delete btn.dataset.armed; btn.textContent = "Reboot"; } }, 5000);
|
|
850
|
+
}
|
|
851
|
+
};
|
|
852
|
+
|
|
853
|
+
// views: #calendar (default), #sequences, #bridge
|
|
854
|
+
function showView() {
|
|
855
|
+
const view = ["#sequences", "#bridge"].includes(location.hash) ? location.hash.slice(1) : "calendar";
|
|
856
|
+
document.body.dataset.view = view;
|
|
857
|
+
if (view === "bridge") loadBridge();
|
|
858
|
+
for (const v of ["calendar", "sequences", "bridge"]) $(`tab-${v}`).removeAttribute("aria-current");
|
|
859
|
+
$(`tab-${view}`).setAttribute("aria-current", "page");
|
|
860
|
+
closePop(true);
|
|
861
|
+
}
|
|
862
|
+
addEventListener("hashchange", showView);
|
|
863
|
+
showView();
|
|
864
|
+
document.addEventListener("dragend", () => { drag = null; document.querySelectorAll(".over,.drop-before,.drop-after").forEach((n) => n.classList.remove("over", "drop-before", "drop-after")); });
|
|
865
|
+
|
|
866
|
+
fetch("/api/state").then((r) => r.json()).then((s) => take(s)).catch((e) => ($("saved").textContent = `Couldn't load: ${e.message}`));
|
|
867
|
+
</script>
|
|
868
|
+
</body>
|
|
869
|
+
</html>
|