FileTreeManager.js 20 KB

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