FileTreeManager.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  1. /* eslint-disable
  2. camelcase,
  3. n/handle-callback-err,
  4. max-len,
  5. no-dupe-class-members,
  6. no-return-assign,
  7. no-unused-vars,
  8. */
  9. // TODO: This file was created by bulk-decaffeinate.
  10. // Fix any style issues and re-enable lint.
  11. /*
  12. * decaffeinate suggestions:
  13. * DS101: Remove unnecessary use of Array.from
  14. * DS102: Remove unnecessary code created because of implicit returns
  15. * DS103: Rewrite code to no longer use __guard__
  16. * DS202: Simplify dynamic range loops
  17. * DS205: Consider reworking code to avoid use of IIFEs
  18. * DS207: Consider shorter variations of null checks
  19. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  20. */
  21. import './directives/fileEntity'
  22. import './controllers/FileTreeController'
  23. import './controllers/FileTreeEntityController'
  24. import './controllers/FileTreeFolderController'
  25. import '../../features/file-tree/controllers/file-tree-controller'
  26. import { debugConsole } from '@/utils/debugging'
  27. let FileTreeManager
  28. export default FileTreeManager = class FileTreeManager {
  29. constructor(ide, $scope) {
  30. this.ide = ide
  31. this.$scope = $scope
  32. this.$scope.$on('project:joined', () => {
  33. this.loadRootFolder()
  34. this.loadDeletedDocs()
  35. return this.$scope.$emit('file-tree:initialized')
  36. })
  37. this.$scope.$on('entities:multiSelected', (_event, data) => {
  38. this.$scope.$apply(() => {
  39. this.$scope.multiSelectedCount = data.count
  40. this.$scope.editor.multiSelectedCount = data.count
  41. })
  42. })
  43. this.$scope.$watch('rootFolder', rootFolder => {
  44. if (rootFolder != null) {
  45. return this.recalculateDocList()
  46. }
  47. })
  48. this._bindToSocketEvents()
  49. this.$scope.multiSelectedCount = 0
  50. $(document).on('click', () => {
  51. this.clearMultiSelectedEntities()
  52. setTimeout(() => this.$scope.$digest(), 0)
  53. })
  54. }
  55. _bindToSocketEvents() {
  56. this.ide.socket.on('reciveNewDoc', (parent_folder_id, doc) => {
  57. const parent_folder =
  58. this.findEntityById(parent_folder_id) || this.$scope.rootFolder
  59. return this.$scope.$apply(() => {
  60. parent_folder.children.push({
  61. name: doc.name,
  62. id: doc._id,
  63. type: 'doc',
  64. })
  65. return this.recalculateDocList()
  66. })
  67. })
  68. this.ide.socket.on(
  69. 'reciveNewFile',
  70. (parent_folder_id, file, source, linkedFileData) => {
  71. const parent_folder =
  72. this.findEntityById(parent_folder_id) || this.$scope.rootFolder
  73. return this.$scope.$apply(() => {
  74. parent_folder.children.push({
  75. name: file.name,
  76. id: file._id,
  77. type: 'file',
  78. linkedFileData,
  79. created: file.created,
  80. })
  81. return this.recalculateDocList()
  82. })
  83. }
  84. )
  85. this.ide.socket.on('reciveNewFolder', (parent_folder_id, folder) => {
  86. const parent_folder =
  87. this.findEntityById(parent_folder_id) || this.$scope.rootFolder
  88. return this.$scope.$apply(() => {
  89. parent_folder.children.push({
  90. name: folder.name,
  91. id: folder._id,
  92. type: 'folder',
  93. children: [],
  94. })
  95. return this.recalculateDocList()
  96. })
  97. })
  98. this.ide.socket.on('reciveEntityRename', (entity_id, name) => {
  99. const entity = this.findEntityById(entity_id)
  100. if (entity == null) {
  101. return
  102. }
  103. return this.$scope.$apply(() => {
  104. entity.name = name
  105. return this.recalculateDocList()
  106. })
  107. })
  108. this.ide.socket.on('removeEntity', entity_id => {
  109. const entity = this.findEntityById(entity_id)
  110. if (entity == null) {
  111. return
  112. }
  113. this.$scope.$apply(() => {
  114. this._deleteEntityFromScope(entity)
  115. return this.recalculateDocList()
  116. })
  117. return this.$scope.$broadcast('entity:deleted', entity)
  118. })
  119. return this.ide.socket.on('reciveEntityMove', (entity_id, folder_id) => {
  120. const entity = this.findEntityById(entity_id)
  121. const folder = this.findEntityById(folder_id)
  122. return this.$scope.$apply(() => {
  123. this._moveEntityInScope(entity, folder)
  124. return this.recalculateDocList()
  125. })
  126. })
  127. }
  128. selectEntity(entity) {
  129. this.selected_entity_id = entity.id // For reselecting after a reconnect
  130. this.ide.fileTreeManager.forEachEntity(entity => (entity.selected = false))
  131. return (entity.selected = true)
  132. }
  133. toggleMultiSelectEntity(entity) {
  134. entity.multiSelected = !entity.multiSelected
  135. this.$scope.multiSelectedCount = this.multiSelectedCount()
  136. this.$scope.editor.multiSelectedCount = this.$scope.multiSelectedCount
  137. }
  138. multiSelectedCount() {
  139. let count = 0
  140. this.forEachEntity(function (entity) {
  141. if (entity.multiSelected) {
  142. return count++
  143. }
  144. })
  145. return count
  146. }
  147. getMultiSelectedEntities() {
  148. const entities = []
  149. this.forEachEntity(function (e) {
  150. if (e.multiSelected) {
  151. return entities.push(e)
  152. }
  153. })
  154. return entities
  155. }
  156. getFullCount() {
  157. const entities = []
  158. this.forEachEntity(function (e) {
  159. if (!e.deleted) entities.push(e)
  160. })
  161. return entities.length
  162. }
  163. getMultiSelectedEntityChildNodes() {
  164. // use pathnames with a leading slash to avoid
  165. // problems with reserved Object properties
  166. const entities = this.getMultiSelectedEntities()
  167. const paths = {}
  168. for (const entity of Array.from(entities)) {
  169. paths['/' + this.getEntityPath(entity)] = entity
  170. }
  171. const prefixes = {}
  172. for (const path in paths) {
  173. const entity = paths[path]
  174. const parts = path.split('/')
  175. if (parts.length <= 2) {
  176. continue
  177. } else {
  178. // Record prefixes a/b/c.tex -> 'a' and 'a/b'
  179. for (
  180. let i = 1, end = parts.length - 1, asc = end >= 1;
  181. asc ? i <= end : i >= end;
  182. asc ? i++ : i--
  183. ) {
  184. prefixes['/' + parts.slice(0, i).join('/')] = true
  185. }
  186. }
  187. }
  188. const child_entities = []
  189. for (const path in paths) {
  190. // If the path is in the prefixes, then it's a parent folder and
  191. // should be ignore
  192. const entity = paths[path]
  193. if (prefixes[path] == null) {
  194. child_entities.push(entity)
  195. }
  196. }
  197. return child_entities
  198. }
  199. clearMultiSelectedEntities() {
  200. if (this.$scope.multiSelectedCount === 0) {
  201. return
  202. } // Be efficient, this is called a lot on 'click'
  203. this.forEachEntity(entity => (entity.multiSelected = false))
  204. return (this.$scope.multiSelectedCount = 0)
  205. }
  206. multiSelectSelectedEntity() {
  207. const entity = this.findSelectedEntity()
  208. if (entity) {
  209. entity.multiSelected = true
  210. }
  211. this.$scope.multiSelectedCount = this.multiSelectedCount()
  212. }
  213. existsInFolder(folder_id, name) {
  214. const folder = this.findEntityById(folder_id)
  215. if (folder == null) {
  216. return false
  217. }
  218. const entity = this._findEntityByPathInFolder(folder, name)
  219. return entity != null
  220. }
  221. findSelectedEntity() {
  222. let selected = null
  223. this.forEachEntity(function (entity) {
  224. if (entity.selected) {
  225. return (selected = entity)
  226. }
  227. })
  228. return selected
  229. }
  230. findEntityById(id, options) {
  231. if (options == null) {
  232. options = {}
  233. }
  234. if (this.$scope.rootFolder.id === id) {
  235. return this.$scope.rootFolder
  236. }
  237. let entity = this._findEntityByIdInFolder(this.$scope.rootFolder, id)
  238. if (entity != null) {
  239. return entity
  240. }
  241. if (options.includeDeleted) {
  242. for (entity of Array.from(this.$scope.deletedDocs)) {
  243. if (entity.id === id) {
  244. return entity
  245. }
  246. }
  247. }
  248. return null
  249. }
  250. _findEntityByIdInFolder(folder, id) {
  251. for (const entity of Array.from(folder.children || [])) {
  252. if (entity.id === id) {
  253. return entity
  254. } else if (entity.children != null) {
  255. const result = this._findEntityByIdInFolder(entity, id)
  256. if (result != null) {
  257. return result
  258. }
  259. }
  260. }
  261. return null
  262. }
  263. findEntityByPath(path) {
  264. return this._findEntityByPathInFolder(this.$scope.rootFolder, path)
  265. }
  266. getPreviewByPath(path) {
  267. for (const suffix of [
  268. '',
  269. '.png',
  270. '.jpg',
  271. '.jpeg',
  272. '.pdf',
  273. '.PNG',
  274. '.JPG',
  275. '.JPEG',
  276. '.PDF',
  277. ]) {
  278. const entity = this.findEntityByPath(path + suffix)
  279. if (entity) {
  280. return {
  281. url: `/project/${this.$scope.project._id}/file/${entity.id}`,
  282. extension: entity.name.split('.').pop(),
  283. }
  284. }
  285. }
  286. return null
  287. }
  288. _findEntityByPathInFolder(folder, path) {
  289. if (path == null || folder == null) {
  290. return null
  291. }
  292. if (path === '') {
  293. return folder
  294. }
  295. const parts = path.split('/')
  296. const name = parts.shift()
  297. const rest = parts.join('/')
  298. if (name === '.') {
  299. return this._findEntityByPathInFolder(folder, rest)
  300. }
  301. for (const entity of Array.from(folder.children)) {
  302. if (entity.name === name) {
  303. if (rest === '') {
  304. return entity
  305. } else if (entity.type === 'folder') {
  306. return this._findEntityByPathInFolder(entity, rest)
  307. }
  308. }
  309. }
  310. return null
  311. }
  312. forEachEntity(callback) {
  313. if (callback == null) {
  314. callback = function () {}
  315. }
  316. this._forEachEntityInFolder(this.$scope.rootFolder, null, callback)
  317. return (() => {
  318. const result = []
  319. for (const entity of Array.from(this.$scope.deletedDocs || [])) {
  320. result.push(callback(entity))
  321. }
  322. return result
  323. })()
  324. }
  325. _forEachEntityInFolder(folder, path, callback) {
  326. return (() => {
  327. const result = []
  328. for (const entity of Array.from(folder.children || [])) {
  329. let childPath
  330. if (path != null) {
  331. childPath = path + '/' + entity.name
  332. } else {
  333. childPath = entity.name
  334. }
  335. callback(entity, folder, childPath)
  336. if (entity.children != null) {
  337. result.push(this._forEachEntityInFolder(entity, childPath, callback))
  338. } else {
  339. result.push(undefined)
  340. }
  341. }
  342. return result
  343. })()
  344. }
  345. getEntityPath(entity) {
  346. return this._getEntityPathInFolder(this.$scope.rootFolder, entity)
  347. }
  348. _getEntityPathInFolder(folder, entity) {
  349. for (const child of Array.from(folder.children || [])) {
  350. if (child === entity) {
  351. return entity.name
  352. } else if (child.type === 'folder') {
  353. const path = this._getEntityPathInFolder(child, entity)
  354. if (path != null) {
  355. return child.name + '/' + path
  356. }
  357. }
  358. }
  359. return null
  360. }
  361. getRootDocDirname() {
  362. const rootDoc = this.findEntityById(this.$scope.project.rootDoc_id)
  363. if (rootDoc == null) {
  364. return
  365. }
  366. return this._getEntityDirname(rootDoc)
  367. }
  368. _getEntityDirname(entity) {
  369. const path = this.getEntityPath(entity)
  370. if (path == null) {
  371. return
  372. }
  373. return path.split('/').slice(0, -1).join('/')
  374. }
  375. _findParentFolder(entity) {
  376. const dirname = this._getEntityDirname(entity)
  377. if (dirname == null) {
  378. return
  379. }
  380. return this.findEntityByPath(dirname)
  381. }
  382. loadRootFolder() {
  383. return (this.$scope.rootFolder = this._parseFolder(
  384. __guard__(
  385. this.$scope != null ? this.$scope.project : undefined,
  386. x => x.rootFolder[0]
  387. )
  388. ))
  389. }
  390. _parseFolder(rawFolder) {
  391. const folder = {
  392. name: rawFolder.name,
  393. id: rawFolder._id,
  394. type: 'folder',
  395. children: [],
  396. selected: rawFolder._id === this.selected_entity_id,
  397. }
  398. for (const doc of Array.from(rawFolder.docs || [])) {
  399. folder.children.push({
  400. name: doc.name,
  401. id: doc._id,
  402. type: 'doc',
  403. selected: doc._id === this.selected_entity_id,
  404. })
  405. }
  406. for (const file of Array.from(rawFolder.fileRefs || [])) {
  407. folder.children.push({
  408. name: file.name,
  409. id: file._id,
  410. type: 'file',
  411. selected: file._id === this.selected_entity_id,
  412. linkedFileData: file.linkedFileData,
  413. created: file.created,
  414. })
  415. }
  416. for (const childFolder of Array.from(rawFolder.folders || [])) {
  417. folder.children.push(this._parseFolder(childFolder))
  418. }
  419. return folder
  420. }
  421. loadDeletedDocs() {
  422. this.$scope.deletedDocs = []
  423. return Array.from(this.$scope.project.deletedDocs || []).map(doc =>
  424. this.$scope.deletedDocs.push({
  425. name: doc.name,
  426. id: doc._id,
  427. type: 'doc',
  428. deleted: true,
  429. })
  430. )
  431. }
  432. recalculateDocList() {
  433. this.$scope.docs = []
  434. this.forEachEntity((entity, parentFolder, path) => {
  435. if (entity.type === 'doc' && !entity.deleted) {
  436. return this.$scope.docs.push({
  437. doc: entity,
  438. path,
  439. })
  440. }
  441. })
  442. // Keep list ordered by folders, then name
  443. return this.$scope.docs.sort(function (a, b) {
  444. const aDepth = (a.path.match(/\//g) || []).length
  445. const bDepth = (b.path.match(/\//g) || []).length
  446. if (aDepth - bDepth !== 0) {
  447. return -(aDepth - bDepth) // Deeper path == folder first
  448. } else if (a.path < b.path) {
  449. return -1
  450. } else if (a.path > b.path) {
  451. return 1
  452. } else {
  453. return 0
  454. }
  455. })
  456. }
  457. getCurrentFolder() {
  458. // Return the root folder if nothing is selected
  459. return (
  460. this._getCurrentFolder(this.$scope.rootFolder) || this.$scope.rootFolder
  461. )
  462. }
  463. _getCurrentFolder(startFolder) {
  464. if (startFolder == null) {
  465. startFolder = this.$scope.rootFolder
  466. }
  467. for (const entity of Array.from(startFolder.children || [])) {
  468. // The 'current' folder is either the one selected, or
  469. // the one containing the selected doc/file
  470. if (entity.selected) {
  471. if (entity.type === 'folder') {
  472. return entity
  473. } else {
  474. return startFolder
  475. }
  476. }
  477. if (entity.type === 'folder') {
  478. const result = this._getCurrentFolder(entity)
  479. if (result != null) {
  480. return result
  481. }
  482. }
  483. }
  484. return null
  485. }
  486. projectContainsFolder() {
  487. for (const entity of Array.from(this.$scope.rootFolder.children)) {
  488. if (entity.type === 'folder') {
  489. return true
  490. }
  491. }
  492. return false
  493. }
  494. existsInThisFolder(folder, name) {
  495. for (const entity of Array.from(
  496. (folder != null ? folder.children : undefined) || []
  497. )) {
  498. if (entity.name === name) {
  499. return true
  500. }
  501. }
  502. return false
  503. }
  504. nameExistsError(message) {
  505. if (message == null) {
  506. message = 'already exists'
  507. }
  508. const nameExists = this.ide.$q.defer()
  509. nameExists.reject({ data: message })
  510. return nameExists.promise
  511. }
  512. createDoc(name, parent_folder) {
  513. // check if a doc/file/folder already exists with this name
  514. if (parent_folder == null) {
  515. parent_folder = this.getCurrentFolder()
  516. }
  517. if (this.existsInThisFolder(parent_folder, name)) {
  518. return this.nameExistsError()
  519. }
  520. // We'll wait for the socket.io notification to actually
  521. // add the doc for us.
  522. return this.ide.$http.post(`/project/${this.ide.project_id}/doc`, {
  523. name,
  524. parent_folder_id: parent_folder != null ? parent_folder.id : undefined,
  525. _csrf: window.csrfToken,
  526. })
  527. }
  528. createFolder(name, parent_folder) {
  529. // check if a doc/file/folder already exists with this name
  530. if (parent_folder == null) {
  531. parent_folder = this.getCurrentFolder()
  532. }
  533. if (this.existsInThisFolder(parent_folder, name)) {
  534. return this.nameExistsError()
  535. }
  536. // We'll wait for the socket.io notification to actually
  537. // add the folder for us.
  538. return this.ide.$http.post(`/project/${this.ide.project_id}/folder`, {
  539. name,
  540. parent_folder_id: parent_folder != null ? parent_folder.id : undefined,
  541. _csrf: window.csrfToken,
  542. })
  543. }
  544. createLinkedFile(name, parent_folder, provider, data) {
  545. // check if a doc/file/folder already exists with this name
  546. if (parent_folder == null) {
  547. parent_folder = this.getCurrentFolder()
  548. }
  549. if (this.existsInThisFolder(parent_folder, name)) {
  550. return this.nameExistsError()
  551. }
  552. // We'll wait for the socket.io notification to actually
  553. // add the file for us.
  554. return this.ide.$http.post(
  555. `/project/${this.ide.project_id}/linked_file`,
  556. {
  557. name,
  558. parent_folder_id: parent_folder != null ? parent_folder.id : undefined,
  559. provider,
  560. data,
  561. _csrf: window.csrfToken,
  562. },
  563. {
  564. disableAutoLoginRedirect: true,
  565. }
  566. )
  567. }
  568. refreshLinkedFile(file) {
  569. const parent_folder = this._findParentFolder(file)
  570. const provider =
  571. file.linkedFileData != null ? file.linkedFileData.provider : undefined
  572. if (provider == null) {
  573. debugConsole.warn(`>> no provider for ${file.name}`, file)
  574. return
  575. }
  576. return this.ide.$http.post(
  577. `/project/${this.ide.project_id}/linked_file/${file.id}/refresh`,
  578. {
  579. _csrf: window.csrfToken,
  580. },
  581. {
  582. disableAutoLoginRedirect: true,
  583. }
  584. )
  585. }
  586. renameEntity(entity, name, callback) {
  587. if (callback == null) {
  588. callback = function () {}
  589. }
  590. if (entity.name === name) {
  591. return
  592. }
  593. if (name.length >= 150) {
  594. return
  595. }
  596. // check if a doc/file/folder already exists with this name
  597. const parent_folder = this.getCurrentFolder()
  598. if (this.existsInThisFolder(parent_folder, name)) {
  599. return this.nameExistsError()
  600. }
  601. entity.renamingToName = name
  602. return this.ide.$http
  603. .post(
  604. `/project/${this.ide.project_id}/${entity.type}/${entity.id}/rename`,
  605. {
  606. name,
  607. _csrf: window.csrfToken,
  608. }
  609. )
  610. .then(() => (entity.name = name))
  611. .finally(() => (entity.renamingToName = null))
  612. }
  613. deleteEntity(entity, callback) {
  614. // We'll wait for the socket.io notification to
  615. // delete from scope.
  616. if (callback == null) {
  617. callback = function () {}
  618. }
  619. return this.ide.queuedHttp({
  620. method: 'DELETE',
  621. url: `/project/${this.ide.project_id}/${entity.type}/${entity.id}`,
  622. headers: {
  623. 'X-Csrf-Token': window.csrfToken,
  624. },
  625. })
  626. }
  627. moveEntity(entity, parent_folder) {
  628. // Abort move if the folder being moved (entity) has the parent_folder as child
  629. // since that would break the tree structure.
  630. if (this._isChildFolder(entity, parent_folder)) {
  631. return
  632. }
  633. // check if a doc/file/folder already exists with this name
  634. if (this.existsInThisFolder(parent_folder, entity.name)) {
  635. throw new Error('file exists in this location')
  636. }
  637. // Wait for the http response before doing the move
  638. this.ide.queuedHttp
  639. .post(
  640. `/project/${this.ide.project_id}/${entity.type}/${entity.id}/move`,
  641. {
  642. folder_id: parent_folder.id,
  643. _csrf: window.csrfToken,
  644. }
  645. )
  646. .then(() => {
  647. this._moveEntityInScope(entity, parent_folder)
  648. })
  649. }
  650. _isChildFolder(parent_folder, child_folder) {
  651. const parent_path = this.getEntityPath(parent_folder) || '' // null if root folder
  652. const child_path = this.getEntityPath(child_folder) || '' // null if root folder
  653. // is parent path the beginning of child path?
  654. return child_path.slice(0, parent_path.length) === parent_path
  655. }
  656. _deleteEntityFromScope(entity, options) {
  657. if (options == null) {
  658. options = { moveToDeleted: true }
  659. }
  660. if (entity == null) {
  661. return
  662. }
  663. let parent_folder = null
  664. this.forEachEntity(function (possible_entity, folder) {
  665. if (possible_entity === entity) {
  666. return (parent_folder = folder)
  667. }
  668. })
  669. if (parent_folder != null) {
  670. const index = parent_folder.children.indexOf(entity)
  671. if (index > -1) {
  672. parent_folder.children.splice(index, 1)
  673. }
  674. }
  675. if (entity.type !== 'folder' && entity.selected) {
  676. this.$scope.ui.view = null
  677. }
  678. if (entity.type === 'doc' && options.moveToDeleted) {
  679. entity.deleted = true
  680. return this.$scope.deletedDocs.push(entity)
  681. }
  682. }
  683. _moveEntityInScope(entity, parent_folder) {
  684. if (Array.from(parent_folder.children).includes(entity)) {
  685. return
  686. }
  687. this._deleteEntityFromScope(entity, { moveToDeleted: false })
  688. return parent_folder.children.push(entity)
  689. }
  690. }
  691. function __guard__(value, transform) {
  692. return typeof value !== 'undefined' && value !== null
  693. ? transform(value)
  694. : undefined
  695. }