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: () ->
|
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 07:10:42 -04:00
|
|
|
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) ->) ->
|
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.
|
|
|
|
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) ->
|
|
|
|
callback(null, doc)
|
|
|
|
failure: (error) -> callback(error)
|
|
|
|
}
|
2014-06-22 09:39:38 -04:00
|
|
|
|
|
|
|
createFolderInCurrentFolder: (name, callback = (error, doc) ->) ->
|
|
|
|
# We'll wait for the socket.io notification to actually
|
|
|
|
# add the folder for us.
|
|
|
|
parent_folder = @getCurrentFolder()
|
|
|
|
$.ajax {
|
|
|
|
url: "/project/#{@ide.project_id}/folder"
|
|
|
|
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: (folder) ->
|
|
|
|
callback(null, folder)
|
|
|
|
failure: (error) -> callback(error)
|
|
|
|
}
|