2018-11-05 05:06:39 -05:00
|
|
|
/* eslint-disable
|
|
|
|
camelcase,
|
2022-05-16 10:25:49 -04:00
|
|
|
n/handle-callback-err,
|
2018-11-05 05:06:39 -05:00
|
|
|
max-len,
|
|
|
|
no-dupe-class-members,
|
|
|
|
no-return-assign,
|
|
|
|
no-unused-vars,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS101: Remove unnecessary use of Array.from
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* DS103: Rewrite code to no longer use __guard__
|
|
|
|
* DS202: Simplify dynamic range loops
|
|
|
|
* DS205: Consider reworking code to avoid use of IIFEs
|
|
|
|
* DS207: Consider shorter variations of null checks
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
2020-05-19 05:02:56 -04:00
|
|
|
import './directives/fileEntity'
|
|
|
|
import './controllers/FileTreeController'
|
|
|
|
import './controllers/FileTreeEntityController'
|
|
|
|
import './controllers/FileTreeFolderController'
|
2020-11-26 09:22:30 -05:00
|
|
|
import '../../features/file-tree/controllers/file-tree-controller'
|
2020-05-19 05:02:56 -04:00
|
|
|
let FileTreeManager
|
|
|
|
|
2020-12-15 05:23:54 -05:00
|
|
|
export default FileTreeManager = class FileTreeManager {
|
2020-05-19 05:02:56 -04:00
|
|
|
constructor(ide, $scope) {
|
|
|
|
this.ide = ide
|
|
|
|
this.$scope = $scope
|
|
|
|
this.$scope.$on('project:joined', () => {
|
|
|
|
this.loadRootFolder()
|
|
|
|
this.loadDeletedDocs()
|
|
|
|
return this.$scope.$emit('file-tree:initialized')
|
|
|
|
})
|
|
|
|
|
2020-11-26 09:22:30 -05:00
|
|
|
this.$scope.$on('entities:multiSelected', (_event, data) => {
|
|
|
|
this.$scope.$apply(() => {
|
|
|
|
this.$scope.multiSelectedCount = data.count
|
2022-03-21 10:47:26 -04:00
|
|
|
this.$scope.editor.multiSelectedCount = data.count
|
2020-11-26 09:22:30 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
this.$scope.$watch('rootFolder', rootFolder => {
|
|
|
|
if (rootFolder != null) {
|
|
|
|
return this.recalculateDocList()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
this._bindToSocketEvents()
|
|
|
|
|
|
|
|
this.$scope.multiSelectedCount = 0
|
|
|
|
|
|
|
|
$(document).on('click', () => {
|
|
|
|
this.clearMultiSelectedEntities()
|
2022-05-18 06:35:52 -04:00
|
|
|
setTimeout(() => this.$scope.$digest(), 0)
|
2020-05-19 05:02:56 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
_bindToSocketEvents() {
|
|
|
|
this.ide.socket.on('reciveNewDoc', (parent_folder_id, doc) => {
|
|
|
|
const parent_folder =
|
|
|
|
this.findEntityById(parent_folder_id) || this.$scope.rootFolder
|
|
|
|
return this.$scope.$apply(() => {
|
|
|
|
parent_folder.children.push({
|
|
|
|
name: doc.name,
|
|
|
|
id: doc._id,
|
2021-04-27 03:52:58 -04:00
|
|
|
type: 'doc',
|
2018-11-05 05:06:39 -05:00
|
|
|
})
|
2020-05-19 05:02:56 -04:00
|
|
|
return this.recalculateDocList()
|
2018-11-05 05:06:39 -05:00
|
|
|
})
|
2020-05-19 05:02:56 -04:00
|
|
|
})
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
this.ide.socket.on(
|
|
|
|
'reciveNewFile',
|
|
|
|
(parent_folder_id, file, source, linkedFileData) => {
|
2018-11-05 05:06:39 -05:00
|
|
|
const parent_folder =
|
|
|
|
this.findEntityById(parent_folder_id) || this.$scope.rootFolder
|
|
|
|
return this.$scope.$apply(() => {
|
|
|
|
parent_folder.children.push({
|
2020-05-19 05:02:56 -04:00
|
|
|
name: file.name,
|
|
|
|
id: file._id,
|
|
|
|
type: 'file',
|
|
|
|
linkedFileData,
|
2021-04-27 03:52:58 -04:00
|
|
|
created: file.created,
|
2018-11-05 05:06:39 -05:00
|
|
|
})
|
|
|
|
return this.recalculateDocList()
|
|
|
|
})
|
2020-05-19 05:02:56 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
this.ide.socket.on('reciveNewFolder', (parent_folder_id, folder) => {
|
|
|
|
const parent_folder =
|
|
|
|
this.findEntityById(parent_folder_id) || this.$scope.rootFolder
|
|
|
|
return this.$scope.$apply(() => {
|
|
|
|
parent_folder.children.push({
|
|
|
|
name: folder.name,
|
|
|
|
id: folder._id,
|
|
|
|
type: 'folder',
|
2021-04-27 03:52:58 -04:00
|
|
|
children: [],
|
2018-11-05 05:06:39 -05:00
|
|
|
})
|
2020-05-19 05:02:56 -04:00
|
|
|
return this.recalculateDocList()
|
2018-11-05 05:06:39 -05:00
|
|
|
})
|
2020-05-19 05:02:56 -04:00
|
|
|
})
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
this.ide.socket.on('reciveEntityRename', (entity_id, name) => {
|
|
|
|
const entity = this.findEntityById(entity_id)
|
|
|
|
if (entity == null) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return this.$scope.$apply(() => {
|
|
|
|
entity.name = name
|
|
|
|
return this.recalculateDocList()
|
2018-11-05 05:06:39 -05:00
|
|
|
})
|
2020-05-19 05:02:56 -04:00
|
|
|
})
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
this.ide.socket.on('removeEntity', entity_id => {
|
|
|
|
const entity = this.findEntityById(entity_id)
|
|
|
|
if (entity == null) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
this.$scope.$apply(() => {
|
|
|
|
this._deleteEntityFromScope(entity)
|
|
|
|
return this.recalculateDocList()
|
2018-11-05 05:06:39 -05:00
|
|
|
})
|
2020-05-19 05:02:56 -04:00
|
|
|
return this.$scope.$broadcast('entity:deleted', entity)
|
|
|
|
})
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
return this.ide.socket.on('reciveEntityMove', (entity_id, folder_id) => {
|
|
|
|
const entity = this.findEntityById(entity_id)
|
|
|
|
const folder = this.findEntityById(folder_id)
|
|
|
|
return this.$scope.$apply(() => {
|
|
|
|
this._moveEntityInScope(entity, folder)
|
|
|
|
return this.recalculateDocList()
|
2020-03-03 11:36:49 -05:00
|
|
|
})
|
2020-05-19 05:02:56 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
selectEntity(entity) {
|
|
|
|
this.selected_entity_id = entity.id // For reselecting after a reconnect
|
|
|
|
this.ide.fileTreeManager.forEachEntity(entity => (entity.selected = false))
|
|
|
|
return (entity.selected = true)
|
|
|
|
}
|
|
|
|
|
|
|
|
toggleMultiSelectEntity(entity) {
|
|
|
|
entity.multiSelected = !entity.multiSelected
|
2022-03-21 10:47:26 -04:00
|
|
|
this.$scope.multiSelectedCount = this.multiSelectedCount()
|
|
|
|
this.$scope.editor.multiSelectedCount = this.$scope.multiSelectedCount
|
2020-05-19 05:02:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
multiSelectedCount() {
|
|
|
|
let count = 0
|
2021-04-14 09:17:21 -04:00
|
|
|
this.forEachEntity(function (entity) {
|
2020-05-19 05:02:56 -04:00
|
|
|
if (entity.multiSelected) {
|
|
|
|
return count++
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return count
|
|
|
|
}
|
|
|
|
|
|
|
|
getMultiSelectedEntities() {
|
|
|
|
const entities = []
|
2021-04-14 09:17:21 -04:00
|
|
|
this.forEachEntity(function (e) {
|
2020-05-19 05:02:56 -04:00
|
|
|
if (e.multiSelected) {
|
|
|
|
return entities.push(e)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return entities
|
|
|
|
}
|
|
|
|
|
|
|
|
getFullCount() {
|
|
|
|
const entities = []
|
2021-04-14 09:17:21 -04:00
|
|
|
this.forEachEntity(function (e) {
|
2020-05-19 05:02:56 -04:00
|
|
|
if (!e.deleted) entities.push(e)
|
|
|
|
})
|
|
|
|
return entities.length
|
|
|
|
}
|
|
|
|
|
|
|
|
getMultiSelectedEntityChildNodes() {
|
|
|
|
// use pathnames with a leading slash to avoid
|
|
|
|
// problems with reserved Object properties
|
|
|
|
const entities = this.getMultiSelectedEntities()
|
|
|
|
const paths = {}
|
2021-10-26 04:08:56 -04:00
|
|
|
for (const entity of Array.from(entities)) {
|
2020-05-19 05:02:56 -04:00
|
|
|
paths['/' + this.getEntityPath(entity)] = entity
|
|
|
|
}
|
|
|
|
const prefixes = {}
|
2021-10-26 04:08:56 -04:00
|
|
|
for (const path in paths) {
|
|
|
|
const entity = paths[path]
|
2020-05-19 05:02:56 -04:00
|
|
|
const parts = path.split('/')
|
|
|
|
if (parts.length <= 2) {
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
// Record prefixes a/b/c.tex -> 'a' and 'a/b'
|
|
|
|
for (
|
|
|
|
let i = 1, end = parts.length - 1, asc = end >= 1;
|
|
|
|
asc ? i <= end : i >= end;
|
|
|
|
asc ? i++ : i--
|
|
|
|
) {
|
|
|
|
prefixes['/' + parts.slice(0, i).join('/')] = true
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-19 05:02:56 -04:00
|
|
|
const child_entities = []
|
2021-10-26 04:08:56 -04:00
|
|
|
for (const path in paths) {
|
2020-05-19 05:02:56 -04:00
|
|
|
// If the path is in the prefixes, then it's a parent folder and
|
|
|
|
// should be ignore
|
2021-10-26 04:08:56 -04:00
|
|
|
const entity = paths[path]
|
2020-05-19 05:02:56 -04:00
|
|
|
if (prefixes[path] == null) {
|
|
|
|
child_entities.push(entity)
|
2019-12-16 05:55:45 -05:00
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
2020-05-19 05:02:56 -04:00
|
|
|
return child_entities
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
clearMultiSelectedEntities() {
|
|
|
|
if (this.$scope.multiSelectedCount === 0) {
|
|
|
|
return
|
|
|
|
} // Be efficient, this is called a lot on 'click'
|
|
|
|
this.forEachEntity(entity => (entity.multiSelected = false))
|
|
|
|
return (this.$scope.multiSelectedCount = 0)
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
multiSelectSelectedEntity() {
|
|
|
|
const entity = this.findSelectedEntity()
|
|
|
|
if (entity) {
|
|
|
|
entity.multiSelected = true
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
2020-05-19 05:02:56 -04:00
|
|
|
this.$scope.multiSelectedCount = this.multiSelectedCount()
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
existsInFolder(folder_id, name) {
|
|
|
|
const folder = this.findEntityById(folder_id)
|
|
|
|
if (folder == null) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
const entity = this._findEntityByPathInFolder(folder, name)
|
|
|
|
return entity != null
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
findSelectedEntity() {
|
|
|
|
let selected = null
|
2021-04-14 09:17:21 -04:00
|
|
|
this.forEachEntity(function (entity) {
|
2020-05-19 05:02:56 -04:00
|
|
|
if (entity.selected) {
|
|
|
|
return (selected = entity)
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
2020-05-19 05:02:56 -04:00
|
|
|
})
|
|
|
|
return selected
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
findEntityById(id, options) {
|
|
|
|
if (options == null) {
|
|
|
|
options = {}
|
|
|
|
}
|
|
|
|
if (this.$scope.rootFolder.id === id) {
|
|
|
|
return this.$scope.rootFolder
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
let entity = this._findEntityByIdInFolder(this.$scope.rootFolder, id)
|
|
|
|
if (entity != null) {
|
|
|
|
return entity
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
if (options.includeDeleted) {
|
|
|
|
for (entity of Array.from(this.$scope.deletedDocs)) {
|
2018-11-05 05:06:39 -05:00
|
|
|
if (entity.id === id) {
|
|
|
|
return entity
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
return null
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
_findEntityByIdInFolder(folder, id) {
|
2021-05-05 09:05:04 -04:00
|
|
|
for (const entity of Array.from(folder.children || [])) {
|
2020-05-19 05:02:56 -04:00
|
|
|
if (entity.id === id) {
|
|
|
|
return entity
|
|
|
|
} else if (entity.children != null) {
|
|
|
|
const result = this._findEntityByIdInFolder(entity, id)
|
|
|
|
if (result != null) {
|
|
|
|
return result
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
return null
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
findEntityByPath(path) {
|
|
|
|
return this._findEntityByPathInFolder(this.$scope.rootFolder, path)
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
_findEntityByPathInFolder(folder, path) {
|
|
|
|
if (path == null || folder == null) {
|
2018-11-05 05:06:39 -05:00
|
|
|
return null
|
|
|
|
}
|
2020-05-19 05:02:56 -04:00
|
|
|
if (path === '') {
|
|
|
|
return folder
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
const parts = path.split('/')
|
|
|
|
const name = parts.shift()
|
|
|
|
const rest = parts.join('/')
|
|
|
|
|
|
|
|
if (name === '.') {
|
|
|
|
return this._findEntityByPathInFolder(folder, rest)
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
|
|
|
|
2021-05-05 09:05:04 -04:00
|
|
|
for (const entity of Array.from(folder.children)) {
|
2020-05-19 05:02:56 -04:00
|
|
|
if (entity.name === name) {
|
|
|
|
if (rest === '') {
|
|
|
|
return entity
|
|
|
|
} else if (entity.type === 'folder') {
|
|
|
|
return this._findEntityByPathInFolder(entity, rest)
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
|
|
|
}
|
2020-05-19 05:02:56 -04:00
|
|
|
return null
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
forEachEntity(callback) {
|
|
|
|
if (callback == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
callback = function () {}
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
2020-05-19 05:02:56 -04:00
|
|
|
this._forEachEntityInFolder(this.$scope.rootFolder, null, callback)
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
return (() => {
|
|
|
|
const result = []
|
2021-05-05 09:05:04 -04:00
|
|
|
for (const entity of Array.from(this.$scope.deletedDocs || [])) {
|
2020-05-19 05:02:56 -04:00
|
|
|
result.push(callback(entity))
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
2020-05-19 05:02:56 -04:00
|
|
|
return result
|
|
|
|
})()
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
_forEachEntityInFolder(folder, path, callback) {
|
|
|
|
return (() => {
|
|
|
|
const result = []
|
2021-05-05 09:05:04 -04:00
|
|
|
for (const entity of Array.from(folder.children || [])) {
|
2021-10-26 04:08:56 -04:00
|
|
|
let childPath
|
2020-05-19 05:02:56 -04:00
|
|
|
if (path != null) {
|
|
|
|
childPath = path + '/' + entity.name
|
|
|
|
} else {
|
|
|
|
childPath = entity.name
|
|
|
|
}
|
|
|
|
callback(entity, folder, childPath)
|
|
|
|
if (entity.children != null) {
|
|
|
|
result.push(this._forEachEntityInFolder(entity, childPath, callback))
|
|
|
|
} else {
|
|
|
|
result.push(undefined)
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
2020-05-19 05:02:56 -04:00
|
|
|
return result
|
|
|
|
})()
|
|
|
|
}
|
|
|
|
|
|
|
|
getEntityPath(entity) {
|
|
|
|
return this._getEntityPathInFolder(this.$scope.rootFolder, entity)
|
|
|
|
}
|
|
|
|
|
|
|
|
_getEntityPathInFolder(folder, entity) {
|
2021-05-05 09:05:04 -04:00
|
|
|
for (const child of Array.from(folder.children || [])) {
|
2020-05-19 05:02:56 -04:00
|
|
|
if (child === entity) {
|
|
|
|
return entity.name
|
|
|
|
} else if (child.type === 'folder') {
|
|
|
|
const path = this._getEntityPathInFolder(child, entity)
|
|
|
|
if (path != null) {
|
|
|
|
return child.name + '/' + path
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
2020-05-19 05:02:56 -04:00
|
|
|
}
|
|
|
|
return null
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
getRootDocDirname() {
|
|
|
|
const rootDoc = this.findEntityById(this.$scope.project.rootDoc_id)
|
|
|
|
if (rootDoc == null) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return this._getEntityDirname(rootDoc)
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
_getEntityDirname(entity) {
|
|
|
|
const path = this.getEntityPath(entity)
|
|
|
|
if (path == null) {
|
|
|
|
return
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
2021-04-14 09:17:21 -04:00
|
|
|
return path.split('/').slice(0, -1).join('/')
|
2020-05-19 05:02:56 -04:00
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
_findParentFolder(entity) {
|
|
|
|
const dirname = this._getEntityDirname(entity)
|
|
|
|
if (dirname == null) {
|
|
|
|
return
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
2020-05-19 05:02:56 -04:00
|
|
|
return this.findEntityByPath(dirname)
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
loadRootFolder() {
|
|
|
|
return (this.$scope.rootFolder = this._parseFolder(
|
|
|
|
__guard__(
|
|
|
|
this.$scope != null ? this.$scope.project : undefined,
|
|
|
|
x => x.rootFolder[0]
|
|
|
|
)
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
_parseFolder(rawFolder) {
|
|
|
|
const folder = {
|
|
|
|
name: rawFolder.name,
|
|
|
|
id: rawFolder._id,
|
|
|
|
type: 'folder',
|
|
|
|
children: [],
|
2021-04-27 03:52:58 -04:00
|
|
|
selected: rawFolder._id === this.selected_entity_id,
|
2020-05-19 05:02:56 -04:00
|
|
|
}
|
|
|
|
|
2021-05-05 09:05:04 -04:00
|
|
|
for (const doc of Array.from(rawFolder.docs || [])) {
|
2020-05-19 05:02:56 -04:00
|
|
|
folder.children.push({
|
|
|
|
name: doc.name,
|
|
|
|
id: doc._id,
|
|
|
|
type: 'doc',
|
2021-04-27 03:52:58 -04:00
|
|
|
selected: doc._id === this.selected_entity_id,
|
2018-11-05 05:06:39 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-05-05 09:05:04 -04:00
|
|
|
for (const file of Array.from(rawFolder.fileRefs || [])) {
|
2020-05-19 05:02:56 -04:00
|
|
|
folder.children.push({
|
|
|
|
name: file.name,
|
|
|
|
id: file._id,
|
|
|
|
type: 'file',
|
|
|
|
selected: file._id === this.selected_entity_id,
|
|
|
|
linkedFileData: file.linkedFileData,
|
2021-04-27 03:52:58 -04:00
|
|
|
created: file.created,
|
2020-05-19 05:02:56 -04:00
|
|
|
})
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
|
|
|
|
2021-05-05 09:05:04 -04:00
|
|
|
for (const childFolder of Array.from(rawFolder.folders || [])) {
|
2020-05-19 05:02:56 -04:00
|
|
|
folder.children.push(this._parseFolder(childFolder))
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
return folder
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
loadDeletedDocs() {
|
|
|
|
this.$scope.deletedDocs = []
|
|
|
|
return Array.from(this.$scope.project.deletedDocs || []).map(doc =>
|
|
|
|
this.$scope.deletedDocs.push({
|
|
|
|
name: doc.name,
|
|
|
|
id: doc._id,
|
|
|
|
type: 'doc',
|
2021-04-27 03:52:58 -04:00
|
|
|
deleted: true,
|
2020-05-19 05:02:56 -04:00
|
|
|
})
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
recalculateDocList() {
|
|
|
|
this.$scope.docs = []
|
|
|
|
this.forEachEntity((entity, parentFolder, path) => {
|
|
|
|
if (entity.type === 'doc' && !entity.deleted) {
|
|
|
|
return this.$scope.docs.push({
|
|
|
|
doc: entity,
|
2021-04-27 03:52:58 -04:00
|
|
|
path,
|
2020-05-19 05:02:56 -04:00
|
|
|
})
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
2020-05-19 05:02:56 -04:00
|
|
|
})
|
|
|
|
// Keep list ordered by folders, then name
|
2021-04-14 09:17:21 -04:00
|
|
|
return this.$scope.docs.sort(function (a, b) {
|
2020-05-19 05:02:56 -04:00
|
|
|
const aDepth = (a.path.match(/\//g) || []).length
|
|
|
|
const 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
|
2021-03-31 08:20:55 -04:00
|
|
|
} else if (a.path > b.path) {
|
2020-05-19 05:02:56 -04:00
|
|
|
return 1
|
2021-03-31 08:20:55 -04:00
|
|
|
} else {
|
|
|
|
return 0
|
2020-05-19 05:02:56 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
getCurrentFolder() {
|
|
|
|
// Return the root folder if nothing is selected
|
|
|
|
return (
|
|
|
|
this._getCurrentFolder(this.$scope.rootFolder) || this.$scope.rootFolder
|
|
|
|
)
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
_getCurrentFolder(startFolder) {
|
|
|
|
if (startFolder == null) {
|
|
|
|
startFolder = this.$scope.rootFolder
|
|
|
|
}
|
2021-05-05 09:05:04 -04:00
|
|
|
for (const entity of Array.from(startFolder.children || [])) {
|
2020-05-19 05:02:56 -04:00
|
|
|
// The 'current' folder is either the one selected, or
|
|
|
|
// the one containing the selected doc/file
|
|
|
|
if (entity.selected) {
|
2018-11-05 05:06:39 -05:00
|
|
|
if (entity.type === 'folder') {
|
2020-05-19 05:02:56 -04:00
|
|
|
return entity
|
|
|
|
} else {
|
|
|
|
return startFolder
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
if (entity.type === 'folder') {
|
|
|
|
const result = this._getCurrentFolder(entity)
|
|
|
|
if (result != null) {
|
|
|
|
return result
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
return null
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
projectContainsFolder() {
|
2021-05-05 09:05:04 -04:00
|
|
|
for (const entity of Array.from(this.$scope.rootFolder.children)) {
|
2020-05-19 05:02:56 -04:00
|
|
|
if (entity.type === 'folder') {
|
|
|
|
return true
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
|
|
|
}
|
2020-05-19 05:02:56 -04:00
|
|
|
return false
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
existsInThisFolder(folder, name) {
|
2021-05-05 09:05:04 -04:00
|
|
|
for (const entity of Array.from(
|
2020-05-19 05:02:56 -04:00
|
|
|
(folder != null ? folder.children : undefined) || []
|
|
|
|
)) {
|
|
|
|
if (entity.name === name) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
nameExistsError(message) {
|
|
|
|
if (message == null) {
|
|
|
|
message = 'already exists'
|
|
|
|
}
|
|
|
|
const nameExists = this.ide.$q.defer()
|
|
|
|
nameExists.reject({ data: message })
|
|
|
|
return nameExists.promise
|
|
|
|
}
|
|
|
|
|
|
|
|
createDoc(name, parent_folder) {
|
|
|
|
// check if a doc/file/folder already exists with this name
|
|
|
|
if (parent_folder == null) {
|
|
|
|
parent_folder = this.getCurrentFolder()
|
|
|
|
}
|
|
|
|
if (this.existsInThisFolder(parent_folder, name)) {
|
|
|
|
return this.nameExistsError()
|
|
|
|
}
|
|
|
|
// We'll wait for the socket.io notification to actually
|
|
|
|
// add the doc for us.
|
|
|
|
return this.ide.$http.post(`/project/${this.ide.project_id}/doc`, {
|
|
|
|
name,
|
|
|
|
parent_folder_id: parent_folder != null ? parent_folder.id : undefined,
|
2021-04-27 03:52:58 -04:00
|
|
|
_csrf: window.csrfToken,
|
2020-05-19 05:02:56 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
createFolder(name, parent_folder) {
|
|
|
|
// check if a doc/file/folder already exists with this name
|
|
|
|
if (parent_folder == null) {
|
|
|
|
parent_folder = this.getCurrentFolder()
|
|
|
|
}
|
|
|
|
if (this.existsInThisFolder(parent_folder, name)) {
|
|
|
|
return this.nameExistsError()
|
|
|
|
}
|
|
|
|
// We'll wait for the socket.io notification to actually
|
|
|
|
// add the folder for us.
|
|
|
|
return this.ide.$http.post(`/project/${this.ide.project_id}/folder`, {
|
|
|
|
name,
|
|
|
|
parent_folder_id: parent_folder != null ? parent_folder.id : undefined,
|
2021-04-27 03:52:58 -04:00
|
|
|
_csrf: window.csrfToken,
|
2020-05-19 05:02:56 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
createLinkedFile(name, parent_folder, provider, data) {
|
|
|
|
// check if a doc/file/folder already exists with this name
|
|
|
|
if (parent_folder == null) {
|
|
|
|
parent_folder = this.getCurrentFolder()
|
|
|
|
}
|
|
|
|
if (this.existsInThisFolder(parent_folder, name)) {
|
|
|
|
return this.nameExistsError()
|
|
|
|
}
|
|
|
|
// We'll wait for the socket.io notification to actually
|
|
|
|
// add the file for us.
|
|
|
|
return this.ide.$http.post(
|
|
|
|
`/project/${this.ide.project_id}/linked_file`,
|
|
|
|
{
|
2018-11-05 05:06:39 -05:00
|
|
|
name,
|
|
|
|
parent_folder_id: parent_folder != null ? parent_folder.id : undefined,
|
2020-05-19 05:02:56 -04:00
|
|
|
provider,
|
|
|
|
data,
|
2021-04-27 03:52:58 -04:00
|
|
|
_csrf: window.csrfToken,
|
2020-05-19 05:02:56 -04:00
|
|
|
},
|
|
|
|
{
|
2021-04-27 03:52:58 -04:00
|
|
|
disableAutoLoginRedirect: true,
|
2020-05-19 05:02:56 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
refreshLinkedFile(file) {
|
|
|
|
const parent_folder = this._findParentFolder(file)
|
|
|
|
const provider =
|
|
|
|
file.linkedFileData != null ? file.linkedFileData.provider : undefined
|
|
|
|
if (provider == null) {
|
|
|
|
console.warn(`>> no provider for ${file.name}`, file)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return this.ide.$http.post(
|
|
|
|
`/project/${this.ide.project_id}/linked_file/${file.id}/refresh`,
|
|
|
|
{
|
2021-04-27 03:52:58 -04:00
|
|
|
_csrf: window.csrfToken,
|
2020-05-19 05:02:56 -04:00
|
|
|
},
|
|
|
|
{
|
2021-04-27 03:52:58 -04:00
|
|
|
disableAutoLoginRedirect: true,
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
2020-05-19 05:02:56 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
renameEntity(entity, name, callback) {
|
|
|
|
if (callback == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
callback = function () {}
|
2020-05-19 05:02:56 -04:00
|
|
|
}
|
|
|
|
if (entity.name === name) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (name.length >= 150) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// check if a doc/file/folder already exists with this name
|
|
|
|
const parent_folder = this.getCurrentFolder()
|
|
|
|
if (this.existsInThisFolder(parent_folder, name)) {
|
|
|
|
return this.nameExistsError()
|
|
|
|
}
|
|
|
|
entity.renamingToName = name
|
|
|
|
return this.ide.$http
|
|
|
|
.post(
|
|
|
|
`/project/${this.ide.project_id}/${entity.type}/${entity.id}/rename`,
|
2018-11-05 05:06:39 -05:00
|
|
|
{
|
|
|
|
name,
|
2021-04-27 03:52:58 -04:00
|
|
|
_csrf: window.csrfToken,
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
|
|
|
)
|
2020-05-19 05:02:56 -04:00
|
|
|
.then(() => (entity.name = name))
|
|
|
|
.finally(() => (entity.renamingToName = null))
|
|
|
|
}
|
|
|
|
|
|
|
|
deleteEntity(entity, callback) {
|
|
|
|
// We'll wait for the socket.io notification to
|
|
|
|
// delete from scope.
|
|
|
|
if (callback == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
callback = function () {}
|
2020-05-19 05:02:56 -04:00
|
|
|
}
|
|
|
|
return this.ide.queuedHttp({
|
|
|
|
method: 'DELETE',
|
|
|
|
url: `/project/${this.ide.project_id}/${entity.type}/${entity.id}`,
|
|
|
|
headers: {
|
2021-04-27 03:52:58 -04:00
|
|
|
'X-Csrf-Token': window.csrfToken,
|
|
|
|
},
|
2020-05-19 05:02:56 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
moveEntity(entity, parent_folder) {
|
|
|
|
// Abort move if the folder being moved (entity) has the parent_folder as child
|
|
|
|
// since that would break the tree structure.
|
|
|
|
if (this._isChildFolder(entity, parent_folder)) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// check if a doc/file/folder already exists with this name
|
|
|
|
if (this.existsInThisFolder(parent_folder, entity.name)) {
|
|
|
|
throw new Error('file exists in this location')
|
|
|
|
}
|
|
|
|
// Wait for the http response before doing the move
|
|
|
|
this.ide.queuedHttp
|
|
|
|
.post(
|
|
|
|
`/project/${this.ide.project_id}/${entity.type}/${entity.id}/move`,
|
2018-11-05 05:06:39 -05:00
|
|
|
{
|
2020-05-19 05:02:56 -04:00
|
|
|
folder_id: parent_folder.id,
|
2021-04-27 03:52:58 -04:00
|
|
|
_csrf: window.csrfToken,
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
|
|
|
)
|
2020-05-19 05:02:56 -04:00
|
|
|
.then(() => {
|
|
|
|
this._moveEntityInScope(entity, parent_folder)
|
2018-11-05 05:06:39 -05:00
|
|
|
})
|
2020-05-19 05:02:56 -04:00
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
_isChildFolder(parent_folder, child_folder) {
|
|
|
|
const parent_path = this.getEntityPath(parent_folder) || '' // null if root folder
|
|
|
|
const child_path = this.getEntityPath(child_folder) || '' // null if root folder
|
|
|
|
// is parent path the beginning of child path?
|
|
|
|
return child_path.slice(0, parent_path.length) === parent_path
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
_deleteEntityFromScope(entity, options) {
|
|
|
|
if (options == null) {
|
|
|
|
options = { moveToDeleted: true }
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
2020-05-19 05:02:56 -04:00
|
|
|
if (entity == null) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
let parent_folder = null
|
2021-04-14 09:17:21 -04:00
|
|
|
this.forEachEntity(function (possible_entity, folder) {
|
2020-05-19 05:02:56 -04:00
|
|
|
if (possible_entity === entity) {
|
|
|
|
return (parent_folder = folder)
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
2020-05-19 05:02:56 -04:00
|
|
|
})
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
if (parent_folder != null) {
|
|
|
|
const index = parent_folder.children.indexOf(entity)
|
|
|
|
if (index > -1) {
|
|
|
|
parent_folder.children.splice(index, 1)
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
2020-05-19 05:02:56 -04:00
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
if (entity.type !== 'folder' && entity.selected) {
|
|
|
|
this.$scope.ui.view = null
|
|
|
|
}
|
2019-12-16 05:55:45 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
if (entity.type === 'doc' && options.moveToDeleted) {
|
|
|
|
entity.deleted = true
|
|
|
|
return this.$scope.deletedDocs.push(entity)
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
2020-05-19 05:02:56 -04:00
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
_moveEntityInScope(entity, parent_folder) {
|
|
|
|
if (Array.from(parent_folder.children).includes(entity)) {
|
|
|
|
return
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
2020-05-19 05:02:56 -04:00
|
|
|
this._deleteEntityFromScope(entity, { moveToDeleted: false })
|
|
|
|
return parent_folder.children.push(entity)
|
|
|
|
}
|
2020-12-15 05:23:54 -05:00
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
|
|
|
function __guard__(value, transform) {
|
|
|
|
return typeof value !== 'undefined' && value !== null
|
|
|
|
? transform(value)
|
|
|
|
: undefined
|
|
|
|
}
|