diff --git a/app/assets/javascripts/angular/directives/components/focus.js b/app/assets/javascripts/angular/directives/components/focus.js index 13ad309e4f..145d35781f 100644 --- a/app/assets/javascripts/angular/directives/components/focus.js +++ b/app/assets/javascripts/angular/directives/components/focus.js @@ -29,16 +29,37 @@ // TODO move to UI components angular.module('openproject.uiComponents') -.directive('focus', ['$timeout', function($timeout) { +.constant('FOCUSABLE_SELECTOR', 'a, button, :input, [tabindex]') + +.directive('focus', ['$timeout', 'FOCUSABLE_SELECTOR', function($timeout, FOCUSABLE_SELECTOR) { + + function focusElement(element) { + $timeout(function() { + var focusable = element; + + if (!focusable.is(FOCUSABLE_SELECTOR)) { + focusable = element.find(FOCUSABLE_SELECTOR); + } + + focusable[0].focus(); + }); + } + + function updateFocus(scope, element, attrs) { + var condition = (attrs.focus) ? scope.$eval(attrs.focus) : true; + + if (condition) { + focusElement(element); + } + } + return { link: function(scope, element, attrs) { - var condition = (attrs.focus) ? scope.$eval(attrs.focus) : true; + updateFocus(scope, element, attrs); - if (condition) { - $timeout(function() { - element[0].focus(); - }); - } + scope.$on('updateFocus', function() { + updateFocus(scope, element, attrs); + }); } }; }]); diff --git a/app/assets/javascripts/angular/work_packages/controllers/details-tab-watchers-controller.js b/app/assets/javascripts/angular/work_packages/controllers/details-tab-watchers-controller.js index 1cda57ddfa..ca1f9e1a21 100644 --- a/app/assets/javascripts/angular/work_packages/controllers/details-tab-watchers-controller.js +++ b/app/assets/javascripts/angular/work_packages/controllers/details-tab-watchers-controller.js @@ -28,8 +28,17 @@ angular.module('openproject.workPackages.controllers') -.controller('DetailsTabWatchersController', ['$scope', 'workPackage', 'I18n', function($scope, workPackage, I18n) { +.constant('ADD_WATCHER_SELECT_INDEX', -1) + +.controller('DetailsTabWatchersController', + ['$scope', + '$timeout', + 'workPackage', + 'I18n', + 'ADD_WATCHER_SELECT_INDEX', + function($scope, $timeout, workPackage, I18n, ADD_WATCHER_SELECT_INDEX) { $scope.I18n = I18n; + $scope.focusElementIndex = ($scope.watchers.length > 0) ? 0 : ADD_WATCHER_SELECT_INDEX; $scope.$watch('watchers.length', fetchAvailableWatchers); fetchAvailableWatchers(); @@ -93,6 +102,8 @@ angular.module('openproject.workPackages.controllers') function addWatcherSuccess() { $scope.outputMessage(I18n.t("js.label_watcher_added_successfully")); $scope.refreshWorkPackage(); + + $scope.focusElementIndex = ADD_WATCHER_SELECT_INDEX; } $scope.deleteWatcher = function(watcher) { @@ -111,9 +122,23 @@ angular.module('openproject.workPackages.controllers') if (index >= 0) { $scope.watchers.splice(index, 1); + + updateWatcherFocus(index); } } + function updateWatcherFocus(index) { + if ($scope.watchers.length == 0) { + $scope.focusElementIndex = ADD_WATCHER_SELECT_INDEX; + } else { + $scope.focusElementIndex = (index < $scope.watchers.length) ? index : $scope.watchers.length - 1; + } + + $timeout(function() { + $scope.$broadcast('updateFocus'); + }); + } + $scope.selectedWatcher = { id: null }; $scope.$watch('selectedWatcher.id', addWatcher); diff --git a/public/templates/work_packages/tabs/watchers.html b/public/templates/work_packages/tabs/watchers.html index e484fa26d2..98067fd3e0 100644 --- a/public/templates/work_packages/tabs/watchers.html +++ b/public/templates/work_packages/tabs/watchers.html @@ -2,7 +2,7 @@