-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.js
205 lines (176 loc) · 8.19 KB
/
main.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
require.config({
paths: {
"text" : "lib/text",
"i18n" : "lib/i18n"
},
locale: brackets.getLocale()
});
define(function (require, exports, module) {
"use strict";
var AppInit = brackets.getModule("utils/AppInit"),
DocumentManager = brackets.getModule("document/DocumentManager"),
MainViewManager = brackets.getModule("view/MainViewManager"),
ProjectManager = brackets.getModule("project/ProjectManager"),
CommandManager = brackets.getModule("command/CommandManager"),
PreferencesManager = brackets.getModule("preferences/PreferencesManager"),
Menus = brackets.getModule("command/Menus"),
Dialogs = brackets.getModule("widgets/Dialogs"),
StringUtils = brackets.getModule("utils/StringUtils"),
ColorUtils = brackets.getModule("utils/ColorUtils"),
ExtensionUtils = brackets.getModule("utils/ExtensionUtils"),
BracketsStrings = brackets.getModule("strings"),
Strings = require("strings"),
ProjectNameDialogTemplate = require("text!./htmlContent/project-name-dialog.html"),
packageJSON = require("text!package.json"),
fileMenu = Menus.getMenu(Menus.AppMenuBar.FILE_MENU),
COMMAND_ID = JSON.parse(packageJSON).name,
prefs = PreferencesManager.getExtensionPrefs(COMMAND_ID);
/*
* @private
* @method _showProjectNameDialog()
* @description Show project-name-dialog "Project Name" is clicked in the File-menu.
*/
function _showProjectNameDialog() {
var dialog,
projectRoot = ProjectManager.getProjectRoot(),
projectNameConfig = _getProjectNameConfig(projectRoot._path),
projectName = projectNameConfig ? projectNameConfig.name : projectRoot._name,
bgColor = projectNameConfig ? projectNameConfig.bgColor : "transparent",
textColor = projectNameConfig ? projectNameConfig.textColor : "black",
title = StringUtils.format(Strings.PROJECT_DIALOG_TITLE, projectRoot._name),
templateVars = {
title: title,
path: projectRoot._path,
Strings: Strings,
BracketsStrings: BracketsStrings,
projectName: projectName,
bgColor: bgColor,
textColor: textColor
},
bgColorInput,
bgColorHint,
textColorInput,
textColorHint;
dialog = Dialogs.showModalDialogUsingTemplate(Mustache.render(ProjectNameDialogTemplate, templateVars));
bgColorInput = dialog.getElement().find("input[name='bgColorValue']");
textColorInput = dialog.getElement().find("input[name='textColorValue']");
bgColorHint = dialog.getElement().find(".petetnt-name-a-project-bgcolor-hint");
textColorHint = dialog.getElement().find(".petetnt-name-a-project-textcolor-hint");
dialog.done(function (id) {
if (id === Dialogs.DIALOG_BTN_OK) {
var config = {},
value = dialog.getElement().find("input[name='projectName']").val(),
name = value.length ? value : projectRoot._name,
bgColor = bgColorInput.val(),
textColor = textColorInput.val(),
scope = dialog.getElement().find("#petetnt-scope-selection").val();
config = {
_parentPath: projectRoot._path,
_name: projectRoot._name,
name: name,
bgColor: bgColor,
textColor: textColor
};
_setProjectNameConfig(config, projectRoot._path, scope);
}
});
bgColorInput.on("keyup", function() {
var val = $(this).val();
bgColorHint.html("");
ColorUtils.formatColorHint(bgColorHint, val);
});
textColorInput.on("keyup", function() {
var val = $(this).val();
textColorHint.html("");
ColorUtils.formatColorHint(textColorHint, val);
});
}
/*
* @private
* @method _prefixRecentFolders()
* @description Prefix Recent folders when the Project Dropdown is opened
*/
function _prefixRecentFolders() {
var projectDropdown = $("#project-dropdown"),
recentFolders = projectDropdown.find(".recent-folder-link");
recentFolders.each(function (i, elem) {
var $elem = $(elem),
path = $elem.data("path") + "/",
projectNameConfig = _getProjectNameConfig(path);
if (projectNameConfig) {
var recentFolder = $elem.find("span").eq(0);
recentFolder.html(projectNameConfig.name);
recentFolder.addClass("petetnt-name-a-project-is-named");
recentFolder.attr("style", "background-color:" + projectNameConfig.bgColor + "; color:" + projectNameConfig.textColor);
}
});
}
/*
* @private
* @method _renameProjectTitle
* @description Rename the Project Title bar and construct document title with the current name
*/
function _renameProjectTitle() {
var projectRoot = ProjectManager.getProjectRoot(),
projectPath = projectRoot._path,
projectNameConfig = _getProjectNameConfig(projectPath),
$projectTitle = $("#sidebar").find("#project-title");
if (projectNameConfig) {
$projectTitle.html(projectNameConfig.name);
_renameWindowTitle(projectNameConfig.name);
}
}
/*
* @private
* @method _renameWindowTitle
* @description Rename the window title after DocumentCommandHandler has finished
*/
function _renameWindowTitle(name) {
setTimeout(function () {
window.document.title = window.document.title.replace(/\((.*?)\)/g, "(" + name + ")");
}, 1);
}
/*
* Get a object representing the path, original name, current name and background color according to the given path
* @param {string} path
* @returns {Object.<string, string, string, string>} Object of _parentPath, _name, name and bgColor
*/
function _getProjectNameConfig(path) {
var namedProjects = prefs.get("namedProjects");
return namedProjects ? namedProjects[path] : null;
}
/*
* Save current config after changes
* @param {Object.<string, string, string, string>} config Object of _parentPath, _name, name and bgColor
* @param {string} path Current project path
* @param {string} Scope scope to save preference at, can be undefined.
*/
function _setProjectNameConfig(config, path, scope) {
if (scope === "default") {
scope = prefs.get("defaultScope");
}
var namedProjects = scope !== "project" ? (prefs.get("namedProjects") || {}) : {};
namedProjects[path] = config;
prefs.set("namedProjects", namedProjects, {
location: {
scope: scope
}
});
}
// Listen for changes that require updating the editor titlebar
ProjectManager.on("projectOpen", _renameProjectTitle);
DocumentManager.on("dirtyFlagChange", _renameProjectTitle);
DocumentManager.on("fileNameChange", _renameProjectTitle);
MainViewManager.on("currentFileChange", _renameProjectTitle);
prefs.on("change", _renameProjectTitle);
prefs.definePreference("defaultScope", "string", prefs.get("defaultScope") || "user");
// Handle event dropdown open after htmlReady
AppInit.htmlReady(function () {
$("#project-dropdown-toggle").on("click", _prefixRecentFolders);
});
// Load stylesheet with ExtensionUtils
ExtensionUtils.loadStyleSheet(module, "style/style.less");
// Add command menu items
CommandManager.register(Strings.PROJECT_NAME, COMMAND_ID, _showProjectNameDialog);
fileMenu.addMenuItem(COMMAND_ID, null, Menus.LAST_IN_SECTION, Menus.MenuSection.FILE_LIVE);
});