forked from phcode-dev/staging.phcode.dev
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFileTreeViewModel.js
More file actions
1 lines (1 loc) · 13.6 KB
/
FileTreeViewModel.js
File metadata and controls
1 lines (1 loc) · 13.6 KB
1
define(function(require,exports,module){var Immutable=require("thirdparty/immutable"),_=require("thirdparty/lodash"),EventDispatcher=require("utils/EventDispatcher"),FileUtils=require("file/FileUtils"),EVENT_CHANGE="change";function isFile(entry){return void 0===entry.get("children")}function FileTreeViewModel(){this._commit=this._commit.bind(this)}function _filePathToObjectPath(treeData,path){if(null===path)return null;if(""===path)return[];for(var parts=path.split("/"),part=parts.shift(),result=[],node;part;){if(!treeData)return null;if(void 0===(node=treeData.get(part)))return null;result.push(part),(part=parts.shift())&&(treeData=node.get("children"))&&result.push("children")}return result}function _isFilePathVisible(treeData,path){if(null===path)return null;if(""===path)return!0;for(var parts=path.split("/"),part=parts.shift(),result=[],node;part;){if(null===treeData)return!1;if(void 0===(node=treeData.get(part)))return null;if(result.push(part),part=parts.shift()){if(!node.get("open"))return!1;(treeData=node.get("children"))&&result.push("children")}}return!0}function _getOpenNodes(treeData,projectRootPath){var openNodes=[];function addNodesAtDepth(treeData,parent,depth){treeData&&treeData.forEach(function(value,key){if(!isFile(value)){var directoryPath=parent+key+"/";if(value.get("open")){var nodeList=openNodes[depth];nodeList||(nodeList=openNodes[depth]=[]),nodeList.push(directoryPath),addNodesAtDepth(value.get("children"),directoryPath,depth+1)}}})}return addNodesAtDepth(treeData,projectRootPath,0),openNodes}function _getChildDirectories(treeData,projectRootPath){var childDirectories=[];function addNodesAtDepth(treeData,parent,depth){treeData&&treeData.forEach(function(value,key){if(!isFile(value)){var directoryPath=key+"/";childDirectories.push(directoryPath)}})}return addNodesAtDepth(treeData,projectRootPath,0),childDirectories}function _setIn(treeData,objectPath,newValue){return treeData.updateIn(objectPath,function(oldValue){return newValue})}function _moveMarker(treeData,markerName,oldPath,newPath){var objectPath;newPath&&(objectPath=_filePathToObjectPath(treeData,newPath));var newTreeData=treeData;if(oldPath&&oldPath!==newPath){var lastObjectPath=_filePathToObjectPath(treeData,oldPath);lastObjectPath&&(newTreeData=newTreeData.updateIn(lastObjectPath,function(entry){return entry.delete(markerName)}))}return newPath&&objectPath&&0!==objectPath.length&&(newTreeData=newTreeData.updateIn(objectPath,function(entry){return entry.set(markerName,!0)})),newTreeData}function _setDirectoryOpen(treeData,path,open){var objectPath=_filePathToObjectPath(treeData,path),directory=treeData.getIn(objectPath);if(!objectPath)return{needsLoading:!0,treeData:treeData};if(!isFile(directory)){var alreadyOpen=!0===directory.get("open");if(!(alreadyOpen&&open||!alreadyOpen&&!open))return treeData=treeData.updateIn(objectPath,function(directory){return open?directory.set("open",!0):directory.delete("open")}),open&&(null===directory.get("children")||directory.get("notFullyLoaded"))?{needsLoading:!0,treeData:treeData}:{needsLoading:!1,treeData:treeData}}}function _closeSubtree(directory){var children=(directory=directory.delete("open")).get("children");return children&&children.keySeq().forEach(function(name){var subdir=children.get(name);isFile(subdir)||(subdir=_closeSubtree(subdir),children=children.set(name,subdir))}),directory=directory.set("children",children)}function _mergeContentsIntoChildren(children,contents){var keysSeen=[];return children=children.withMutations(function(children){contents.forEach(function(entry){keysSeen.push(entry.name);var match=children.get(entry.name),matchIsFile;match&&(isFile(match)!==entry.isFile&&(match=void 0));match||(entry.isFile?children.set(entry.name,Immutable.Map()):children.set(entry.name,Immutable.Map({children:null})))});var currentEntries=children.keySeq().toJS(),deletedEntries;_.difference(currentEntries,keysSeen).forEach(function(name){children.delete(name)})})}function _createNotFullyLoadedDirectory(directory){return directory?directory.merge({notFullyLoaded:!0,children:Immutable.Map()}):Immutable.Map({notFullyLoaded:!0,children:Immutable.Map()})}function _createIntermediateDirectories(treeData,path){var objectPath=[],result={objectPath:objectPath,treeData:treeData},treePointer=treeData;if(""===path)return result;for(var parts=path.split("/"),part=parts.shift(),node;part;){if(null===treePointer)return null;node=treePointer.get(part),objectPath.push(part),void 0===node&&(node=(treeData=treeData.updateIn(objectPath,_createNotFullyLoadedDirectory)).getIn(objectPath)),(part=parts.shift())&&((treePointer=node.get("children"))?objectPath.push("children"):(treeData=treeData.updateIn(objectPath,_createNotFullyLoadedDirectory),objectPath.push("children"),treePointer=treeData.getIn(objectPath)))}return result.treeData=treeData,result}function _openPath(treeData,path){var objectPath=_filePathToObjectPath(treeData,path);function setOpen(node){return node.set("open",!0)}for(;objectPath&&objectPath.length;){var node=treeData.getIn(objectPath);isFile(node)?objectPath.pop():(node.get("open")||(treeData=treeData.updateIn(objectPath,setOpen)),objectPath.pop(),objectPath.length&&objectPath.pop())}return treeData}function _createPlaceholder(treeData,basedir,name,isFolder,options){options=options||{};var parentPath=_filePathToObjectPath(treeData,basedir);if(parentPath){var newObject={};options.notInCreateMode||(newObject.creating=!0),isFolder&&(options.notInCreateMode?newObject.children=null:newObject.children=Immutable.Map());var newFile=Immutable.Map(newObject);if(options.doNotOpen||(treeData=_openPath(treeData,basedir)),parentPath.length>0){var childrenPath=_.clone(parentPath);childrenPath.push("children"),treeData=treeData.updateIn(childrenPath,function(children){return children.set(name,newFile)})}else treeData=treeData.set(name,newFile);return treeData}}function _deleteAtPath(treeData,path){var objectPath=_filePathToObjectPath(treeData,path);if(objectPath){var originalName=_.last(objectPath);return objectPath.pop(),treeData=treeData.updateIn(objectPath,function(directory){return directory=directory.delete(originalName)})}}function _addTimestamp(item){return item.set("_timestamp",(new Date).getTime())}function _markAsChanged(treeData,changed){return changed.forEach(function(filePath){var objectPath=_filePathToObjectPath(treeData,filePath);objectPath&&(treeData=treeData.updateIn(objectPath,_addTimestamp))}),treeData}function _addNewEntries(treeData,added){return added.forEach(function(filePath){var isFolder="/"===_.last(filePath);filePath=isFolder?filePath.substr(0,filePath.length-1):filePath;var parentPath=FileUtils.getDirectoryPath(filePath),parentObjectPath=_filePathToObjectPath(treeData,parentPath),basename=FileUtils.getBaseName(filePath);if(parentObjectPath){var childrenPath=_.clone(parentObjectPath);if(childrenPath.push("children"),null===treeData.getIn(childrenPath))return;treeData=_createPlaceholder(treeData,parentPath,basename,isFolder,{notInCreateMode:!0,doNotOpen:!0})}}),treeData}EventDispatcher.makeEventDispatcher(FileTreeViewModel.prototype),FileTreeViewModel.prototype.sortDirectoriesFirst=!1,FileTreeViewModel.prototype._treeData=Immutable.Map(),Object.defineProperty(FileTreeViewModel.prototype,"treeData",{get:function(){return this._treeData}}),FileTreeViewModel.prototype._selectionViewInfo=Immutable.Map({width:0,scrollTop:0,scrollLeft:0,offsetTop:0,hasContext:!1,hasSelection:!1}),Object.defineProperty(FileTreeViewModel.prototype,"selectionViewInfo",{get:function(){return this._selectionViewInfo}}),FileTreeViewModel.prototype._rootChanged=function(){this._treeData=Immutable.Map()},FileTreeViewModel.prototype._commit=function(treeData,selectionViewInfo){var changed=!1;treeData&&treeData!==this._treeData&&(this._treeData=treeData,changed=!0),selectionViewInfo&&selectionViewInfo!==this._selectionViewInfo&&(this._selectionViewInfo=selectionViewInfo,changed=!0),changed&&this.trigger("change")},FileTreeViewModel.prototype.isFilePathVisible=function(path){return _isFilePathVisible(this._treeData,path)},FileTreeViewModel.prototype.isPathLoaded=function(path){var objectPath=_filePathToObjectPath(this._treeData,path);if(!objectPath)return!1;if("/"===_.last(path)){var directory=this._treeData.getIn(objectPath);if(!directory.get("children")||directory.get("notFullyLoaded"))return!1}return!0},FileTreeViewModel.prototype.getOpenNodes=function(projectRootPath){return _getOpenNodes(this._treeData,projectRootPath)},FileTreeViewModel.prototype.getChildDirectories=function(parent){var treeData,objectPath=_filePathToObjectPath(this._treeData,parent),children;if(objectPath)return 0===objectPath.length?children=this._treeData:(objectPath.push("children"),children=this._treeData.getIn(objectPath)),_getChildDirectories(children,parent)},FileTreeViewModel.prototype.moveMarker=function(markerName,oldPath,newPath){var newTreeData=_moveMarker(this._treeData,markerName,oldPath,newPath),selectionViewInfo=this._selectionViewInfo;"selected"===markerName?selectionViewInfo=selectionViewInfo.set("hasSelection",!!newPath):"context"===markerName&&(selectionViewInfo=selectionViewInfo.set("hasContext",!!newPath)),this._commit(newTreeData,selectionViewInfo)},FileTreeViewModel.prototype.renameItem=function(oldPath,newPath){var treeData=this._treeData,oldObjectPath=_filePathToObjectPath(treeData,oldPath),newDirectoryPath=FileUtils.getParentPath(newPath),newObjectPath=_filePathToObjectPath(treeData,newDirectoryPath);if(oldObjectPath&&newObjectPath){var originalName=_.last(oldObjectPath),newName=FileUtils.getBaseName(newPath),currentObject;oldObjectPath.pop(),treeData=treeData.updateIn(oldObjectPath,function(directory){return currentObject=directory.get(originalName),directory=directory.delete(originalName)}),this.isPathLoaded(newDirectoryPath)||(treeData=treeData.updateIn(newObjectPath,_createNotFullyLoadedDirectory)),newObjectPath.length>0&&newObjectPath.push("children"),treeData=treeData.updateIn(newObjectPath,function(children){return children.set(newName,currentObject)}),this._commit(treeData)}},FileTreeViewModel.prototype.setDirectoryOpen=function(path,open){var result=_setDirectoryOpen(this._treeData,path,open);return result&&result.treeData&&this._commit(result.treeData),!!result&&result.needsLoading},FileTreeViewModel.prototype._getObject=function(path){var objectPath=_filePathToObjectPath(this._treeData,path);return objectPath?this._treeData.getIn(objectPath):null},FileTreeViewModel.prototype.closeSubtree=function(path){var treeData=this._treeData,subtreePath=_filePathToObjectPath(treeData,path);if(subtreePath){var directory=treeData.getIn(subtreePath);treeData=_setIn(treeData,subtreePath,directory=_closeSubtree(directory)),this._commit(treeData)}},FileTreeViewModel.prototype.setDirectoryContents=function(path,contents){path=FileUtils.stripTrailingSlash(path);var intermediate=_createIntermediateDirectories(this._treeData,path),objectPath=intermediate.objectPath,treeData=intermediate.treeData;if(null!==objectPath){var directory=treeData.getIn(objectPath),children=directory;if(""!==path){if(isFile(directory))return;directory.get("notFullyLoaded")&&(directory=directory.delete("notFullyLoaded")),directory.get("children")||(directory=directory.set("children",Immutable.Map())),treeData=_setIn(treeData,objectPath,directory),objectPath.push("children"),children=directory.get("children")}treeData=_setIn(treeData,objectPath,children=_mergeContentsIntoChildren(children,contents)),this._commit(treeData)}},FileTreeViewModel.prototype.openPath=function(path){this._commit(_openPath(this._treeData,path))},FileTreeViewModel.prototype.createPlaceholder=function(basedir,name,isFolder){var treeData=_createPlaceholder(this._treeData,basedir,name,isFolder);this._commit(treeData)},FileTreeViewModel.prototype.deleteAtPath=function(path){var treeData=_deleteAtPath(this._treeData,path);treeData&&this._commit(treeData)},FileTreeViewModel.prototype.processChanges=function(changes){var treeData=this._treeData;changes.changed&&(treeData=_markAsChanged(treeData,changes.changed)),changes.added&&(treeData=_addNewEntries(treeData,changes.added)),changes.removed&&changes.removed.forEach(function(path){treeData=_deleteAtPath(treeData,path)}),this._commit(treeData)},FileTreeViewModel.prototype.ensureDirectoryExists=function(path){var treeData=this._treeData,pathWithoutSlash=FileUtils.stripTrailingSlash(path),parentPath=FileUtils.getDirectoryPath(pathWithoutSlash),name=pathWithoutSlash.substr(parentPath.length),targetPath=[];if(parentPath){if(!(targetPath=_filePathToObjectPath(treeData,parentPath)))return;if(targetPath.push("children"),!treeData.getIn(targetPath))return}targetPath.push(name),treeData.getIn(targetPath)||(treeData=_setIn(treeData,targetPath,Immutable.Map({children:null})),this._commit(treeData))},FileTreeViewModel.prototype.setSortDirectoriesFirst=function(sortDirectoriesFirst){sortDirectoriesFirst!==this.sortDirectoriesFirst&&(this.sortDirectoriesFirst=sortDirectoriesFirst,this.trigger("change"))},FileTreeViewModel.prototype.setSelectionWidth=function(width){var selectionViewInfo=this._selectionViewInfo;selectionViewInfo=selectionViewInfo.set("width",width),this._commit(null,selectionViewInfo)},FileTreeViewModel.prototype.setSelectionScrollerInfo=function(scrollWidth,scrollTop,scrollLeft,offsetTop){this._selectionViewInfo=this._selectionViewInfo.set("scrollWidth",scrollWidth),this._selectionViewInfo=this._selectionViewInfo.set("scrollTop",scrollTop),void 0!==scrollLeft&&(this._selectionViewInfo=this._selectionViewInfo.set("scrollLeft",scrollLeft)),void 0!==offsetTop&&(this._selectionViewInfo=this._selectionViewInfo.set("offsetTop",offsetTop))},exports.EVENT_CHANGE="change",exports._filePathToObjectPath=_filePathToObjectPath,exports._isFilePathVisible=_isFilePathVisible,exports._createPlaceholder=_createPlaceholder,exports.isFile=isFile,exports.FileTreeViewModel=FileTreeViewModel});