-
Notifications
You must be signed in to change notification settings - Fork 37
/
options.js
130 lines (111 loc) · 3.9 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
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
;(function ($) {
'use strict';
function saveOptionsToChromeStorage(options, callback) {
chrome.storage.sync.set(options, callback);
}
function loadItemsFromChromeStorage(options, callback) {
chrome.storage.sync.get(options, function (items) {
callback(items);
});
}
function addNewAutoCollapseField(value) {
var controlForm = $('#auto-collapse-form'),
currentEntry = $(controlForm).children('.entry:last'),
newEntry = $(currentEntry.clone()).appendTo(controlForm);
newEntry.find('input').val(value);
controlForm.find('.entry:not(:last) .btn-add')
.removeClass('btn-add').addClass('btn-remove')
.removeClass('btn-success').addClass('btn-danger')
.html('<span class="glyphicon glyphicon-minus"></span>');
}
function resetAutoCollapseFields() {
var controlForm = $('#auto-collapse-form'),
currentEntry = $(controlForm).children('.entry:last'),
newEntry = $(currentEntry.clone());
controlForm.find('.entry').each((index, item) => {
item.remove();
});
newEntry.appendTo(controlForm);
}
function setFormValues(options) {
resetAutoCollapseFields();
for (var key in options) {
var $formInput = $('input[name="' + key + '"]');
if ($formInput.prop('type') === 'checkbox') {
$formInput.prop('checked', options[key]);
} else {
if (options[key].constructor === Array) {
var values = options[key];
$formInput.val(values.pop())
values.forEach(value => {
addNewAutoCollapseField(value);
});
} else {
$formInput.prop('value', options[key]);
}
}
}
}
function getChangedValuesObject(changedAttributes) {
var updates = {};
for (var key in changedAttributes) {
updates[key] = changedAttributes[key].newValue;
}
return updates;
}
$(document).ready(function () {
loadItemsFromChromeStorage({
url: '',
saveCollapsedDiffs: true,
tabSwitchingEnabled: false,
autoCollapseExpressions: []
// Add new items here to get them loaded and their values put in the form.
}, setFormValues);
$('#options-form').submit(function (e) {
e.preventDefault();
var $form = $(e.currentTarget);
var url = $form.find('input[name="url"]').val();
var origin = url.match(/https?:\/\/[^/?#]+/);
var saveCollapsedDiffs = $form.find('input[name="saveCollapsedDiffs"]').prop('checked');
var tabSwitchingEnabled = $form.find('input[name="tabSwitchingEnabled"]').prop('checked');
var autoCollapseExpressions = [];
$('input[name="autoCollapseExpressions"]').each((index, item) => {
autoCollapseExpressions.push($(item).val());
});
autoCollapseExpressions = autoCollapseExpressions.filter(item => {return item !== ''});
if (autoCollapseExpressions.length == 0) {
autoCollapseExpressions = [''];
}
if (origin) {
origin[0] += '/*';
chrome.permissions.request({
origins: origin
});
}
saveOptionsToChromeStorage({
'url': url,
'saveCollapsedDiffs': saveCollapsedDiffs,
'tabSwitchingEnabled': tabSwitchingEnabled,
'autoCollapseExpressions': autoCollapseExpressions
}, function () {
var status = $('#status');
status.text('Options saved.');
setTimeout(function () {
status.text('');
}, 3000);
});
});
$(document).on('click', '.btn-add', function(e) {
e.preventDefault;
addNewAutoCollapseField('');
}).on('click', '.btn-remove', function(e){
$(this).parents('.entry:first').remove();
e.preventDefault();
return false;
});
chrome.storage.onChanged.addListener(function (changes) {
// The options were updated, update them here too.
setFormValues(getChangedValuesObject(changes));
});
});
} (this.$));