forked from phcode-dev/staging.phcode.dev
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEditorStatusBar.js
More file actions
1 lines (1 loc) · 13.9 KB
/
EditorStatusBar.js
File metadata and controls
1 lines (1 loc) · 13.9 KB
1
define(function(require,exports,module){const _=require("thirdparty/lodash"),AnimationUtils=require("utils/AnimationUtils"),AppInit=require("utils/AppInit"),DropdownButton=require("widgets/DropdownButton"),EditorManager=require("editor/EditorManager"),MainViewManager=require("view/MainViewManager"),Editor=require("editor/Editor").Editor,KeyEvent=require("utils/KeyEvent"),LanguageManager=require("language/LanguageManager"),PreferencesManager=require("preferences/PreferencesManager"),StatusBar=require("widgets/StatusBar"),Strings=require("strings"),InMemoryFile=require("document/InMemoryFile"),ProjectManager=require("project/ProjectManager"),Async=require("utils/Async"),TaskManager=require("features/TaskManager"),FileSystem=require("filesystem/FileSystem"),CommandManager=require("command/CommandManager"),Commands=require("command/Commands"),DocumentManager=require("document/DocumentManager"),StringUtils=require("utils/StringUtils"),Metrics=require("utils/Metrics"),SupportedEncodings=fs.SUPPORTED_ENCODINGS.sort();var languageSelect,encodingSelect,tasksSelect,$cursorInfo,$statusInfo,$fileInfo,$indentType,$indentAuto,$indentWidthLabel,$indentWidthInput,$statusOverwrite,LANGUAGE_SET_AS_DEFAULT={};function _formatCountable(number,singularStr,pluralStr){return StringUtils.format(number>1?pluralStr:singularStr,number)}function _updateLanguageInfo(editor){var doc,lang=editor.document.getLanguage();languageSelect.$button.text(lang.getName())}function _updateEncodingInfo(editor){var doc=editor.document;doc.file._encoding||(doc.file._encoding="utf8"),encodingSelect.$button.text(doc.file._encoding)}function _getLineCountStr(editor){const lines=editor.lineCount();return _formatCountable(lines,Strings.STATUSBAR_LINE_COUNT_SINGULAR,Strings.STATUSBAR_LINE_COUNT_PLURAL)}function _updateFileInfo(editor){$fileInfo.text(_getLineCountStr(editor))}function _updateIndentType(fullPath){var indentWithTabs=Editor.getUseTabChar(fullPath);$indentType.text(indentWithTabs?Strings.STATUSBAR_TAB_SIZE:Strings.STATUSBAR_SPACES),$indentType.attr("title",indentWithTabs?Strings.STATUSBAR_INDENT_TOOLTIP_SPACES:Strings.STATUSBAR_INDENT_TOOLTIP_TABS),$indentWidthLabel.attr("title",indentWithTabs?Strings.STATUSBAR_INDENT_SIZE_TOOLTIP_TABS:Strings.STATUSBAR_INDENT_SIZE_TOOLTIP_SPACES)}function _updateAutoIndent(fullPath){const autoIndent=Editor.getAutoTabSpaces(fullPath);$indentAuto.html(autoIndent?Strings.STATUSBAR_AUTO_INDENT:Strings.STATUSBAR_FIXED_INDENT)}function _getIndentSize(fullPath){return Editor.getUseTabChar(fullPath)?Editor.getTabSize(fullPath):Editor.getSpaceUnits(fullPath)}function _updateIndentSize(fullPath){var size=_getIndentSize(fullPath);$indentWidthLabel.text(size),$indentWidthInput.val(size)}function _toggleIndentType(){var current=EditorManager.getActiveEditor(),fullPath=current&¤t.document.file.fullPath;Editor.setUseTabChar(!Editor.getUseTabChar(fullPath),fullPath),_updateIndentType(fullPath),_updateAutoIndent(fullPath),_updateIndentSize(fullPath)}function _toggleAutoIndent(){const current=EditorManager.getActiveEditor(),fullPath=current&¤t.document.file.fullPath;Editor.setAutoTabSpaces(!Editor.getAutoTabSpaces(fullPath),fullPath),Editor.getAutoTabSpaces(fullPath)&&Editor._autoDetectTabSpaces(current,!0,!0),_updateIndentType(fullPath),_updateAutoIndent(fullPath),_updateIndentSize(fullPath)}function _updateCursorInfo(event,editor){const cursor=(editor=editor||EditorManager.getActiveEditor()).getCursorPos(!0);let cursorStr=StringUtils.format(Strings.STATUSBAR_CURSOR_POSITION,cursor.line+1,cursor.ch+1),cursorStrShort=StringUtils.format(Strings.STATUSBAR_CURSOR_POSITION_SHORT,cursor.line+1,cursor.ch+1),sels=editor.getSelections(),selStr="",shortSelStr="";if(sels.length>1)Metrics.countEvent(Metrics.EVENT_TYPE.EDITOR,"multiCursor","usage"),selStr=StringUtils.format(Strings.STATUSBAR_SELECTION_MULTIPLE,sels.length),shortSelStr=StringUtils.format(Strings.STATUSBAR_SELECTION_MULTIPLE_SHORT_DO_NOT_TRANSLATE,sels.length);else if(editor.hasSelection()){const sel=sels[0];if(sel.start.line!==sel.end.line){let lines=sel.end.line-sel.start.line+1;0===sel.end.ch&&lines--,selStr=_formatCountable(lines,Strings.STATUSBAR_SELECTION_LINE_SINGULAR,Strings.STATUSBAR_SELECTION_LINE_PLURAL),shortSelStr=StringUtils.format(Strings.STATUSBAR_SELECTION_SHORT_DO_NOT_TRANSLATE,lines)}else{const cols=editor.getColOffset(sel.end)-editor.getColOffset(sel.start);selStr=_formatCountable(cols,Strings.STATUSBAR_SELECTION_CH_SINGULAR,Strings.STATUSBAR_SELECTION_CH_PLURAL),shortSelStr=StringUtils.format(Strings.STATUSBAR_SELECTION_SHORT_DO_NOT_TRANSLATE,cols)}}$cursorInfo.text(cursorStrShort+shortSelStr),$statusInfo.attr("title",cursorStr+selStr+" "+_getLineCountStr(editor)+"\n"+Strings.STATUSBAR_CURSOR_GOTO)}function _changeIndentWidth(fullPath,value){$indentWidthLabel.removeClass("hidden"),$indentWidthInput.addClass("hidden"),$indentWidthInput.off("blur keyup"),MainViewManager.focusActivePane();var valInt=parseInt(value,10);if(Editor.getUseTabChar(fullPath)){if(!Editor.setTabSize(valInt,fullPath))return}else if(!Editor.setSpaceUnits(valInt,fullPath))return;_updateIndentSize(fullPath),_updateCursorInfo()}function _updateOverwriteLabel(event,editor,newstate,doNotAnimate){$statusOverwrite.text()!==(newstate?Strings.STATUSBAR_OVERWRITE:Strings.STATUSBAR_INSERT)&&($statusOverwrite.text(newstate?Strings.STATUSBAR_OVERWRITE:Strings.STATUSBAR_INSERT),doNotAnimate||AnimationUtils.animateUsingClass($statusOverwrite[0],"flash",1500))}function _updateEditorOverwriteMode(event){var editor=EditorManager.getActiveEditor(),newstate=!editor._codeMirror.state.overwrite;_updateOverwriteLabel(event,editor,newstate,!0),editor.toggleOverwrite(newstate)}function _initOverwriteMode(currentEditor){currentEditor.toggleOverwrite($statusOverwrite.text()===Strings.STATUSBAR_OVERWRITE),$statusOverwrite.attr("title",Strings.STATUSBAR_INSOVR_TOOLTIP)}function _onActiveEditorChange(event,current,previous){if(previous&&(previous.off(".statusbar"),previous.document.off(".statusbar"),previous.document.releaseRef()),current){Editor._autoDetectTabSpaces(current);const fullPath=current.document.file.fullPath;StatusBar.showAllPanes(),current.on("cursorActivity.statusbar",_updateCursorInfo),current.on("optionChange.statusbar",function(){_updateIndentType(fullPath),_updateAutoIndent(fullPath),_updateIndentSize(fullPath)}),current.on("change.statusbar",function(){window.setTimeout(function(){_updateFileInfo(current)},0)}),current.on("overwriteToggle.statusbar",_updateOverwriteLabel),current.document.addRef(),current.document.on("languageChanged.statusbar",function(){_updateLanguageInfo(current)}),_updateCursorInfo(null,current),_updateLanguageInfo(current),_updateEncodingInfo(current),_updateFileInfo(current),_initOverwriteMode(current),_updateIndentType(fullPath),_updateAutoIndent(fullPath),_updateIndentSize(fullPath)}else StatusBar.hideAllPanes()}function _populateLanguageDropdown(){var languages=_.values(LanguageManager.getLanguages()).filter(function(language){return!language.isBinary()});languages.sort(function(a,b){return a.getName().toLowerCase().localeCompare(b.getName().toLowerCase())}),languageSelect.items=languages,languageSelect.items.unshift("---"),languageSelect.items.unshift(LANGUAGE_SET_AS_DEFAULT)}function _changeEncodingAndReloadDoc(document){var promise=document.reload();promise.done(function(text,readTimestamp){encodingSelect.$button.text(document.file._encoding);const encoding=PreferencesManager.getViewState("encoding",PreferencesManager.STATE_PROJECT_CONTEXT);encoding[document.file.fullPath]=document.file._encoding,PreferencesManager.setViewState("encoding",encoding,PreferencesManager.STATE_PROJECT_CONTEXT)}),promise.fail(function(error){console.log("Error reloading contents of "+document.file.fullPath,error)})}function _populateEncodingDropdown(){encodingSelect.items=SupportedEncodings}function _init(){$cursorInfo=$("#status-cursor"),$statusInfo=$("#status-info"),$fileInfo=$("#status-file"),$indentType=$("#indent-type"),$indentAuto=$("#indent-auto"),$indentWidthLabel=$("#indent-width-label"),$indentWidthInput=$("#indent-width-input"),$statusOverwrite=$("#status-overwrite"),$statusInfo.click(event=>{event.preventDefault(),event.stopPropagation(),CommandManager.execute(Commands.NAVIGATE_GOTO_LINE)}),(languageSelect=new DropdownButton.DropdownButton("",[],function(item,index){var document=EditorManager.getActiveEditor().document,defaultLang=LanguageManager.getLanguageForPath(document.file.fullPath,!0),label;if(item===LANGUAGE_SET_AS_DEFAULT)return{html:_.escape(StringUtils.format(Strings.STATUSBAR_SET_DEFAULT_LANG,LanguageManager.getCompoundFileExtension(document.file.fullPath))),enabled:document.getLanguage()!==defaultLang};var html=_.escape(item.getName());return item===defaultLang&&(html+=" <span class='default-language'>"+Strings.STATUSBAR_DEFAULT_LANG+"</span>"),item===document.getLanguage()&&(html="<span class='checked-language'></span>"+html),html})).dropdownExtraClasses="dropdown-status-bar",languageSelect.$button.addClass("btn-status-bar"),$("#status-language").append(languageSelect.$button),languageSelect.$button.attr("title",Strings.STATUSBAR_LANG_TOOLTIP),(encodingSelect=new DropdownButton.DropdownButton("",[],function(item,index){var document=EditorManager.getActiveEditor().document,html=_.escape(item);return"utf8"===item&&(html+=" <span class='default-language'>"+Strings.STATUSBAR_DEFAULT_LANG+"</span>"),item===document.file._encoding&&(html="<span class='checked-language'></span>"+html),html})).dropdownExtraClasses="dropdown-status-bar",encodingSelect.$button.addClass("btn-status-bar"),$("#status-encoding").append(encodingSelect.$button),encodingSelect.$button.attr("title",Strings.STATUSBAR_ENCODING_TOOLTIP);let hideSpinner=PreferencesManager.getViewState("StatusBar.HideSpinner");hideSpinner&&$("#status-tasks .spinner").addClass("hide-spinner"),tasksSelect=new DropdownButton.DropdownButton(Strings.STATUSBAR_TASKS,[Strings.STATUSBAR_TASKS_HIDE_SPINNER],function(item,index){return item===Strings.STATUSBAR_TASKS_HIDE_SPINNER?(hideSpinner=PreferencesManager.getViewState("StatusBar.HideSpinner"))?"<span class='checked-spinner'></span>"+item:item:TaskManager._renderItem(item,index)}),TaskManager._setTaskSelect(tasksSelect),tasksSelect.dropdownExtraClasses="dropdown-status-bar",tasksSelect.$button.addClass("btn-status-bar"),$("#status-tasks").append(tasksSelect.$button),tasksSelect.$button.attr("title",Strings.STATUSBAR_TASKS_TOOLTIP),tasksSelect.on("select",function(e,selection){return selection===Strings.STATUSBAR_TASKS_HIDE_SPINNER?(hideSpinner=!PreferencesManager.getViewState("StatusBar.HideSpinner"),PreferencesManager.setViewState("StatusBar.HideSpinner",hideSpinner),void(hideSpinner?$("#status-tasks .spinner").addClass("hide-spinner"):$("#status-tasks .spinner").removeClass("hide-spinner"))):TaskManager._onSelect(e,selection)}),tasksSelect.on(DropdownButton.EVENT_DROPDOWN_SHOWN,evt=>TaskManager._onDropdownShown(evt)),$indentType.on("click",_toggleIndentType),$indentAuto.on("click",_toggleAutoIndent),$indentWidthLabel.on("click",function(){var fullPath=EditorManager.getActiveEditor().document.file.fullPath;$indentWidthInput.val(_getIndentSize(fullPath)),$indentWidthLabel.addClass("hidden"),$indentWidthInput.removeClass("hidden"),$indentWidthInput.focus(),$indentWidthInput.on("blur",function(){_changeIndentWidth(fullPath,$indentWidthInput.val())}).on("keyup",function(event){event.keyCode===KeyEvent.DOM_VK_RETURN?$indentWidthInput.blur():event.keyCode===KeyEvent.DOM_VK_ESCAPE&&_changeIndentWidth(fullPath,!1)})}),$indentWidthInput.focus(function(){$indentWidthInput.select()}),languageSelect.on("select",function(e,lang){var document=EditorManager.getActiveEditor().document,fullPath=document.file.fullPath,fileType=document.file instanceof InMemoryFile?"newFile":"existingFile",filelanguageName=lang&&lang._name||"";if(Metrics.countEvent(Metrics.EVENT_TYPE.EDITOR,"languageChange",`${filelanguageName.toLowerCase()}-${fileType}`),lang===LANGUAGE_SET_AS_DEFAULT){var fileExtensionMap=PreferencesManager.get("language.fileExtensions");fileExtensionMap[LanguageManager.getCompoundFileExtension(fullPath)]=document.getLanguage().getId(),PreferencesManager.set("language.fileExtensions",fileExtensionMap)}else{var defaultLang=LanguageManager.getLanguageForPath(fullPath,!0);LanguageManager.setLanguageOverrideForPath(fullPath,lang===defaultLang?null:lang)}}),encodingSelect.on("select",function(e,encoding){var document=EditorManager.getActiveEditor().document,originalPath=document.file.fullPath,originalEncoding=document.file._encoding;document.file._encoding=encoding,document.file instanceof InMemoryFile||!document.isDirty?document.file instanceof InMemoryFile?encodingSelect.$button.text(encoding):document.isDirty||_changeEncodingAndReloadDoc(document):CommandManager.execute(Commands.FILE_SAVE_AS,{doc:document}).done(function(){var doc=DocumentManager.getCurrentDocument();originalPath===doc.file.fullPath?_changeEncodingAndReloadDoc(doc):document.file._encoding=originalEncoding}).fail(function(){document.file._encoding=originalEncoding})}),$statusOverwrite.on("click",_updateEditorOverwriteMode)}function _checkFileExistance(filePath,index,encoding){var deferred=new $.Deferred,fileEntry;return FileSystem.getFileForPath(filePath).exists(function(err,exists){!err&&exists?deferred.resolve():(delete encoding[filePath],deferred.reject())}),deferred.promise()}EditorManager.on("activeEditorChange",_onActiveEditorChange),ProjectManager.on("projectOpen",function(){let encoding=PreferencesManager.getViewState("encoding",PreferencesManager.STATE_PROJECT_CONTEXT);encoding||(encoding={},PreferencesManager.setViewState("encoding",encoding,PreferencesManager.STATE_PROJECT_CONTEXT)),Async.doSequentially(Object.keys(encoding),function(filePath,index){return _checkFileExistance(filePath,index,encoding)},!1).always(function(){PreferencesManager.setViewState("encoding",encoding,PreferencesManager.STATE_PROJECT_CONTEXT)})}),AppInit.htmlReady(_init),AppInit.appReady(function(){_populateLanguageDropdown(),_populateEncodingDropdown(),LanguageManager.on("languageAdded languageModified",_populateLanguageDropdown),_onActiveEditorChange(null,EditorManager.getActiveEditor(),null),StatusBar.show()})});