FileTreeManager.js 21 KB

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