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

117 lines
3 KiB
CoffeeScript
Raw Normal View History

2014-06-21 17:20:37 -04:00
define [
"ide/file-tree/directives/fileEntity"
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/FileTreeFolderController"
"ide/file-tree/controllers/FileTreeEntityController"
], () ->
class FileTreeManager
constructor: (@ide, @$scope) ->
@$scope.$on "project:joined", =>
@loadRootFolder()
2014-06-22 07:10:42 -04:00
@_bindToSocketEvents()
_bindToSocketEvents: () ->
@ide.socket.on "docCreated", (parent_folder_id, doc) =>
console.log "Doc created", parent_folder_id, doc
parent_folder = @findEntityById(parent_folder_id) or @$scope.rootFolder
@$scope.$apply () ->
parent_folder.children.push {
name: doc.name
id: doc._id
type: "doc"
}
findEntityById: (id) ->
@_findEntityByIdInFolder @$scope.rootFolder, id
_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
2014-06-21 17:20:37 -04:00
forEachEntity: (callback) ->
@_forEachEntityInFolder(@$scope.rootFolder, callback)
_forEachEntityInFolder: (folder, callback) ->
2014-06-22 07:10:42 -04:00
for entity in folder.children or []
2014-06-21 17:20:37 -04:00
callback(entity)
if entity.children?
@_forEachEntityInFolder(entity, callback)
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: []
}
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-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-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
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
createDocInCurrentFolder: (name, callback = (error, doc) ->) ->
# We'll wait for the socket.io 'createDoc' notification to actually
# add the doc for us.
parent_folder = @getCurrentFolder()
$.ajax {
url: "/project/#{@ide.project_id}/doc"
type: "POST"
contentType: "application/json; charset=utf-8"
data: JSON.stringify {
name: name,
parent_folder_id: parent_folder?.id
_csrf: window.csrfToken
}
dataType: "json"
success: (doc) ->
console.log "Got doc from POST", doc
callback(null, doc)
failure: (error) -> callback(error)
}