FileTreeController.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. /* eslint-disable
  2. camelcase,
  3. handle-callback-err,
  4. max-len,
  5. no-return-assign,
  6. no-undef,
  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. * DS102: Remove unnecessary code created because of implicit returns
  14. * DS103: Rewrite code to no longer use __guard__
  15. * DS207: Consider shorter variations of null checks
  16. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  17. */
  18. define(['base'], function(App) {
  19. App.controller('FileTreeController', function(
  20. $scope,
  21. $modal,
  22. ide,
  23. $rootScope
  24. ) {
  25. $scope.openNewDocModal = () =>
  26. $modal.open({
  27. templateUrl: 'newFileModalTemplate',
  28. controller: 'NewFileModalController',
  29. size: 'lg',
  30. resolve: {
  31. parent_folder() {
  32. return ide.fileTreeManager.getCurrentFolder()
  33. },
  34. projectFeatures() {
  35. return ide.$scope.project.features
  36. },
  37. type() {
  38. return 'doc'
  39. },
  40. userFeatures() {
  41. return ide.$scope.user.features
  42. }
  43. }
  44. })
  45. $scope.openNewFolderModal = () =>
  46. $modal.open({
  47. templateUrl: 'newFolderModalTemplate',
  48. controller: 'NewFolderModalController',
  49. resolve: {
  50. parent_folder() {
  51. return ide.fileTreeManager.getCurrentFolder()
  52. }
  53. }
  54. })
  55. $scope.openUploadFileModal = () =>
  56. $modal.open({
  57. templateUrl: 'newFileModalTemplate',
  58. controller: 'NewFileModalController',
  59. size: 'lg',
  60. resolve: {
  61. projectFeatures() {
  62. return ide.$scope.project.features
  63. },
  64. parent_folder() {
  65. return ide.fileTreeManager.getCurrentFolder()
  66. },
  67. type() {
  68. return 'upload'
  69. },
  70. userFeatures() {
  71. return ide.$scope.user.features
  72. }
  73. }
  74. })
  75. $scope.orderByFoldersFirst = function(entity) {
  76. if ((entity != null ? entity.type : undefined) === 'folder') {
  77. return '0'
  78. }
  79. return '1'
  80. }
  81. $scope.startRenamingSelected = () => $scope.$broadcast('rename:selected')
  82. return ($scope.openDeleteModalForSelected = () =>
  83. $scope.$broadcast('delete:selected'))
  84. })
  85. App.controller('NewFolderModalController', function(
  86. $scope,
  87. ide,
  88. $modalInstance,
  89. $timeout,
  90. parent_folder
  91. ) {
  92. $scope.inputs = { name: 'name' }
  93. $scope.state = { inflight: false }
  94. $modalInstance.opened.then(() =>
  95. $timeout(() => $scope.$broadcast('open'), 200)
  96. )
  97. $scope.create = function() {
  98. const { name } = $scope.inputs
  99. if (name == null || name.length === 0) {
  100. return
  101. }
  102. $scope.state.inflight = true
  103. return ide.fileTreeManager
  104. .createFolder(name, $scope.parent_folder)
  105. .then(function() {
  106. $scope.state.inflight = false
  107. return $modalInstance.dismiss('done')
  108. })
  109. .catch(function(response) {
  110. const { data } = response
  111. $scope.error = data
  112. return ($scope.state.inflight = false)
  113. })
  114. }
  115. return ($scope.cancel = () => $modalInstance.dismiss('cancel'))
  116. })
  117. App.controller('DuplicateFileModalController', function(
  118. $scope,
  119. $modalInstance,
  120. fileName
  121. ) {
  122. $scope.fileName = fileName
  123. $scope.cancel = () => $modalInstance.dismiss('cancel')
  124. })
  125. App.controller('NewFileModalController', function(
  126. $scope,
  127. ide,
  128. type,
  129. parent_folder,
  130. $modalInstance,
  131. eventTracking,
  132. projectFeatures,
  133. userFeatures
  134. ) {
  135. $scope.file_count = ide.fileTreeManager.getFullCount()
  136. $scope.type = type
  137. $scope.parent_folder = parent_folder
  138. $scope.state = {
  139. inflight: false,
  140. valid: true
  141. }
  142. $scope.cancel = () => $modalInstance.dismiss('cancel')
  143. $scope.create = () => $scope.$broadcast('create')
  144. const hasMendeleyFeature =
  145. (projectFeatures && projectFeatures.references) ||
  146. (projectFeatures && projectFeatures.mendeley) ||
  147. (userFeatures && userFeatures.references) ||
  148. (userFeatures && userFeatures.mendeley)
  149. const hasZoteroFeature =
  150. (projectFeatures && projectFeatures.references) ||
  151. (projectFeatures && projectFeatures.zotero) ||
  152. (userFeatures && userFeatures.references) ||
  153. (userFeatures && userFeatures.zotero)
  154. $scope.$watch('type', function() {
  155. if ($scope.type === 'mendeley' && !hasMendeleyFeature) {
  156. eventTracking.send(
  157. 'subscription-funnel',
  158. 'editor-click-feature',
  159. $scope.type
  160. )
  161. }
  162. if ($scope.type === 'zotero' && !hasZoteroFeature) {
  163. eventTracking.send(
  164. 'subscription-funnel',
  165. 'editor-click-feature',
  166. $scope.type
  167. )
  168. }
  169. })
  170. return $scope.$on('done', (e, opts = {}) => {
  171. isBibFile = opts.name && /^.*\.bib$/.test(opts.name)
  172. if (opts.shouldReindexReferences || isBibFile) {
  173. ide.$scope.$emit('references:should-reindex', {})
  174. }
  175. $modalInstance.dismiss('done')
  176. })
  177. })
  178. App.controller('NewDocModalController', function($scope, ide, $timeout) {
  179. $scope.inputs = { name: 'name.tex' }
  180. $timeout(() => $scope.$broadcast('open'), 200)
  181. return $scope.$on('create', function() {
  182. const { name } = $scope.inputs
  183. if (name == null || name.length === 0) {
  184. return
  185. }
  186. $scope.state.inflight = true
  187. return ide.fileTreeManager
  188. .createDoc(name, $scope.parent_folder)
  189. .then(function() {
  190. $scope.state.inflight = false
  191. return $scope.$emit('done')
  192. })
  193. .catch(function(response) {
  194. const { data } = response
  195. $scope.error = data
  196. $scope.state.inflight = false
  197. })
  198. })
  199. })
  200. App.controller('UploadFileModalController', function(
  201. $scope,
  202. $rootScope,
  203. ide,
  204. $timeout,
  205. $window
  206. ) {
  207. $scope.parent_folder_id =
  208. $scope.parent_folder != null ? $scope.parent_folder.id : undefined
  209. $scope.project_id = ide.project_id
  210. $scope.tooManyFiles = false
  211. $scope.rateLimitHit = false
  212. $scope.secondsToRedirect = 10
  213. $scope.notLoggedIn = false
  214. $scope.conflicts = []
  215. $scope.control = {}
  216. const needToLogBackIn = function() {
  217. $scope.notLoggedIn = true
  218. var decreseTimeout = () =>
  219. $timeout(function() {
  220. if ($scope.secondsToRedirect === 0) {
  221. return ($window.location.href = `/login?redir=/project/${
  222. ide.project_id
  223. }`)
  224. } else {
  225. decreseTimeout()
  226. return ($scope.secondsToRedirect = $scope.secondsToRedirect - 1)
  227. }
  228. }, 1000)
  229. return decreseTimeout()
  230. }
  231. $scope.max_files = 40
  232. $scope.onComplete = (error, name, response) =>
  233. $timeout(function() {
  234. uploadCount--
  235. if (response.success) {
  236. $rootScope.$broadcast('file:upload:complete', response)
  237. }
  238. if (uploadCount === 0 && response != null && response.success) {
  239. return $scope.$emit('done', { name: name })
  240. }
  241. }, 250)
  242. $scope.onValidateBatch = function(files) {
  243. if (files.length > $scope.max_files) {
  244. $timeout(() => ($scope.tooManyFiles = true), 1)
  245. return false
  246. } else {
  247. return true
  248. }
  249. }
  250. $scope.onError = function(id, name, reason) {
  251. console.log(id, name, reason)
  252. if (reason.indexOf('429') !== -1) {
  253. return ($scope.rateLimitHit = true)
  254. } else if (reason.indexOf('403') !== -1) {
  255. return needToLogBackIn()
  256. }
  257. }
  258. let _uploadTimer = null
  259. const uploadIfNoConflicts = function() {
  260. if ($scope.conflicts.length === 0) {
  261. return $scope.doUpload()
  262. }
  263. }
  264. var uploadCount = 0
  265. $scope.onSubmit = function(id, name) {
  266. uploadCount++
  267. if (ide.fileTreeManager.existsInFolder($scope.parent_folder_id, name)) {
  268. $scope.conflicts.push(name)
  269. $scope.$apply()
  270. }
  271. if (_uploadTimer == null) {
  272. _uploadTimer = setTimeout(function() {
  273. _uploadTimer = null
  274. return uploadIfNoConflicts()
  275. }, 0)
  276. }
  277. return true
  278. }
  279. $scope.onCancel = function(id, name) {
  280. uploadCount--
  281. const index = $scope.conflicts.indexOf(name)
  282. if (index > -1) {
  283. $scope.conflicts.splice(index, 1)
  284. }
  285. $scope.$apply()
  286. return uploadIfNoConflicts()
  287. }
  288. return ($scope.doUpload = () =>
  289. __guard__($scope.control != null ? $scope.control.q : undefined, x =>
  290. x.uploadStoredFiles()
  291. ))
  292. })
  293. App.controller('ProjectLinkedFileModalController', function(
  294. $scope,
  295. ide,
  296. $timeout
  297. ) {
  298. $scope.data = {
  299. projects: null, // or []
  300. selectedProjectId: null,
  301. projectEntities: null, // or []
  302. projectOutputFiles: null, // or []
  303. selectedProjectEntity: null,
  304. selectedProjectOutputFile: null,
  305. buildId: null,
  306. name: null
  307. }
  308. $scope.state.inFlight = {
  309. projects: false,
  310. entities: false,
  311. compile: false
  312. }
  313. $scope.state.isOutputFilesMode = false
  314. $scope.state.error = false
  315. $scope.$watch('data.selectedProjectId', function(newVal, oldVal) {
  316. if (!newVal) {
  317. return
  318. }
  319. $scope.data.selectedProjectEntity = null
  320. $scope.data.selectedProjectOutputFile = null
  321. if ($scope.state.isOutputFilesMode) {
  322. return $scope.compileProjectAndGetOutputFiles(
  323. $scope.data.selectedProjectId
  324. )
  325. } else {
  326. return $scope.getProjectEntities($scope.data.selectedProjectId)
  327. }
  328. })
  329. $scope.$watch('state.isOutputFilesMode', function(newVal, oldVal) {
  330. if (!newVal && !oldVal) {
  331. return
  332. }
  333. $scope.data.selectedProjectOutputFile = null
  334. if (newVal === true) {
  335. return $scope.compileProjectAndGetOutputFiles(
  336. $scope.data.selectedProjectId
  337. )
  338. } else {
  339. return $scope.getProjectEntities($scope.data.selectedProjectId)
  340. }
  341. })
  342. // auto-set filename based on selected file
  343. $scope.$watch('data.selectedProjectEntity', function(newVal, oldVal) {
  344. if (!newVal) {
  345. return
  346. }
  347. const fileName = newVal.split('/').reverse()[0]
  348. if (fileName) {
  349. $scope.data.name = fileName
  350. }
  351. })
  352. // auto-set filename based on selected file
  353. $scope.$watch('data.selectedProjectOutputFile', function(newVal, oldVal) {
  354. if (!newVal) {
  355. return
  356. }
  357. if (newVal === 'output.pdf') {
  358. const project = _.find(
  359. $scope.data.projects,
  360. p => p._id === $scope.data.selectedProjectId
  361. )
  362. $scope.data.name =
  363. (project != null ? project.name : undefined) != null
  364. ? `${project.name}.pdf`
  365. : 'output.pdf'
  366. } else {
  367. const fileName = newVal.split('/').reverse()[0]
  368. if (fileName) {
  369. $scope.data.name = fileName
  370. }
  371. }
  372. })
  373. const _setInFlight = type => ($scope.state.inFlight[type] = true)
  374. const _reset = function(opts) {
  375. const isError = opts.err === true
  376. const { inFlight } = $scope.state
  377. inFlight.projects = inFlight.entities = inFlight.compile = false
  378. $scope.state.inflight = false
  379. return ($scope.state.error = isError)
  380. }
  381. $scope.toggleOutputFilesMode = function() {
  382. if (!$scope.data.selectedProjectId) {
  383. return
  384. }
  385. return ($scope.state.isOutputFilesMode = !$scope.state.isOutputFilesMode)
  386. }
  387. $scope.shouldEnableProjectSelect = function() {
  388. const { state, data } = $scope
  389. return !state.inFlight.projects && data.projects
  390. }
  391. $scope.hasNoProjects = function() {
  392. const { state, data } = $scope
  393. return (
  394. !state.inFlight.projects &&
  395. (data.projects == null || data.projects.length === 0)
  396. )
  397. }
  398. $scope.shouldEnableProjectEntitySelect = function() {
  399. const { state, data } = $scope
  400. return (
  401. !state.inFlight.projects &&
  402. !state.inFlight.entities &&
  403. data.projects &&
  404. data.selectedProjectId
  405. )
  406. }
  407. $scope.shouldEnableProjectOutputFileSelect = function() {
  408. const { state, data } = $scope
  409. return (
  410. !state.inFlight.projects &&
  411. !state.inFlight.compile &&
  412. data.projects &&
  413. data.selectedProjectId
  414. )
  415. }
  416. const validate = function() {
  417. const { state } = $scope
  418. const { data } = $scope
  419. $scope.state.valid =
  420. !state.inFlight.projects &&
  421. !state.inFlight.entities &&
  422. data.projects &&
  423. data.selectedProjectId &&
  424. ((!$scope.state.isOutputFilesMode &&
  425. data.projectEntities &&
  426. data.selectedProjectEntity) ||
  427. ($scope.state.isOutputFilesMode &&
  428. data.projectOutputFiles &&
  429. data.selectedProjectOutputFile)) &&
  430. data.name
  431. }
  432. $scope.$watch('state', validate, true)
  433. $scope.$watch('data', validate, true)
  434. $scope.getUserProjects = function() {
  435. _setInFlight('projects')
  436. return ide.$http
  437. .get('/user/projects', {
  438. _csrf: window.csrfToken
  439. })
  440. .then(function(resp) {
  441. $scope.data.projectEntities = null
  442. $scope.data.projects = resp.data.projects.filter(
  443. p => p._id !== ide.project_id
  444. )
  445. return _reset({ err: false })
  446. })
  447. .catch(err => _reset({ err: true }))
  448. }
  449. $scope.getProjectEntities = project_id => {
  450. _setInFlight('entities')
  451. return ide.$http
  452. .get(`/project/${project_id}/entities`, {
  453. _csrf: window.csrfToken
  454. })
  455. .then(function(resp) {
  456. if ($scope.data.selectedProjectId === resp.data.project_id) {
  457. $scope.data.projectEntities = resp.data.entities
  458. return _reset({ err: false })
  459. }
  460. })
  461. .catch(err => _reset({ err: true }))
  462. }
  463. $scope.compileProjectAndGetOutputFiles = project_id => {
  464. _setInFlight('compile')
  465. return ide.$http
  466. .post(`/project/${project_id}/compile`, {
  467. check: 'silent',
  468. draft: false,
  469. incrementalCompilesEnabled: false,
  470. _csrf: window.csrfToken
  471. })
  472. .then(function(resp) {
  473. if (resp.data.status === 'success') {
  474. const filteredFiles = resp.data.outputFiles.filter(f =>
  475. f.path.match(/.*\.(pdf|png|jpeg|jpg|gif)/)
  476. )
  477. $scope.data.projectOutputFiles = filteredFiles
  478. $scope.data.buildId = __guard__(
  479. filteredFiles != null ? filteredFiles[0] : undefined,
  480. x => x.build
  481. )
  482. console.log('>> build_id', $scope.data.buildId)
  483. return _reset({ err: false })
  484. } else {
  485. $scope.data.projectOutputFiles = null
  486. return _reset({ err: true })
  487. }
  488. })
  489. .catch(function(err) {
  490. console.error(err)
  491. return _reset({ err: true })
  492. })
  493. }
  494. $scope.init = () => $scope.getUserProjects()
  495. $timeout($scope.init, 0)
  496. return $scope.$on('create', function() {
  497. let payload, provider
  498. const projectId = $scope.data.selectedProjectId
  499. const { name } = $scope.data
  500. if ($scope.state.isOutputFilesMode) {
  501. provider = 'project_output_file'
  502. payload = {
  503. source_project_id: projectId,
  504. source_output_file_path: $scope.data.selectedProjectOutputFile,
  505. build_id: $scope.data.buildId
  506. }
  507. } else {
  508. provider = 'project_file'
  509. payload = {
  510. source_project_id: projectId,
  511. source_entity_path: $scope.data.selectedProjectEntity
  512. }
  513. }
  514. _setInFlight('create')
  515. ide.fileTreeManager
  516. .createLinkedFile(name, $scope.parent_folder, provider, payload)
  517. .then(function() {
  518. _reset({ err: false })
  519. return $scope.$emit('done', { name: name })
  520. })
  521. .catch(function(response) {
  522. const { data } = response
  523. $scope.error = data
  524. })
  525. })
  526. })
  527. return App.controller('UrlLinkedFileModalController', function(
  528. $scope,
  529. ide,
  530. $timeout
  531. ) {
  532. $scope.inputs = {
  533. name: '',
  534. url: ''
  535. }
  536. $scope.nameChangedByUser = false
  537. $timeout(() => $scope.$broadcast('open'), 200)
  538. const validate = function() {
  539. const { name, url } = $scope.inputs
  540. if (name == null || name.length === 0) {
  541. return ($scope.state.valid = false)
  542. } else if (url == null || url.length === 0) {
  543. return ($scope.state.valid = false)
  544. } else {
  545. return ($scope.state.valid = true)
  546. }
  547. }
  548. $scope.$watch('inputs.name', validate)
  549. $scope.$watch('inputs.url', validate)
  550. $scope.$watch('inputs.url', function(url) {
  551. if (url != null && url !== '' && !$scope.nameChangedByUser) {
  552. url = url.replace('://', '') // Ignore http:// etc
  553. const parts = url.split('/').reverse()
  554. if (parts.length > 1) {
  555. // Wait for at one /
  556. return ($scope.inputs.name = parts[0])
  557. }
  558. }
  559. })
  560. return $scope.$on('create', function() {
  561. const { name, url } = $scope.inputs
  562. if (name == null || name.length === 0) {
  563. return
  564. }
  565. if (url == null || url.length === 0) {
  566. return
  567. }
  568. $scope.state.inflight = true
  569. return ide.fileTreeManager
  570. .createLinkedFile(name, $scope.parent_folder, 'url', { url })
  571. .then(function() {
  572. $scope.state.inflight = false
  573. return $scope.$emit('done', { name: name })
  574. })
  575. .catch(function(response) {
  576. const { data } = response
  577. $scope.error = data
  578. return ($scope.state.inflight = false)
  579. })
  580. })
  581. })
  582. })
  583. function __guard__(value, transform) {
  584. return typeof value !== 'undefined' && value !== null
  585. ? transform(value)
  586. : undefined
  587. }