historyEntry.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import _ from 'lodash'
  2. /* eslint-disable
  3. max-len,
  4. */
  5. // TODO: This file was created by bulk-decaffeinate.
  6. // Fix any style issues and re-enable lint.
  7. /*
  8. * decaffeinate suggestions:
  9. * DS102: Remove unnecessary code created because of implicit returns
  10. * DS207: Consider shorter variations of null checks
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. import App from '../../../base'
  14. import ColorManager from '../../colors/ColorManager'
  15. import displayNameForUser from '../util/displayNameForUser'
  16. const historyEntryController = function ($scope, $element, $attrs) {
  17. const ctrl = this
  18. // This method (and maybe the one below) will be removed soon. User details data will be
  19. // injected into the history API responses, so we won't need to fetch user data from other
  20. // local data structures.
  21. const _getUserById = id =>
  22. _.find(ctrl.users, function (user) {
  23. const curUserId =
  24. (user != null ? user._id : undefined) ||
  25. (user != null ? user.id : undefined)
  26. return curUserId === id
  27. })
  28. ctrl.displayName = displayNameForUser
  29. ctrl.displayNameById = id => displayNameForUser(_getUserById(id))
  30. ctrl.getProjectOpDoc = function (projectOp) {
  31. if (projectOp.rename != null) {
  32. return `${projectOp.rename.pathname} → ${projectOp.rename.newPathname}`
  33. } else if (projectOp.add != null) {
  34. return `${projectOp.add.pathname}`
  35. } else if (projectOp.remove != null) {
  36. return `${projectOp.remove.pathname}`
  37. }
  38. }
  39. ctrl.getUserCSSStyle = function (user) {
  40. const curUserId =
  41. (user != null ? user._id : undefined) ||
  42. (user != null ? user.id : undefined)
  43. const hue = ColorManager.getHueForUserId(curUserId) || 100
  44. if (ctrl.isEntrySelected() || ctrl.isEntryHoverSelected()) {
  45. return { color: '#FFF' }
  46. } else {
  47. return { color: `hsl(${hue}, 70%, 50%)` }
  48. }
  49. }
  50. ctrl.isEntrySelected = function () {
  51. if (ctrl.rangeSelectionEnabled) {
  52. return (
  53. ctrl.entry.toV <= ctrl.selectedHistoryRange.toV &&
  54. ctrl.entry.fromV >= ctrl.selectedHistoryRange.fromV
  55. )
  56. } else {
  57. return ctrl.entry.toV === ctrl.selectedHistoryVersion
  58. }
  59. }
  60. ctrl.isEntryHoverSelected = function () {
  61. return (
  62. ctrl.rangeSelectionEnabled &&
  63. ctrl.entry.toV <= ctrl.hoveredHistoryRange.toV &&
  64. ctrl.entry.fromV >= ctrl.hoveredHistoryRange.fromV
  65. )
  66. }
  67. ctrl.onDraggingStart = () => {
  68. ctrl.historyEntriesList.onDraggingStart()
  69. }
  70. ctrl.onDraggingStop = (isValidDrop, boundary) =>
  71. ctrl.historyEntriesList.onDraggingStop(isValidDrop, boundary)
  72. ctrl.onDrop = boundary => {
  73. if (boundary === 'toV') {
  74. $scope.$applyAsync(() =>
  75. ctrl.historyEntriesList.setRangeToV(ctrl.entry.toV)
  76. )
  77. } else if (boundary === 'fromV') {
  78. $scope.$applyAsync(() =>
  79. ctrl.historyEntriesList.setRangeFromV(ctrl.entry.fromV)
  80. )
  81. }
  82. }
  83. ctrl.onOver = boundary => {
  84. if (boundary === 'toV') {
  85. $scope.$applyAsync(() =>
  86. ctrl.historyEntriesList.setHoveredRangeToV(ctrl.entry.toV)
  87. )
  88. } else if (boundary === 'fromV') {
  89. $scope.$applyAsync(() =>
  90. ctrl.historyEntriesList.setHoveredRangeFromV(ctrl.entry.fromV)
  91. )
  92. }
  93. }
  94. ctrl.$onInit = () => {
  95. ctrl.$entryEl = $element.find('> .history-entry')
  96. ctrl.$entryDetailsEl = $element.find('.history-entry-details')
  97. ctrl.$toVHandleEl = $element.find('.history-entry-toV-handle')
  98. ctrl.$fromVHandleEl = $element.find('.history-entry-fromV-handle')
  99. ctrl.historyEntriesList.onEntryLinked(ctrl.entry, ctrl.$entryEl)
  100. }
  101. }
  102. export default App.component('historyEntry', {
  103. bindings: {
  104. entry: '<',
  105. currentUser: '<',
  106. users: '<',
  107. rangeSelectionEnabled: '<',
  108. isDragging: '<',
  109. selectedHistoryVersion: '<?',
  110. selectedHistoryRange: '<?',
  111. hoveredHistoryRange: '<?',
  112. onSelect: '&',
  113. onLabelDelete: '&',
  114. },
  115. require: {
  116. historyEntriesList: '^historyEntriesList',
  117. },
  118. controller: historyEntryController,
  119. templateUrl: 'historyEntryTpl',
  120. })