overleaf/services/web/frontend/js/directives/focus.js
Mathias Jakobsen 1a92e1b664 Merge pull request #15248 from overleaf/mj-eslint-angular-components
[web] Add eslint rules for angularjs components

GitOrigin-RevId: 1343d584368faeb912f04c5879228bcbd07a042a
2023-10-18 08:03:57 +00:00

118 lines
2.6 KiB
JavaScript

/* eslint-disable
no-return-assign,
*/
// TODO: This file was created by bulk-decaffeinate.
// Fix any style issues and re-enable lint.
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* DS207: Consider shorter variations of null checks
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
import App from '../base'
App.directive('focusWhen', [
'$timeout',
function ($timeout) {
return {
restrict: 'A',
link(scope, element, attr) {
return scope.$watch(attr.focusWhen, function (value) {
if (value) {
return $timeout(() => element.focus())
}
})
},
}
},
])
App.directive('focusOn', function () {
return {
restrict: 'A',
link(scope, element, attrs) {
return scope.$on(attrs.focusOn, () => element.focus())
},
}
})
App.directive('selectWhen', [
'$timeout',
function ($timeout) {
return {
restrict: 'A',
link(scope, element, attr) {
return scope.$watch(attr.selectWhen, function (value) {
if (value) {
return $timeout(() => element.select())
}
})
},
}
},
])
App.directive('selectOn', function () {
return {
restrict: 'A',
link(scope, element, attrs) {
return scope.$on(attrs.selectOn, () => element.select())
},
}
})
App.directive('selectNameWhen', [
'$timeout',
function ($timeout) {
return {
restrict: 'A',
link(scope, element, attrs) {
return scope.$watch(attrs.selectNameWhen, function (value) {
if (value) {
return $timeout(() => selectName(element))
}
})
},
}
},
])
App.directive('selectNameOn', function () {
return {
restrict: 'A',
link(scope, element, attrs) {
return scope.$on(attrs.selectNameOn, () => selectName(element))
},
}
})
App.directive('focus', [
'$timeout',
function ($timeout) {
return {
scope: {
trigger: '@focus',
},
link(scope, element) {
return scope.$watch('trigger', function (value) {
if (value === 'true') {
return $timeout(() => element[0].focus())
}
})
},
}
},
])
function selectName(element) {
// Select up to last '.'. I.e. everything except the file extension
element.focus()
const name = element.val()
if (element[0].setSelectionRange != null) {
let selectionEnd = name.lastIndexOf('.')
if (selectionEnd === -1) {
selectionEnd = name.length
}
return element[0].setSelectionRange(0, selectionEnd)
}
}