-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add resolution option, give elements but paper row an action handler
- Loading branch information
Showing
6 changed files
with
97 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/** secondsToDuration is adapted from the custom-card-helpers library, | ||
which in turn was adapted from Home Assistant. | ||
https://github.com/home-assistant/frontend/blob/dev/src/common/datetime/seconds_to_duration.ts | ||
I've added the configurable resolution. | ||
*/ | ||
|
||
type Resolution = "seconds"|"minutes"|"automatic"; | ||
|
||
const leftPad = (num: number) => (num < 10 ? `0${num}` : num); | ||
|
||
/** Returns "minutes" if seconds>=1hr, otherwise minutes. */ | ||
function automaticRes(seconds: number) { | ||
if (seconds >= 3600) return "minutes" | ||
return "seconds" | ||
} | ||
|
||
/** Returns the h:m:s components as numbers. */ | ||
function toHMS(d: number, resolution: Resolution) { | ||
switch(resolution === "automatic" ? automaticRes(d) : resolution) { | ||
case "seconds": { | ||
const h = Math.floor(d / 3600); | ||
const m = Math.floor((d % 3600) / 60); | ||
const s = Math.floor((d % 3600) % 60); | ||
return [h,m,s]; | ||
} | ||
case "minutes": { | ||
const h = Math.floor(d / 3600); | ||
const m = Math.floor((d % 3600) / 60); | ||
return [0,h,m]; | ||
} | ||
} | ||
} | ||
|
||
/** Convert some number of seconds into a duration string. */ | ||
export default function secondsToDuration(d: number, resolution: Resolution) { | ||
const [h, m, s] = toHMS(d, resolution) | ||
|
||
if (h > 0) { | ||
return `${h}:${leftPad(m)}:${leftPad(s)}`; | ||
} | ||
if (m > 0) { | ||
return `${m}:${leftPad(s)}`; | ||
} | ||
if (s > 0) { | ||
return "" + s; | ||
} | ||
return null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters