HistoryV2ToolbarController.js 3.7 KB

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