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

282 lines
7.9 KiB
CoffeeScript
Raw Normal View History

2014-06-21 17:20:37 -04:00
define [
"ide/file-tree/directives/fileEntity"
2014-06-23 05:57:31 -04:00
"ide/file-tree/directives/draggable"
"ide/file-tree/directives/droppable"
2014-06-22 07:10:42 -04:00
"ide/file-tree/controllers/FileTreeController"
2014-06-21 17:20:37 -04:00
"ide/file-tree/controllers/FileTreeEntityController"
2014-06-23 05:57:31 -04:00
"ide/file-tree/controllers/FileTreeFolderController"
2014-07-03 06:01:41 -04:00
"ide/file-tree/controllers/FileTreeRootFolderController"
2014-06-21 17:20:37 -04:00
], () ->
class FileTreeManager
constructor: (@ide, @$scope) ->
@$scope.$on "project:joined", =>
@loadRootFolder()
2014-06-27 12:11:26 -04:00
@loadDeletedDocs()
2014-06-24 12:15:27 -04:00
@$scope.$emit "file-tree:initialized"
2014-06-21 17:20:37 -04:00
2014-06-22 07:10:42 -04:00
@_bindToSocketEvents()
_bindToSocketEvents: () ->
2014-06-22 09:39:38 -04:00
@ide.socket.on "reciveNewDoc", (parent_folder_id, doc) =>
2014-06-22 07:10:42 -04:00
parent_folder = @findEntityById(parent_folder_id) or @$scope.rootFolder
@$scope.$apply () ->
parent_folder.children.push {
name: doc.name
id: doc._id
type: "doc"
}
2014-06-22 09:39:38 -04:00
@ide.socket.on "reciveNewFile", (parent_folder_id, file) =>
parent_folder = @findEntityById(parent_folder_id) or @$scope.rootFolder
@$scope.$apply () ->
parent_folder.children.push {
name: file.name
id: file._id
type: "file"
}
@ide.socket.on "reciveNewFolder", (parent_folder_id, folder) =>
parent_folder = @findEntityById(parent_folder_id) or @$scope.rootFolder
@$scope.$apply () ->
parent_folder.children.push {
name: folder.name
id: folder._id
type: "folder"
children: []
}
2014-06-22 15:21:31 -04:00
@ide.socket.on "reciveEntityRename", (entity_id, name) =>
entity = @findEntityById(entity_id)
return if !entity?
@$scope.$apply () ->
entity.name = name
2014-06-22 15:49:58 -04:00
@ide.socket.on "removeEntity", (entity_id) =>
entity = @findEntityById(entity_id)
return if !entity?
@$scope.$apply () =>
@_deleteEntityFromScope entity
2014-06-23 05:57:31 -04:00
@ide.socket.on "reciveEntityMove", (entity_id, folder_id) =>
entity = @findEntityById(entity_id)
folder = @findEntityById(folder_id)
2014-07-03 06:01:41 -04:00
console.log "Got recive ENTITY", entity_id, folder_id, entity, folder
2014-06-23 05:57:31 -04:00
@$scope.$apply () =>
@_moveEntityInScope(entity, folder)
2014-06-24 12:15:27 -04:00
selectEntity: (entity) ->
2014-06-24 12:24:29 -04:00
@selected_entity_id = entity.id # For reselecting after a reconnect
2014-06-24 12:15:27 -04:00
@ide.fileTreeManager.forEachEntity (entity) ->
entity.selected = false
entity.selected = true
findSelectedEntity: () ->
selected = null
@forEachEntity (entity) ->
selected = entity if entity.selected
return selected
2014-06-27 12:11:26 -04:00
findEntityById: (id, options = {}) ->
2014-07-03 06:01:41 -04:00
return @$scope.rootFolder if @$scope.rootFolder.id == id
2014-06-27 12:11:26 -04:00
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
2014-06-22 07:10:42 -04:00
_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) ->
parts = path.split("/")
name = parts.shift()
rest = parts.join("/")
2014-06-30 13:35:01 -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-06-22 15:49:58 -04:00
forEachEntity: (callback = (entity, parent_folder) ->) ->
2014-06-21 17:20:37 -04:00
@_forEachEntityInFolder(@$scope.rootFolder, callback)
2014-06-27 12:11:26 -04:00
for entity in @$scope.deletedDocs or []
callback(entity)
2014-06-21 17:20:37 -04:00
_forEachEntityInFolder: (folder, callback) ->
2014-06-22 07:10:42 -04:00
for entity in folder.children or []
2014-06-22 15:49:58 -04:00
callback(entity, folder)
2014-06-21 17:20:37 -04:00
if entity.children?
@_forEachEntityInFolder(entity, callback)
2014-06-30 13:35:01 -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?
path = @getEntityPath(rootDoc)
return if !path?
return path.split("/").slice(0, -1).join("/")
2014-06-22 07:10:42 -04:00
# forEachFolder: (callback) ->
# @forEachEntity (entity) ->
# if entity.type == "folder"
# callback(entity)
2014-06-21 17:20:37 -04:00
loadRootFolder: () ->
@$scope.rootFolder = @_parseFolder(@$scope.project.rootFolder[0])
_parseFolder: (rawFolder) ->
folder = {
name: rawFolder.name
2014-06-22 07:10:42 -04:00
id: rawFolder._id
2014-06-21 17:20:37 -04:00
type: "folder"
children: []
2014-06-24 12:24:29 -04:00
selected: (rawFolder._id == @selected_entity_id)
2014-06-21 17:20:37 -04:00
}
for doc in rawFolder.docs or []
folder.children.push {
name: doc.name
id: doc._id
2014-06-22 07:10:42 -04:00
type: "doc"
2014-06-24 12:24:29 -04:00
selected: (doc._id == @selected_entity_id)
2014-06-21 17:20:37 -04:00
}
for file in rawFolder.fileRefs or []
folder.children.push {
name: file.name
id: file._id
2014-06-22 07:10:42 -04:00
type: "file"
2014-06-24 12:24:29 -04:00
selected: (file._id == @selected_entity_id)
2014-06-21 17:20:37 -04:00
}
for childFolder in rawFolder.folders or []
folder.children.push @_parseFolder(childFolder)
return folder
2014-06-22 07:10:42 -04:00
2014-06-27 12:11:26 -04:00
loadDeletedDocs: () ->
@$scope.deletedDocs = []
for doc in @$scope.project.deletedDocs
@$scope.deletedDocs.push {
name: doc.name
id: doc._id
type: "doc"
deleted: true
}
2014-06-22 12:32:15 -04:00
getCurrentFolder: () ->
# Return the root folder if nothing is selected
@_getCurrentFolder(@$scope.rootFolder) or @$scope.rootFolder
_getCurrentFolder: (startFolder = @$scope.rootFolder) ->
2014-06-22 07:10:42 -04:00
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"
2014-06-22 12:32:15 -04:00
result = @_getCurrentFolder(entity)
2014-06-22 07:10:42 -04:00
return result if result?
return null
2014-06-23 07:37:05 -04:00
createDoc: (name, parent_folder = @getCurrentFolder()) ->
2014-06-22 09:39:38 -04:00
# We'll wait for the socket.io notification to actually
2014-06-22 07:10:42 -04:00
# add the doc for us.
2014-06-23 07:37:05 -04:00
@ide.$http.post "/project/#{@ide.project_id}/doc", {
name: name,
parent_folder_id: parent_folder?.id
_csrf: window.csrfToken
2014-06-22 07:10:42 -04:00
}
2014-06-22 09:39:38 -04:00
2014-06-23 07:37:05 -04:00
createFolder: (name, parent_folder = @getCurrentFolder()) ->
2014-06-22 09:39:38 -04:00
# We'll wait for the socket.io notification to actually
# add the folder for us.
2014-06-23 06:10:18 -04:00
return @ide.$http.post "/project/#{@ide.project_id}/folder", {
name: name,
parent_folder_id: parent_folder?.id
_csrf: window.csrfToken
2014-06-22 09:39:38 -04:00
}
2014-06-22 15:08:56 -04:00
renameEntity: (entity, name, callback = (error) ->) ->
return if entity.name == name
entity.name = name
2014-06-23 06:10:18 -04:00
return @ide.$http.post "/project/#{@ide.project_id}/#{entity.type}/#{entity.id}/rename", {
name: name,
_csrf: window.csrfToken
2014-06-22 15:08:56 -04:00
}
2014-06-22 15:49:58 -04:00
deleteEntity: (entity, callback = (error) ->) ->
# We'll wait for the socket.io notification to
# delete from scope.
2014-06-23 06:10:18 -04:00
return @ide.$http {
method: "DELETE"
url: "/project/#{@ide.project_id}/#{entity.type}/#{entity.id}"
2014-06-22 15:49:58 -04:00
headers:
"X-Csrf-Token": window.csrfToken
}
2014-06-23 05:57:31 -04:00
moveEntity: (entity, parent_folder, callback = (error) ->) ->
@_moveEntityInScope(entity, parent_folder)
2014-06-23 06:10:18 -04:00
return @ide.$http.post "/project/#{@ide.project_id}/#{entity.type}/#{entity.id}/move", {
folder_id: parent_folder.id
_csrf: window.csrfToken
2014-06-23 05:57:31 -04:00
}
2014-07-03 06:01:41 -04:00
_deleteEntityFromScope: (entity, options = { moveToDeleted: true }) ->
2014-06-22 15:49:58 -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)
2014-06-23 05:57:31 -04:00
2014-07-03 06:01:41 -04:00
if entity.type == "doc" and options.moveToDeleted
2014-06-27 12:11:26 -04:00
entity.deleted = true
@$scope.deletedDocs.push entity
2014-06-23 05:57:31 -04:00
_moveEntityInScope: (entity, parent_folder) ->
return if entity in parent_folder.children
2014-07-03 06:01:41 -04:00
@_deleteEntityFromScope(entity, moveToDeleted: false)
2014-06-23 05:57:31 -04:00
parent_folder.children.push(entity)