forked from phcode-dev/staging.phcode.dev
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWorkingSetSort.js
More file actions
1 lines (1 loc) · 5.37 KB
/
WorkingSetSort.js
File metadata and controls
1 lines (1 loc) · 5.37 KB
1
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"),_sorts=[],_currentSort=null,_automaticSort=!1,_sortPrefConversionMap={"view.sortWorkingSetByAdded":"cmd.sortWorkingSetByAdded","view.sortWorkingSetByName":"cmd.sortWorkingSetByName","view.sortWorkingSetByType":"cmd.sortWorkingSetByType"},_SORT_EVENT_NAMES="workingSetAdd workingSetAddList",_WORKING_SET_SORT_PREF="workingSetSortMethod",_LEGACY_SORT_PREF="currentSort";function get(command){var commandID;if(command)return commandID="string"==typeof command?command:command.getID(),_sorts[commandID];console.error("Attempting to get a Sort method with a missing required parameter: command")}function _convertSortPref(sortMethod){return sortMethod?(_sortPrefConversionMap.hasOwnProperty(sortMethod)?(sortMethod=_sortPrefConversionMap[sortMethod],PreferencesManager.setViewState(_WORKING_SET_SORT_PREF,sortMethod)):sortMethod=null,sortMethod):null}function getAutomatic(){return _automaticSort}function _removeListeners(){MainViewManager.off(".sort")}function setAutomatic(enable){_automaticSort=enable,PreferencesManager.setViewState("automaticSort",_automaticSort),CommandManager.get(Commands.CMD_WORKING_SORT_TOGGLE_AUTO).setChecked(_automaticSort),_currentSort.setChecked(_automaticSort),_automaticSort?_currentSort.sort():_removeListeners()}function _addListeners(){_automaticSort&&_currentSort&&_currentSort.getEvents()&&MainViewManager.on(_currentSort.getEvents(),function(){_currentSort.sort()}).on("_workingSetDisableAutoSort.sort",function(){setAutomatic(!1)})}function _setCurrentSort(newSort){_currentSort!==newSort&&(null!==_currentSort&&_currentSort.setChecked(!1),_automaticSort&&newSort.setChecked(!0),CommandManager.get(Commands.CMD_WORKING_SORT_TOGGLE_AUTO).setEnabled(!!newSort.getEvents()),PreferencesManager.setViewState(_WORKING_SET_SORT_PREF,newSort.getCommandID()),_currentSort=newSort)}function Sort(commandID,compareFn,events,automaticFn){this._commandID=commandID,this._compareFn=compareFn,this._events=events}function register(command,compareFn,events){var commandID="";if(command&&compareFn){if(commandID="string"==typeof command?command:command.getID(),!_sorts[commandID]){events&&((events=events.split(" ")).forEach(function(event,index){events[index]=events[index].trim()+".sort"}),events=events.join(" "));var sort=new Sort(commandID,compareFn,events);return _sorts[commandID]=sort,sort}console.log("Attempting to register an already-registered Sort method: "+command)}else console.log("Attempting to register a Sort method with a missing required parameter: command or compare function")}function _handleToggleAutoSort(){setAutomatic(!getAutomatic())}function _handleSort(commandId){get(commandId).execute()}function initSortMethod(){var sortMethod=PreferencesManager.getViewState(_WORKING_SET_SORT_PREF);return sortMethod||(sortMethod=_convertSortPref(PreferencesManager.getViewState(_LEGACY_SORT_PREF))),sortMethod||(sortMethod=Commands.CMD_WORKINGSET_SORT_BY_ADDED),sortMethod}Sort.prototype.getCommandID=function(){return this._commandID},Sort.prototype.getCompareFn=function(){return this._compareFn},Sort.prototype.getEvents=function(){return this._events},Sort.prototype.setChecked=function(value){var command=CommandManager.get(this._commandID);command&&command.setChecked(value)},Sort.prototype.execute=function(){_setCurrentSort(this),this.sort()},Sort.prototype.sort=function(){_currentSort===this&&(_removeListeners(),MainViewManager._sortWorkingSet(MainViewManager.ALL_PANES,this._compareFn),_addListeners())},register(Commands.CMD_WORKINGSET_SORT_BY_ADDED,function(paneId,file1,file2){var index1,index2;return MainViewManager.findInWorkingSetByAddedOrder(paneId,file1.fullPath)-MainViewManager.findInWorkingSetByAddedOrder(paneId,file2.fullPath)},_SORT_EVENT_NAMES),register(Commands.CMD_WORKINGSET_SORT_BY_NAME,function(paneId,file1,file2){return FileUtils.compareFilenames(file1.name,file2.name,!1)},_SORT_EVENT_NAMES),register(Commands.CMD_WORKINGSET_SORT_BY_TYPE,function(paneId,file1,file2){return FileUtils.compareFilenames(file1.name,file2.name,!0)},_SORT_EVENT_NAMES),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),PreferencesManager.stateManager.definePreference("automaticSort","boolean",!1),PreferencesManager.stateManager.definePreference(_WORKING_SET_SORT_PREF,"string",""),AppInit.appReady(function(){var sortMethod,curSort=get(initSortMethod()),autoSort=PreferencesManager.getViewState("automaticSort");curSort&&_setCurrentSort(curSort),autoSort&&setAutomatic(autoSort),curSort&&autoSort&&curSort.sort()}),exports.register=register,exports.get=get,exports.getAutomatic=getAutomatic,exports.setAutomatic=setAutomatic});