HistoryV2ToolbarController.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /* eslint-disable
  2. max-len,
  3. no-return-assign,
  4. camelcase,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. import App from '../../../base'
  14. import { debugConsole } from '@/utils/debugging'
  15. export default App.controller('HistoryV2ToolbarController', [
  16. '$scope',
  17. '$modal',
  18. 'ide',
  19. 'eventTracking',
  20. 'waitFor',
  21. function ($scope, $modal, ide, eventTracking, waitFor) {
  22. $scope.currentUpdate = null
  23. $scope.currentLabel = null
  24. $scope.restoreState = {
  25. inflight: false,
  26. error: false,
  27. }
  28. $scope.toolbarUIConfig = {
  29. showOnlyLabels: false,
  30. }
  31. const _deregistershowOnlyLabelsWatcher = $scope.$watch(
  32. 'history.showOnlyLabels',
  33. showOnlyLabels => {
  34. if (showOnlyLabels != null) {
  35. $scope.toolbarUIConfig.showOnlyLabels = showOnlyLabels
  36. _deregistershowOnlyLabelsWatcher()
  37. }
  38. }
  39. )
  40. $scope.$watch('toolbarUIConfig.showOnlyLabels', (newVal, oldVal) => {
  41. if (newVal != null && newVal !== oldVal) {
  42. if (newVal) {
  43. ide.historyManager.showOnlyLabels()
  44. } else {
  45. ide.historyManager.showAllUpdates()
  46. }
  47. }
  48. })
  49. $scope.$watch('history.viewMode', (newVal, oldVal) => {
  50. if (newVal != null && newVal !== oldVal) {
  51. $scope.currentUpdate = ide.historyManager.getUpdateForVersion(newVal)
  52. }
  53. })
  54. $scope.$watch('history.selection.range.toV', (newVal, oldVal) => {
  55. if (
  56. newVal != null &&
  57. newVal !== oldVal &&
  58. $scope.history.viewMode === $scope.HistoryViewModes.POINT_IN_TIME
  59. ) {
  60. $scope.currentUpdate = ide.historyManager.getUpdateForVersion(newVal)
  61. }
  62. })
  63. $scope.toggleHistoryViewMode = () => {
  64. ide.historyManager.toggleHistoryViewMode()
  65. }
  66. $scope.restoreDeletedFile = function () {
  67. const { pathname, deletedAtV } = $scope.history.selection.file
  68. if (pathname == null || deletedAtV == null) {
  69. return
  70. }
  71. eventTracking.sendMB('history-v2-restore-deleted')
  72. $scope.restoreState.inflight = true
  73. return ide.historyManager
  74. .restoreFile(deletedAtV, pathname)
  75. .then(function (response) {
  76. const { data } = response
  77. return openEntity(data)
  78. })
  79. .catch(() =>
  80. ide.showGenericMessageModal(
  81. 'Sorry, something went wrong with the restore'
  82. )
  83. )
  84. .finally(() => ($scope.restoreState.inflight = false))
  85. }
  86. $scope.showAddLabelDialog = () => {
  87. $modal.open({
  88. templateUrl: 'historyV2AddLabelModalTemplate',
  89. controller: 'HistoryV2AddLabelModalController',
  90. resolve: {
  91. update() {
  92. return $scope.history.selection.update
  93. },
  94. },
  95. })
  96. }
  97. function openEntity(data) {
  98. const { id, type } = data
  99. return waitFor(() => ide.fileTreeManager.findEntityById(id), 3000)
  100. .then(function (entity) {
  101. if (type === 'doc') {
  102. ide.editorManager.openDoc(entity)
  103. ide.$timeout(() => {
  104. $scope.$broadcast('history:toggle')
  105. }, 0)
  106. } else if (type === 'file') {
  107. ide.binaryFilesManager.openFile(entity)
  108. ide.$timeout(() => {
  109. $scope.$broadcast('history:toggle')
  110. }, 0)
  111. }
  112. })
  113. .catch(debugConsole.error)
  114. }
  115. },
  116. ])