-
Notifications
You must be signed in to change notification settings - Fork 0
/
adblock-options-themes.js
186 lines (157 loc) · 7.12 KB
/
adblock-options-themes.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
/*
* This file is part of AdBlock <https://getadblock.com/>,
* Copyright (C) 2013-present Adblock, Inc.
*
* AdBlock is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* AdBlock is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with AdBlock. If not, see <http://www.gnu.org/licenses/>.
*/
/* For ESLint: List any global identifiers used in this file below */
/* global translate, License, MABPayment, settingsNotifier, settings,
isValidTheme, initializeProxies, send
*/
(function onThemesLoaded() {
const updateThemeSettings = ($newTheme) => {
const key = $newTheme.data('key');
const newTheme = $newTheme.data('theme');
// get local copy of the Color Themes object
const colorThemes = JSON.parse(JSON.stringify(settings.color_themes));
colorThemes[key] = newTheme;
settings.color_themes = colorThemes;
window.location.reload();
};
const updateSelection = (changeEvent) => {
if (!changeEvent || $.isEmptyObject(changeEvent)) {
return;
}
const $selectedTheme = $(changeEvent.target).closest('.theme-box');
if ($selectedTheme.closest('.theme-wrapper').hasClass('locked')) {
send('openTab', { urlToOpen: License.MAB_CONFIG.payURL });
return;
}
const $otherThemes = $selectedTheme.closest('section').find('.theme-box').not($selectedTheme);
$otherThemes.removeClass('selected');
$selectedTheme.addClass('selected');
updateThemeSettings($selectedTheme);
};
const selectCurrentThemes = async (currentThemes) => {
const popupMenuTheme = await isValidTheme(currentThemes.popup_menu) ? currentThemes.popup_menu : 'default_theme';
const optionsPageTheme = await isValidTheme(currentThemes.options_page) ? currentThemes.options_page : 'default_theme';
// reset selected theme
$('.popup-menu-themes .selected').removeClass('selected');
$('.options-page-themes .selected').removeClass('selected');
// Get theme nodes to select
const $popupTheme = $(`.popup-menu-themes [data-theme=${popupMenuTheme}]`);
const $optionsTheme = $(`.options-page-themes [data-theme=${optionsPageTheme}]`);
const $popupInput = $popupTheme.find('input[name=popup-menu-theme]');
const $optionsInput = $optionsTheme.find('input[name=options-page-theme]');
// Select theme nodes
$popupTheme.addClass('selected');
$optionsTheme.addClass('selected');
$popupInput.prop('checked', true);
$optionsInput.prop('checked', true);
$('.options-page-theme-preview').attr('src', `icons/${optionsPageTheme}/optionscard.svg`);
$('.options-page-theme-preview').attr('alt', translate('a_theme_preview', translate(`${optionsPageTheme}`), translate('options_page')));
};
const showHoveredPopupThemePreview = ($themeBox) => {
const hoveredTheme = $themeBox.data('theme');
$('.popup-menu-theme-title').text(translate(`${hoveredTheme}`));
$('.popup-menu-theme-preview').attr('src', `icons/${hoveredTheme}/previewcard.svg`);
$('.popup-menu-theme-preview').attr('alt', translate('a_theme_preview', translate(`${hoveredTheme}`), translate('popup_menu')));
};
const showSelectedPopupThemePreview = () => {
const popupMenuTheme = $('.popup-menu-themes .selected').data('theme');
$('.popup-menu-theme-title').text(translate(`${popupMenuTheme}`));
$('.popup-menu-theme-preview').attr('src', `icons/${popupMenuTheme}/previewcard.svg`);
$('.popup-menu-theme-preview').attr('alt', translate('a_theme_preview', translate(`${popupMenuTheme}`), translate('popup_menu')));
};
const showHoveredOptionsThemePreview = ($themeBox) => {
const hoveredTheme = $themeBox.data('theme');
$('.options-page-theme-title').text(translate(`${hoveredTheme}`));
$('.options-page-theme-preview').attr('src', `icons/${hoveredTheme}/optionscard.svg`);
$('.options-page-theme-preview').attr('alt', translate('a_theme_preview', translate(`${hoveredTheme}`), translate('options_page')));
};
const showSelectedOptionsThemePreview = () => {
const optionsPageTheme = $('.options-page-themes .selected').data('theme');
$('.options-page-theme-title').text(translate(`${optionsPageTheme}`));
$('.options-page-theme-preview').attr('src', `icons/${optionsPageTheme}/optionscard.svg`);
$('.options-page-theme-preview').attr('alt', translate('a_theme_preview', translate(`${optionsPageTheme}`), translate('options_page')));
};
const showShadowOnLockedHover = ($themeBox) => {
if ($themeBox.parent().hasClass('locked')) {
$('#get-it-now-themes').addClass('shadow');
}
};
const hideShadowNoHover = () => {
$('#get-it-now-themes').removeClass('shadow');
};
const documentEventsHandling = () => {
// Hover events
$('.popup-menu-themes .theme-box:not(.selected)')
.on('mouseenter', function handleIn() {
showHoveredPopupThemePreview($(this));
showShadowOnLockedHover($(this));
})
// eslint-disable-next-line prefer-arrow-callback
.on('mouseleave', function handleOut() {
showSelectedPopupThemePreview();
hideShadowNoHover();
});
$('.options-page-themes .theme-box:not(.selected)')
.on('mouseenter', function handleIn() {
showHoveredOptionsThemePreview($(this));
showShadowOnLockedHover($(this));
})
// eslint-disable-next-line prefer-arrow-callback
.on('mouseleave', function handleOut() {
showSelectedOptionsThemePreview();
hideShadowNoHover();
});
// Change events
$('input.invisible-radio-button').on('change', event => updateSelection(event));
};
$(async () => {
await initializeProxies();
let colorThemes = {};
if (settings) {
colorThemes = settings.color_themes;
}
$('.theme-wrapper:not(.locked) .overlay-icon').each(function i18nSupport() {
const $preview = $(this);
const theme = $preview.closest('.theme-box').data('theme');
const component = $preview.closest('.theme-box').data('key');
$preview.attr('aria-label', translate('preview_a_theme', [
translate(`${theme}`),
translate(`${component}`),
]));
});
await selectCurrentThemes(colorThemes);
showSelectedOptionsThemePreview();
showSelectedPopupThemePreview();
if (!License || $.isEmptyObject(License) || !MABPayment) {
return;
}
const payInfo = MABPayment.initialize('themes');
if (License.shouldShowMyAdBlockEnrollment()) {
MABPayment.freeUserLogic(payInfo);
$('#get-it-now-themes').on('click', MABPayment.userClickedPremiumCTA);
} else if (License.isActiveLicense()) {
MABPayment.paidUserLogic(payInfo);
}
documentEventsHandling();
});
const onSettingsChanged = function (name, currentValue) {
if (name === 'color_themes') {
selectCurrentThemes(currentValue);
}
};
settingsNotifier.on('settings.changed', onSettingsChanged);
}());