| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761 |
- /* eslint-disable
- camelcase,
- node/handle-callback-err,
- 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
- */
- import './directives/fileEntity'
- import './directives/draggable'
- import './directives/droppable'
- import './controllers/FileTreeController'
- import './controllers/FileTreeEntityController'
- import './controllers/FileTreeFolderController'
- import './controllers/FileTreeRootFolderController'
- import '../../features/file-tree/controllers/file-tree-controller'
- let FileTreeManager
- export default FileTreeManager = class FileTreeManager {
- 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')
- })
- this.$scope.$on('entities:multiSelected', (_event, data) => {
- this.$scope.$apply(() => {
- this.$scope.multiSelectedCount = data.count
- })
- })
- this.$scope.$watch('rootFolder', rootFolder => {
- if (rootFolder != null) {
- return this.recalculateDocList()
- }
- })
- this._bindToSocketEvents()
- this.$scope.multiSelectedCount = 0
- $(document).on('click', () => {
- this.clearMultiSelectedEntities()
- return this.$scope.$digest()
- })
- }
- _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,
- type: 'doc',
- })
- return this.recalculateDocList()
- })
- })
- this.ide.socket.on(
- 'reciveNewFile',
- (parent_folder_id, file, source, linkedFileData) => {
- const parent_folder =
- this.findEntityById(parent_folder_id) || this.$scope.rootFolder
- return this.$scope.$apply(() => {
- parent_folder.children.push({
- name: file.name,
- id: file._id,
- type: 'file',
- linkedFileData,
- created: file.created,
- })
- return this.recalculateDocList()
- })
- }
- )
- 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',
- children: [],
- })
- return this.recalculateDocList()
- })
- })
- 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()
- })
- })
- 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()
- })
- return this.$scope.$broadcast('entity:deleted', entity)
- })
- 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()
- })
- })
- }
- 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
- return (this.$scope.multiSelectedCount = this.multiSelectedCount())
- }
- multiSelectedCount() {
- let count = 0
- this.forEachEntity(function (entity) {
- if (entity.multiSelected) {
- return count++
- }
- })
- return count
- }
- getMultiSelectedEntities() {
- const entities = []
- this.forEachEntity(function (e) {
- if (e.multiSelected) {
- return entities.push(e)
- }
- })
- return entities
- }
- getFullCount() {
- const entities = []
- this.forEachEntity(function (e) {
- 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 = {}
- for (var entity of Array.from(entities)) {
- paths['/' + this.getEntityPath(entity)] = entity
- }
- const prefixes = {}
- for (var path in paths) {
- entity = paths[path]
- 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
- }
- }
- }
- const child_entities = []
- for (path in paths) {
- // If the path is in the prefixes, then it's a parent folder and
- // should be ignore
- entity = paths[path]
- if (prefixes[path] == null) {
- child_entities.push(entity)
- }
- }
- return child_entities
- }
- 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)
- }
- multiSelectSelectedEntity() {
- const entity = this.findSelectedEntity()
- if (entity) {
- entity.multiSelected = true
- }
- this.$scope.multiSelectedCount = this.multiSelectedCount()
- }
- existsInFolder(folder_id, name) {
- const folder = this.findEntityById(folder_id)
- if (folder == null) {
- return false
- }
- const entity = this._findEntityByPathInFolder(folder, name)
- return entity != null
- }
- findSelectedEntity() {
- let selected = null
- this.forEachEntity(function (entity) {
- if (entity.selected) {
- return (selected = entity)
- }
- })
- return selected
- }
- findEntityById(id, options) {
- if (options == null) {
- options = {}
- }
- if (this.$scope.rootFolder.id === id) {
- return this.$scope.rootFolder
- }
- let entity = this._findEntityByIdInFolder(this.$scope.rootFolder, id)
- if (entity != null) {
- return entity
- }
- if (options.includeDeleted) {
- for (entity of Array.from(this.$scope.deletedDocs)) {
- if (entity.id === id) {
- return entity
- }
- }
- }
- return null
- }
- _findEntityByIdInFolder(folder, id) {
- for (const entity of Array.from(folder.children || [])) {
- if (entity.id === id) {
- return entity
- } else if (entity.children != null) {
- const result = this._findEntityByIdInFolder(entity, id)
- if (result != null) {
- return result
- }
- }
- }
- return null
- }
- findEntityByPath(path) {
- return this._findEntityByPathInFolder(this.$scope.rootFolder, path)
- }
- _findEntityByPathInFolder(folder, path) {
- if (path == null || folder == null) {
- return null
- }
- if (path === '') {
- return folder
- }
- const parts = path.split('/')
- const name = parts.shift()
- const rest = parts.join('/')
- if (name === '.') {
- return this._findEntityByPathInFolder(folder, rest)
- }
- for (const entity of Array.from(folder.children)) {
- if (entity.name === name) {
- if (rest === '') {
- return entity
- } else if (entity.type === 'folder') {
- return this._findEntityByPathInFolder(entity, rest)
- }
- }
- }
- return null
- }
- forEachEntity(callback) {
- if (callback == null) {
- callback = function (entity, parent_folder, path) {}
- }
- this._forEachEntityInFolder(this.$scope.rootFolder, null, callback)
- return (() => {
- const result = []
- for (const entity of Array.from(this.$scope.deletedDocs || [])) {
- result.push(callback(entity))
- }
- return result
- })()
- }
- _forEachEntityInFolder(folder, path, callback) {
- return (() => {
- const result = []
- for (const entity of Array.from(folder.children || [])) {
- var childPath
- 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)
- }
- }
- return result
- })()
- }
- getEntityPath(entity) {
- return this._getEntityPathInFolder(this.$scope.rootFolder, entity)
- }
- _getEntityPathInFolder(folder, entity) {
- for (const child of Array.from(folder.children || [])) {
- if (child === entity) {
- return entity.name
- } else if (child.type === 'folder') {
- const path = this._getEntityPathInFolder(child, entity)
- if (path != null) {
- return child.name + '/' + path
- }
- }
- }
- return null
- }
- getRootDocDirname() {
- const rootDoc = this.findEntityById(this.$scope.project.rootDoc_id)
- if (rootDoc == null) {
- return
- }
- return this._getEntityDirname(rootDoc)
- }
- _getEntityDirname(entity) {
- const path = this.getEntityPath(entity)
- if (path == null) {
- return
- }
- return path.split('/').slice(0, -1).join('/')
- }
- _findParentFolder(entity) {
- const dirname = this._getEntityDirname(entity)
- if (dirname == null) {
- return
- }
- return this.findEntityByPath(dirname)
- }
- 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: [],
- selected: rawFolder._id === this.selected_entity_id,
- }
- for (const doc of Array.from(rawFolder.docs || [])) {
- folder.children.push({
- name: doc.name,
- id: doc._id,
- type: 'doc',
- selected: doc._id === this.selected_entity_id,
- })
- }
- for (const file of Array.from(rawFolder.fileRefs || [])) {
- folder.children.push({
- name: file.name,
- id: file._id,
- type: 'file',
- selected: file._id === this.selected_entity_id,
- linkedFileData: file.linkedFileData,
- created: file.created,
- })
- }
- for (const childFolder of Array.from(rawFolder.folders || [])) {
- folder.children.push(this._parseFolder(childFolder))
- }
- return folder
- }
- 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',
- deleted: true,
- })
- )
- }
- recalculateDocList() {
- this.$scope.docs = []
- this.forEachEntity((entity, parentFolder, path) => {
- if (entity.type === 'doc' && !entity.deleted) {
- return this.$scope.docs.push({
- doc: entity,
- path,
- })
- }
- })
- // Keep list ordered by folders, then name
- return this.$scope.docs.sort(function (a, b) {
- 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
- } else if (a.path > b.path) {
- return 1
- } else {
- return 0
- }
- })
- }
- getEntityPath(entity) {
- return this._getEntityPathInFolder(this.$scope.rootFolder, entity)
- }
- _getEntityPathInFolder(folder, entity) {
- for (const child of Array.from(folder.children || [])) {
- if (child === entity) {
- return entity.name
- } else if (child.type === 'folder') {
- const path = this._getEntityPathInFolder(child, entity)
- if (path != null) {
- return child.name + '/' + path
- }
- }
- }
- return null
- }
- getCurrentFolder() {
- // Return the root folder if nothing is selected
- return (
- this._getCurrentFolder(this.$scope.rootFolder) || this.$scope.rootFolder
- )
- }
- _getCurrentFolder(startFolder) {
- if (startFolder == null) {
- startFolder = this.$scope.rootFolder
- }
- for (const entity of Array.from(startFolder.children || [])) {
- // 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') {
- const result = this._getCurrentFolder(entity)
- if (result != null) {
- return result
- }
- }
- }
- return null
- }
- projectContainsFolder() {
- for (const entity of Array.from(this.$scope.rootFolder.children)) {
- if (entity.type === 'folder') {
- return true
- }
- }
- return false
- }
- existsInThisFolder(folder, name) {
- for (const entity of Array.from(
- (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,
- _csrf: window.csrfToken,
- })
- }
- 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,
- _csrf: window.csrfToken,
- })
- }
- 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`,
- {
- name,
- parent_folder_id: parent_folder != null ? parent_folder.id : undefined,
- provider,
- data,
- _csrf: window.csrfToken,
- },
- {
- disableAutoLoginRedirect: true,
- }
- )
- }
- 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`,
- {
- _csrf: window.csrfToken,
- },
- {
- disableAutoLoginRedirect: true,
- }
- )
- }
- renameEntity(entity, name, callback) {
- if (callback == null) {
- callback = function (error) {}
- }
- 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`,
- {
- name,
- _csrf: window.csrfToken,
- }
- )
- .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) {
- callback = function (error) {}
- }
- return this.ide.queuedHttp({
- method: 'DELETE',
- url: `/project/${this.ide.project_id}/${entity.type}/${entity.id}`,
- headers: {
- 'X-Csrf-Token': window.csrfToken,
- },
- })
- }
- 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`,
- {
- folder_id: parent_folder.id,
- _csrf: window.csrfToken,
- }
- )
- .then(() => {
- this._moveEntityInScope(entity, parent_folder)
- })
- }
- _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
- }
- _deleteEntityFromScope(entity, options) {
- if (options == null) {
- options = { moveToDeleted: true }
- }
- if (entity == null) {
- return
- }
- let parent_folder = null
- this.forEachEntity(function (possible_entity, folder) {
- if (possible_entity === entity) {
- return (parent_folder = folder)
- }
- })
- if (parent_folder != null) {
- const index = parent_folder.children.indexOf(entity)
- if (index > -1) {
- parent_folder.children.splice(index, 1)
- }
- }
- if (entity.type !== 'folder' && entity.selected) {
- this.$scope.ui.view = null
- }
- if (entity.type === 'doc' && options.moveToDeleted) {
- entity.deleted = true
- return this.$scope.deletedDocs.push(entity)
- }
- }
- _moveEntityInScope(entity, parent_folder) {
- if (Array.from(parent_folder.children).includes(entity)) {
- return
- }
- this._deleteEntityFromScope(entity, { moveToDeleted: false })
- return parent_folder.children.push(entity)
- }
- }
- function __guard__(value, transform) {
- return typeof value !== 'undefined' && value !== null
- ? transform(value)
- : undefined
- }
|