forked from njx/brackets-recent-projects
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
149 lines (129 loc) · 6.02 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
/*
* Copyright (c) 2012 Adobe Systems Incorporated. All rights reserved.
*
* 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.
*
*/
/*jslint vars: true, plusplus: true, devel: true, nomen: true, regexp: true, indent: 4, maxerr: 50 */
/*global define, brackets, window, $ */
// TODO: remember working set for each project
define(function (require, exports, module) {
'use strict';
var PREFERENCES_KEY = "com.adobe.brackets.brackets-recent-projects";
// Brackets modules
var DocumentManager = brackets.getModule("document/DocumentManager"),
ProjectManager = brackets.getModule("project/ProjectManager"),
PreferencesManager = brackets.getModule("preferences/PreferencesManager"),
Commands = brackets.getModule("command/Commands"),
CommandManager = brackets.getModule("command/CommandManager"),
ExtensionUtils = brackets.getModule("utils/ExtensionUtils"),
strings = brackets.getModule("strings");
var $dropdownToggle;
function add() {
var root = ProjectManager.getProjectRoot().fullPath,
prefs = PreferencesManager.getPreferenceStorage(PREFERENCES_KEY),
recentProjects = prefs.getValue("recentProjects") || [],
index = recentProjects.indexOf(root);
if (index !== -1) {
recentProjects.splice(index, 1);
}
recentProjects.unshift(root);
if (recentProjects.length > 20) {
recentProjects = recentProjects.slice(0, 20);
}
prefs.setValue("recentProjects", recentProjects);
console.log("Recent projects: " + recentProjects);
}
function renderPath(path) {
if (path.length && path[path.length - 1] === "/") {
path = path.slice(0, path.length - 1);
}
var lastSlash = path.lastIndexOf("/"), folder, rest;
if (lastSlash === path.length - 1) {
lastSlash = path.slice(0, path.length - 1).lastIndexOf("/");
}
if (lastSlash >= 0) {
rest = path.slice(0, lastSlash);
folder = path.slice(lastSlash + 1);
} else {
rest = "";
folder = path;
}
var folderSpan = $("<span></span>").addClass("recent-folder").text(folder),
restSpan = $("<span></span>").addClass("recent-folder-path").text(" in " + rest);
return $("<a></a>").append(folderSpan).append(restSpan);
}
function toggle(e) {
// If the dropdown is already visible, just return (so the root click handler on html
// will close it).
if ($("#project-dropdown").length) {
return;
}
// TODO: Can't just use Bootstrap 1.4 dropdowns for this since they're hard-coded to <li>s.
// Have to do this stopProp to avoid the html click handler from firing when this returns.
e.stopPropagation();
var prefs = PreferencesManager.getPreferenceStorage(PREFERENCES_KEY),
recentProjects = prefs.getValue("recentProjects") || [],
$dropdown = $("<ul id='project-dropdown' class='dropdown-menu'></ul>"),
toggleOffset = $dropdownToggle.offset();
function closeDropdown() {
$("html").off("click", closeDropdown);
$dropdown.remove();
}
var currentProject = ProjectManager.getProjectRoot().fullPath,
hasProject = false;
recentProjects.forEach(function (root) {
if (root !== currentProject) {
var $link = renderPath(root)
.click(function () {
ProjectManager.openProject(root);
closeDropdown();
});
$("<li></li>")
.append($link)
.appendTo($dropdown);
hasProject = true;
}
});
if (hasProject) {
$("<li class='divider'>").appendTo($dropdown);
}
$("<li><a>" + strings.CMD_OPEN_FOLDER + "</a></li>")
.click(function () {
CommandManager.execute(Commands.FILE_OPEN_FOLDER);
})
.appendTo($dropdown);
$dropdown.css({
left: toggleOffset.left,
top: toggleOffset.top + $dropdownToggle.outerHeight()
})
.appendTo($("body"));
// 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);
}
// Initialize extension
ExtensionUtils.loadStyleSheet(module, "styles.css");
$("#project-title")
.wrap("<div id='project-dropdown-toggle'></div>")
.after("<span class='dropdown-arrow'></span>");
$dropdownToggle = $("#project-dropdown-toggle").click(toggle);
$(ProjectManager).on("projectOpen", add);
$(ProjectManager).on("beforeProjectClose", add);
});