-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
180 lines (155 loc) · 4.75 KB
/
script.js
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
const zones = moment.tz.names();
const localZoneGuess = moment.tz.guess();
const links =
{
"America/New_York": "US/Eastern",
"America/Chicago": "US/Central",
"America/Denver": "US/Mountain",
"America/Los_Angeles": "US/Pacific",
};
const localZone = localZoneGuess in links ? links[localZoneGuess] : localZoneGuess;
const nepotism =
[
"US/Eastern",
"US/Central",
"US/Mountain",
"US/Pacific",
"Asia/Hong_Kong",
"Europe/London",
];
const zoneSelectionList =
nepotism.concat(moment.tz.names().filter(x => !nepotism.includes(x)));
const defaultState = () =>
({
when: new Date(),
where: [localZone],
initial: true,
edit: true,
});
const unpackState = (base64) => {
const packed = Uint8Array.from([...atob(base64)].map(c => c.charCodeAt(0)));
const view = new DataView(packed.buffer);
const version = view.getUint8(0);
// version tag with backwards-compatible loading
let pos = (version == 1) ? 1 : 0;
const when = new Date(view.getFloat64(pos));
pos += 8;
const where = [];
for (; pos < view.byteLength; pos += 2) {
where.push(zones[view.getUint16(pos)]);
}
return { when, where, initial: false, edit: false };
};
const packState = (state) => {
const size = 1 + 8 + state.where.length * 2;
const packed = new Uint8Array(size);
const view = new DataView(packed.buffer);
const version = 1;
view.setUint8(0, version);
view.setFloat64(1, state.when.valueOf());
state.where.forEach((tz, i) => {
view.setUint16((i * 2) + 1 + 8, zones.indexOf(tz));
});
return btoa(String.fromCharCode(...packed));
};
const parseQuery = (search) =>
(search[0] === '?')
// Sometimes services insert query parameters
? unpackState(search.slice(1).replace(/&.+/, ''))
: defaultState();
const serializeQuery = (state) => `?${packState(state)}`;
const when = document.querySelector("#when");
const where =
new Tagify(document.querySelector("#where"), {
whitelist: zoneSelectionList,
duplicates: false,
enforceWhitelist: true,
editTags: false,
dropdown: {
enabled: 0,
closeOnSelect: false,
highlightFirst: true,
}
});
const deets = document.querySelector("#deets");
const state = {};
// Because there doesn't seem to be a way to replace the set of tags without
// firing an add event for each tag, we have to build a sort of event silencer
const resetTags = (tags, values) => {
tags.isInReset = true;
tags.loadOriginalValues(values);
tags.isInReset = false;
}
const updateHistory = (f) => {
history[f](state, '', serializeQuery(state));
}
const fromInput = () => {
Object.assign(state, {
when: new Date(when.value),
where: where.value.map(tag => tag.value),
});
updateHistory('pushState');
render();
};
const fromState = (newState, withHistoryUpdate) => {
Object.assign(state, newState);
if (withHistoryUpdate) updateHistory(withHistoryUpdate);
when.value = moment(state.when).format(moment.HTML5_FMT.DATETIME_LOCAL);
resetTags(where, state.where);
render();
}
const render = () => {
const format = (m) => m.format("LT (ddd MMM D, YYYY)");
const compareBy = (m) => m.utcOffset();
const times =
state.where
.map(tz => ({ where: tz, moment: moment.tz(state.when, tz) }))
.sort((a, b) => compareBy(a.moment) - compareBy(b.moment));
deets.innerHTML = `
<table>
<thead>
<tr>
<th>Where</th>
<th>When</th>
</tr>
</thead>
<tbody>
<tr>
<td>Everywhere</td>
<td>${moment().to(state.when)}</td>
</tr>
${times.map((t => `
<tr${t.where === localZone ? " class='local'" : " class='remote'"}>
<td>${t.where}</td>
<td>${format(t.moment)}</td>
</tr>
`)).join('')}
</tbody>
</table>`;
const controls = document.querySelector("#controls");
const link = document.querySelector("#link");
state.edit
? (controls.classList.remove("noedit"),
link.href = document.location.href)
: controls.classList.add("noedit");
state.initial
? controls.parentElement.prepend(controls)
: controls.parentElement.append(controls);
};
// setup
when.addEventListener("change", fromInput);
where.on("add remove", _ => {
if (!where.isInReset) fromInput();
});
const updateOnClick = (selector, update) => {
document.querySelector(selector).addEventListener("click", () => {
fromState(Object.assign({}, state, update), 'pushState');
});
};
updateOnClick("#resetWhen", { when: defaultState().when });
updateOnClick("#resetWhere", { where: defaultState().where });
updateOnClick("#edit", { edit: true });
updateOnClick("#new", defaultState());
window.addEventListener("popstate", (event) => fromState(event.state));
// init
fromState(parseQuery(document.location.search), 'replaceState');