forked from patternfly/angular-patternfly
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautofocus.js
More file actions
58 lines (51 loc) · 1.57 KB
/
Copy pathautofocus.js
File metadata and controls
58 lines (51 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
* @ngdoc directive
* @name patternfly.autofocus:pfFocused
* @restrict A
* @element ANY
* @param {expression=} pfFocused If the expression is true, the element is focused and selected (if possible).
*
* @description
* The focus on element is evaluated from given expression. If the expression provided as an attribute to this directive
* is evaluated as true, the element is selected (and focused).
*
* @example
<example module="patternfly.autofocus">
<file name="index.html">
<div>
<form class="form-horizontal">
<div class="form-group">
<label class="col-sm-2 control-label" for="i1">Focus next input:</label>
<div class="col-sm-10">
<input id="i1" ng-model="isFocus" type="checkbox"></input>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label" for="i2">Focused input:</label>
<div class="col-sm-10">
<input class="form-control" id="i1" ng-model="i2" pf-focused="isFocus" placeholder="This will be selected after checking the box above."></input>
</div>
</div>
</form>
</div>
</file>
</example>
*/
angular.module('patternfly.autofocus', []).directive('pfFocused', function ($timeout) {
'use strict';
return {
restrict: 'A',
link: function (scope, element, attrs) {
scope.$watch(attrs.pfFocused, function (newValue) {
$timeout(function () {
if (newValue) {
element[0].focus();
if (element[0].select) {
element[0].select();
}
}
});
});
}
};
});