FileTreeManager.js 21 KB

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