forked from Maps4HTML/mapml-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
90 lines (83 loc) · 2.96 KB
/
popup.js
File metadata and controls
90 lines (83 loc) · 2.96 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
let options = {};
/**
* Saves the options to storage
*/
function saveOptions() {
chrome.runtime.sendMessage({type: 'options', options});
chrome.storage.local.set({
options: options
});
}
/**
* Loads the users options from storage and sets it to the global (global to this file) options object
* it also updates the respective elements to reflect the current state
*/
function loadOptions() {
chrome.storage.local.get("options", function (o) {
options = o.options || {
announceMovement: true,
announceScale: 'metric',
featureIndexOverlayOption: false,
renderMap: false,
defaultExtCoor: 'pcrs',
defaultLocCoor: 'gcrs',
contentPreference: []
};
for (let name in options) {
let elem = document.getElementById(name);
if (elem) {
switch (typeof options[name]) {
case "boolean":
elem.checked = options[name];
break;
case "string":
Array.from(elem.children).forEach(el => el.value === options[name]?
el.selected = true : el.selected = false);
break;
case "object":
if (Array.isArray(options[name])) {
Array.from(elem.children).forEach(o => {
if (options[name].includes(o.value)) {
o.defaultSelected = true;
}
});
}
break;
}
}
}
});
}
/**
* Handles checkbox changes, changes are then reflected in the users options in storage
* @param e - Event object
*/
function handleCheckboxChange(e) {
let option = e.target.id;
options[option] = e.target.checked;
console.log(options);
saveOptions();
}
function handleDropdownChange(e) {
let option = e.target.id;
if (option === 'contentPreference') {
options[option] = Array.from(e.target.children).filter(el => el.selected).map( el => el.value);
} else {
options[option] = Array.from(e.target.children).find(el => el.selected).value;
}
saveOptions();
}
// You cannot call a function directly from popup.html, you need to attach a listener in the accompanying JS file
/**
* Attaches event handlers to the user inputs (i.e. checkboxes), and initializes the options
*/
document.addEventListener("DOMContentLoaded", () => {
loadOptions();
document.getElementById("announceMovement").addEventListener("change", handleCheckboxChange);
document.getElementById("announceScale").addEventListener("change", handleDropdownChange);
document.getElementById("featureIndexOverlayOption").addEventListener("change", handleCheckboxChange);
document.getElementById("renderMap").addEventListener("change", handleCheckboxChange);
document.getElementById("defaultExtCoor").addEventListener("change", handleDropdownChange);
document.getElementById("defaultLocCoor").addEventListener("change", handleDropdownChange);
document.getElementById('contentPreference').addEventListener("change", handleDropdownChange);
});