FileTreeController.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  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.type = type
  136. $scope.parent_folder = parent_folder
  137. $scope.state = {
  138. inflight: false,
  139. valid: true
  140. }
  141. $scope.cancel = () => $modalInstance.dismiss('cancel')
  142. $scope.create = () => $scope.$broadcast('create')
  143. const hasMendeleyFeature =
  144. (projectFeatures && projectFeatures.references) ||
  145. (projectFeatures && projectFeatures.mendeley) ||
  146. (userFeatures && userFeatures.references) ||
  147. (userFeatures && userFeatures.mendeley)
  148. const hasZoteroFeature =
  149. (projectFeatures && projectFeatures.references) ||
  150. (projectFeatures && projectFeatures.zotero) ||
  151. (userFeatures && userFeatures.references) ||
  152. (userFeatures && userFeatures.zotero)
  153. $scope.$watch('type', function() {
  154. if ($scope.type === 'mendeley' && !hasMendeleyFeature) {
  155. eventTracking.send(
  156. 'subscription-funnel',
  157. 'editor-click-feature',
  158. $scope.type
  159. )
  160. }
  161. if ($scope.type === 'zotero' && !hasZoteroFeature) {
  162. eventTracking.send(
  163. 'subscription-funnel',
  164. 'editor-click-feature',
  165. $scope.type
  166. )
  167. }
  168. })
  169. return $scope.$on('done', (e, opts = {}) => {
  170. isBibFile = opts.name && /^.*\.bib$/.test(opts.name)
  171. if (opts.shouldReindexReferences || isBibFile) {
  172. ide.$scope.$emit('references:should-reindex', {})
  173. }
  174. $modalInstance.dismiss('done')
  175. })
  176. })
  177. App.controller('NewDocModalController', function($scope, ide, $timeout) {
  178. $scope.inputs = { name: 'name.tex' }
  179. $timeout(() => $scope.$broadcast('open'), 200)
  180. return $scope.$on('create', function() {
  181. const { name } = $scope.inputs
  182. if (name == null || name.length === 0) {
  183. return
  184. }
  185. $scope.state.inflight = true
  186. return ide.fileTreeManager
  187. .createDoc(name, $scope.parent_folder)
  188. .then(function() {
  189. $scope.state.inflight = false
  190. return $scope.$emit('done')
  191. })
  192. .catch(function(response) {
  193. const { data } = response
  194. $scope.error = data
  195. $scope.state.inflight = false
  196. })
  197. })
  198. })
  199. App.controller('UploadFileModalController', function(
  200. $scope,
  201. $rootScope,
  202. ide,
  203. $timeout,
  204. $window
  205. ) {
  206. $scope.parent_folder_id =
  207. $scope.parent_folder != null ? $scope.parent_folder.id : undefined
  208. $scope.project_id = ide.project_id
  209. $scope.tooManyFiles = false
  210. $scope.rateLimitHit = false
  211. $scope.secondsToRedirect = 10
  212. $scope.notLoggedIn = false
  213. $scope.conflicts = []
  214. $scope.control = {}
  215. const needToLogBackIn = function() {
  216. $scope.notLoggedIn = true
  217. var decreseTimeout = () =>
  218. $timeout(function() {
  219. if ($scope.secondsToRedirect === 0) {
  220. return ($window.location.href = `/login?redir=/project/${
  221. ide.project_id
  222. }`)
  223. } else {
  224. decreseTimeout()
  225. return ($scope.secondsToRedirect = $scope.secondsToRedirect - 1)
  226. }
  227. }, 1000)
  228. return decreseTimeout()
  229. }
  230. $scope.max_files = 40
  231. $scope.onComplete = (error, name, response) =>
  232. $timeout(function() {
  233. uploadCount--
  234. if (response.success) {
  235. $rootScope.$broadcast('file:upload:complete', response)
  236. }
  237. if (uploadCount === 0 && response != null && response.success) {
  238. return $scope.$emit('done', { name: name })
  239. }
  240. }, 250)
  241. $scope.onValidateBatch = function(files) {
  242. if (files.length > $scope.max_files) {
  243. $timeout(() => ($scope.tooManyFiles = true), 1)
  244. return false
  245. } else {
  246. return true
  247. }
  248. }
  249. $scope.onError = function(id, name, reason) {
  250. console.log(id, name, reason)
  251. if (reason.indexOf('429') !== -1) {
  252. return ($scope.rateLimitHit = true)
  253. } else if (reason.indexOf('403') !== -1) {
  254. return needToLogBackIn()
  255. }
  256. }
  257. let _uploadTimer = null
  258. const uploadIfNoConflicts = function() {
  259. if ($scope.conflicts.length === 0) {
  260. return $scope.doUpload()
  261. }
  262. }
  263. var uploadCount = 0
  264. $scope.onSubmit = function(id, name) {
  265. uploadCount++
  266. if (ide.fileTreeManager.existsInFolder($scope.parent_folder_id, name)) {
  267. $scope.conflicts.push(name)
  268. $scope.$apply()
  269. }
  270. if (_uploadTimer == null) {
  271. _uploadTimer = setTimeout(function() {
  272. _uploadTimer = null
  273. return uploadIfNoConflicts()
  274. }, 0)
  275. }
  276. return true
  277. }
  278. $scope.onCancel = function(id, name) {
  279. uploadCount--
  280. const index = $scope.conflicts.indexOf(name)
  281. if (index > -1) {
  282. $scope.conflicts.splice(index, 1)
  283. }
  284. $scope.$apply()
  285. return uploadIfNoConflicts()
  286. }
  287. return ($scope.doUpload = () =>
  288. __guard__($scope.control != null ? $scope.control.q : undefined, x =>
  289. x.uploadStoredFiles()
  290. ))
  291. })
  292. App.controller('ProjectLinkedFileModalController', function(
  293. $scope,
  294. ide,
  295. $timeout
  296. ) {
  297. $scope.data = {
  298. projects: null, // or []
  299. selectedProjectId: null,
  300. projectEntities: null, // or []
  301. projectOutputFiles: null, // or []
  302. selectedProjectEntity: null,
  303. selectedProjectOutputFile: null,
  304. buildId: null,
  305. name: null
  306. }
  307. $scope.state.inFlight = {
  308. projects: false,
  309. entities: false,
  310. compile: false
  311. }
  312. $scope.state.isOutputFilesMode = false
  313. $scope.state.error = false
  314. $scope.$watch('data.selectedProjectId', function(newVal, oldVal) {
  315. if (!newVal) {
  316. return
  317. }
  318. $scope.data.selectedProjectEntity = null
  319. $scope.data.selectedProjectOutputFile = null
  320. if ($scope.state.isOutputFilesMode) {
  321. return $scope.compileProjectAndGetOutputFiles(
  322. $scope.data.selectedProjectId
  323. )
  324. } else {
  325. return $scope.getProjectEntities($scope.data.selectedProjectId)
  326. }
  327. })
  328. $scope.$watch('state.isOutputFilesMode', function(newVal, oldVal) {
  329. if (!newVal && !oldVal) {
  330. return
  331. }
  332. $scope.data.selectedProjectOutputFile = null
  333. if (newVal === true) {
  334. return $scope.compileProjectAndGetOutputFiles(
  335. $scope.data.selectedProjectId
  336. )
  337. } else {
  338. return $scope.getProjectEntities($scope.data.selectedProjectId)
  339. }
  340. })
  341. // auto-set filename based on selected file
  342. $scope.$watch('data.selectedProjectEntity', function(newVal, oldVal) {
  343. if (!newVal) {
  344. return
  345. }
  346. const fileName = newVal.split('/').reverse()[0]
  347. if (fileName) {
  348. $scope.data.name = fileName
  349. }
  350. })
  351. // auto-set filename based on selected file
  352. $scope.$watch('data.selectedProjectOutputFile', function(newVal, oldVal) {
  353. if (!newVal) {
  354. return
  355. }
  356. if (newVal === 'output.pdf') {
  357. const project = _.find(
  358. $scope.data.projects,
  359. p => p._id === $scope.data.selectedProjectId
  360. )
  361. $scope.data.name =
  362. (project != null ? project.name : undefined) != null
  363. ? `${project.name}.pdf`
  364. : 'output.pdf'
  365. } else {
  366. const fileName = newVal.split('/').reverse()[0]
  367. if (fileName) {
  368. $scope.data.name = fileName
  369. }
  370. }
  371. })
  372. const _setInFlight = type => ($scope.state.inFlight[type] = true)
  373. const _reset = function(opts) {
  374. const isError = opts.err === true
  375. const { inFlight } = $scope.state
  376. inFlight.projects = inFlight.entities = inFlight.compile = false
  377. $scope.state.inflight = false
  378. return ($scope.state.error = isError)
  379. }
  380. $scope.toggleOutputFilesMode = function() {
  381. if (!$scope.data.selectedProjectId) {
  382. return
  383. }
  384. return ($scope.state.isOutputFilesMode = !$scope.state.isOutputFilesMode)
  385. }
  386. $scope.shouldEnableProjectSelect = function() {
  387. const { state, data } = $scope
  388. return !state.inFlight.projects && data.projects
  389. }
  390. $scope.hasNoProjects = function() {
  391. const { state, data } = $scope
  392. return (
  393. !state.inFlight.projects &&
  394. (data.projects == null || data.projects.length === 0)
  395. )
  396. }
  397. $scope.shouldEnableProjectEntitySelect = function() {
  398. const { state, data } = $scope
  399. return (
  400. !state.inFlight.projects &&
  401. !state.inFlight.entities &&
  402. data.projects &&
  403. data.selectedProjectId
  404. )
  405. }
  406. $scope.shouldEnableProjectOutputFileSelect = function() {
  407. const { state, data } = $scope
  408. return (
  409. !state.inFlight.projects &&
  410. !state.inFlight.compile &&
  411. data.projects &&
  412. data.selectedProjectId
  413. )
  414. }
  415. const validate = function() {
  416. const { state } = $scope
  417. const { data } = $scope
  418. $scope.state.valid =
  419. !state.inFlight.projects &&
  420. !state.inFlight.entities &&
  421. data.projects &&
  422. data.selectedProjectId &&
  423. ((!$scope.state.isOutputFilesMode &&
  424. data.projectEntities &&
  425. data.selectedProjectEntity) ||
  426. ($scope.state.isOutputFilesMode &&
  427. data.projectOutputFiles &&
  428. data.selectedProjectOutputFile)) &&
  429. data.name
  430. }
  431. $scope.$watch('state', validate, true)
  432. $scope.$watch('data', validate, true)
  433. $scope.getUserProjects = function() {
  434. _setInFlight('projects')
  435. return ide.$http
  436. .get('/user/projects', {
  437. _csrf: window.csrfToken
  438. })
  439. .then(function(resp) {
  440. $scope.data.projectEntities = null
  441. $scope.data.projects = resp.data.projects.filter(
  442. p => p._id !== ide.project_id
  443. )
  444. return _reset({ err: false })
  445. })
  446. .catch(err => _reset({ err: true }))
  447. }
  448. $scope.getProjectEntities = project_id => {
  449. _setInFlight('entities')
  450. return ide.$http
  451. .get(`/project/${project_id}/entities`, {
  452. _csrf: window.csrfToken
  453. })
  454. .then(function(resp) {
  455. if ($scope.data.selectedProjectId === resp.data.project_id) {
  456. $scope.data.projectEntities = resp.data.entities
  457. return _reset({ err: false })
  458. }
  459. })
  460. .catch(err => _reset({ err: true }))
  461. }
  462. $scope.compileProjectAndGetOutputFiles = project_id => {
  463. _setInFlight('compile')
  464. return ide.$http
  465. .post(`/project/${project_id}/compile`, {
  466. check: 'silent',
  467. draft: false,
  468. incrementalCompilesEnabled: false,
  469. _csrf: window.csrfToken
  470. })
  471. .then(function(resp) {
  472. if (resp.data.status === 'success') {
  473. const filteredFiles = resp.data.outputFiles.filter(f =>
  474. f.path.match(/.*\.(pdf|png|jpeg|jpg|gif)/)
  475. )
  476. $scope.data.projectOutputFiles = filteredFiles
  477. $scope.data.buildId = __guard__(
  478. filteredFiles != null ? filteredFiles[0] : undefined,
  479. x => x.build
  480. )
  481. console.log('>> build_id', $scope.data.buildId)
  482. return _reset({ err: false })
  483. } else {
  484. $scope.data.projectOutputFiles = null
  485. return _reset({ err: true })
  486. }
  487. })
  488. .catch(function(err) {
  489. console.error(err)
  490. return _reset({ err: true })
  491. })
  492. }
  493. $scope.init = () => $scope.getUserProjects()
  494. $timeout($scope.init, 0)
  495. return $scope.$on('create', function() {
  496. let payload, provider
  497. const projectId = $scope.data.selectedProjectId
  498. const { name } = $scope.data
  499. if ($scope.state.isOutputFilesMode) {
  500. provider = 'project_output_file'
  501. payload = {
  502. source_project_id: projectId,
  503. source_output_file_path: $scope.data.selectedProjectOutputFile,
  504. build_id: $scope.data.buildId
  505. }
  506. } else {
  507. provider = 'project_file'
  508. payload = {
  509. source_project_id: projectId,
  510. source_entity_path: $scope.data.selectedProjectEntity
  511. }
  512. }
  513. _setInFlight('create')
  514. ide.fileTreeManager
  515. .createLinkedFile(name, $scope.parent_folder, provider, payload)
  516. .then(function() {
  517. _reset({ err: false })
  518. return $scope.$emit('done', { name: name })
  519. })
  520. .catch(function(response) {
  521. const { data } = response
  522. $scope.error = data
  523. })
  524. })
  525. })
  526. return App.controller('UrlLinkedFileModalController', function(
  527. $scope,
  528. ide,
  529. $timeout
  530. ) {
  531. $scope.inputs = {
  532. name: '',
  533. url: ''
  534. }
  535. $scope.nameChangedByUser = false
  536. $timeout(() => $scope.$broadcast('open'), 200)
  537. const validate = function() {
  538. const { name, url } = $scope.inputs
  539. if (name == null || name.length === 0) {
  540. return ($scope.state.valid = false)
  541. } else if (url == null || url.length === 0) {
  542. return ($scope.state.valid = false)
  543. } else {
  544. return ($scope.state.valid = true)
  545. }
  546. }
  547. $scope.$watch('inputs.name', validate)
  548. $scope.$watch('inputs.url', validate)
  549. $scope.$watch('inputs.url', function(url) {
  550. if (url != null && url !== '' && !$scope.nameChangedByUser) {
  551. url = url.replace('://', '') // Ignore http:// etc
  552. const parts = url.split('/').reverse()
  553. if (parts.length > 1) {
  554. // Wait for at one /
  555. return ($scope.inputs.name = parts[0])
  556. }
  557. }
  558. })
  559. return $scope.$on('create', function() {
  560. const { name, url } = $scope.inputs
  561. if (name == null || name.length === 0) {
  562. return
  563. }
  564. if (url == null || url.length === 0) {
  565. return
  566. }
  567. $scope.state.inflight = true
  568. return ide.fileTreeManager
  569. .createLinkedFile(name, $scope.parent_folder, 'url', { url })
  570. .then(function() {
  571. $scope.state.inflight = false
  572. return $scope.$emit('done', { name: name })
  573. })
  574. .catch(function(response) {
  575. const { data } = response
  576. $scope.error = data
  577. return ($scope.state.inflight = false)
  578. })
  579. })
  580. })
  581. })
  582. function __guard__(value, transform) {
  583. return typeof value !== 'undefined' && value !== null
  584. ? transform(value)
  585. : undefined
  586. }