FileTreeManager.js 20 KB

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