-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.js
86 lines (73 loc) · 3.38 KB
/
options.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
(function() {
let commentsPanelDirection;
const urlPatternInput = document.getElementById("urlPattern");
const commentsPanelExpandedInput = document.getElementById("comments-expanded-by-default");
const commentsPanelDirectionInfo = {
vert: {
sides: ["top", "bottom"],
top: document.getElementById("comments-vert-top"),
bottom: document.getElementById("comments-vert-bottom")
},
horiz: {
sides: ["left", "right"],
left: document.getElementById("comments-horiz-left"),
right: document.getElementById("comments-horiz-right")
}
};
function validateDirection(data) {
data = data || {};
const verticalValid = commentsPanelDirectionInfo.vert.sides.reduce((res, curr) => res + (data[curr] ? 1 : 0), 0);
const horizontalValid = commentsPanelDirectionInfo.horiz.sides.reduce((res, curr) => res + (data[curr] ? 1 : 0), 0);
if (verticalValid !== 1 || horizontalValid !== 1) {
data = { top: true, left: true };
}
return data;
}
function setCommentsPanelDirection(block, side) {
commentsPanelDirectionInfo[block].sides.forEach(s => {
commentsPanelDirection[s] = (s === side);
commentsPanelDirectionInfo[block][s].classList.add(commentsPanelDirection[s] ? "btn-primary" : "btn-secondary");
commentsPanelDirectionInfo[block][s].classList.remove(commentsPanelDirection[s] ? "btn-secondary" : "btn-primary");
});
}
function loadOptions() {
chrome.storage.sync.get(["urlPattern", "commentsPanelDirection", "commentsPanelExpanded"], function(data) {
const urlPattern = data && data.urlPattern || "https://gitlab.*";
urlPatternInput.value = urlPattern;
const commentsPanelExpanded = data && data.commentsPanelExpanded || false;
commentsPanelExpandedInput.checked = commentsPanelExpanded;
commentsPanelDirection = validateDirection(data && data.commentsPanelDirection);
setCommentsPanelDirection("vert", commentsPanelDirection.top ? "top" : "bottom");
setCommentsPanelDirection("horiz", commentsPanelDirection.left ? "left" : "right");
});
}
loadOptions();
function saveOptions() {
urlPatternInput.classList.remove("is-invalid");
const urlPattern = urlPatternInput.value;
if (!urlPattern) {
urlPatternInput.classList.add("is-invalid");
return;
}
const commentsPanelExpanded = commentsPanelExpandedInput.checked;
chrome.storage.sync.set({
urlPattern,
commentsPanelExpanded,
commentsPanelDirection
}, function() {
window.close();
});
}
function resetCommentsPanelPosition() {
chrome.storage.sync.set({
commentsPanelPosition: null
});
}
document.getElementById("save").addEventListener("click", saveOptions);
document.getElementById("reset-comments-position").addEventListener("click", resetCommentsPanelPosition);
Object.keys(commentsPanelDirectionInfo).forEach(block => {
commentsPanelDirectionInfo[block].sides.forEach(side => {
commentsPanelDirectionInfo[block][side].addEventListener("click", function() { setCommentsPanelDirection(block, side) });
});
});
})();