Repository navigation
Expand file tree
/
Copy pathModel.js
More file actions
582 lines (534 loc) · 21.8 KB
/
Copy pathModel.js
File metadata and controls
582 lines (534 loc) · 21.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
// Parsing/formatting helpers for notch's "-json" output (status, tasks,
// summary, entries, projects, clients), kept out of BarWidget.qml the same
// way every first-party panel (e.g. shell/plugins/panels/weather/Model.js)
// keeps its parsing separate from layout. The parsers fail closed to a safe default
// rather than throwing, since they run on whatever a Process happened to
// print (an unbuilt/old notch binary, a transient empty stdout on process
// spawn, etc).
.pragma library
function parseStatus(raw) {
var fallback = {
text: "", tooltip: "No timer running", class: "idle",
task: "", notes: "", startTime: "", projectId: 0, projectName: "", clientName: "", entryId: 0
}
if (!raw) return fallback
try {
var parsed = JSON.parse(raw)
return {
text: typeof parsed.text === "string" ? parsed.text : "",
tooltip: typeof parsed.tooltip === "string" ? parsed.tooltip : fallback.tooltip,
class: typeof parsed.class === "string" ? parsed.class : "idle",
task: typeof parsed.task === "string" ? parsed.task : "",
notes: typeof parsed.notes === "string" ? parsed.notes : "",
startTime: typeof parsed.startTime === "string" ? parsed.startTime : "",
projectId: typeof parsed.projectId === "number" ? parsed.projectId : 0,
projectName: typeof parsed.projectName === "string" ? parsed.projectName : "",
clientName: typeof parsed.clientName === "string" ? parsed.clientName : "",
entryId: typeof parsed.entryId === "number" ? parsed.entryId : 0
}
} catch (e) {
return fallback
}
}
// "notch projects -json" -- every project, for the "add a timer" picker
// (which needs to offer projects that have no recent-task history yet,
// unlike the recency-ordered list "notch tasks -json" drives).
function parseProjects(raw) {
if (!raw) return []
try {
var parsed = JSON.parse(raw)
if (!Array.isArray(parsed)) return []
return parsed.filter(function(p) {
return p && typeof p.id === "number" && typeof p.name === "string"
})
} catch (e) {
return []
}
}
// "notch clients -json" -- for the Add Timer form's client picker, which
// narrows the project picker to that client's own projects (see notch's
// TUI: task, then client, then project).
function parseClients(raw) {
if (!raw) return []
try {
var parsed = JSON.parse(raw)
if (!Array.isArray(parsed)) return []
return parsed.filter(function(c) {
return c && typeof c.id === "number" && typeof c.name === "string"
})
} catch (e) {
return []
}
}
function parseTasks(raw) {
if (!raw) return []
try {
var parsed = JSON.parse(raw)
if (!Array.isArray(parsed)) return []
return parsed.filter(function(t) {
return t && typeof t.projectId === "number" && typeof t.projectName === "string"
})
} catch (e) {
return []
}
}
// "notch summary -json" -- today's and this week's finished-entry totals.
// null (not zeros) when unavailable, e.g. a notch older than the summary
// command: that falls through to launching the TUI, which fails with no
// TTY and prints nothing on stdout, and the popout hides the stats strip
// rather than showing a misleading 0:00:00. v0.11.0 adds money
// (earnedToday, earnedWeek, unbilled, outstanding); hasMoney says whether
// this notch sent it, so the unbilled tile hides on v0.10.
function parseSummary(raw) {
if (!raw) return null
try {
var parsed = JSON.parse(raw)
if (!parsed || typeof parsed.todaySeconds !== "number" || typeof parsed.weekSeconds !== "number") return null
var num = function(v) { return typeof v === "number" && isFinite(v) ? v : 0 }
return {
todaySeconds: parsed.todaySeconds,
weekSeconds: parsed.weekSeconds,
hasMoney: typeof parsed.unbilled === "number",
earnedToday: num(parsed.earnedToday),
earnedWeek: num(parsed.earnedWeek),
unbilled: num(parsed.unbilled),
outstanding: num(parsed.outstanding)
}
} catch (e) {
return null
}
}
// ---------- Version gating ----------
//
// An unknown subcommand on an old notch falls through to the TUI, which
// exits 1 with no TTY and prints nothing. So the panel asks "notch version
// -json" once (v0.9.0 and later answer it) and hides what this notch can't
// do, instead of running a command that fails.
// "notch version -json" ({"version":"0.11.0",...}) or the plain line
// ("notch v0.11.0 (...) linux/amd64"). Returns {major, minor, patch, dev}
// or null when it can't tell (no output: a notch older than v0.9.0, or no
// notch at all). A local build reports "dev" and is treated as the newest.
function parseVersion(raw) {
var text = String(raw || "").trim()
if (text === "") return null
var v = text
try {
var parsed = JSON.parse(text)
if (parsed && typeof parsed.version === "string") v = parsed.version
else return null
} catch (e) {
var m = text.match(/^notch\s+(\S+)/)
if (!m) return null
v = m[1]
}
if (v === "dev") return { major: 0, minor: 0, patch: 0, dev: true }
var mv = String(v).match(/^v?(\d+)\.(\d+)\.(\d+)/)
if (!mv) return null
return { major: parseInt(mv[1], 10), minor: parseInt(mv[2], 10), patch: parseInt(mv[3], 10), dev: false }
}
// Whether version (from parseVersion) is at least "x.y.z". A dev build is;
// an unknown one (null) isn't.
function versionAtLeast(version, min) {
if (!version) return false
if (version.dev) return true
var m = String(min).match(/^v?(\d+)\.(\d+)\.(\d+)/)
if (!m) return false
var want = [parseInt(m[1], 10), parseInt(m[2], 10), parseInt(m[3], 10)]
var have = [version.major, version.minor, version.patch]
for (var i = 0; i < 3; i++) {
if (have[i] !== want[i]) return have[i] > want[i]
}
return true
}
// What the panel may call on this notch. Every flag names the CLI it
// unlocks, so a tab body can gate a control on exactly what it runs.
// v0.10.0: summary, entries --project/--task, delete, edit --from-*
// and --start/--end on an entry.
// v0.11.0: money in summary and clients/projects -json, report,
// entries --from/--to, clients/projects add/edit/delete,
// edit --invoiced/--paid, start --start/--end and @placement.
function features(version) {
var v10 = versionAtLeast(version, "0.10.0")
var v11 = versionAtLeast(version, "0.11.0")
return {
summary: v10,
sessions: v10,
delete: v10,
editTask: v10,
money: v11,
report: v11,
entriesRange: v11,
catalog: v11,
billing: v11,
logPast: v11,
placement: v11
}
}
// ---------- Money ----------
// A client's hourly rate from "notch clients -json" (v0.11.0), matched by
// name the way status reports it (client names are unique). 0 when the
// client isn't there or has no rate.
function clientRate(clients, clientName) {
var name = String(clientName || "")
if (!name || !clients) return 0
for (var i = 0; i < clients.length; i++) {
var c = clients[i]
if (c && c.name === name) return typeof c.hourlyRate === "number" && isFinite(c.hourlyRate) ? c.hourlyRate : 0
}
return 0
}
// Seconds at an hourly rate, rounded to cents once, as notch does.
function earnedFor(seconds, rate) {
var s = Number(seconds) || 0
var r = Number(rate) || 0
if (s <= 0 || r <= 0) return 0
return Math.round(s / 3600 * r * 100) / 100
}
// "$1,057.50". notch has no currency setting; the TUI writes dollars too.
function formatMoney(amount) {
var n = Number(amount)
if (!isFinite(n)) n = 0
var neg = n < 0
var cents = Math.round(Math.abs(n) * 100)
var whole = Math.floor(cents / 100)
var frac = cents % 100
var digits = String(whole).replace(/\B(?=(\d{3})+(?!\d))/g, ",")
return (neg ? "-$" : "$") + digits + "." + (frac < 10 ? "0" : "") + frac
}
// ---------- Colour ----------
// The theme's green (colors.toml's "green" or "color2"), for the running
// state. The shell's palette has no green, so the panel reads the theme
// file itself. "" when there's none.
function parseThemeGreen(raw) {
var lines = String(raw || "").split("\n")
var named = {}
for (var i = 0; i < lines.length; i++) {
var m = lines[i].match(/^\s*([A-Za-z0-9_-]+)\s*=\s*["']?(#[0-9A-Fa-f]{6})/)
if (m) named[m[1]] = m[2]
}
return named.green || named.color2 || ""
}
function _channels(c) {
if (typeof c === "string") {
var m = c.match(/^#?([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})$/)
if (!m) return null
return { r: parseInt(m[1], 16) / 255, g: parseInt(m[2], 16) / 255, b: parseInt(m[3], 16) / 255 }
}
if (c && typeof c.r === "number") return { r: c.r, g: c.g, b: c.b }
return null
}
function _lin(v) {
return v <= 0.04045 ? v / 12.92 : Math.pow((v + 0.055) / 1.055, 2.4)
}
// WCAG 2.x relative luminance of "#rrggbb" or a QML color ({r,g,b} 0..1).
function luminance(c) {
var ch = _channels(c)
if (!ch) return 0
return 0.2126 * _lin(ch.r) + 0.7152 * _lin(ch.g) + 0.0722 * _lin(ch.b)
}
function contrastRatio(a, b) {
var la = luminance(a)
var lb = luminance(b)
var hi = Math.max(la, lb)
var lo = Math.min(la, lb)
return (hi + 0.05) / (lo + 0.05)
}
// fg at alpha over an opaque bg, as {r,g,b} 0..1 -- what a translucent
// fill or ring actually shows, for measuring its contrast. Rounded to 8
// bits a channel, as the screen draws it (#D4E2E4 at 0.55 over #0A0C10 is
// #798285, the ring on the design boards).
function over(fg, alpha, bg) {
var f = _channels(fg)
var b = _channels(bg)
if (!f || !b) return null
var a = Math.max(0, Math.min(1, Number(alpha)))
var mix = function(x, y) { return Math.round((x * a + y * (1 - a)) * 255) / 255 }
return { r: mix(f.r, b.r), g: mix(f.g, b.g), b: mix(f.b, b.b) }
}
// The first candidate colour that reaches min contrast on bg, else the
// last one (the caller's safe fallback, normally the foreground). Keeps a
// theme whose green or accent is too faint from drawing unreadable text.
function readable(candidates, bg, min) {
for (var i = 0; i < candidates.length; i++) {
var c = candidates[i]
if (_channels(c) && contrastRatio(c, bg) >= min) return c
}
return candidates[candidates.length - 1]
}
// Quick-add's live filter over the recent-timers list: a case-insensitive
// substring match on task, project, or client, so typing a name you've
// used before surfaces the existing row to restart instead of creating a
// duplicate on Uncategorized.
function filterTasks(tasks, query) {
var q = String(query || "").trim().toLowerCase()
if (q === "") return tasks
return tasks.filter(function(t) {
return (t.task || "").toLowerCase().indexOf(q) !== -1
|| (t.projectName || "").toLowerCase().indexOf(q) !== -1
|| (t.clientName || "").toLowerCase().indexOf(q) !== -1
})
}
// Local midnight today / on this week's Monday, in ms -- the same
// boundaries notch's status.StartOfDay/StartOfWeek use, so the running
// entry's live contribution is clipped exactly where the finished-entry
// totals from "notch summary" are.
function startOfDayMs(nowMs) {
var d = new Date(nowMs)
d.setHours(0, 0, 0, 0)
return d.getTime()
}
// Midnight of the calendar day after dayStartMs. By date, not +24h, so a
// DST change that night doesn't shift it an hour.
function nextDayStartMs(dayStartMs) {
var d = new Date(dayStartMs)
d.setDate(d.getDate() + 1)
d.setHours(0, 0, 0, 0)
return d.getTime()
}
function startOfWeekMs(nowMs) {
var d = new Date(startOfDayMs(nowMs))
var weekday = d.getDay() === 0 ? 7 : d.getDay()
d.setDate(d.getDate() - (weekday - 1))
return d.getTime()
}
// Identity of one recent-timers row: notch groups entries by exact
// (project, task), so that pair is the key everywhere -- pinning, stable
// ordering, and which row is expanded. NUL can't appear in either part.
function taskKey(t) {
return t.projectId + "\u0000" + t.task
}
// "notch entries -json" -- one task's sessions, newest first.
function parseEntries(raw) {
if (!raw) return []
try {
var parsed = JSON.parse(raw)
if (!Array.isArray(parsed)) return []
return parsed.filter(function(e) {
return e && typeof e.id === "number" && typeof e.startTime === "string"
})
} catch (e) {
return []
}
}
var _weekdays = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
var _months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
function _clock(d) {
var h = d.getHours() % 12 || 12
var m = d.getMinutes()
return h + ":" + (m < 10 ? "0" : "") + m
}
function _meridiem(d) {
return d.getHours() < 12 ? "AM" : "PM"
}
// A session's "when": "Today 9:00–9:40 AM", "Yesterday 4:50 PM–12:10 AM",
// "Mon Sep 22 9:00–9:40 AM", or "Today 9:00 AM–now" while running. The
// meridiem is written once when both ends share it.
function formatSessionLabel(startIso, endIso, nowMs) {
var start = new Date(startIso)
if (isNaN(start.getTime())) return ""
var dayMs = startOfDayMs(start.getTime())
var todayMs = startOfDayMs(nowMs)
var day
if (dayMs === todayMs) day = "Today"
else if (dayMs === startOfDayMs(todayMs - 1)) day = "Yesterday"
else day = _weekdays[start.getDay()] + " " + _months[start.getMonth()] + " " + start.getDate()
if (!endIso) return day + " " + _clock(start) + " " + _meridiem(start) + "–now"
var end = new Date(endIso)
if (isNaN(end.getTime())) return day + " " + _clock(start) + " " + _meridiem(start)
if (_meridiem(start) === _meridiem(end))
return day + " " + _clock(start) + "–" + _clock(end) + " " + _meridiem(end)
return day + " " + _clock(start) + " " + _meridiem(start) + "–" + _clock(end) + " " + _meridiem(end)
}
// A session's start or end as typed into the edit form: "9", "9:05",
// "905", "9:05pm", "9:05 PM", "21:05". Returns ms on the local day that
// dayStartMs begins, or NaN if unreadable. Without am/pm, an hour of 1-11
// keeps the AM/PM of meridiemFromMs (the time the field was pre-filled
// with), so retyping "2:00 PM" as "2:15" stays in the afternoon; with no
// meridiemFromMs it's taken as written ("9" is 9 AM). A bare 12 is noon,
// unless the field was pre-filled with a 12 AM time (12:40 AM retyped as
// "12:30" stays just after midnight). 0 and 13-23 are always 24-hour.
function parseClockTime(text, dayStartMs, meridiemFromMs) {
var m = String(text || "").trim().toLowerCase().match(/^(\d{1,2})(?::?(\d{2}))?\s*(a|am|p|pm)?$/)
if (!m) return NaN
var h = parseInt(m[1], 10)
var min = m[2] ? parseInt(m[2], 10) : 0
var ampm = m[3] ? m[3][0] : ""
if (min > 59) return NaN
if (ampm) {
if (h < 1 || h > 12) return NaN
if (h === 12) h = 0
if (ampm === "p") h += 12
} else if (h > 23) {
return NaN
} else if (meridiemFromMs !== undefined && !isNaN(meridiemFromMs)) {
var hintHour = new Date(meridiemFromMs).getHours()
if (h >= 1 && h <= 11 && hintHour >= 12) h += 12
else if (h === 12 && hintHour === 0) h = 0
}
var d = new Date(dayStartMs)
d.setHours(h, min, 0, 0)
return d.getTime()
}
// "9:05 AM" -- how the edit form pre-fills a session's times.
function formatClockTime(ms) {
var d = new Date(ms)
return _clock(d) + " " + _meridiem(d)
}
// RFC3339 without milliseconds, for "notch edit --start/--end".
function toRfc3339(ms) {
return new Date(ms).toISOString().replace(/\.\d{3}Z$/, "Z")
}
// A task row's billing summary from "tasks -json"'s counts: "" when none
// is invoiced, "paid"/"invoiced" when all are, else "2/3 invoiced" (paid
// counts as invoiced -- notch only allows paid on an invoiced entry).
function billingLabel(t) {
var n = t.entryCount || 0
var inv = t.invoicedCount || 0
var paid = t.paidCount || 0
if (n === 0 || inv === 0) return ""
if (paid === n) return "paid"
if (inv === n) return "invoiced"
return inv + "/" + n + " invoiced"
}
// Re-applies a fresh "notch tasks -json" response onto the currently
// displayed row order instead of just replacing it outright. The server
// always sorts by most-recent-use, so a plain replace reshuffles the whole
// list -- including rows that didn't change at all -- every time a
// start/stop refresh lands while the popout is open (starting or stopping
// a task changes exactly its own recency, which is enough to move it,
// which shifts every row after it). This keeps each row's position fixed
// once shown, refreshing only its data in place; rows the server dropped
// (aged out of its own limit) are simply left off, and any row the server
// reports that wasn't already on screen is appended at the end. Call this
// for refreshes that happen *while already open* -- use the raw parsed
// list instead (a real re-sort) when the popout freshly opens, so it's
// still accurate whenever you deliberately go look at it.
function reorderTasksInPlace(previous, incoming) {
var key = taskKey
var incomingByKey = {}
for (var i = 0; i < incoming.length; i++) incomingByKey[key(incoming[i])] = incoming[i]
var seen = {}
var merged = []
for (var j = 0; j < previous.length; j++) {
var k = key(previous[j])
if (incomingByKey.hasOwnProperty(k)) {
merged.push(incomingByKey[k])
seen[k] = true
}
}
for (var m = 0; m < incoming.length; m++) {
var k2 = key(incoming[m])
if (!seen[k2]) merged.push(incoming[m])
}
return merged
}
// After a whole-task edit (rename and/or move to another client/project)
// the row comes back from "notch tasks" under a different taskKey, which
// reorderTasksInPlace would treat as a brand-new row and append at the
// bottom. This swaps the edited row's old entry in `previous` for its new
// one in `incoming`, so the stable merge keeps it where it was. The new
// row is the incoming one that wasn't already shown, with the new task
// name, in the same project when the task wasn't moved, or under the
// project (and client, if one was given) it was moved to. If there's none
// (the edit merged into a task already on screen) the old row is just
// dropped and the merged-into row keeps its place.
// `rename` is {oldKey, newTask, projectId, projectName, clientName}.
// Returns {previous, newKey}; newKey is "" when there's nothing to follow.
function applyTaskRename(previous, incoming, rename) {
var shown = {}
for (var i = 0; i < previous.length; i++) shown[taskKey(previous[i])] = true
var same = function(a, b) { return String(a || "").toLowerCase() === String(b || "").toLowerCase() }
var landed = function(t) {
if (rename.projectId > 0) return t.projectId === rename.projectId
return same(t.projectName, rename.projectName)
&& (rename.clientName === "" || same(t.clientName, rename.clientName))
}
var replacement = null
for (var j = 0; j < incoming.length; j++) {
var t = incoming[j]
if (t.task === rename.newTask && !shown[taskKey(t)] && landed(t)) {
replacement = t
break
}
}
var out = []
for (var k = 0; k < previous.length; k++) {
if (taskKey(previous[k]) === rename.oldKey) {
if (replacement) out.push(replacement)
} else {
out.push(previous[k])
}
}
return { previous: out, newKey: replacement ? taskKey(replacement) : "" }
}
// "H:MM:SS", matching notch's own model.FormatDuration so the bar/popout
// never disagrees with the TUI's own formatting.
function formatSeconds(totalSeconds) {
var s = Math.max(0, Math.floor(Number(totalSeconds) || 0))
var h = Math.floor(s / 3600)
var m = Math.floor((s % 3600) / 60)
var sec = s % 60
return h + ":" + (m < 10 ? "0" : "") + m + ":" + (sec < 10 ? "0" : "") + sec
}
// The least alpha, from `from` up in steps of 0.05, at which fg over bg
// reaches min contrast against bg; 1 if none does. Keeps a translucent
// line (a control border, the focus ring) at the shell's alpha where that
// passes, and strengthens it on a theme where it wouldn't.
function alphaFor(fg, bg, min, from) {
var a = Math.max(0, Math.min(1, Number(from) || 0))
for (; a < 1; a = Math.round((a + 0.05) * 100) / 100) {
var mixed = over(fg, a, bg)
if (mixed && contrastRatio(mixed, bg) >= min) return a
}
return 1
}
// The focus ring's alpha: the lowest, from `from` up in 0.05 steps, at
// which the ring (ring at alpha over bg) reaches min contrast against bg
// and against every fill a focused control can sit on. fills is a list of
// {color, alpha}, each drawn over bg (the hover, selected, pressed and
// primary fills). Where no alpha clears them all, 1: the strongest ring,
// and the gap ring in the background colour that NotchButton draws inside
// it keeps the ring itself against the background (3:1 from alphaFor).
function focusAlphaFor(ring, bg, fills, min, from) {
var a = Math.max(0, Math.min(1, Number(from) || 0))
var under = (fills || []).map(function(f) { return over(f.color, f.alpha, bg) })
for (;; a = Math.min(1, Math.round((a + 0.05) * 100) / 100)) {
var mixed = over(ring, a, bg)
var ok = mixed && contrastRatio(mixed, bg) >= min
for (var i = 0; ok && i < under.length; i++)
ok = under[i] !== null && contrastRatio(mixed, under[i]) >= min
if (ok || a >= 1) return a
}
}
// ---------- Focus order ----------
// Tab and Shift+Tab through the panel's controls. `available` is one flag
// per focus stop, in visual order; `current` is the stop holding focus, or
// -1 for none (focus on the panel itself). Returns the next available stop
// in `direction` (1 or -1), or -1 past the last (or before the first): the
// shell then hands Tab on to the bar's next panel, as it did before the
// panel had controls on the Tab path. From none, Tab goes to the first
// stop and Shift+Tab to the last.
function nextFocusStop(available, current, direction) {
var n = available ? available.length : 0
var step = direction < 0 ? -1 : 1
var i = current < 0 || current >= n ? (step > 0 ? -1 : n) : current
for (i += step; i >= 0 && i < n; i += step) {
if (available[i]) return i
}
return -1
}
// ---------- Tabs ----------
// The panel's tabs, in order, with the number key that opens each. The
// shell draws the tab row from this; each id has a matching *Tab.qml.
var tabs = [
{ id: "timers", label: "Timers", key: "1" },
{ id: "clients", label: "Clients", key: "2" },
{ id: "reports", label: "Reports", key: "3" }
]
// Index into tabs for a typed key ("1", "2", "3"), or -1.
function tabIndexForKey(text) {
for (var i = 0; i < tabs.length; i++) {
if (tabs[i].key === text) return i
}
return -1
}