focus.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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', $timeout => ({
  14. restrict: 'A',
  15. link(scope, element, attr) {
  16. return scope.$watch(attr.focusWhen, function (value) {
  17. if (value) {
  18. return $timeout(() => element.focus())
  19. }
  20. })
  21. },
  22. }))
  23. App.directive('focusOn', $timeout => ({
  24. restrict: 'A',
  25. link(scope, element, attrs) {
  26. return scope.$on(attrs.focusOn, () => element.focus())
  27. },
  28. }))
  29. App.directive('selectWhen', $timeout => ({
  30. restrict: 'A',
  31. link(scope, element, attr) {
  32. return scope.$watch(attr.selectWhen, function (value) {
  33. if (value) {
  34. return $timeout(() => element.select())
  35. }
  36. })
  37. },
  38. }))
  39. App.directive('selectOn', $timeout => ({
  40. restrict: 'A',
  41. link(scope, element, attrs) {
  42. return scope.$on(attrs.selectOn, () => element.select())
  43. },
  44. }))
  45. App.directive('selectNameWhen', $timeout => ({
  46. restrict: 'A',
  47. link(scope, element, attrs) {
  48. return scope.$watch(attrs.selectNameWhen, function (value) {
  49. if (value) {
  50. return $timeout(() => selectName(element))
  51. }
  52. })
  53. },
  54. }))
  55. App.directive('selectNameOn', () => ({
  56. restrict: 'A',
  57. link(scope, element, attrs) {
  58. return scope.$on(attrs.selectNameOn, () => selectName(element))
  59. },
  60. }))
  61. App.directive('focus', $timeout => ({
  62. scope: {
  63. trigger: '@focus',
  64. },
  65. link(scope, element) {
  66. return scope.$watch('trigger', function (value) {
  67. if (value === 'true') {
  68. return $timeout(() => element[0].focus())
  69. }
  70. })
  71. },
  72. }))
  73. function selectName(element) {
  74. // Select up to last '.'. I.e. everything except the file extension
  75. element.focus()
  76. const name = element.val()
  77. if (element[0].setSelectionRange != null) {
  78. let selectionEnd = name.lastIndexOf('.')
  79. if (selectionEnd === -1) {
  80. selectionEnd = name.length
  81. }
  82. return element[0].setSelectionRange(0, selectionEnd)
  83. }
  84. }