HistoryV2ToolbarController.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. export default App.controller(
  15. 'HistoryV2ToolbarController',
  16. ($scope, $modal, ide, eventTracking, waitFor) => {
  17. let openEntity
  18. $scope.currentUpdate = null
  19. $scope.currentLabel = null
  20. $scope.restoreState = {
  21. inflight: false,
  22. error: false
  23. }
  24. $scope.toolbarUIConfig = {
  25. showOnlyLabels: false
  26. }
  27. let _deregistershowOnlyLabelsWatcher = $scope.$watch(
  28. 'history.showOnlyLabels',
  29. showOnlyLabels => {
  30. if (showOnlyLabels != null) {
  31. $scope.toolbarUIConfig.showOnlyLabels = showOnlyLabels
  32. _deregistershowOnlyLabelsWatcher()
  33. }
  34. }
  35. )
  36. $scope.$watch('toolbarUIConfig.showOnlyLabels', (newVal, oldVal) => {
  37. if (newVal != null && newVal !== oldVal) {
  38. if (newVal) {
  39. ide.historyManager.showOnlyLabels()
  40. } else {
  41. ide.historyManager.showAllUpdates()
  42. }
  43. }
  44. })
  45. $scope.$watch('history.viewMode', (newVal, oldVal) => {
  46. if (newVal != null && newVal !== oldVal) {
  47. $scope.currentUpdate = ide.historyManager.getUpdateForVersion(newVal)
  48. }
  49. })
  50. $scope.$watch('history.selection.range.toV', (newVal, oldVal) => {
  51. if (
  52. newVal != null &&
  53. newVal !== oldVal &&
  54. $scope.history.viewMode === $scope.HistoryViewModes.POINT_IN_TIME
  55. ) {
  56. $scope.currentUpdate = ide.historyManager.getUpdateForVersion(newVal)
  57. }
  58. })
  59. $scope.toggleHistoryViewMode = () => {
  60. ide.historyManager.toggleHistoryViewMode()
  61. }
  62. $scope.restoreDeletedFile = function() {
  63. const { pathname, deletedAtV } = $scope.history.selection.file
  64. if (pathname == null || deletedAtV == null) {
  65. return
  66. }
  67. eventTracking.sendMB('history-v2-restore-deleted')
  68. $scope.restoreState.inflight = true
  69. return ide.historyManager
  70. .restoreFile(deletedAtV, pathname)
  71. .then(function(response) {
  72. const { data } = response
  73. return openEntity(data)
  74. })
  75. .catch(() =>
  76. ide.showGenericMessageModal(
  77. 'Sorry, something went wrong with the restore'
  78. )
  79. )
  80. .finally(() => ($scope.restoreState.inflight = false))
  81. }
  82. $scope.showAddLabelDialog = () => {
  83. $modal.open({
  84. templateUrl: 'historyV2AddLabelModalTemplate',
  85. controller: 'HistoryV2AddLabelModalController',
  86. resolve: {
  87. update() {
  88. return $scope.history.selection.update
  89. }
  90. }
  91. })
  92. }
  93. openEntity = function(data) {
  94. const { id, type } = data
  95. return waitFor(() => ide.fileTreeManager.findEntityById(id), 3000)
  96. .then(function(entity) {
  97. if (type === 'doc') {
  98. ide.editorManager.openDoc(entity)
  99. this.ide.$timeout(() => {
  100. this.$scope.$broadcast('history:toggle')
  101. }, 0)
  102. } else if (type === 'file') {
  103. ide.binaryFilesManager.openFile(entity)
  104. this.ide.$timeout(() => {
  105. this.$scope.$broadcast('history:toggle')
  106. }, 0)
  107. }
  108. })
  109. .catch(err => console.warn(err))
  110. }
  111. }
  112. )