HistoryV2ToolbarController.js 3.4 KB

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