Repository navigation
Expand file tree
/
Copy pathmain.js
More file actions
332 lines (289 loc) · 12.5 KB
/
Copy pathmain.js
File metadata and controls
332 lines (289 loc) · 12.5 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/*
* GNU AGPL-3.0 License
*
* Copyright (c) 2021 - present core.ai . All rights reserved.
* Original work Copyright (c) 2012 - 2021 Adobe Systems Incorporated. All rights reserved.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 Affero General Public License
* for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://opensource.org/licenses/AGPL-3.0.
*
*/
/*global Phoenix*/
define(function (require, exports, module) {
// Brackets modules
const ProjectManager = require("project/ProjectManager"),
SidebarView = require("project/SidebarView"),
PreferencesManager = require("preferences/PreferencesManager"),
Commands = require("command/Commands"),
CommandManager = require("command/CommandManager"),
Menus = require("command/Menus"),
FileSystem = require("filesystem/FileSystem"),
AppInit = require("utils/AppInit"),
KeyEvent = require("utils/KeyEvent"),
FileUtils = require("file/FileUtils"),
PopUpManager = require("widgets/PopUpManager"),
Strings = require("strings"),
Mustache = require("thirdparty/mustache/mustache"),
ProjectsMenuTemplate = require("text!./htmlContent/projects-menu.html"),
ExtensionInterface = require("utils/ExtensionInterface");
const RECENT_PROJECTS_INTERFACE = "Extn.Phoenix.recentProjects";
ExtensionInterface.registerExtensionInterface(RECENT_PROJECTS_INTERFACE, exports);
const RECENT_PROJECT_STATE = "recentProjects";
/** @const {number} Maximum number of displayed recent projects */
var MAX_PROJECTS = 20;
/** @type {$.Element} jQuery elements used for the dropdown menu */
let $dropdown;
/**
* Get the stored list of recent projects, fixing up paths as appropriate.
* Warning: unlike most paths in Brackets, these lack a trailing "/"
*/
function getRecentProjects() {
var recentProjects = PreferencesManager.getViewState(RECENT_PROJECT_STATE) || [],
i;
for (i = 0; i < recentProjects.length; i++) {
// We have to canonicalize & then de-canonicalize the path here, since our pref format uses no trailing "/"
recentProjects[i] = FileUtils.stripTrailingSlash(ProjectManager.updateWelcomeProjectPath(recentProjects[i] + "/"));
}
return recentProjects;
}
/**
* Add a project to the stored list of recent projects, up to MAX_PROJECTS.
*/
function add() {
const projectToAdd = ProjectManager.getProjectRoot().fullPath;
if(projectToAdd === ProjectManager.getPlaceholderProjectPath()){
return;
}
var root = FileUtils.stripTrailingSlash(projectToAdd),
recentProjects = getRecentProjects(),
index = recentProjects.indexOf(root);
if (index !== -1) {
recentProjects.splice(index, 1);
}
recentProjects.unshift(root);
if (recentProjects.length > MAX_PROJECTS) {
recentProjects = recentProjects.slice(0, MAX_PROJECTS);
}
PreferencesManager.setViewState(RECENT_PROJECT_STATE, recentProjects);
}
function removeFromRecentProject(fullPath) {
fullPath = FileUtils.stripTrailingSlash(fullPath);
let recentProjects = getRecentProjects(),
index = recentProjects.indexOf(fullPath),
newProjects = [],
i;
for (i = 0; i < recentProjects.length; i++) {
if (i !== index) {
newProjects.push(recentProjects[i]);
}
}
PreferencesManager.setViewState(RECENT_PROJECT_STATE, newProjects);
}
/**
* Handles the Key Down events
* @param {KeyboardEvent} event
* @param $popUp
* @return {boolean} True if the key was handled
*/
function _handlePopupKeyEvents(event, $popUp) {
if(event.keyCode === KeyEvent.DOM_VK_DELETE){
event.stopPropagation();
const $selectedItem = $popUp.find(".selected");
if ($selectedItem.length && $selectedItem.data("path")) {
// Remove the project from the preferences.
removeFromRecentProject($selectedItem.data("path"));
PopUpManager.selectNextItem(+1, $popUp);
$selectedItem.closest("li").remove();
if (getRecentProjects().length === 1) {
$dropdown.find(".divider").remove();
}
}
return true;
}
}
/**
* Close the dropdown.
*/
function closeDropdown() {
// Since we passed "true" for autoRemove to addPopUp(), this will
// automatically remove the dropdown from the DOM. Also, PopUpManager
// will call cleanupDropdown().
if ($dropdown) {
PopUpManager.removePopUp($dropdown);
}
}
/**
* Remove the various event handlers that close the dropdown. This is called by the
* PopUpManager when the dropdown is closed.
*/
function cleanupDropdown() {
$("html").off("click", closeDropdown);
$("#project-files-container").off("scroll", closeDropdown);
$("#titlebar .nav").off("click", closeDropdown);
$dropdown = null;
}
function openProjectWithPath(fullPath) {
return new Promise((resolve, reject)=>{
ProjectManager.openProject(fullPath)
.then(resolve)
.fail(function () {
// Remove the project from the list only if it does not exist on disk
var recentProjects = getRecentProjects(),
index = recentProjects.indexOf(fullPath);
if (index === -1) {
reject();
return;
}
FileSystem.resolve(fullPath, function (err, item) {
if (err) {
removeFromRecentProject(fullPath);
}
reject();
});
});
});
}
/**
* Adds the click and mouse enter/leave events to the dropdown
*/
function _handleListEvents() {
$dropdown
.on("click", ".recent-project-delete", function (e) {
// Don't let the click bubble upward.
e.stopPropagation();
// Remove the project from the preferences.
removeFromRecentProject($(this).parent().data("path"));
$(this).closest("li").remove();
if (getRecentProjects().length === 1) {
$dropdown.find(".divider").remove();
}
})
.on("click", "a", function () {
var $link = $(this),
id = $link.attr("id"),
path = $link.data("path");
if (path) {
openProjectWithPath(path);
closeDropdown();
} else if (id === "open-folder-link") {
CommandManager.execute(Commands.FILE_OPEN_FOLDER);
} else if (id === "new-project-link") {
CommandManager.execute(Commands.FILE_NEW_PROJECT);
} else if (id === "download-project-link") {
CommandManager.execute(Commands.FILE_DOWNLOAD_PROJECT);
}
});
}
/**
* Parses the path and returns an object with the full path, the folder name and the path without the folder.
* @param {string} fullPath The full path to the folder.
* @return {{path: string, folder: string, rest: string}}
*/
function renderPath(fullPath) {
let parentDirPath = Phoenix.VFS.ensureTrailingSlash(window.path.dirname(fullPath));
let rest;
if(parentDirPath.startsWith(Phoenix.VFS.getTauriDir())) {
rest = " - " + window.fs.getTauriPlatformPath(parentDirPath);
} else if(parentDirPath.startsWith(Phoenix.VFS.getMountDir())) {
const displayPath = parentDirPath.replace(Phoenix.VFS.getMountDir(), "");
if(displayPath){
rest = " - " + displayPath;
}
} else {
rest = " - " + Strings.PROJECT_FROM_BROWSER_TERSE;
}
return {path: fullPath, folder: window.path.basename(fullPath), rest: rest};
}
/**
* Create the list of projects in the dropdown menu.
* @return {string} The html content
*/
function renderList() {
const recentProjects = getRecentProjects(),
downloadProjectCommand = CommandManager.get(Commands.FILE_DOWNLOAD_PROJECT),
currentProject = FileUtils.stripTrailingSlash(ProjectManager.getProjectRoot().fullPath),
templateVars = {
projectList: [],
Strings: Strings,
downloadProjectClass: downloadProjectCommand.getEnabled() ? "": "forced-hidden"
};
recentProjects.forEach(function (root) {
if (root !== currentProject) {
templateVars.projectList.push(renderPath(root));
}
});
return Mustache.render(ProjectsMenuTemplate, templateVars);
}
/**
* Show or hide the recent projects dropdown.
*
* @param {{pageX:number, pageY:number}} position - the absolute position where to open the dropdown
*/
function showDropdown(position) {
// If the dropdown is already visible, just return (so the root click handler on html
// will close it).
if ($dropdown) {
return;
}
Menus.closeAll();
$dropdown = $(renderList())
.css({
left: position.pageX,
top: position.pageY
})
.appendTo($("body"));
PopUpManager.addPopUp($dropdown, cleanupDropdown, true, {closeCurrentPopups: true});
PopUpManager.handleSelectionEvents($dropdown, {
enableSearchFilter: true,
keyboardEventHandler: _handlePopupKeyEvents
});
// TODO: should use capture, otherwise clicking on the menus doesn't close it. More fallout
// from the fact that we can't use the Boostrap (1.4) dropdowns.
$("html").on("click", closeDropdown);
// Hide the menu if the user scrolls in the project tree. Otherwise the Lion scrollbar
// overlaps it.
// TODO: This duplicates logic that's already in ProjectManager (which calls Menus.close()).
// We should fix this when the popup handling is centralized in PopupManager, as well
// as making Esc close the dropdown. See issue #1381.
$("#project-files-container").on("scroll", closeDropdown);
// Note: PopUpManager will automatically hide the sidebar in other cases, such as when a
// command is run, Esc is pressed, or the menu is focused.
// Hacky: if we detect a click in the menubar, close ourselves.
// TODO: again, we should have centralized popup management.
$("#titlebar .nav").on("click", closeDropdown);
_handleListEvents();
}
// Initialize extension
AppInit.appReady(function () {
PreferencesManager.stateManager.definePreference(RECENT_PROJECT_STATE, 'array', [])
.watchExternalChanges();
ProjectManager.on("projectOpen", add);
ProjectManager.on("beforeProjectClose", add);
// add the current project at startup.
add();
});
AppInit.htmlReady(function () {
$("#project-dropdown-toggle .dropdown-arrow").removeClass("forced-hidden");
var cmenuAdapter = {
open: showDropdown,
close: closeDropdown,
isOpen: function () {
return !!$dropdown;
}
};
Menus.ContextMenu.assignContextMenuToSelector("#project-dropdown-toggle", cmenuAdapter);
});
exports.getRecentProjects = getRecentProjects;
exports.openProjectWithPath = openProjectWithPath;
exports.removeFromRecentProject = removeFromRecentProject;
});