-
-
Notifications
You must be signed in to change notification settings - Fork 194
Expand file tree
/
Copy pathWorkingSetSort.js
More file actions
455 lines (400 loc) · 13.2 KB
/
WorkingSetSort.js
File metadata and controls
455 lines (400 loc) · 13.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
/*
* GNU AGPL-3.0 License
*
* Copyright (c) 2021 - present core.ai . All rights reserved.
* Original work Copyright (c) 2014 - 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.
*
*/
// @INCLUDE_IN_API_DOCS
/**
* Manages the workingSetList sort methods.
*/
define(function (require, exports, module) {
var Commands = require("command/Commands"),
CommandManager = require("command/CommandManager"),
MainViewManager = require("view/MainViewManager"),
PreferencesManager = require("preferences/PreferencesManager"),
FileUtils = require("file/FileUtils"),
AppInit = require("utils/AppInit"),
Strings = require("strings"),
_ = require("thirdparty/lodash");
/**
* List of sorting method objects
*
* @private
* @type {Array.<Sort>}
*/
var _sorts = [];
/**
* Denotes the current sort method object
*
* @private
* @type {Sort}
*/
var _currentSort = null;
/**
* Denotes if automatic sorting is enabled or not
*
* @private
* @type {boolean}
*/
var _automaticSort = false;
/**
* Maps legacy sort method names to new sort method names.
*
* @private
* @type {Object.<string, string>} An object where each key is the legacy method name
* and the corresponding value is the new method name.
*/
var _sortPrefConversionMap = {
"view.sortWorkingSetByAdded": "cmd.sortWorkingSetByAdded",
"view.sortWorkingSetByName": "cmd.sortWorkingSetByName",
"view.sortWorkingSetByType": "cmd.sortWorkingSetByType"
};
/**
* Events which the sort command will listen for to trigger a sort
*
* @constant {string}
* @private
*/
var _SORT_EVENT_NAMES = "workingSetAdd workingSetAddList";
/**
* Preference name
*
* @constant {string}
* @private
*/
var _WORKING_SET_SORT_PREF = "workingSetSortMethod";
/**
* Legacy preference name
*
* @constant {string}
* @private
*/
var _LEGACY_SORT_PREF = "currentSort";
/**
* 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];
}
/**
* Converts the old brackets working set sort preference into the modern paneview sort preference
*
* @private
* @param {!string} sortMethod - sort preference to convert
* @return {?string} new sort preference string or undefined if an sortMethod is not found
*/
function _convertSortPref(sortMethod) {
if (!sortMethod) {
return null;
}
if (_sortPrefConversionMap.hasOwnProperty(sortMethod)) {
sortMethod = _sortPrefConversionMap[sortMethod];
PreferencesManager.setViewState(_WORKING_SET_SORT_PREF, sortMethod);
} else {
sortMethod = null;
}
return sortMethod;
}
/**
* @return {boolean} Enabled state of Automatic Sort.
*/
function getAutomatic() {
return _automaticSort;
}
/**
* Removes the sort listeners.
*
* @private
*/
function _removeListeners() {
MainViewManager.off(".sort");
}
/**
* Enables/Disables Automatic Sort depending on the value.
*
* @param {boolean} enable True to enable, false to disable.
*/
function setAutomatic(enable) {
_automaticSort = enable;
PreferencesManager.setViewState("automaticSort", _automaticSort);
CommandManager.get(Commands.CMD_WORKING_SORT_TOGGLE_AUTO).setChecked(_automaticSort);
_currentSort.setChecked(_automaticSort);
if (_automaticSort) {
_currentSort.sort();
} else {
_removeListeners();
}
}
/**
* Adds the current sort MainViewManager listeners.
*
* @private
*/
function _addListeners() {
if (_automaticSort && _currentSort && _currentSort.getEvents()) {
MainViewManager
.on(_currentSort.getEvents(), function () {
_currentSort.sort();
})
.on("_workingSetDisableAutoSort.sort", function () {
setAutomatic(false);
});
}
}
/**
* Sets the current sort method and checks it on the context menu.
*
* @private
* @param {Sort} newSort
*/
function _setCurrentSort(newSort) {
if (_currentSort !== newSort) {
if (_currentSort !== null) {
_currentSort.setChecked(false);
}
if (_automaticSort) {
newSort.setChecked(true);
}
CommandManager.get(Commands.CMD_WORKING_SORT_TOGGLE_AUTO).setEnabled(!!newSort.getEvents());
PreferencesManager.setViewState(_WORKING_SET_SORT_PREF, newSort.getCommandID());
_currentSort = newSort;
}
}
/**
* @constructor
* @param {string} commandID A valid command identifier.
* @param {function(File, File): number} compareFn A valid sort
* function (see register for a longer explanation).
* @param {string} events Space-separated WorkingSetSort possible events
* ending with ".sort".
*/
function Sort(commandID, compareFn, events, automaticFn) {
this._commandID = commandID;
this._compareFn = compareFn;
this._events = events;
}
/**
* The Command ID
*
* @return {string}
*/
Sort.prototype.getCommandID = function () {
return this._commandID;
};
/**
* The compare function
*
* @return {function(File, File): number}
*/
Sort.prototype.getCompareFn = function () {
return this._compareFn;
};
/**
* Gets the event that this sort object is listening to
*
* @return {string}
*/
Sort.prototype.getEvents = function () {
return this._events;
};
/**
* Checks/Unchecks the command which will show a check in the menu
*
* @param {boolean} value
*/
Sort.prototype.setChecked = function (value) {
var command = CommandManager.get(this._commandID);
if (command) {
command.setChecked(value);
}
};
/**
* 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();
MainViewManager._sortWorkingSet(MainViewManager.ALL_PANES, this._compareFn);
_addListeners();
}
};
/**
* Registers a working set sort method.
*
* @param {(string|Command)} command A command ID or a command object
* @param {function(File, File): 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 Handler for CMD_WORKING_SORT_TOGGLE_AUTO
*
* @private
*/
function _handleToggleAutoSort() {
setAutomatic(!getAutomatic());
}
/**
* Command Handler for CMD_WORKINGSET_SORT_BY_*
*
* @private
* @param {!string} commandId identifies the sort method to use
*/
function _handleSort(commandId) {
get(commandId).execute();
}
/**
* Register Sort Methods
*/
register(
Commands.CMD_WORKINGSET_SORT_BY_ADDED,
function (paneId, file1, file2) {
var index1 = MainViewManager.findInWorkingSetByAddedOrder(paneId, file1.fullPath),
index2 = MainViewManager.findInWorkingSetByAddedOrder(paneId, file2.fullPath);
return index1 - index2;
},
_SORT_EVENT_NAMES
);
register(
Commands.CMD_WORKINGSET_SORT_BY_NAME,
function (paneId, file1, file2) {
return FileUtils.compareFilenames(file1.name, file2.name, false);
},
_SORT_EVENT_NAMES
);
register(
Commands.CMD_WORKINGSET_SORT_BY_TYPE,
function (paneId, file1, file2) {
return FileUtils.compareFilenames(file1.name, file2.name, true);
},
_SORT_EVENT_NAMES
);
/**
* Register Command Handlers
*/
CommandManager.register(Strings.CMD_WORKINGSET_SORT_BY_ADDED, Commands.CMD_WORKINGSET_SORT_BY_ADDED, _.partial(_handleSort, Commands.CMD_WORKINGSET_SORT_BY_ADDED));
CommandManager.register(Strings.CMD_WORKINGSET_SORT_BY_NAME, Commands.CMD_WORKINGSET_SORT_BY_NAME, _.partial(_handleSort, Commands.CMD_WORKINGSET_SORT_BY_NAME));
CommandManager.register(Strings.CMD_WORKINGSET_SORT_BY_TYPE, Commands.CMD_WORKINGSET_SORT_BY_TYPE, _.partial(_handleSort, Commands.CMD_WORKINGSET_SORT_BY_TYPE));
CommandManager.register(Strings.CMD_WORKING_SORT_TOGGLE_AUTO, Commands.CMD_WORKING_SORT_TOGGLE_AUTO, _handleToggleAutoSort);
/**
* Initialize default values for sorting preferences
*
* @private
*/
PreferencesManager.stateManager.definePreference("automaticSort", "boolean", false);
/**
* Define a default sort method that's empty so that we
* just convert and use the legacy sort method
*
* @private
*/
PreferencesManager.stateManager.definePreference(_WORKING_SET_SORT_PREF, "string", "");
/*
* initializes global sort method from preference settings or the default
*/
function initSortMethod() {
var sortMethod = PreferencesManager.getViewState(_WORKING_SET_SORT_PREF);
if (!sortMethod) {
sortMethod = _convertSortPref(PreferencesManager.getViewState(_LEGACY_SORT_PREF));
}
if (!sortMethod) {
sortMethod = Commands.CMD_WORKINGSET_SORT_BY_ADDED;
}
return sortMethod;
}
/**
* Initialize items dependent on extensions/workingSetList
*
* @private
*/
AppInit.appReady(function () {
var sortMethod = initSortMethod(),
curSort = get(sortMethod),
autoSort = PreferencesManager.getViewState("automaticSort");
if (curSort) {
_setCurrentSort(curSort);
}
if (autoSort) {
setAutomatic(autoSort);
}
if (curSort && autoSort) {
curSort.sort();
}
});
// Public API
exports.register = register;
exports.get = get;
exports.getAutomatic = getAutomatic;
exports.setAutomatic = setAutomatic;
});