forget-cancellare-vecchi-toot/components/Banner.html

134 lines
3.0 KiB
HTML
Raw Normal View History

2018-04-19 16:18:31 +02:00
<div class='banner {policy_enabled?'enabled':'disabled'}'>
2017-08-31 18:59:09 +02:00
<div class='btn-group right'>
2019-05-02 00:47:44 +02:00
<button class='btn {policy_enabled?"secondary":"primary"}' on:click={toggle}>{policy_enabled?'Disable':'Enable'}</button>
2017-08-31 18:59:09 +02:00
</div>
<div>
2017-08-31 21:56:17 +02:00
Forget is currently
2018-04-19 16:18:31 +02:00
{#if policy_enabled }
2017-08-31 21:56:17 +02:00
<b>enabled</b>
2018-04-19 16:18:31 +02:00
{:else}
2017-08-31 21:56:17 +02:00
disabled
2018-04-19 16:18:31 +02:00
{/if}
2017-08-31 21:56:17 +02:00
on your account.
2017-08-31 18:59:09 +02:00
</div>
<div class='timers'>
2019-05-02 00:47:44 +02:00
<span class='last-delete' class:hidden={!last_delete} title={last_delete}>
2018-04-19 16:18:31 +02:00
{#if last_delete }
Last delete {rel_past(now - last_delete)}.
{/if }
2017-08-31 21:56:17 +02:00
</span>
2019-05-02 00:47:44 +02:00
<span class='next-delete'
class:hidden={!policy_enabled || !next_delete || !eligible_for_delete_estimate} title={next_delete}>
2018-04-19 16:18:31 +02:00
Next delete {rel_future(next_delete - now)}.
2017-08-31 18:59:09 +02:00
</span>
</div>
</div>
<style>
.timers {
font-size: 0.8em;
}
.timers > * {
transition-property: opacity, transform;
transition-duration: 0.4s;
2017-08-31 21:56:17 +02:00
display: inline-block;
2017-08-31 18:59:09 +02:00
}
.timers > .hidden {
opacity: 0;
2017-08-31 21:56:17 +02:00
transform: translateY(-0.3em);
2017-08-31 18:59:09 +02:00
pointer-events: none;
}
.banner {
transition: background-color 0.6s;
}
</style>
<script>
function absmod(n, x){
2019-05-02 00:47:44 +02:00
// it's like modulo but never negative
2017-08-31 18:59:09 +02:00
n = n % x;
if(n < 0){
n += x
}
return n
}
function s(n){
// utility for plurals
if(n > 1){
return 's';
}
return '';
}
function rel(millis){
2019-05-02 00:47:44 +02:00
// returns human-readable duration from duration in millis
2017-08-31 21:13:41 +02:00
let secs = Math.round(millis/1000)
if(secs <= 120){
2017-08-31 18:59:09 +02:00
return `${secs} seconds`;
}
2017-08-31 21:13:41 +02:00
let mins = Math.round(secs/60);
if(mins <= 60){
2017-08-31 18:59:09 +02:00
return `${mins} minute${s(mins)}`;
}
let hours = Math.floor(mins/60);
mins = mins % 60;
if(hours < 6){
return `${hours}h ${mins}m`;
}
2017-08-31 21:13:41 +02:00
if(hours <= 48){
2017-08-31 18:59:09 +02:00
return `${hours} hour${s(hours)}`;
}
2017-08-31 21:13:41 +02:00
let days = Math.round(hours/24);
2017-08-31 18:59:09 +02:00
return `${days} days`;
}
2019-05-02 00:47:44 +02:00
function rel_future(millis){
// returns relative time from timestamp, assuming time is in the future
if(millis < 2000){
let secs = Math.floor(millis/1000)
let ndots = absmod(-secs, 3);
let out = 'anytime now';
for(; ndots > 0; ndots--){
out += '.';
2017-08-31 18:59:09 +02:00
}
2019-05-02 00:47:44 +02:00
return out;
}
return `in ${rel(millis)}`;
}
function rel_past(millis){
// returns relative time from timestamp, assuming time is in the past
if(millis < 2000){
return 'just now';
}
return `${rel(millis)} ago`;
}
2017-08-31 18:59:09 +02:00
2019-05-02 00:47:44 +02:00
import { onDestroy, createEventDispatcher } from 'svelte';
2017-08-31 18:59:09 +02:00
2019-05-02 00:47:44 +02:00
const dispatch = createEventDispatcher();
function toggle(){
console.log(policy_enabled);
policy_enabled = !policy_enabled;
if(policy_enabled){
next_delete = null;
}
dispatch('toggle', policy_enabled);
}
export let next_delete, last_delete, eligible_for_delete_estimate;
export let policy_enabled = false;
2017-08-31 18:59:09 +02:00
2019-05-02 00:47:44 +02:00
let now = +(new Date());
2017-08-31 18:59:09 +02:00
2019-05-02 00:47:44 +02:00
let interval = setInterval(() =>
now = +(new Date())
, 1000 );
2017-08-31 18:59:09 +02:00
2019-05-02 00:47:44 +02:00
onDestroy(()=> clearInterval(interval));
2017-08-31 18:59:09 +02:00
</script>