Modules (181)

WorkingSetSort

Description

Manages the workingSetList sort methods.

Dependencies

Variables

Private

_LEGACY_SORT_PREF

Private

Legacy preference name

    var _LEGACY_SORT_PREF = "currentSort";
Private

_SORT_EVENT_NAMES

Private

Events which the sort command will listen for to trigger a sort

    var _SORT_EVENT_NAMES = "workingSetAdd workingSetAddList";
Private

_WORKING_SET_SORT_PREF

Private

Preference name

    var _WORKING_SET_SORT_PREF = "workingSetSortMethod";
Private

_automaticSort

Type
boolean
Private

Denotes if automatic sorting is enabled or not

    var _automaticSort = false;
Private

_currentSort

Type
Sort
Private

Denotes the current sort method object

    var _currentSort = null;
Private

_sortPrefConversionMap

Type
object.<string: string>
Private

Maps Legacy sort method names to new sort method names

    var _sortPrefConversionMap = {
        "view.sortWorkingSetByAdded" : "cmd.sortWorkingSetByAdded",
        "view.sortWorkingSetByName"  : "cmd.sortWorkingSetByName",
        "view.sortWorkingSetByType"  : "cmd.sortWorkingSetByType"
    };
Private

_sorts

Type
Array.<Sort>
Private

List of sorting method objects

    var _sorts = [];

Functions

Private

_addListeners

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);
                });
        }
    }
Private

_convertSortPref

Converts the old brackets working set sort preference into the modern paneview sort preference

sortMethod
non-nullable
string
- sort preference to convert
Returns:
?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;
    }
Private

_handleSort

Command Handler for CMD_WORKINGSET_SORTBY*

commandId
non-nullable
string
identifies the sort method to use
    function _handleSort(commandId) {
        get(commandId).execute();
    }
Private

_handleToggleAutoSort

Command Handler for CMD_WORKING_SORT_TOGGLE_AUTO

    function _handleToggleAutoSort() {
        setAutomatic(!getAutomatic());
    }
Private

_removeListeners

Removes the sort listeners.

    function _removeListeners() {
        MainViewManager.off(".sort");
    }
Private

_setCurrentSort

Sets the current sort method and checks it on the context menu.

newSort
Sort
    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;
        }
    }
Public API

get

Retrieves a Sort object by id

command
(string,Command)
A command ID or a command object.
Returns:
?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];
    }
Public API

getAutomatic

Returns:
boolean
Enabled state of Automatic Sort.
    function getAutomatic() {
        return _automaticSort;
    }

initSortMethod

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;
    }
Public API

register

Registers a working set sort method.

command
(string,Command)
A command ID or a command object
compareFn
function(File,File): number
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
events
nullable
string
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.
Returns:
?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;
    }
Public API

setAutomatic

Enables/Disables Automatic Sort depending on the value.

enable
boolean
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();
        }
    }

Classes

Constructor

Sort

commandID
string
A valid command identifier.
compareFn
function(File,File): number
A valid sort function (see register for a longer explanation).
events
string
Space-separated WorkingSetSort possible events ending with ".sort".
    function Sort(commandID, compareFn, events, automaticFn) {
        this._commandID = commandID;
        this._compareFn = compareFn;
        this._events    = events;
    }

Methods

execute

Performs the sort and makes it the current sort method.

    Sort.prototype.execute = function () {
        _setCurrentSort(this);
        this.sort();
    };

getCommandID

The Command ID

Returns:
string
    Sort.prototype.getCommandID = function () {
        return this._commandID;
    };

getCompareFn

The compare function

Returns:
function(File,File): number
    Sort.prototype.getCompareFn = function () {
        return this._compareFn;
    };

getEvents

Gets the event that this sort object is listening to

Returns:
string
    Sort.prototype.getEvents = function () {
        return this._events;
    };

setChecked

Checks/Unchecks the command which will show a check in the menu

value
boolean
    Sort.prototype.setChecked = function (value) {
        var command = CommandManager.get(this._commandID);
        if (command) {
            command.setChecked(value);
        }
    };

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();
        }
    };