-
Notifications
You must be signed in to change notification settings - Fork 44
/
ThemeManager.js
98 lines (82 loc) · 2.96 KB
/
ThemeManager.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
/**
* Brackets Themes Copyright (c) 2014 Miguel Castillo.
*
* Licensed under MIT
*/
define(function (require, exports, module) {
"use strict";
var _ = brackets.getModule("thirdparty/lodash"),
FileSystem = brackets.getModule("filesystem/FileSystem"),
FileUtils = brackets.getModule("file/FileUtils"),
_ThemeManager = brackets.getModule("view/ThemeManager"),
SettingsManager = require("SettingsManager"),
ColorProcessor = require("ColorProcessor"),
validExtensions = ["css", "less"];
/**
* @private
* Verifies that the file passed in is a valid theme file type.
*
* @param {File} file is object to verify if it is a valid theme file type
* @return {boolean} to confirm if the file is a valid theme file type
*/
function isFileTypeValid(file) {
return file.isFile &&
validExtensions.indexOf(FileUtils.getFileExtension(file.name)) !== -1;
}
/**
* Load css/less files from a directory to be treated as themes
*
* @param {string} path where theme files are to be loaded from
* @return {$.Deferred} promise object resolved with the themes to be loaded from the directory
*/
function loadDirectory(path) {
var result = new $.Deferred();
if (!path) {
return result.reject({
path: path,
error: "Path not defined"
});
}
function getDirectoryContent(err, entries) {
var i, files = [];
entries = entries || [];
for (i = 0; i < entries.length; i++) {
if (isFileTypeValid(entries[i])) {
files.push(entries[i].name);
}
}
if (err) {
result.reject({
path: path,
error: err
});
}
else {
result.resolve({
files: files,
path: path
});
}
}
function loadThemesFiles(themes) {
// Iterate through each name in the themes and make them theme objects
var deferred = _.map(themes.files, function (themeFile) {
return _ThemeManager.loadFile(themes.path + "/" + themeFile).done(function(theme) {
theme.file.read(function( err, content /*, stat*/ ) {
if (!err) {
theme.dark = ColorProcessor.isDark(content);
}
});
});
});
return $.when.apply(undefined, deferred);
}
FileSystem.getDirectoryForPath(path).getContents(getDirectoryContent);
return result.then(loadThemesFiles).done(function() {
setTimeout(function() {
_ThemeManager.refresh(true);
}, 0);
});
}
exports.loadDirectory = loadDirectory;
});