This repository was archived by the owner on Sep 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
Expand file tree
/
Copy pathThemeSettings.js
More file actions
183 lines (160 loc) · 6.89 KB
/
ThemeSettings.js
File metadata and controls
183 lines (160 loc) · 6.89 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
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
/**
* Brackets Themes Copyright (c) 2014 Miguel Castillo.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/
define(function (require, exports, module) {
"use strict";
var _ = require("thirdparty/lodash"),
Mustache = require("thirdparty/mustache/mustache"),
Dialogs = require("widgets/Dialogs"),
Strings = require("strings"),
ViewCommandHandlers = require("view/ViewCommandHandlers"),
settingsTemplate = require("text!htmlContent/themes-settings.html"),
PreferencesManager = require("preferences/PreferencesManager"),
prefs = PreferencesManager.getExtensionPrefs("themes");
/**
* @type {Object}
* Currently loaded themes that are available to choose from.
*/
var loadedThemes = {};
/**
* Object with all default values that can be configure via the settings UI
*/
var defaults = {
"themeScrollbars": true,
"theme": "light-theme"
};
/**
* Cached html settings jQuery object for easier processing when opening the settings dialog
*/
var $settings = $(settingsTemplate).addClass("themeSettings");
/**
* @private
* Gets all the configurable settings that need to be loaded in the settings dialog
*
* @return {Object} a collection with all the settings
*/
function getValues() {
var result = {};
Object.keys(defaults).forEach(function (key) {
result[key] = prefs.get(key);
});
result.fontFamily = ViewCommandHandlers.getFontFamily();
result.fontSize = ViewCommandHandlers.getFontSize();
result.validFontSizeRegExp = ViewCommandHandlers.validFontSizeRegExp;
return result;
}
/**
* Opens the settings dialog
*/
function showDialog() {
var currentSettings = getValues();
var newSettings = {};
var themes = _.map(loadedThemes, function (theme) { return theme; });
var template = $("<div>").append($settings).html();
var $template = $(Mustache.render(template, {"settings": currentSettings, "themes": themes, "Strings": Strings}));
// Select the correct theme.
var $currentThemeOption = $template
.find("[value='" + currentSettings.theme + "']");
if ($currentThemeOption.length === 0) {
$currentThemeOption = $template.find("[value='" + defaults.theme + "']");
}
$currentThemeOption.attr("selected", "selected");
$template
.find("[data-toggle=tab].default")
.tab("show");
$template
.on("change", "[data-target]:checkbox", function () {
var $target = $(this);
var attr = $target.attr("data-target");
newSettings[attr] = $target.is(":checked");
})
.on("input", "[data-target='fontSize']", function () {
var target = this;
var targetValue = $(this).val();
var $btn = $("#theme-settings-done-btn")[0];
// Make sure that the font size is expressed in terms
// we can handle (px or em). If not, 'done' button is
// disabled until input has been corrected.
if (target.checkValidity() === true) {
$btn.disabled = false;
newSettings["fontSize"] = targetValue;
} else {
$btn.disabled = true;
}
})
.on("input", "[data-target='fontFamily']", function () {
var targetValue = $(this).val();
newSettings["fontFamily"] = targetValue;
})
.on("change", "select", function () {
var $target = $(":selected", this);
var attr = $target.attr("data-target");
if (attr) {
prefs.set(attr, $target.val());
}
});
Dialogs.showModalDialogUsingTemplate($template).done(function (id) {
var setterFn;
if (id === "save") {
// Go through each new setting and apply it
Object.keys(newSettings).forEach(function (setting) {
if (defaults.hasOwnProperty(setting)) {
prefs.set(setting, newSettings[setting]);
} else {
// Figure out if the setting is in the ViewCommandHandlers, which means it is
// a font setting
setterFn = "set" + setting[0].toLocaleUpperCase() + setting.substr(1);
if (typeof ViewCommandHandlers[setterFn] === "function") {
ViewCommandHandlers[setterFn](newSettings[setting]);
}
}
});
} else if (id === "cancel") {
// Make sure we revert any changes to theme selection
prefs.set("theme", currentSettings.theme);
}
});
}
/**
* Interface to set the themes that are available to chose from in the setting dialog
* @param {ThemeManager.Theme} themes is a collection of themes created by the ThemeManager
*/
function setThemes(themes) {
loadedThemes = themes;
}
/**
* Restores themes to factory settings.
*/
function restore() {
prefs.set("theme", defaults.theme);
prefs.set("themeScrollbars", defaults.themeScrollbars);
}
prefs.definePreference("theme", "string", defaults.theme, {
description: Strings.DESCRIPTION_THEME
});
prefs.definePreference("themeScrollbars", "boolean", defaults.themeScrollbars, {
description: Strings.DESCRIPTION_USE_THEME_SCROLLBARS
});
exports._setThemes = setThemes;
exports.restore = restore;
exports.showDialog = showDialog;
});