-
Notifications
You must be signed in to change notification settings - Fork 0
/
seasons.js
195 lines (178 loc) · 6.13 KB
/
seasons.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/* javascript for the weewx Seasons skin
* Copyright (c) Tom Keffer, Matthew Wall
* Distributed under terms of GPLv3. See LICENSE.txt for your rights.
*/
const cookie_prefix = "weewx.seasons.";
let year_type = get_state('year_type', 'year');
function setup(widgets) {
// set the state of the history widget
const id = get_state('history', 'day');
choose_history(id);
// if we got a list of widget names, then use it. otherwise, query the doc
// for every object with an id of *_widget, and use that as the name list.
if (!widgets) {
widgets = [];
const items = document.getElementsByClassName('widget');
if (items) {
for (let i = 0; i < items.length; i++) {
if (items[i].id) {
const widget_name = items[i].id.replace('_widget', '');
if (widget_name) {
widgets.push(widget_name);
}
}
}
}
}
// now set the toggle state for each widget based on what the cookies say
for (let i = 0; i < widgets.length; i++) {
const state = get_state(widgets[i] + '.state', 'expanded');
toggle_widget(widgets[i], state);
}
}
function choose_history(id) {
choose_div('history', id, ['day', 'week', 'month', 'year']);
choose_col('hilo', id, ['week', 'month', 'year', 'rainyear']);
choose_rainyear(id);
}
function choose_rainyear(id) {
if (id === 'year') {
choose_col('hilo', year_type, ['year', 'rainyear']);
}
}
function toggle_rainyear() {
if (year_type === 'year') {
year_type = 'rainyear';
} else {
year_type = 'year';
}
set_state('year_type', year_type);
const id = get_active_div('history', ['day', 'week', 'month', 'year'], 'day');
choose_rainyear(id);
}
function toggle_widget(id, state) {
const id_elements = document.getElementById(id + '_widget');
if (id_elements) {
for (let i = 0; i < id_elements.childNodes.length; i++) {
if (id_elements.childNodes[i].className === 'widget_contents') {
if (state === undefined) {
// make it the opposite of the current state
state = id_elements.childNodes[i].style.display === 'block' ? 'collapsed' : 'expanded';
}
id_elements.childNodes[i].style.display = (state === 'expanded') ? 'block' : 'none';
}
}
set_state(id + '.state', state);
}
}
function choose_col(group, selected_id, all_ids) {
for (let i = 0; i < all_ids.length; i++) {
let elements = document.getElementsByClassName(group + '_' + all_ids[i]);
if (elements) {
const display = selected_id === all_ids[i] ? '' : 'none';
for (let j = 0; j < elements.length; j++) {
elements[j].style.display = display;
}
}
}
}
function choose_div(group, selected_id, all_ids) {
for (let i = 0; i < all_ids.length; i++) {
const button = document.getElementById('button_' + group + '_' + all_ids[i]);
if (button) {
button.className = (all_ids[i] === selected_id) ? 'button_selected' : 'button';
}
const element = document.getElementById(group + '_' + all_ids[i]);
if (element) {
element.style.display = (all_ids[i] === selected_id) ? 'block' : 'none';
}
}
set_state(group, selected_id);
}
/* if cookies are disabled, then we must look at page to get state */
function get_active_div(group, all_ids, default_value) {
let id = default_value;
for (let i = 0; i < all_ids.length; i++) {
const button = document.getElementById('button_' + group + '_' + all_ids[i]);
if (button && button.className === 'button_selected') {
id = all_ids[i];
}
}
return id;
}
function set_state(name, value, dur) {
const full_name = cookie_prefix + name;
/* set_cookie(full_name, value, dur); */
window.localStorage.setItem(full_name, value);
}
function get_state(name, default_value) {
const full_name = cookie_prefix + name;
/* return get_cookie(name, default_value); */
let value = window.localStorage.getItem(full_name);
if (value === undefined || value == null) {
value = default_value;
}
return value
}
function set_cookie(name, value, dur) {
if (!dur) dur = 30;
const today = new Date();
let expire = new Date();
expire.setTime(today.getTime() + 24 * 3600000 * dur);
document.cookie = name + "=" + encodeURI(value) + ";expires=" + expire.toUTCString();
}
function get_cookie(name, default_value) {
if (name === "") return default_value;
const cookie = " " + document.cookie;
let i = cookie.indexOf(" " + name + "=");
if (i < 0) i = cookie.indexOf(";" + name + "=");
if (i < 0) return default_value;
let j = cookie.indexOf(";", i + 1);
if (j < 0) j = cookie.length;
return unescape(cookie.substring(i + name.length + 2, j));
}
function get_parameter(name) {
const query = window.location.search.substring(1);
if (query) {
const vars = query.split("&");
for (let i = 0; i < vars.length; i++) {
const pair = vars[i].split("=");
if (pair[0] === name) {
return pair[1];
}
}
}
return false;
}
function load_file(div_id, var_name) {
let content;
const file = get_parameter(var_name);
if (file) {
content = "Loading " + file;
let xhr = new XMLHttpRequest();
xhr.onload = function () {
let e = document.getElementById(div_id);
if (e) {
e.textContent = this.responseText;
}
};
xhr.open('GET', file);
xhr.send();
} else {
content = 'nothing specified';
}
let e = document.getElementById(div_id);
if (e) {
e.innerHTML = content;
}
}
function openNOAAFile(date) {
if (date.match(/^\d\d\d\d/)) {
window.location = "NOAA/NOAA-" + date + ".txt";
}
}
function openTabularFile(date) {
if (date.match(/^\d\d\d\d/)) {
window.location = "tabular.html?report=NOAA/NOAA-" + date + ".txt";
}
}