focus.js 2.4 KB

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