overleaf/services/web/public/coffee/ide/file-tree/FileTreeManager.coffee

478 lines
14 KiB
CoffeeScript
Raw Normal View History

2014-07-08 07:02:26 -04:00
define [
"ide/file-tree/directives/fileEntity"
"ide/file-tree/directives/draggable"
"ide/file-tree/directives/droppable"
"ide/file-tree/controllers/FileTreeController"
"ide/file-tree/controllers/FileTreeEntityController"
"ide/file-tree/controllers/FileTreeFolderController"
"ide/file-tree/controllers/FileTreeRootFolderController"
], () ->
class FileTreeManager
constructor: (@ide, @$scope) ->
@$scope.$on "project:joined", =>
@loadRootFolder()
@loadDeletedDocs()
@$scope.$emit "file-tree:initialized"
2017-11-21 11:26:44 -05:00
2014-07-21 10:09:19 -04:00
@$scope.$watch "rootFolder", (rootFolder) =>
if rootFolder?
@recalculateDocList()
2014-07-08 07:02:26 -04:00
@_bindToSocketEvents()
2017-11-21 11:26:44 -05:00
@$scope.multiSelectedCount = 0
2017-11-21 11:26:44 -05:00
$(document).on "click", =>
@clearMultiSelectedEntities()
CI and local dev environment improvements The need for this became very noticeable due to the slowness of filesystem access in docker-in-mac, with a full compile taking over a minute for me in docker. Using make to introduce incremental compile makes this near instantaneous outside of docker (if only a few files have changed), and quick enough inside docker. With incremental compile via make, it compiles quickly enough that re-compiling and restarting the web service automatically when backend files change is quick enough now. This is how the service is run via docker-compose in https://github.com/sharelatex/sharelatex-dev-environment, so it shouldn't be necessary to manually restart the container each time a coffee file changes. At the moment Jenkins pull web modules in via the GitSCM plugin, but I believe this is creating a dependency in Jenkins, where any commits to any of the modules causes all of the web branches to rebuild. By doing it via our own scripts we can hopefully avoid this. It also creates a build process which is reproducible locally. **Note that at the moment in this PR all modules pull from `ja-dockerize-dev` branches, but these should be merged first, and this PR updated to point to the master branches before merging**. This is necessary for other changes to build process/docker-compose workflow. As well as a Makefile for web, there is now a `Makefile.module`. This is copied into each module directory by the top-level Makefile, and is written in a way to be flexible and support unit tests, acceptance tests, front-end js for the ide and main, and the modules `app/coffee` directory, while allowing modules to have some of these missing (not all modules have e.g. acceptance tests, or front-end JS). This will allows us to refine the build process in future, without needing to update the Makefile in each module repo separately (I found this to be a painful part of this development). This makes web compatible with the docker-compose workflow at https://github.com/sharelatex/sharelatex-dev-environment, where each service is running in its own docker container, with networking managed by docker. Previously the Makefile was set up to run unit tests in docker with `make unit_tests`. This now just runs them natively. In the CI, they are run in docker anyway (all steps in Jenkins are), and locally, they run fine natively with `npm run test:unit`, or can be run in docker via https://github.com/sharelatex/sharelatex-dev-environment with `bin/run web_sl npm run test:unit`. Previously we did a lot of juggling with only mounting source files (coffee, less, etc) into the docker container for acceptance tests. This was to avoid creating root owned files if the whole directory was mounted. Now instead the whole web directory is mounted read-only, with the compilation step done outside of the container before running the tests. This allows the host and container to share the `node_modules` folder as well, which avoids needing to `npm install` twice on the CI box, and should speed up the build by a few minutes. On macOS, this would cause a problem with compiled modules if you tried to use the same `node_modules` to run the app natively. However, if running via docker-compose in https://github.com/sharelatex/sharelatex-dev-environment, this is no longer a problem.
2017-12-28 15:11:27 -05:00
@$scope.$digest()
2018-05-03 09:29:03 -04:00
window.doLinkedFileImportFromProject = (project, path, name) =>
parent_folder = @getCurrentFolder()
@ide.$http.post "/project/#{@ide.project_id}/linked_file", {
name: name,
parent_folder_id: parent_folder?.id
provider: 'project_file',
data: {
source_project_id: project,
source_entity_path: path
},
_csrf: window.csrfToken
}
2014-07-08 07:02:26 -04:00
_bindToSocketEvents: () ->
@ide.socket.on "reciveNewDoc", (parent_folder_id, doc) =>
parent_folder = @findEntityById(parent_folder_id) or @$scope.rootFolder
@$scope.$apply () =>
2014-07-08 07:02:26 -04:00
parent_folder.children.push {
name: doc.name
id: doc._id
type: "doc"
}
@recalculateDocList()
2014-07-08 07:02:26 -04:00
2018-02-20 06:49:02 -05:00
@ide.socket.on "reciveNewFile", (parent_folder_id, file, source, linkedFileData) =>
2014-07-08 07:02:26 -04:00
parent_folder = @findEntityById(parent_folder_id) or @$scope.rootFolder
@$scope.$apply () =>
2014-07-08 07:02:26 -04:00
parent_folder.children.push {
name: file.name
id: file._id
2018-02-20 06:49:02 -05:00
type: "file",
linkedFileData: linkedFileData
2014-07-08 07:02:26 -04:00
}
@recalculateDocList()
2017-11-21 11:26:44 -05:00
2014-07-08 07:02:26 -04:00
@ide.socket.on "reciveNewFolder", (parent_folder_id, folder) =>
parent_folder = @findEntityById(parent_folder_id) or @$scope.rootFolder
@$scope.$apply () =>
2014-07-08 07:02:26 -04:00
parent_folder.children.push {
name: folder.name
id: folder._id
type: "folder"
children: []
}
@recalculateDocList()
2014-07-08 07:02:26 -04:00
@ide.socket.on "reciveEntityRename", (entity_id, name) =>
entity = @findEntityById(entity_id)
return if !entity?
@$scope.$apply () =>
2014-07-08 07:02:26 -04:00
entity.name = name
@recalculateDocList()
2014-07-08 07:02:26 -04:00
@ide.socket.on "removeEntity", (entity_id) =>
entity = @findEntityById(entity_id)
return if !entity?
@$scope.$apply () =>
@_deleteEntityFromScope entity
@recalculateDocList()
@$scope.$broadcast "entity:deleted", entity
2014-07-08 07:02:26 -04:00
@ide.socket.on "reciveEntityMove", (entity_id, folder_id) =>
entity = @findEntityById(entity_id)
folder = @findEntityById(folder_id)
@$scope.$apply () =>
@_moveEntityInScope(entity, folder)
@recalculateDocList()
2014-07-08 07:02:26 -04:00
selectEntity: (entity) ->
@selected_entity_id = entity.id # For reselecting after a reconnect
@ide.fileTreeManager.forEachEntity (entity) ->
entity.selected = false
entity.selected = true
2017-11-21 11:26:44 -05:00
toggleMultiSelectEntity: (entity) ->
entity.multiSelected = !entity.multiSelected
@$scope.multiSelectedCount = @multiSelectedCount()
2017-11-21 11:26:44 -05:00
multiSelectedCount: () ->
count = 0
@forEachEntity (entity) ->
if entity.multiSelected
count++
return count
2017-11-21 11:26:44 -05:00
getMultiSelectedEntities: () ->
entities = []
@forEachEntity (e) ->
if e.multiSelected
entities.push e
return entities
2017-11-21 11:26:44 -05:00
getMultiSelectedEntityChildNodes: () ->
entities = @getMultiSelectedEntities()
paths = {}
for entity in entities
paths[@getEntityPath(entity)] = entity
prefixes = {}
for path, entity of paths
parts = path.split("/")
if parts.length <= 1
continue
else
# Record prefixes a/b/c.tex -> 'a' and 'a/b'
for i in [1..(parts.length - 1)]
prefixes[parts.slice(0,i).join("/")] = true
child_entities = []
for path, entity of paths
# If the path is in the prefixes, then it's a parent folder and
# should be ignore
if !prefixes[path]?
child_entities.push entity
return child_entities
2017-11-21 11:26:44 -05:00
clearMultiSelectedEntities: () ->
return if @$scope.multiSelectedCount == 0 # Be efficient, this is called a lot on 'click'
@forEachEntity (entity) ->
entity.multiSelected = false
@$scope.multiSelectedCount = 0
2017-11-21 11:26:44 -05:00
multiSelectSelectedEntity: () ->
@findSelectedEntity()?.multiSelected = true
2014-07-08 07:02:26 -04:00
existsInFolder: (folder_id, name) ->
folder = @findEntityById(folder_id)
return false if !folder?
entity = @_findEntityByPathInFolder(folder, name)
return entity?
2017-11-21 11:26:44 -05:00
2014-07-08 07:02:26 -04:00
findSelectedEntity: () ->
selected = null
@forEachEntity (entity) ->
selected = entity if entity.selected
return selected
findEntityById: (id, options = {}) ->
return @$scope.rootFolder if @$scope.rootFolder.id == id
entity = @_findEntityByIdInFolder @$scope.rootFolder, id
return entity if entity?
if options.includeDeleted
for entity in @$scope.deletedDocs
return entity if entity.id == id
return null
_findEntityByIdInFolder: (folder, id) ->
for entity in folder.children or []
if entity.id == id
return entity
else if entity.children?
result = @_findEntityByIdInFolder(entity, id)
return result if result?
return null
findEntityByPath: (path) ->
@_findEntityByPathInFolder @$scope.rootFolder, path
_findEntityByPathInFolder: (folder, path) ->
if !path? or !folder?
return null
2018-02-20 06:49:02 -05:00
if path == ""
return folder
2014-07-08 07:02:26 -04:00
parts = path.split("/")
name = parts.shift()
rest = parts.join("/")
2017-11-21 11:26:44 -05:00
2014-07-08 07:02:26 -04:00
if name == "."
return @_findEntityByPathInFolder(folder, rest)
for entity in folder.children
if entity.name == name
if rest == ""
return entity
else if entity.type == "folder"
return @_findEntityByPathInFolder(entity, rest)
return null
2014-07-21 10:09:19 -04:00
forEachEntity: (callback = (entity, parent_folder, path) ->) ->
@_forEachEntityInFolder(@$scope.rootFolder, null, callback)
2014-07-08 07:02:26 -04:00
for entity in @$scope.deletedDocs or []
callback(entity)
2014-07-21 10:09:19 -04:00
_forEachEntityInFolder: (folder, path, callback) ->
2014-07-08 07:02:26 -04:00
for entity in folder.children or []
2014-07-21 10:09:19 -04:00
if path?
childPath = path + "/" + entity.name
else
childPath = entity.name
callback(entity, folder, childPath)
2014-07-08 07:02:26 -04:00
if entity.children?
2014-07-21 10:09:19 -04:00
@_forEachEntityInFolder(entity, childPath, callback)
2014-07-08 07:02:26 -04:00
getEntityPath: (entity) ->
@_getEntityPathInFolder @$scope.rootFolder, entity
_getEntityPathInFolder: (folder, entity) ->
for child in folder.children or []
if child == entity
return entity.name
else if child.type == "folder"
path = @_getEntityPathInFolder(child, entity)
if path?
return child.name + "/" + path
return null
getRootDocDirname: () ->
rootDoc = @findEntityById @$scope.project.rootDoc_id
return if !rootDoc?
2018-02-20 06:49:02 -05:00
return @_getEntityDirname(rootDoc)
_getEntityDirname: (entity) ->
path = @getEntityPath(entity)
2014-07-08 07:02:26 -04:00
return if !path?
return path.split("/").slice(0, -1).join("/")
2018-02-20 06:49:02 -05:00
_findParentFolder: (entity) ->
dirname = @_getEntityDirname(entity)
return if !dirname?
return @findEntityByPath(dirname)
2014-07-08 07:02:26 -04:00
loadRootFolder: () ->
2015-02-18 17:31:45 -05:00
@$scope.rootFolder = @_parseFolder(@$scope?.project?.rootFolder[0])
2014-07-08 07:02:26 -04:00
_parseFolder: (rawFolder) ->
folder = {
name: rawFolder.name
id: rawFolder._id
type: "folder"
children: []
selected: (rawFolder._id == @selected_entity_id)
}
for doc in rawFolder.docs or []
folder.children.push {
name: doc.name
id: doc._id
type: "doc"
selected: (doc._id == @selected_entity_id)
}
for file in rawFolder.fileRefs or []
folder.children.push {
name: file.name
id: file._id
type: "file"
selected: (file._id == @selected_entity_id)
2018-02-20 05:37:55 -05:00
linkedFileData: file.linkedFileData
created: file.created
2014-07-08 07:02:26 -04:00
}
for childFolder in rawFolder.folders or []
folder.children.push @_parseFolder(childFolder)
return folder
loadDeletedDocs: () ->
@$scope.deletedDocs = []
2014-07-09 10:01:33 -04:00
for doc in @$scope.project.deletedDocs or []
2014-07-08 07:02:26 -04:00
@$scope.deletedDocs.push {
name: doc.name
id: doc._id
type: "doc"
deleted: true
}
2017-11-21 11:26:44 -05:00
2014-07-21 10:09:19 -04:00
recalculateDocList: () ->
@$scope.docs = []
@forEachEntity (entity, parentFolder, path) =>
if entity.type == "doc" and !entity.deleted
@$scope.docs.push {
doc: entity
path: path
}
# Keep list ordered by folders, then name
@$scope.docs.sort (a,b) ->
aDepth = (a.path.match(/\//g) || []).length
bDepth = (b.path.match(/\//g) || []).length
if aDepth - bDepth != 0
return -(aDepth - bDepth) # Deeper path == folder first
else if a.path < b.path
return -1
else
return 1
2017-11-21 11:26:44 -05:00
2014-07-21 10:09:19 -04:00
getEntityPath: (entity) ->
@_getEntityPathInFolder @$scope.rootFolder, entity
_getEntityPathInFolder: (folder, entity) ->
for child in folder.children or []
if child == entity
return entity.name
else if child.type == "folder"
path = @_getEntityPathInFolder(child, entity)
if path?
return child.name + "/" + path
return null
2014-07-08 07:02:26 -04:00
getCurrentFolder: () ->
# Return the root folder if nothing is selected
@_getCurrentFolder(@$scope.rootFolder) or @$scope.rootFolder
_getCurrentFolder: (startFolder = @$scope.rootFolder) ->
for entity in startFolder.children or []
# The 'current' folder is either the one selected, or
# the one containing the selected doc/file
if entity.selected
if entity.type == "folder"
return entity
else
return startFolder
if entity.type == "folder"
result = @_getCurrentFolder(entity)
return result if result?
return null
existsInThisFolder: (folder, name) ->
for entity in folder?.children or []
return true if entity.name is name
return false
nameExistsError: (message = "already exists") ->
nameExists = @ide.$q.defer()
nameExists.reject({data: message})
return nameExists.promise
2014-07-08 07:02:26 -04:00
createDoc: (name, parent_folder = @getCurrentFolder()) ->
# check if a doc/file/folder already exists with this name
if @existsInThisFolder parent_folder, name
return @nameExistsError()
2014-07-08 07:02:26 -04:00
# We'll wait for the socket.io notification to actually
# add the doc for us.
@ide.$http.post "/project/#{@ide.project_id}/doc", {
name: name,
parent_folder_id: parent_folder?.id
_csrf: window.csrfToken
}
createFolder: (name, parent_folder = @getCurrentFolder()) ->
# check if a doc/file/folder already exists with this name
if @existsInThisFolder parent_folder, name
return @nameExistsError()
2014-07-08 07:02:26 -04:00
# We'll wait for the socket.io notification to actually
# add the folder for us.
return @ide.$http.post "/project/#{@ide.project_id}/folder", {
name: name,
parent_folder_id: parent_folder?.id
_csrf: window.csrfToken
}
2018-02-20 06:49:02 -05:00
createLinkedFile: (name, parent_folder = @getCurrentFolder(), provider, data) ->
# check if a doc/file/folder already exists with this name
if @existsInThisFolder parent_folder, name
return @nameExistsError()
# We'll wait for the socket.io notification to actually
# add the file for us.
return @ide.$http.post "/project/#{@ide.project_id}/linked_file", {
name: name,
parent_folder_id: parent_folder?.id
provider,
data,
_csrf: window.csrfToken
}
2018-02-20 06:49:02 -05:00
refreshLinkedFile: (file) ->
parent_folder = @_findParentFolder(file)
data = file.linkedFileData
provider = data?.provider
return if !provider?
return @ide.$http.post "/project/#{@ide.project_id}/linked_file", {
name: file.name,
parent_folder_id: parent_folder?.id
provider,
data,
_csrf: window.csrfToken
}
2014-07-08 07:02:26 -04:00
renameEntity: (entity, name, callback = (error) ->) ->
return if entity.name == name
2018-01-19 06:27:22 -05:00
return if name.length >= 150
# check if a doc/file/folder already exists with this name
parent_folder = @getCurrentFolder()
if @existsInThisFolder parent_folder, name
return @nameExistsError()
entity.renamingToName = name
2018-01-19 06:27:22 -05:00
@ide.$http.post("/project/#{@ide.project_id}/#{entity.type}/#{entity.id}/rename", {
name: name,
2014-07-08 07:02:26 -04:00
_csrf: window.csrfToken
})
.then () ->
entity.name = name
.finally () ->
entity.renamingToName = null
2014-07-08 07:02:26 -04:00
deleteEntity: (entity, callback = (error) ->) ->
2017-11-21 11:26:44 -05:00
# We'll wait for the socket.io notification to
2014-07-08 07:02:26 -04:00
# delete from scope.
return @ide.queuedHttp {
2014-07-08 07:02:26 -04:00
method: "DELETE"
url: "/project/#{@ide.project_id}/#{entity.type}/#{entity.id}"
headers:
"X-Csrf-Token": window.csrfToken
}
moveEntity: (entity, parent_folder, callback = (error) ->) ->
# Abort move if the folder being moved (entity) has the parent_folder as child
# since that would break the tree structure.
return if @_isChildFolder(entity, parent_folder)
# check if a doc/file/folder already exists with this name
2018-01-29 10:27:52 -05:00
if @existsInThisFolder parent_folder, entity.name
return @nameExistsError()
# Wait for the http response before doing the move
2018-01-19 06:27:22 -05:00
@ide.queuedHttp.post("/project/#{@ide.project_id}/#{entity.type}/#{entity.id}/move", {
2014-07-08 07:02:26 -04:00
folder_id: parent_folder.id
_csrf: window.csrfToken
2018-01-19 06:27:22 -05:00
}).then () =>
@_moveEntityInScope(entity, parent_folder)
2017-11-21 11:26:44 -05:00
_isChildFolder: (parent_folder, child_folder) ->
parent_path = @getEntityPath(parent_folder) or "" # null if root folder
child_path = @getEntityPath(child_folder) or "" # null if root folder
# is parent path the beginning of child path?
return (child_path.slice(0, parent_path.length) == parent_path)
2014-07-08 07:02:26 -04:00
_deleteEntityFromScope: (entity, options = { moveToDeleted: true }) ->
return if !entity?
2014-07-08 07:02:26 -04:00
parent_folder = null
@forEachEntity (possible_entity, folder) ->
if possible_entity == entity
parent_folder = folder
if parent_folder?
index = parent_folder.children.indexOf(entity)
if index > -1
parent_folder.children.splice(index, 1)
if entity.type == "doc" and options.moveToDeleted
entity.deleted = true
@$scope.deletedDocs.push entity
_moveEntityInScope: (entity, parent_folder) ->
return if entity in parent_folder.children
@_deleteEntityFromScope(entity, moveToDeleted: false)
parent_folder.children.push(entity)