FileTreeManager.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  1. /* eslint-disable
  2. camelcase,
  3. handle-callback-err,
  4. max-len,
  5. no-dupe-class-members,
  6. no-return-assign,
  7. no-undef,
  8. no-unused-vars,
  9. */
  10. // TODO: This file was created by bulk-decaffeinate.
  11. // Fix any style issues and re-enable lint.
  12. /*
  13. * decaffeinate suggestions:
  14. * DS101: Remove unnecessary use of Array.from
  15. * DS102: Remove unnecessary code created because of implicit returns
  16. * DS103: Rewrite code to no longer use __guard__
  17. * DS202: Simplify dynamic range loops
  18. * DS205: Consider reworking code to avoid use of IIFEs
  19. * DS207: Consider shorter variations of null checks
  20. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  21. */
  22. define([
  23. 'ide/file-tree/directives/fileEntity',
  24. 'ide/file-tree/directives/draggable',
  25. 'ide/file-tree/directives/droppable',
  26. 'ide/file-tree/controllers/FileTreeController',
  27. 'ide/file-tree/controllers/FileTreeEntityController',
  28. 'ide/file-tree/controllers/FileTreeFolderController',
  29. 'ide/file-tree/controllers/FileTreeRootFolderController'
  30. ], function() {
  31. let FileTreeManager
  32. return (FileTreeManager = class FileTreeManager {
  33. constructor(ide, $scope) {
  34. this.ide = ide
  35. this.$scope = $scope
  36. this.$scope.$on('project:joined', () => {
  37. this.loadRootFolder()
  38. this.loadDeletedDocs()
  39. return this.$scope.$emit('file-tree:initialized')
  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(
  129. entity => (entity.selected = false)
  130. )
  131. return (entity.selected = true)
  132. }
  133. toggleMultiSelectEntity(entity) {
  134. entity.multiSelected = !entity.multiSelected
  135. return (this.$scope.multiSelectedCount = this.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. getMultiSelectedEntityChildNodes() {
  156. // use pathnames with a leading slash to avoid
  157. // problems with reserved Object properties
  158. const entities = this.getMultiSelectedEntities()
  159. const paths = {}
  160. for (var entity of Array.from(entities)) {
  161. paths['/' + this.getEntityPath(entity)] = entity
  162. }
  163. const prefixes = {}
  164. for (var path in paths) {
  165. entity = paths[path]
  166. const parts = path.split('/')
  167. if (parts.length <= 2) {
  168. continue
  169. } else {
  170. // Record prefixes a/b/c.tex -> 'a' and 'a/b'
  171. for (
  172. let i = 1, end = parts.length - 1, asc = end >= 1;
  173. asc ? i <= end : i >= end;
  174. asc ? i++ : i--
  175. ) {
  176. prefixes['/' + parts.slice(0, i).join('/')] = true
  177. }
  178. }
  179. }
  180. const child_entities = []
  181. for (path in paths) {
  182. // If the path is in the prefixes, then it's a parent folder and
  183. // should be ignore
  184. entity = paths[path]
  185. if (prefixes[path] == null) {
  186. child_entities.push(entity)
  187. }
  188. }
  189. return child_entities
  190. }
  191. clearMultiSelectedEntities() {
  192. if (this.$scope.multiSelectedCount === 0) {
  193. return
  194. } // Be efficient, this is called a lot on 'click'
  195. this.forEachEntity(entity => (entity.multiSelected = false))
  196. return (this.$scope.multiSelectedCount = 0)
  197. }
  198. multiSelectSelectedEntity() {
  199. const entity = this.findSelectedEntity()
  200. if (entity) {
  201. entity.multiSelected = true
  202. }
  203. this.$scope.multiSelectedCount = this.multiSelectedCount()
  204. }
  205. existsInFolder(folder_id, name) {
  206. const folder = this.findEntityById(folder_id)
  207. if (folder == null) {
  208. return false
  209. }
  210. const entity = this._findEntityByPathInFolder(folder, name)
  211. return entity != null
  212. }
  213. findSelectedEntity() {
  214. let selected = null
  215. this.forEachEntity(function(entity) {
  216. if (entity.selected) {
  217. return (selected = entity)
  218. }
  219. })
  220. return selected
  221. }
  222. findEntityById(id, options) {
  223. if (options == null) {
  224. options = {}
  225. }
  226. if (this.$scope.rootFolder.id === id) {
  227. return this.$scope.rootFolder
  228. }
  229. let entity = this._findEntityByIdInFolder(this.$scope.rootFolder, id)
  230. if (entity != null) {
  231. return entity
  232. }
  233. if (options.includeDeleted) {
  234. for (entity of Array.from(this.$scope.deletedDocs)) {
  235. if (entity.id === id) {
  236. return entity
  237. }
  238. }
  239. }
  240. return null
  241. }
  242. _findEntityByIdInFolder(folder, id) {
  243. for (let entity of Array.from(folder.children || [])) {
  244. if (entity.id === id) {
  245. return entity
  246. } else if (entity.children != null) {
  247. const result = this._findEntityByIdInFolder(entity, id)
  248. if (result != null) {
  249. return result
  250. }
  251. }
  252. }
  253. return null
  254. }
  255. findEntityByPath(path) {
  256. return this._findEntityByPathInFolder(this.$scope.rootFolder, path)
  257. }
  258. _findEntityByPathInFolder(folder, path) {
  259. if (path == null || folder == null) {
  260. return null
  261. }
  262. if (path === '') {
  263. return folder
  264. }
  265. const parts = path.split('/')
  266. const name = parts.shift()
  267. const rest = parts.join('/')
  268. if (name === '.') {
  269. return this._findEntityByPathInFolder(folder, rest)
  270. }
  271. for (let entity of Array.from(folder.children)) {
  272. if (entity.name === name) {
  273. if (rest === '') {
  274. return entity
  275. } else if (entity.type === 'folder') {
  276. return this._findEntityByPathInFolder(entity, rest)
  277. }
  278. }
  279. }
  280. return null
  281. }
  282. forEachEntity(callback) {
  283. if (callback == null) {
  284. callback = function(entity, parent_folder, path) {}
  285. }
  286. this._forEachEntityInFolder(this.$scope.rootFolder, null, callback)
  287. return (() => {
  288. const result = []
  289. for (let entity of Array.from(this.$scope.deletedDocs || [])) {
  290. result.push(callback(entity))
  291. }
  292. return result
  293. })()
  294. }
  295. _forEachEntityInFolder(folder, path, callback) {
  296. return (() => {
  297. const result = []
  298. for (let entity of Array.from(folder.children || [])) {
  299. var childPath
  300. if (path != null) {
  301. childPath = path + '/' + entity.name
  302. } else {
  303. childPath = entity.name
  304. }
  305. callback(entity, folder, childPath)
  306. if (entity.children != null) {
  307. result.push(
  308. this._forEachEntityInFolder(entity, childPath, callback)
  309. )
  310. } else {
  311. result.push(undefined)
  312. }
  313. }
  314. return result
  315. })()
  316. }
  317. getEntityPath(entity) {
  318. return this._getEntityPathInFolder(this.$scope.rootFolder, entity)
  319. }
  320. _getEntityPathInFolder(folder, entity) {
  321. for (let child of Array.from(folder.children || [])) {
  322. if (child === entity) {
  323. return entity.name
  324. } else if (child.type === 'folder') {
  325. const path = this._getEntityPathInFolder(child, entity)
  326. if (path != null) {
  327. return child.name + '/' + path
  328. }
  329. }
  330. }
  331. return null
  332. }
  333. getRootDocDirname() {
  334. const rootDoc = this.findEntityById(this.$scope.project.rootDoc_id)
  335. if (rootDoc == null) {
  336. return
  337. }
  338. return this._getEntityDirname(rootDoc)
  339. }
  340. _getEntityDirname(entity) {
  341. const path = this.getEntityPath(entity)
  342. if (path == null) {
  343. return
  344. }
  345. return path
  346. .split('/')
  347. .slice(0, -1)
  348. .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 (let 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 (let 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 (let 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 {
  426. return 1
  427. }
  428. })
  429. }
  430. getEntityPath(entity) {
  431. return this._getEntityPathInFolder(this.$scope.rootFolder, entity)
  432. }
  433. _getEntityPathInFolder(folder, entity) {
  434. for (let child of Array.from(folder.children || [])) {
  435. if (child === entity) {
  436. return entity.name
  437. } else if (child.type === 'folder') {
  438. const path = this._getEntityPathInFolder(child, entity)
  439. if (path != null) {
  440. return child.name + '/' + path
  441. }
  442. }
  443. }
  444. return null
  445. }
  446. getCurrentFolder() {
  447. // Return the root folder if nothing is selected
  448. return (
  449. this._getCurrentFolder(this.$scope.rootFolder) || this.$scope.rootFolder
  450. )
  451. }
  452. _getCurrentFolder(startFolder) {
  453. if (startFolder == null) {
  454. startFolder = this.$scope.rootFolder
  455. }
  456. for (let entity of Array.from(startFolder.children || [])) {
  457. // The 'current' folder is either the one selected, or
  458. // the one containing the selected doc/file
  459. if (entity.selected) {
  460. if (entity.type === 'folder') {
  461. return entity
  462. } else {
  463. return startFolder
  464. }
  465. }
  466. if (entity.type === 'folder') {
  467. const result = this._getCurrentFolder(entity)
  468. if (result != null) {
  469. return result
  470. }
  471. }
  472. }
  473. return null
  474. }
  475. projectContainsFolder() {
  476. for (let entity of Array.from(this.$scope.rootFolder.children)) {
  477. if (entity.type === 'folder') {
  478. return true
  479. }
  480. }
  481. return false
  482. }
  483. existsInThisFolder(folder, name) {
  484. for (let entity of Array.from(
  485. (folder != null ? folder.children : undefined) || []
  486. )) {
  487. if (entity.name === name) {
  488. return true
  489. }
  490. }
  491. return false
  492. }
  493. nameExistsError(message) {
  494. if (message == null) {
  495. message = 'already exists'
  496. }
  497. const nameExists = this.ide.$q.defer()
  498. nameExists.reject({ data: message })
  499. return nameExists.promise
  500. }
  501. createDoc(name, parent_folder) {
  502. // check if a doc/file/folder already exists with this name
  503. if (parent_folder == null) {
  504. parent_folder = this.getCurrentFolder()
  505. }
  506. if (this.existsInThisFolder(parent_folder, name)) {
  507. return this.nameExistsError()
  508. }
  509. // We'll wait for the socket.io notification to actually
  510. // add the doc for us.
  511. return this.ide.$http.post(`/project/${this.ide.project_id}/doc`, {
  512. name,
  513. parent_folder_id: parent_folder != null ? parent_folder.id : undefined,
  514. _csrf: window.csrfToken
  515. })
  516. }
  517. createFolder(name, parent_folder) {
  518. // check if a doc/file/folder already exists with this name
  519. if (parent_folder == null) {
  520. parent_folder = this.getCurrentFolder()
  521. }
  522. if (this.existsInThisFolder(parent_folder, name)) {
  523. return this.nameExistsError()
  524. }
  525. // We'll wait for the socket.io notification to actually
  526. // add the folder for us.
  527. return this.ide.$http.post(`/project/${this.ide.project_id}/folder`, {
  528. name,
  529. parent_folder_id: parent_folder != null ? parent_folder.id : undefined,
  530. _csrf: window.csrfToken
  531. })
  532. }
  533. createLinkedFile(name, parent_folder, provider, data) {
  534. // check if a doc/file/folder already exists with this name
  535. if (parent_folder == null) {
  536. parent_folder = this.getCurrentFolder()
  537. }
  538. if (this.existsInThisFolder(parent_folder, name)) {
  539. return this.nameExistsError()
  540. }
  541. // We'll wait for the socket.io notification to actually
  542. // add the file for us.
  543. return this.ide.$http.post(
  544. `/project/${this.ide.project_id}/linked_file`,
  545. {
  546. name,
  547. parent_folder_id:
  548. parent_folder != null ? parent_folder.id : undefined,
  549. provider,
  550. data,
  551. _csrf: window.csrfToken
  552. },
  553. {
  554. disableAutoLoginRedirect: true
  555. }
  556. )
  557. }
  558. refreshLinkedFile(file) {
  559. const parent_folder = this._findParentFolder(file)
  560. const provider =
  561. file.linkedFileData != null ? file.linkedFileData.provider : undefined
  562. if (provider == null) {
  563. console.warn(`>> no provider for ${file.name}`, file)
  564. return
  565. }
  566. return this.ide.$http.post(
  567. `/project/${this.ide.project_id}/linked_file/${file.id}/refresh`,
  568. {
  569. _csrf: window.csrfToken
  570. },
  571. {
  572. disableAutoLoginRedirect: true
  573. }
  574. )
  575. }
  576. renameEntity(entity, name, callback) {
  577. if (callback == null) {
  578. callback = function(error) {}
  579. }
  580. if (entity.name === name) {
  581. return
  582. }
  583. if (name.length >= 150) {
  584. return
  585. }
  586. // check if a doc/file/folder already exists with this name
  587. const parent_folder = this.getCurrentFolder()
  588. if (this.existsInThisFolder(parent_folder, name)) {
  589. return this.nameExistsError()
  590. }
  591. entity.renamingToName = name
  592. return this.ide.$http
  593. .post(
  594. `/project/${this.ide.project_id}/${entity.type}/${entity.id}/rename`,
  595. {
  596. name,
  597. _csrf: window.csrfToken
  598. }
  599. )
  600. .then(() => (entity.name = name))
  601. .finally(() => (entity.renamingToName = null))
  602. }
  603. deleteEntity(entity, callback) {
  604. // We'll wait for the socket.io notification to
  605. // delete from scope.
  606. if (callback == null) {
  607. callback = function(error) {}
  608. }
  609. return this.ide.queuedHttp({
  610. method: 'DELETE',
  611. url: `/project/${this.ide.project_id}/${entity.type}/${entity.id}`,
  612. headers: {
  613. 'X-Csrf-Token': window.csrfToken
  614. }
  615. })
  616. }
  617. moveEntity(entity, parent_folder) {
  618. // Abort move if the folder being moved (entity) has the parent_folder as child
  619. // since that would break the tree structure.
  620. if (this._isChildFolder(entity, parent_folder)) {
  621. return
  622. }
  623. // check if a doc/file/folder already exists with this name
  624. if (this.existsInThisFolder(parent_folder, entity.name)) {
  625. throw new Error('file exists in this location')
  626. }
  627. // Wait for the http response before doing the move
  628. this.ide.queuedHttp
  629. .post(
  630. `/project/${this.ide.project_id}/${entity.type}/${entity.id}/move`,
  631. {
  632. folder_id: parent_folder.id,
  633. _csrf: window.csrfToken
  634. }
  635. )
  636. .then(() => {
  637. this._moveEntityInScope(entity, parent_folder)
  638. })
  639. }
  640. _isChildFolder(parent_folder, child_folder) {
  641. const parent_path = this.getEntityPath(parent_folder) || '' // null if root folder
  642. const child_path = this.getEntityPath(child_folder) || '' // null if root folder
  643. // is parent path the beginning of child path?
  644. return child_path.slice(0, parent_path.length) === parent_path
  645. }
  646. _deleteEntityFromScope(entity, options) {
  647. if (options == null) {
  648. options = { moveToDeleted: true }
  649. }
  650. if (entity == null) {
  651. return
  652. }
  653. let parent_folder = null
  654. this.forEachEntity(function(possible_entity, folder) {
  655. if (possible_entity === entity) {
  656. return (parent_folder = folder)
  657. }
  658. })
  659. if (parent_folder != null) {
  660. const index = parent_folder.children.indexOf(entity)
  661. if (index > -1) {
  662. parent_folder.children.splice(index, 1)
  663. }
  664. }
  665. if (entity.type !== 'folder' && entity.selected) {
  666. this.$scope.ui.view = null
  667. }
  668. if (entity.type === 'doc' && options.moveToDeleted) {
  669. entity.deleted = true
  670. return this.$scope.deletedDocs.push(entity)
  671. }
  672. }
  673. _moveEntityInScope(entity, parent_folder) {
  674. if (Array.from(parent_folder.children).includes(entity)) {
  675. return
  676. }
  677. this._deleteEntityFromScope(entity, { moveToDeleted: false })
  678. return parent_folder.children.push(entity)
  679. }
  680. })
  681. })
  682. function __guard__(value, transform) {
  683. return typeof value !== 'undefined' && value !== null
  684. ? transform(value)
  685. : undefined
  686. }