Manages the workingSetList sort methods.
Legacy preference name
var _LEGACY_SORT_PREF = "currentSort";
Events which the sort command will listen for to trigger a sort
var _SORT_EVENT_NAMES = "workingSetAdd workingSetAddList";
Preference name
var _WORKING_SET_SORT_PREF = "workingSetSortMethod";
Denotes if automatic sorting is enabled or not
var _automaticSort = false;
Maps Legacy sort method names to new sort method names
var _sortPrefConversionMap = {
"view.sortWorkingSetByAdded" : "cmd.sortWorkingSetByAdded",
"view.sortWorkingSetByName" : "cmd.sortWorkingSetByName",
"view.sortWorkingSetByType" : "cmd.sortWorkingSetByType"
};
Adds the current sort MainViewManager listeners.
function _addListeners() {
if (_automaticSort && _currentSort && _currentSort.getEvents()) {
MainViewManager
.on(_currentSort.getEvents(), function () {
_currentSort.sort();
})
.on("_workingSetDisableAutoSort.sort", function () {
setAutomatic(false);
});
}
}
Converts the old brackets working set sort preference into the modern paneview sort preference
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;
}
Command Handler for CMD_WORKINGSET_SORT_BY_*
function _handleSort(commandId) {
get(commandId).execute();
}
Command Handler for CMD_WORKING_SORT_TOGGLE_AUTO
function _handleToggleAutoSort() {
setAutomatic(!getAutomatic());
}
Removes the sort listeners.
function _removeListeners() {
MainViewManager.off(".sort");
}
Sets the current sort method and checks it on the context menu.
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;
}
}
Retrieves a Sort object by id
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];
}
function getAutomatic() {
return _automaticSort;
}
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;
}
Registers a working set sort method.
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;
}
Enables/Disables Automatic Sort depending on the value.
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();
}
}
function Sort(commandID, compareFn, events, automaticFn) {
this._commandID = commandID;
this._compareFn = compareFn;
this._events = events;
}
Performs the sort and makes it the current sort method.
Sort.prototype.execute = function () {
_setCurrentSort(this);
this.sort();
};
The Command ID
Sort.prototype.getCommandID = function () {
return this._commandID;
};
The compare function
Sort.prototype.getCompareFn = function () {
return this._compareFn;
};
Gets the event that this sort object is listening to
Sort.prototype.getEvents = function () {
return this._events;
};