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 pathWorkingSetSort.js
More file actions
370 lines (319 loc) · 12.2 KB
/
WorkingSetSort.js
File metadata and controls
370 lines (319 loc) · 12.2 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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*
* 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, indent: 4, maxerr: 50 */
/*global define, $, window, brackets */
/**
* Manages the workingSet sort methods.
*/
define(function (require, exports, module) {
"use strict";
var Commands = require("command/Commands"),
CommandManager = require("command/CommandManager"),
DocumentManager = require("document/DocumentManager"),
PreferencesManager = require("preferences/PreferencesManager"),
FileUtils = require("file/FileUtils"),
AppInit = require("utils/AppInit"),
Strings = require("strings");
var defaultPrefs = {
currentSort: Commands.SORT_WORKINGSET_BY_ADDED,
automaticSort: false
};
/**
* @private
* @type {PreferenceStorage}
*/
var _prefs = {};
/**
* @private
* @type {Array.<Sort>}
*/
var _sorts = [];
/**
* @private
* @type {Sort}
*/
var _currentSort = null;
/**
* @private
* @type {boolean}
*/
var _automaticSort = false;
/**
* @private
* @type {boolean}
* Used to know when to do the automatic sort for MRU order.
*/
var _openedDocument = false;
/**
* Retrieves a Sort object by id
* @param {(string|Command)} command A command ID or a command object.
* @return {?Sort}
*/
function get(command) {
var commandID;
if (!command) {
console.error("Attempting to get a Sort method with a missing required parameter: command");
return;
}
if (typeof command === "string") {
commandID = command;
} else {
commandID = command.getID();
}
return _sorts[commandID];
}
/**
* @return {boolean} Enabled state of Automatic Sort.
*/
function getAutomatic() {
return _automaticSort;
}
/**
* @private
* Removes the sort DocumentManager listeners.
*/
function _removeListeners() {
$(DocumentManager).off(".sort");
}
/**
* Enables/Disables Automatic Sort depending on the value.
* @param {boolean} enable True to enable, false to disable.
*/
function setAutomatic(enable) {
_automaticSort = enable;
_prefs.setValue("automaticSort", _automaticSort);
CommandManager.get(Commands.SORT_WORKINGSET_AUTO).setChecked(_automaticSort);
if (enable) {
_currentSort.sort();
} else {
_removeListeners();
}
}
/**
* @private
* Adds the current sort DocumentManager listeners.
*/
function _addListeners() {
if (_automaticSort && _currentSort && _currentSort.getEvents()) {
$(DocumentManager)
.on(_currentSort.getEvents(), function () {
_currentSort.sort();
})
.on("workingSetDisableAutoSorting.sort", function () {
setAutomatic(false);
});
}
}
/**
* @private
* Sets the current sort method and checks it on the context menu.
* @param {Sort} newSort
*/
function _setCurrentSort(newSort) {
var command;
if (_currentSort !== newSort) {
if (_currentSort !== null) {
command = CommandManager.get(_currentSort.getCommandID());
if (command) {
command.setChecked(false);
}
}
command = CommandManager.get(newSort.getCommandID());
if (command) {
command.setChecked(true);
}
CommandManager.get(Commands.SORT_WORKINGSET_AUTO).setEnabled(!!newSort.getEvents());
_currentSort = newSort;
_prefs.setValue("currentSort", _currentSort.getCommandID());
}
}
/**
* @constructor
* @private
*
* @param {string} commandID A valid command identifier.
* @param {function(FileEntry, FileEntry): number} compareFn A valid sort
* function (see register for a longer explanation).
* @param {string} events Space-separated DocumentManager possible events
* ending with ".sort".
*/
function Sort(commandID, compareFn, events, automaticFn) {
this._commandID = commandID;
this._compareFn = compareFn;
this._events = events;
}
/** @return {string} The Command ID */
Sort.prototype.getCommandID = function () {
return this._commandID;
};
/** @return {function(FileEntry, FileEntry): number} The compare function */
Sort.prototype.getCompareFn = function () {
return this._compareFn;
};
/** @return {string} The DocumentManager events */
Sort.prototype.getEvents = function () {
return this._events;
};
/**
* Performs the sort and makes it the current sort method.
*/
Sort.prototype.execute = function () {
_setCurrentSort(this);
this.sort();
};
/**
* Only performs the working set sort if this is the current sort.
*/
Sort.prototype.sort = function () {
if (_currentSort === this) {
_removeListeners();
DocumentManager.sortWorkingSet(this._compareFn);
_addListeners();
}
};
/**
* Registers a working set sort method.
* @param {(string|Command)} command A command ID or a command object
* @param {function(FileEntry, FileEntry): number} compareFn The function that
* will be used inside JavaScript's sort function. The return a value
* should be >0 (sort a to a lower index than b), =0 (leaves a and b
* unchanged with respect to each other) or <0 (sort b to a lower index
* than a) and must always returns the same value when given a specific
* pair of elements a and b as its two arguments. Documentation at:
* https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/sort
* @param {?string} events One or more space-separated event types that
* DocumentManger uses. Each event passed will trigger the automatic
* sort. If no events are passed, the automatic sort will be disabled
* for that sort method.
* @return {?Sort}
*/
function register(command, compareFn, events) {
var commandID = "";
if (!command || !compareFn) {
console.log("Attempting to register a Sort method with a missing required parameter: command or compare function");
return;
}
if (typeof command === "string") {
commandID = command;
} else {
commandID = command.getID();
}
if (_sorts[commandID]) {
console.log("Attempting to register an already-registered Sort method: " + command);
return;
}
// Adds ".sort" to the end of each event to make them specific for the automatic sort.
if (events) {
events = events.split(" ");
events.forEach(function (event, index) {
events[index] = events[index].trim() + ".sort";
});
events = events.join(" ");
}
var sort = new Sort(commandID, compareFn, events);
_sorts[commandID] = sort;
return sort;
}
/** Command Handlers */
function _handleSortWorkingSetByAdded() {
get(Commands.SORT_WORKINGSET_BY_ADDED).execute();
}
function _handleSortWorkingSetByName() {
get(Commands.SORT_WORKINGSET_BY_NAME).execute();
}
function _handleSortWorkingSetByType() {
get(Commands.SORT_WORKINGSET_BY_TYPE).execute();
}
function _handleAutomaticSort() {
setAutomatic(!getAutomatic());
}
// Register Sort Methods
register(
Commands.SORT_WORKINGSET_BY_ADDED,
function (file1, file2) {
var index1 = DocumentManager.findInWorkingSetAddedOrder(file1.fullPath),
index2 = DocumentManager.findInWorkingSetAddedOrder(file2.fullPath);
return index1 - index2;
},
"workingSetAdd workingSetAddList"
);
register(
Commands.SORT_WORKINGSET_BY_NAME,
function (file1, file2) {
var name1, name2;
if (brackets.platform === "win") {
name1 = FileUtils.getFileNameExtension(file1.name).name;
name2 = FileUtils.getFileNameExtension(file2.name).name;
} else {
name1 = file1.name;
name2 = file2.name;
}
return name1.toLocaleLowerCase().localeCompare(name2.toLocaleLowerCase());
},
"workingSetAdd workingSetAddList"
);
register(
Commands.SORT_WORKINGSET_BY_TYPE,
function (file1, file2) {
var name1 = FileUtils.getFileNameExtension(file1.name),
name2 = FileUtils.getFileNameExtension(file2.name),
cmpExt = name1.extension.localeCompare(name2.extension);
name1 = brackets.platform === "win" ? name1.name : file1.name;
name2 = brackets.platform === "win" ? name2.name : file2.name;
if (cmpExt === 0) {
return name1.toLocaleLowerCase().localeCompare(name2.toLocaleLowerCase());
} else {
return cmpExt;
}
},
"workingSetAdd workingSetAddList"
);
// Register Command Handlers
CommandManager.register(Strings.CMD_SORT_WORKINGSET_BY_ADDED, Commands.SORT_WORKINGSET_BY_ADDED, _handleSortWorkingSetByAdded);
CommandManager.register(Strings.CMD_SORT_WORKINGSET_BY_NAME, Commands.SORT_WORKINGSET_BY_NAME, _handleSortWorkingSetByName);
CommandManager.register(Strings.CMD_SORT_WORKINGSET_BY_TYPE, Commands.SORT_WORKINGSET_BY_TYPE, _handleSortWorkingSetByType);
CommandManager.register(Strings.CMD_SORT_WORKINGSET_AUTO, Commands.SORT_WORKINGSET_AUTO, _handleAutomaticSort);
// Initialize PreferenceStorage
_prefs = PreferencesManager.getPreferenceStorage(module, defaultPrefs);
//TODO: Remove preferences migration code
PreferencesManager.handleClientIdChange(_prefs, "com.adobe.brackets.WorkingSetSort");
// Initialize items dependent on extensions/workingSet
AppInit.appReady(function () {
var curSort = get(_prefs.getValue("currentSort")),
autoSort = _prefs.getValue("automaticSort");
if (curSort) {
_setCurrentSort(curSort);
}
if (autoSort) {
setAutomatic(true);
}
if (curSort && autoSort) {
curSort.sort();
}
});
// Define public API
exports.register = register;
exports.get = get;
exports.getAutomatic = getAutomatic;
exports.setAutomatic = setAutomatic;
});