|
32 | 32 | .module('openlmis-table') |
33 | 33 | .directive('tr', directive); |
34 | 34 |
|
35 | | - function directive() { |
| 35 | + directive.$inject = ['$rootScope']; |
| 36 | + |
| 37 | + function directive($rootScope) { |
36 | 38 | var focusInRow, focusedRow, rows = []; |
37 | 39 |
|
38 | 40 | return { |
|
43 | 45 | function link(scope, element) { |
44 | 46 | if (!rows.length) { |
45 | 47 | angular.element('body') |
46 | | - .on('focusin', setSelectedRow); |
| 48 | + .on('focusin', setSelectedRow) |
| 49 | + .on('focusout', clearSelectedRowOnLeave); |
47 | 50 | } |
48 | 51 |
|
49 | 52 | if (rows.indexOf(element) === -1) { |
|
76 | 79 |
|
77 | 80 | if (!rows.length) { |
78 | 81 | angular.element('body') |
79 | | - .off('focusin', setSelectedRow); |
| 82 | + .off('focusin', setSelectedRow) |
| 83 | + .off('focusout', clearSelectedRowOnLeave); |
80 | 84 | } |
81 | 85 | } |
82 | 86 | } |
|
97 | 101 | focusInRow = undefined; |
98 | 102 | } |
99 | 103 |
|
| 104 | + function clearSelectedRowOnLeave(event) { |
| 105 | + var related = event.relatedTarget || |
| 106 | + (event.originalEvent && event.originalEvent.relatedTarget); |
| 107 | + |
| 108 | + // When focus moves to another focusable element a focusin follows and |
| 109 | + // setSelectedRow handles the change. Only act when focus leaves to a |
| 110 | + // non-focusable target (e.g. clicking outside the table), where no |
| 111 | + // focusin fires and the row would otherwise keep the is-focused class. |
| 112 | + if (related) { |
| 113 | + return; |
| 114 | + } |
| 115 | + |
| 116 | + if (focusedRow) { |
| 117 | + focusedRow.removeClass('is-focused'); |
| 118 | + focusedRow = undefined; |
| 119 | + $rootScope.$evalAsync(); |
| 120 | + } |
| 121 | + } |
| 122 | + |
100 | 123 | } |
101 | 124 |
|
102 | 125 | })(); |
0 commit comments