FileTreeController.js 18 KB

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