focus.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /* eslint-disable
  2. no-return-assign,
  3. */
  4. // TODO: This file was created by bulk-decaffeinate.
  5. // Fix any style issues and re-enable lint.
  6. /*
  7. * decaffeinate suggestions:
  8. * DS102: Remove unnecessary code created because of implicit returns
  9. * DS207: Consider shorter variations of null checks
  10. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  11. */
  12. import App from '../base'
  13. App.directive('focusWhen', [
  14. '$timeout',
  15. function ($timeout) {
  16. return {
  17. restrict: 'A',
  18. link(scope, element, attr) {
  19. return scope.$watch(attr.focusWhen, function (value) {
  20. if (value) {
  21. return $timeout(() => element.focus())
  22. }
  23. })
  24. },
  25. }
  26. },
  27. ])
  28. App.directive('focusOn', function () {
  29. return {
  30. restrict: 'A',
  31. link(scope, element, attrs) {
  32. return scope.$on(attrs.focusOn, () => element.focus())
  33. },
  34. }
  35. })
  36. App.directive('selectWhen', [
  37. '$timeout',
  38. function ($timeout) {
  39. return {
  40. restrict: 'A',
  41. link(scope, element, attr) {
  42. return scope.$watch(attr.selectWhen, function (value) {
  43. if (value) {
  44. return $timeout(() => element.select())
  45. }
  46. })
  47. },
  48. }
  49. },
  50. ])
  51. App.directive('selectOn', function () {
  52. return {
  53. restrict: 'A',
  54. link(scope, element, attrs) {
  55. return scope.$on(attrs.selectOn, () => element.select())
  56. },
  57. }
  58. })
  59. App.directive('selectNameWhen', [
  60. '$timeout',
  61. function ($timeout) {
  62. return {
  63. restrict: 'A',
  64. link(scope, element, attrs) {
  65. return scope.$watch(attrs.selectNameWhen, function (value) {
  66. if (value) {
  67. return $timeout(() => selectName(element))
  68. }
  69. })
  70. },
  71. }
  72. },
  73. ])
  74. App.directive('selectNameOn', function () {
  75. return {
  76. restrict: 'A',
  77. link(scope, element, attrs) {
  78. return scope.$on(attrs.selectNameOn, () => selectName(element))
  79. },
  80. }
  81. })
  82. App.directive('focus', [
  83. '$timeout',
  84. function ($timeout) {
  85. return {
  86. scope: {
  87. trigger: '@focus',
  88. },
  89. link(scope, element) {
  90. return scope.$watch('trigger', function (value) {
  91. if (value === 'true') {
  92. return $timeout(() => element[0].focus())
  93. }
  94. })
  95. },
  96. }
  97. },
  98. ])
  99. function selectName(element) {
  100. // Select up to last '.'. I.e. everything except the file extension
  101. element.focus()
  102. const name = element.val()
  103. if (element[0].setSelectionRange != null) {
  104. let selectionEnd = name.lastIndexOf('.')
  105. if (selectionEnd === -1) {
  106. selectionEnd = name.length
  107. }
  108. return element[0].setSelectionRange(0, selectionEnd)
  109. }
  110. }