FileTreeManager.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  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. return this.$scope.$digest()
  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. _findEntityByPathInFolder(folder, path) {
  266. if (path == null || folder == null) {
  267. return null
  268. }
  269. if (path === '') {
  270. return folder
  271. }
  272. const parts = path.split('/')
  273. const name = parts.shift()
  274. const rest = parts.join('/')
  275. if (name === '.') {
  276. return this._findEntityByPathInFolder(folder, rest)
  277. }
  278. for (const entity of Array.from(folder.children)) {
  279. if (entity.name === name) {
  280. if (rest === '') {
  281. return entity
  282. } else if (entity.type === 'folder') {
  283. return this._findEntityByPathInFolder(entity, rest)
  284. }
  285. }
  286. }
  287. return null
  288. }
  289. forEachEntity(callback) {
  290. if (callback == null) {
  291. callback = function () {}
  292. }
  293. this._forEachEntityInFolder(this.$scope.rootFolder, null, callback)
  294. return (() => {
  295. const result = []
  296. for (const entity of Array.from(this.$scope.deletedDocs || [])) {
  297. result.push(callback(entity))
  298. }
  299. return result
  300. })()
  301. }
  302. _forEachEntityInFolder(folder, path, callback) {
  303. return (() => {
  304. const result = []
  305. for (const entity of Array.from(folder.children || [])) {
  306. let childPath
  307. if (path != null) {
  308. childPath = path + '/' + entity.name
  309. } else {
  310. childPath = entity.name
  311. }
  312. callback(entity, folder, childPath)
  313. if (entity.children != null) {
  314. result.push(this._forEachEntityInFolder(entity, childPath, callback))
  315. } else {
  316. result.push(undefined)
  317. }
  318. }
  319. return result
  320. })()
  321. }
  322. getEntityPath(entity) {
  323. return this._getEntityPathInFolder(this.$scope.rootFolder, entity)
  324. }
  325. _getEntityPathInFolder(folder, entity) {
  326. for (const child of Array.from(folder.children || [])) {
  327. if (child === entity) {
  328. return entity.name
  329. } else if (child.type === 'folder') {
  330. const path = this._getEntityPathInFolder(child, entity)
  331. if (path != null) {
  332. return child.name + '/' + path
  333. }
  334. }
  335. }
  336. return null
  337. }
  338. getRootDocDirname() {
  339. const rootDoc = this.findEntityById(this.$scope.project.rootDoc_id)
  340. if (rootDoc == null) {
  341. return
  342. }
  343. return this._getEntityDirname(rootDoc)
  344. }
  345. _getEntityDirname(entity) {
  346. const path = this.getEntityPath(entity)
  347. if (path == null) {
  348. return
  349. }
  350. return path.split('/').slice(0, -1).join('/')
  351. }
  352. _findParentFolder(entity) {
  353. const dirname = this._getEntityDirname(entity)
  354. if (dirname == null) {
  355. return
  356. }
  357. return this.findEntityByPath(dirname)
  358. }
  359. loadRootFolder() {
  360. return (this.$scope.rootFolder = this._parseFolder(
  361. __guard__(
  362. this.$scope != null ? this.$scope.project : undefined,
  363. x => x.rootFolder[0]
  364. )
  365. ))
  366. }
  367. _parseFolder(rawFolder) {
  368. const folder = {
  369. name: rawFolder.name,
  370. id: rawFolder._id,
  371. type: 'folder',
  372. children: [],
  373. selected: rawFolder._id === this.selected_entity_id,
  374. }
  375. for (const doc of Array.from(rawFolder.docs || [])) {
  376. folder.children.push({
  377. name: doc.name,
  378. id: doc._id,
  379. type: 'doc',
  380. selected: doc._id === this.selected_entity_id,
  381. })
  382. }
  383. for (const file of Array.from(rawFolder.fileRefs || [])) {
  384. folder.children.push({
  385. name: file.name,
  386. id: file._id,
  387. type: 'file',
  388. selected: file._id === this.selected_entity_id,
  389. linkedFileData: file.linkedFileData,
  390. created: file.created,
  391. })
  392. }
  393. for (const childFolder of Array.from(rawFolder.folders || [])) {
  394. folder.children.push(this._parseFolder(childFolder))
  395. }
  396. return folder
  397. }
  398. loadDeletedDocs() {
  399. this.$scope.deletedDocs = []
  400. return Array.from(this.$scope.project.deletedDocs || []).map(doc =>
  401. this.$scope.deletedDocs.push({
  402. name: doc.name,
  403. id: doc._id,
  404. type: 'doc',
  405. deleted: true,
  406. })
  407. )
  408. }
  409. recalculateDocList() {
  410. this.$scope.docs = []
  411. this.forEachEntity((entity, parentFolder, path) => {
  412. if (entity.type === 'doc' && !entity.deleted) {
  413. return this.$scope.docs.push({
  414. doc: entity,
  415. path,
  416. })
  417. }
  418. })
  419. // Keep list ordered by folders, then name
  420. return this.$scope.docs.sort(function (a, b) {
  421. const aDepth = (a.path.match(/\//g) || []).length
  422. const bDepth = (b.path.match(/\//g) || []).length
  423. if (aDepth - bDepth !== 0) {
  424. return -(aDepth - bDepth) // Deeper path == folder first
  425. } else if (a.path < b.path) {
  426. return -1
  427. } else if (a.path > b.path) {
  428. return 1
  429. } else {
  430. return 0
  431. }
  432. })
  433. }
  434. getCurrentFolder() {
  435. // Return the root folder if nothing is selected
  436. return (
  437. this._getCurrentFolder(this.$scope.rootFolder) || this.$scope.rootFolder
  438. )
  439. }
  440. _getCurrentFolder(startFolder) {
  441. if (startFolder == null) {
  442. startFolder = this.$scope.rootFolder
  443. }
  444. for (const entity of Array.from(startFolder.children || [])) {
  445. // The 'current' folder is either the one selected, or
  446. // the one containing the selected doc/file
  447. if (entity.selected) {
  448. if (entity.type === 'folder') {
  449. return entity
  450. } else {
  451. return startFolder
  452. }
  453. }
  454. if (entity.type === 'folder') {
  455. const result = this._getCurrentFolder(entity)
  456. if (result != null) {
  457. return result
  458. }
  459. }
  460. }
  461. return null
  462. }
  463. projectContainsFolder() {
  464. for (const entity of Array.from(this.$scope.rootFolder.children)) {
  465. if (entity.type === 'folder') {
  466. return true
  467. }
  468. }
  469. return false
  470. }
  471. existsInThisFolder(folder, name) {
  472. for (const entity of Array.from(
  473. (folder != null ? folder.children : undefined) || []
  474. )) {
  475. if (entity.name === name) {
  476. return true
  477. }
  478. }
  479. return false
  480. }
  481. nameExistsError(message) {
  482. if (message == null) {
  483. message = 'already exists'
  484. }
  485. const nameExists = this.ide.$q.defer()
  486. nameExists.reject({ data: message })
  487. return nameExists.promise
  488. }
  489. createDoc(name, parent_folder) {
  490. // check if a doc/file/folder already exists with this name
  491. if (parent_folder == null) {
  492. parent_folder = this.getCurrentFolder()
  493. }
  494. if (this.existsInThisFolder(parent_folder, name)) {
  495. return this.nameExistsError()
  496. }
  497. // We'll wait for the socket.io notification to actually
  498. // add the doc for us.
  499. return this.ide.$http.post(`/project/${this.ide.project_id}/doc`, {
  500. name,
  501. parent_folder_id: parent_folder != null ? parent_folder.id : undefined,
  502. _csrf: window.csrfToken,
  503. })
  504. }
  505. createFolder(name, parent_folder) {
  506. // check if a doc/file/folder already exists with this name
  507. if (parent_folder == null) {
  508. parent_folder = this.getCurrentFolder()
  509. }
  510. if (this.existsInThisFolder(parent_folder, name)) {
  511. return this.nameExistsError()
  512. }
  513. // We'll wait for the socket.io notification to actually
  514. // add the folder for us.
  515. return this.ide.$http.post(`/project/${this.ide.project_id}/folder`, {
  516. name,
  517. parent_folder_id: parent_folder != null ? parent_folder.id : undefined,
  518. _csrf: window.csrfToken,
  519. })
  520. }
  521. createLinkedFile(name, parent_folder, provider, data) {
  522. // check if a doc/file/folder already exists with this name
  523. if (parent_folder == null) {
  524. parent_folder = this.getCurrentFolder()
  525. }
  526. if (this.existsInThisFolder(parent_folder, name)) {
  527. return this.nameExistsError()
  528. }
  529. // We'll wait for the socket.io notification to actually
  530. // add the file for us.
  531. return this.ide.$http.post(
  532. `/project/${this.ide.project_id}/linked_file`,
  533. {
  534. name,
  535. parent_folder_id: parent_folder != null ? parent_folder.id : undefined,
  536. provider,
  537. data,
  538. _csrf: window.csrfToken,
  539. },
  540. {
  541. disableAutoLoginRedirect: true,
  542. }
  543. )
  544. }
  545. refreshLinkedFile(file) {
  546. const parent_folder = this._findParentFolder(file)
  547. const provider =
  548. file.linkedFileData != null ? file.linkedFileData.provider : undefined
  549. if (provider == null) {
  550. console.warn(`>> no provider for ${file.name}`, file)
  551. return
  552. }
  553. return this.ide.$http.post(
  554. `/project/${this.ide.project_id}/linked_file/${file.id}/refresh`,
  555. {
  556. _csrf: window.csrfToken,
  557. },
  558. {
  559. disableAutoLoginRedirect: true,
  560. }
  561. )
  562. }
  563. renameEntity(entity, name, callback) {
  564. if (callback == null) {
  565. callback = function () {}
  566. }
  567. if (entity.name === name) {
  568. return
  569. }
  570. if (name.length >= 150) {
  571. return
  572. }
  573. // check if a doc/file/folder already exists with this name
  574. const parent_folder = this.getCurrentFolder()
  575. if (this.existsInThisFolder(parent_folder, name)) {
  576. return this.nameExistsError()
  577. }
  578. entity.renamingToName = name
  579. return this.ide.$http
  580. .post(
  581. `/project/${this.ide.project_id}/${entity.type}/${entity.id}/rename`,
  582. {
  583. name,
  584. _csrf: window.csrfToken,
  585. }
  586. )
  587. .then(() => (entity.name = name))
  588. .finally(() => (entity.renamingToName = null))
  589. }
  590. deleteEntity(entity, callback) {
  591. // We'll wait for the socket.io notification to
  592. // delete from scope.
  593. if (callback == null) {
  594. callback = function () {}
  595. }
  596. return this.ide.queuedHttp({
  597. method: 'DELETE',
  598. url: `/project/${this.ide.project_id}/${entity.type}/${entity.id}`,
  599. headers: {
  600. 'X-Csrf-Token': window.csrfToken,
  601. },
  602. })
  603. }
  604. moveEntity(entity, parent_folder) {
  605. // Abort move if the folder being moved (entity) has the parent_folder as child
  606. // since that would break the tree structure.
  607. if (this._isChildFolder(entity, parent_folder)) {
  608. return
  609. }
  610. // check if a doc/file/folder already exists with this name
  611. if (this.existsInThisFolder(parent_folder, entity.name)) {
  612. throw new Error('file exists in this location')
  613. }
  614. // Wait for the http response before doing the move
  615. this.ide.queuedHttp
  616. .post(
  617. `/project/${this.ide.project_id}/${entity.type}/${entity.id}/move`,
  618. {
  619. folder_id: parent_folder.id,
  620. _csrf: window.csrfToken,
  621. }
  622. )
  623. .then(() => {
  624. this._moveEntityInScope(entity, parent_folder)
  625. })
  626. }
  627. _isChildFolder(parent_folder, child_folder) {
  628. const parent_path = this.getEntityPath(parent_folder) || '' // null if root folder
  629. const child_path = this.getEntityPath(child_folder) || '' // null if root folder
  630. // is parent path the beginning of child path?
  631. return child_path.slice(0, parent_path.length) === parent_path
  632. }
  633. _deleteEntityFromScope(entity, options) {
  634. if (options == null) {
  635. options = { moveToDeleted: true }
  636. }
  637. if (entity == null) {
  638. return
  639. }
  640. let parent_folder = null
  641. this.forEachEntity(function (possible_entity, folder) {
  642. if (possible_entity === entity) {
  643. return (parent_folder = folder)
  644. }
  645. })
  646. if (parent_folder != null) {
  647. const index = parent_folder.children.indexOf(entity)
  648. if (index > -1) {
  649. parent_folder.children.splice(index, 1)
  650. }
  651. }
  652. if (entity.type !== 'folder' && entity.selected) {
  653. this.$scope.ui.view = null
  654. }
  655. if (entity.type === 'doc' && options.moveToDeleted) {
  656. entity.deleted = true
  657. return this.$scope.deletedDocs.push(entity)
  658. }
  659. }
  660. _moveEntityInScope(entity, parent_folder) {
  661. if (Array.from(parent_folder.children).includes(entity)) {
  662. return
  663. }
  664. this._deleteEntityFromScope(entity, { moveToDeleted: false })
  665. return parent_folder.children.push(entity)
  666. }
  667. }
  668. function __guard__(value, transform) {
  669. return typeof value !== 'undefined' && value !== null
  670. ? transform(value)
  671. : undefined
  672. }