forked from phcode-dev/phcode.dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPane.js
More file actions
1 lines (1 loc) · 20.2 KB
/
Pane.js
File metadata and controls
1 lines (1 loc) · 20.2 KB
1
define(function(require,exports,module){var _=require("thirdparty/lodash"),Mustache=require("thirdparty/mustache/mustache"),EventDispatcher=require("utils/EventDispatcher"),FileSystem=require("filesystem/FileSystem"),InMemoryFile=require("document/InMemoryFile"),Editor=require("editor/Editor").Editor,ViewStateManager=require("view/ViewStateManager"),MainViewManager=require("view/MainViewManager"),PreferencesManager=require("preferences/PreferencesManager"),DocumentManager=require("document/DocumentManager"),CommandManager=require("command/CommandManager"),Commands=require("command/Commands"),Strings=require("strings"),StringUtils=require("utils/StringUtils"),ViewUtils=require("utils/ViewUtils"),ProjectManager=require("project/ProjectManager"),paneTemplate=require("text!htmlContent/pane.html"),FIRST_PANE="first-pane",SECOND_PANE="second-pane";function _makeIndexRequestObject(requestIndex,index){return{indexRequested:requestIndex,index:index}}function _ensurePaneIsFocused(paneId){var pane=MainViewManager._getPane(paneId);setTimeout(function(){pane.focus(),this._lastFocusedElement=pane.$el[0],MainViewManager.setActivePaneId(paneId)},1)}function Pane(id,$container){this._initialize();var self=this,showPaneHeaderButtonsPref=PreferencesManager.get("pane.showPaneHeaderButtons"),$el=$container.append(Mustache.render(paneTemplate,{id:id})).find("#"+id),$header=$el.find(".pane-header"),$headerText=$header.find(".pane-header-text"),$headerFlipViewBtn=$header.find(".pane-header-flipview-btn"),$headerCloseBtn=$header.find(".pane-header-close-btn"),$content=$el.find(".pane-content");switch($el.on("focusin.pane",function(e){self._lastFocusedElement=e.target}),$headerFlipViewBtn.on("click.pane",function(e){var currentFile=self.getCurrentlyViewedFile(),otherPaneId=self.id===FIRST_PANE?SECOND_PANE:FIRST_PANE,otherPane=MainViewManager._getPane(otherPaneId),sameDocInOtherView;if(otherPane.getViewForPath(currentFile.fullPath))CommandManager.execute(Commands.FILE_OPEN,{fullPath:currentFile.fullPath,paneId:otherPaneId}).always(function(){_ensurePaneIsFocused(otherPaneId)});else{var activePaneIdBeforeFlip=MainViewManager.getActivePaneId();MainViewManager._moveView(self.id,otherPaneId,currentFile).always(function(){CommandManager.execute(Commands.FILE_OPEN,{fullPath:currentFile.fullPath,paneId:otherPaneId}).always(function(){self.trigger("viewListChange"),otherPane.trigger("viewListChange"),_ensurePaneIsFocused(activePaneIdBeforeFlip)})})}}),$headerCloseBtn.on("click.pane",function(){MainViewManager.setActivePaneId(self.id);var file=self.getCurrentlyViewedFile();file?(CommandManager.execute(Commands.FILE_CLOSE,{File:file,paneId:self.id}),!self.getCurrentlyViewedFile()&&PreferencesManager.get("pane.mergePanesWhenLastFileClosed")&&MainViewManager.setLayoutScheme(1,1)):MainViewManager.setLayoutScheme(1,1)}),this._lastFocusedElement=$el[0],Object.defineProperty(this,"id",{get:function(){return id},set:function(){console.error("cannot change the id of a working pane")}}),Object.defineProperty(this,"$el",{get:function(){return $el},set:function(){console.error("cannot change the DOM node of a working pane")}}),Object.defineProperty(this,"$header",{get:function(){return $header},set:function(){console.error("cannot change the DOM node of a working pane")}}),Object.defineProperty(this,"$headerText",{get:function(){return $headerText},set:function(){console.error("cannot change the DOM node of a working pane")}}),Object.defineProperty(this,"$headerFlipViewBtn",{get:function(){return $headerFlipViewBtn},set:function(){console.error("cannot change the DOM node of a working pane")}}),Object.defineProperty(this,"$headerCloseBtn",{get:function(){return $headerCloseBtn},set:function(){console.error("cannot change the DOM node of a working pane")}}),Object.defineProperty(this,"$content",{get:function(){return $content},set:function(){console.error("cannot change the DOM node of a working pane")}}),Object.defineProperty(this,"$container",{get:function(){return $container},set:function(){console.error("cannot change the DOM node of a working pane")}}),this.updateHeaderText(),showPaneHeaderButtonsPref){case"always":this.$header.addClass("always-show-header-buttons");break;case"never":this.$headerFlipViewBtn.css("display","none"),this.$headerCloseBtn.css("display","none")}DocumentManager.on(this._makeEventName("fileNameChange"),_.bind(this._handleFileNameChange,this)),DocumentManager.on(this._makeEventName("pathDeleted"),_.bind(this._handleFileDeleted,this)),MainViewManager.on(this._makeEventName("activePaneChange"),_.bind(this._handleActivePaneChange,this)),MainViewManager.on(this._makeEventName("workingSetAdd"),_.bind(this.updateHeaderText,this)),MainViewManager.on(this._makeEventName("workingSetRemove"),_.bind(this.updateHeaderText,this)),MainViewManager.on(this._makeEventName("workingSetAddList"),_.bind(this.updateHeaderText,this)),MainViewManager.on(this._makeEventName("workingSetRemoveList"),_.bind(this.updateHeaderText,this)),MainViewManager.on(this._makeEventName("paneLayoutChange"),_.bind(this.updateFlipViewIcon,this))}PreferencesManager.definePreference("pane.showPaneHeaderButtons","string","hover",{description:Strings.DESCRIPTION_SHOW_PANE_HEADER_BUTTONS,values:["hover","always","never"]}),PreferencesManager.definePreference("pane.mergePanesWhenLastFileClosed","boolean",!1,{description:Strings.DESCRIPTION_MERGE_PANES_WHEN_LAST_FILE_CLOSED}),EventDispatcher.makeEventDispatcher(Pane.prototype),Pane.prototype.id=null,Pane.prototype.$container=null,Pane.prototype.$el=null,Pane.prototype.$header=null,Pane.prototype.$headerText=null,Pane.prototype.$headerFlipViewBtn=null,Pane.prototype.$headerCloseBtn=null,Pane.prototype.$content=null,Pane.prototype._viewList=[],Pane.prototype._viewListMRUOrder=[],Pane.prototype._viewListAddedOrder=[],Pane.prototype._views={},Pane.prototype._currentView=null,Pane.prototype._lastFocusedElement=null,Pane.prototype._initialize=function(){this._viewList=[],this._viewListMRUOrder=[],this._viewListAddedOrder=[],this._views={},this._currentView=null,this.showInterstitial(!0)},Pane.prototype._makeEventName=function(name){return name+".pane-"+this.id},Pane.prototype._reparent=function(view){view.$el.appendTo(this.$content),this._views[view.getFile().fullPath]=view,view.notifyContainerChange&&view.notifyContainerChange()},Pane.prototype._hideCurrentView=function(){if(this._currentView){var currentView=this._currentView;this._setViewVisibility(this._currentView,!1),this.showInterstitial(!0),this._currentView=null,this._notifyCurrentViewChange(null,currentView)}},Pane.prototype.moveView=function(file,destinationPane,destinationIndex){var self=this,openNextPromise=new $.Deferred,result=new $.Deferred;if(this.getCurrentlyViewedPath()===file.fullPath){var nextFile=this.traverseViewListByMRU(1,file.fullPath);nextFile?this._execOpenFile(nextFile.fullPath).fail(function(){self._hideCurrentView()}).always(function(){openNextPromise.resolve()}):(this._hideCurrentView(),openNextPromise.resolve())}else openNextPromise.resolve();return openNextPromise.done(function(){var viewListIndex=self.findInViewList(file.fullPath),shouldAddView=-1!==viewListIndex,view=self._views[file.fullPath];if(!shouldAddView)return view&&self._doDestroyView(view),result.resolve();self._viewList.splice(viewListIndex,1),self._viewListMRUOrder.splice(self.findInViewListMRUOrder(file.fullPath),1),self._viewListAddedOrder.splice(self.findInViewListAddedOrder(file.fullPath),1),destinationPane._addToViewList(file,_makeIndexRequestObject(!0,destinationIndex)),view?(delete self._views[file.fullPath],destinationPane.addView(view,!destinationPane.getCurrentlyViewedFile()),result.resolve()):destinationPane.getCurrentlyViewedFile()?result.resolve():destinationPane._execOpenFile(file.fullPath).always(function(){result.resolve()})}),result.promise()},Pane.prototype.mergeFrom=function(other){var otherCurrentView=other._currentView;other._hideCurrentView(),this._viewList=_.union(this._viewList,other._viewList),this._viewListMRUOrder=_.union(this._viewListMRUOrder,other._viewListMRUOrder),this._viewListAddedOrder=_.union(this._viewListAddedOrder,other._viewListAddedOrder);var self=this,viewsToDestroy=[];_.forEach(other._views,function(view){var file=view.getFile(),fullPath=file&&file.fullPath;fullPath&&-1!==other.findInViewList(fullPath)?self._reparent(view):viewsToDestroy.push(view)}),otherCurrentView&&!other._isViewNeeded(otherCurrentView)&&-1===viewsToDestroy.indexOf(otherCurrentView)&&viewsToDestroy.push(otherCurrentView),_.forEach(viewsToDestroy,function(view){self.trigger("viewDestroy",view),view.destroy()}),other._initialize()},Pane.prototype.destroy=function(){(this._currentView||Object.keys(this._views).length>0||this._viewList.length>0)&&console.warn("destroying a pane that isn't empty"),this._reset(),DocumentManager.off(this._makeEventName("")),MainViewManager.off(this._makeEventName("")),this.$el.off(".pane"),this.$el.remove()},Pane.prototype.getViewList=function(){return _.clone(this._viewList)},Pane.prototype.getViewListSize=function(){return this._viewList.length},Pane.prototype.findInViewList=function(fullPath){return _.findIndex(this._viewList,function(file){return file.fullPath===fullPath})},Pane.prototype.findInViewListAddedOrder=function(fullPath){return _.findIndex(this._viewListAddedOrder,function(file){return file.fullPath===fullPath})},Pane.prototype.findInViewListMRUOrder=function(fullPath){return _.findIndex(this._viewListMRUOrder,function(file){return file.fullPath===fullPath})},Pane.prototype.ITEM_NOT_FOUND=-1,Pane.prototype.ITEM_FOUND_NO_SORT=0,Pane.prototype.ITEM_FOUND_NEEDS_SORT=1,Pane.prototype.reorderItem=function(file,index,force){var indexRequested=null!=index&&index>=0,curIndex=this.findInViewList(file.fullPath);if(-1!==curIndex){if(force||indexRequested&&curIndex!==index){var entry=this._viewList.splice(curIndex,1)[0];return this._viewList.splice(index,0,entry),this.ITEM_FOUND_NEEDS_SORT}return this.ITEM_FOUND_NO_SORT}return this.ITEM_NOT_FOUND},Pane.prototype._canAddFile=function(file){return this._views.hasOwnProperty(file.fullPath)&&-1===this.findInViewList(file.fullPath)||MainViewManager._getPaneIdForPath(file.fullPath)!==this.id},Pane.prototype._addToViewList=function(file,inPlace){inPlace&&inPlace.indexRequested?this._viewList.splice(inPlace.index,0,file):this._viewList.push(file);var currentPath=this.getCurrentlyViewedPath();currentPath&¤tPath===file.fullPath?this._viewListMRUOrder.unshift(file):this._viewListMRUOrder.push(file),this._viewListAddedOrder.unshift(file)},Pane.prototype.addToViewList=function(file,index){var indexRequested=null!=index&&index>=0&&index<this._viewList.length;return this._addToViewList(file,_makeIndexRequestObject(indexRequested,index)),indexRequested||(index=this._viewList.length-1),index},Pane.prototype.addListToViewList=function(fileList){var self=this,uniqueFileList=[];return fileList.forEach(function(file){self._canAddFile(file)&&(self._addToViewList(file),uniqueFileList.push(file))}),uniqueFileList},Pane.prototype._notifyCurrentViewChange=function(newView,oldView){this.updateHeaderText(),this.trigger("currentViewChange",newView,oldView)},Pane.prototype._doDestroyView=function(view){this._currentView===view&&this._hideCurrentView(),delete this._views[view.getFile().fullPath],this.trigger("viewDestroy",view),view.destroy()},Pane.prototype._doRemove=function(file,preventViewChange){var index=this.findInViewList(file.fullPath);index>-1&&(this._viewList.splice(index,1),this._viewListMRUOrder.splice(this.findInViewListMRUOrder(file.fullPath),1),this._viewListAddedOrder.splice(this.findInViewListAddedOrder(file.fullPath),1));var view=this._views[file.fullPath];return view&&(preventViewChange||this._doDestroyView(view)),index>-1||Boolean(view)},Pane.prototype.makeViewMostRecent=function(file){var index=this.findInViewListMRUOrder(file.fullPath);-1!==index&&(this._viewListMRUOrder.splice(index,1),this._viewListMRUOrder.unshift(file))},Pane.prototype.sortViewList=function(compareFn){this._viewList.sort(_.partial(compareFn,this.id))},Pane.prototype.moveWorkingSetItem=function(fromIndex,toIndex){this._viewList.splice(toIndex,0,this._viewList.splice(fromIndex,1)[0])},Pane.prototype.swapViewListIndexes=function(index1,index2){var temp=this._viewList[index1];return this._viewList[index1]=this._viewList[index2],this._viewList[index2]=temp,!0},Pane.prototype.traverseViewListByMRU=function(direction,current){if(!current&&this._currentView){var file=this._currentView.getFile();current=file&&file.fullPath}var index=current?this.findInViewListMRUOrder(current):-1;return ViewUtils.traverseViewArray(this._viewListMRUOrder,index,direction)},Pane.prototype.updateFlipViewIcon=function(){var paneID=this.id,directionIndex=0,ICON_CLASSES=["flipview-icon-none","flipview-icon-top","flipview-icon-right","flipview-icon-bottom","flipview-icon-left"],DIRECTION_STRINGS=["",Strings.TOP,Strings.RIGHT,Strings.BOTTOM,Strings.LEFT],layoutScheme=MainViewManager.getLayoutScheme(),hasFile=this.getCurrentlyViewedFile();layoutScheme.columns>1&&hasFile?directionIndex=paneID===FIRST_PANE?2:4:layoutScheme.rows>1&&hasFile&&(directionIndex=paneID===FIRST_PANE?3:1),this.$headerFlipViewBtn.removeClass(ICON_CLASSES.join(" ")).addClass(ICON_CLASSES[directionIndex]),this.$headerFlipViewBtn.attr("title",StringUtils.format(Strings.FLIPVIEW_BTN_TOOLTIP,DIRECTION_STRINGS[directionIndex].toLowerCase()))},Pane.prototype.updateHeaderText=function(){var file=this.getCurrentlyViewedFile(),files,displayName;file?(files=MainViewManager.getAllOpenFiles().filter(function(item){return item.name===file.name})).length<2?this.$headerText.text(file.name):(displayName=ProjectManager.makeProjectRelativeIfPossible(file.fullPath),this.$headerText.text(displayName)):this.$headerText.html(Strings.EMPTY_VIEW_HEADER),this.updateFlipViewIcon()},Pane.prototype._handleFileNameChange=function(e,oldname,newname){var dispatchEvent=this.findInViewList(newname)>=0;if(this._views.hasOwnProperty(oldname)){var view=this._views[oldname];this._views[newname]=view,delete this._views[oldname]}this.updateHeaderText(),dispatchEvent&&this.trigger("viewListChange")},Pane.prototype._handleFileDeleted=function(e,fullPath){this.removeView({fullPath:fullPath})&&this.trigger("viewListChange")},Pane.prototype.showInterstitial=function(show){this.$content&&this.$content.find(".not-editor").css("display",show?"":"none")},Pane.prototype.getViewForPath=function(path){return this._views[path]},Pane.prototype.addView=function(view,show){var file=view.getFile(),path=file&&file.fullPath;path?(view.$el.parent()!==this.$content?this._reparent(view):this._views[path]=view,view.markPaneId&&view.markPaneId(this.id),show&&this.showView(view)):console.error("cannot add a view that does not have a fullPath")},Pane.prototype._setViewVisibility=function(view,visible){view.$el.css("display",visible?"":"none"),view.notifyVisibilityChange&&view.notifyVisibilityChange(visible)},Pane.prototype.showView=function(view){if(this._currentView&&this._currentView===view)return this._setViewVisibility(this._currentView,!0),void this.updateLayout(!0);var file=view.getFile(),newPath=file&&file.fullPath,oldView=this._currentView;this._currentView?(this._currentView.getFile()&&ViewStateManager.updateViewState(this._currentView),this._setViewVisibility(this._currentView,!1)):this.showInterstitial(!1),this._currentView=view,this._setViewVisibility(this._currentView,!0),this.updateLayout(),this._notifyCurrentViewChange(view,oldView),oldView&&this.destroyViewIfNotNeeded(oldView),this._views.hasOwnProperty(newPath)||console.error(newPath+" found in pane working set but pane.addView() has not been called for the view created for it")},Pane.prototype._updateHeaderHeight=function(){const $el=this.$el,$tabBar=$el.find(".tab-bar-container");let paneContentHeight=$el.height();$tabBar.length>0&&$tabBar.is(":visible")&&(paneContentHeight-=$tabBar.height()),MainViewManager.getPaneCount()>1?(this.$header.show(),paneContentHeight-=this.$header.outerHeight()):this.$header.hide(),this.$content.height(paneContentHeight)},Pane.prototype.updateLayout=function(forceRefresh){this._updateHeaderHeight(),this._currentView&&this._currentView.updateLayout(forceRefresh)},Pane.prototype._isViewNeeded=function(view){var path=view.getFile().fullPath,currentPath=this.getCurrentlyViewedPath();return this._currentView&¤tPath===path||-1!==this.findInViewList(path)},Pane.prototype.getCurrentlyViewedFile=function(){return this._currentView?this._currentView.getFile():null},Pane.prototype.getCurrentlyViewedEditor=function(){return this._currentView instanceof Editor?this._currentView:null},Pane.prototype.getCurrentlyViewedPath=function(){var file=this.getCurrentlyViewedFile();return file?file.fullPath:null},Pane.prototype.destroyViewIfNotNeeded=function(view){if(!this._isViewNeeded(view)){var file=view.getFile(),path=file&&file.fullPath;delete this._views[path],this.trigger("viewDestroy",view),view.destroy()}},Pane.prototype._reset=function(){var self=this,views=[],view=this._currentView;_.forEach(this._views,function(_view){views.push(_view)}),this._currentView&&-1===views.indexOf(this._currentView)&&views.push(this._currentView),this._initialize(),view&&this._notifyCurrentViewChange(null,view),views.forEach(function(_view){self.trigger("viewDestroy",_view),_view.destroy()})},Pane.prototype._execOpenFile=function(fullPath){return CommandManager.execute(Commands.CMD_ADD_TO_WORKINGSET_AND_OPEN,{fullPath:fullPath,paneId:this.id,options:{noPaneActivate:!0}})},Pane.prototype.removeView=function(file,suppressOpenNextFile,preventViewChange){var nextFile=!suppressOpenNextFile&&this.traverseViewListByMRU(1,file.fullPath);if(nextFile&&nextFile.fullPath!==file.fullPath&&this.getCurrentlyViewedPath()===file.fullPath){var self=this,fullPath=nextFile.fullPath,needOpenNextFile=-1!==this.findInViewList(fullPath);return!!this._doRemove(file,needOpenNextFile)&&(needOpenNextFile&&this._execOpenFile(fullPath).fail(function(){self._doDestroyView(self._currentView)}),!0)}return this._doRemove(file,preventViewChange)},Pane.prototype.removeViews=function(list){var self=this,needsDestroyCurrentView=!1,result;if(needsDestroyCurrentView=-1!==_.findIndex(list,function(file){return file.fullPath===self.getCurrentlyViewedPath()}),result=list.filter(function(file){return self.removeView(file,!0,!0)}),needsDestroyCurrentView){var nextFile=this.traverseViewListByMRU(1,this.getCurrentlyViewedPath()),fullPath=nextFile&&nextFile.fullPath,needOpenNextFile;fullPath&&-1!==this.findInViewList(fullPath)?this._execOpenFile(fullPath).fail(function(){self._doDestroyView(self._currentView)}):this._doDestroyView(this._currentView)}return result},Pane.prototype.focus=function(){var current=window.document.activeElement,self=this;function tryFocusingCurrentView(){self._currentView?self._currentView.focus?self._currentView.focus():self._currentView.$el.focus():self.$el.focus()}if(this._lastFocusedElement!==current){"textarea"!==current.tagName.toLowerCase()||this._currentView&&this._currentView._codeMirror||current.blur();var $lfe=$(this._lastFocusedElement);$lfe.length&&!$lfe.is(".view-pane")&&$lfe.is(":visible")?$lfe.focus():tryFocusingCurrentView()}},Pane.prototype._handleActivePaneChange=function(e,activePaneId){this.$el.toggleClass("active-pane",Boolean(activePaneId===this.id))},Pane.prototype.loadState=function(state){var filesToAdd=[],viewStates={},activeFile,data,self=this,getInitialViewFilePath=function(){return self._viewList.length>0?self._viewList[0].fullPath:null};return _.forEach(state,function(entry){filesToAdd.push(FileSystem.getFileForPath(entry.file)),entry.active&&(activeFile=entry.file),entry.viewState&&(viewStates[entry.file]=entry.viewState)}),this.addListToViewList(filesToAdd),ViewStateManager.addViewStates(viewStates),(activeFile=activeFile||getInitialViewFilePath())&&(data={paneId:self.id,fullPath:activeFile}),(new $.Deferred).resolve(data)},Pane.prototype.saveState=function(){var result=[],currentlyViewedPath=this.getCurrentlyViewedPath();return this._currentView&&this._currentView.getFile()&&ViewStateManager.updateViewState(this._currentView),this._viewList.forEach(function(file){file instanceof InMemoryFile||result.push({file:file.fullPath,active:file.fullPath===currentlyViewedPath,viewState:ViewStateManager.getViewState(file)})}),result},Pane.prototype.getScrollState=function(){if(this._currentView&&this._currentView.getScrollPos)return{scrollPos:this._currentView.getScrollPos()}},Pane.prototype.restoreAndAdjustScrollState=function(state,heightDelta){this._currentView&&state&&state.scrollPos&&this._currentView.adjustScrollPos&&this._currentView.adjustScrollPos(state.scrollPos,heightDelta)},exports.Pane=Pane});