historyFileEntity.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* eslint-disable
  2. max-len,
  3. no-return-assign,
  4. no-undef,
  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. define([
  14. 'base',
  15. 'ide/file-tree/util/iconTypeFromName',
  16. 'ide/file-tree/util/fileOperationI18nNames'
  17. ], function(App, iconTypeFromName, fileOperationI18nNames) {
  18. const historyFileEntityController = function($scope, $element, $attrs) {
  19. const ctrl = this
  20. ctrl.hasOperation = false
  21. ctrl.getRenameTooltip = i18nRenamedStr => {
  22. let [simplifiedOldPathname, simplifiedPathname] = _getSimplifiedPaths(
  23. ctrl.fileEntity.oldPathname,
  24. ctrl.fileEntity.pathname
  25. )
  26. return `${
  27. fileOperationI18nNames.renamed
  28. } <strong>${simplifiedOldPathname}</strong> &rarr; <strong>${simplifiedPathname}</strong>`
  29. }
  30. ctrl.getFileOperationName = () => {
  31. if (ctrl.fileEntity.operation === 'edited') {
  32. return fileOperationI18nNames.edited
  33. } else if (ctrl.fileEntity.operation === 'renamed') {
  34. return fileOperationI18nNames.renamed
  35. } else if (ctrl.fileEntity.operation === 'added') {
  36. return fileOperationI18nNames.created
  37. } else if (ctrl.fileEntity.operation === 'removed') {
  38. return fileOperationI18nNames.deleted
  39. } else {
  40. return ''
  41. }
  42. }
  43. const _getSimplifiedPaths = (path1, path2) => {
  44. let path1Parts = path1.split('/')
  45. let path2Parts = path2.split('/')
  46. let maxIterations = Math.min(path1Parts.length, path2Parts.length) - 1
  47. for (
  48. var commonPartIndex = 0;
  49. commonPartIndex < maxIterations;
  50. commonPartIndex++
  51. ) {
  52. if (path1Parts[commonPartIndex] !== path2Parts[commonPartIndex]) {
  53. break
  54. }
  55. }
  56. path1Parts.splice(0, commonPartIndex)
  57. path2Parts.splice(0, commonPartIndex)
  58. return [path1Parts.join('/'), path2Parts.join('/')]
  59. }
  60. const _handleFolderClick = function() {
  61. ctrl.isOpen = !ctrl.isOpen
  62. ctrl.entityTypeIconClass = _getFolderIcon()
  63. }
  64. const _handleFileClick = () =>
  65. ctrl.historyFileTreeController.handleEntityClick(ctrl.fileEntity)
  66. var _getFolderIcon = function() {
  67. if (ctrl.isOpen) {
  68. return 'fa-folder-open'
  69. } else {
  70. return 'fa-folder'
  71. }
  72. }
  73. ctrl.$onInit = function() {
  74. if (ctrl.fileEntity.type === 'folder') {
  75. ctrl.isOpen = true
  76. ctrl.entityTypeIconClass = _getFolderIcon()
  77. ctrl.handleClick = _handleFolderClick
  78. } else {
  79. if (ctrl.fileEntity.operation) {
  80. ctrl.hasOperation = true
  81. }
  82. ctrl.entityTypeIconClass = `fa-${iconTypeFromName(
  83. ctrl.fileEntity.name
  84. )}`
  85. ctrl.entityOpTextClass = ctrl.fileEntity.operation
  86. ? `history-file-entity-name-${ctrl.fileEntity.operation}`
  87. : null
  88. ctrl.handleClick = _handleFileClick
  89. $scope.$watch(
  90. () => ctrl.historyFileTreeController.selectedPathname,
  91. selectedPathname =>
  92. (ctrl.isSelected = ctrl.fileEntity.pathname === selectedPathname)
  93. )
  94. }
  95. }
  96. }
  97. return App.component('historyFileEntity', {
  98. require: {
  99. historyFileTreeController: '^historyFileTree'
  100. },
  101. bindings: {
  102. fileEntity: '<'
  103. },
  104. controller: historyFileEntityController,
  105. templateUrl: 'historyFileEntityTpl'
  106. })
  107. })