focus.js 2.2 KB

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