Welcome to mirror list, hosted at ThFree Co, Russian Federation.

select-on-focus.js « directives « common « angularjs « CoreHome « plugins - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: bcaf868f2d86406660b97c9becde6ba70b3edfe1 (plain)
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
59
60
61
62
63
/*!
 * Piwik - free/libre analytics platform
 *
 * @link http://piwik.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */

/**
 * On focus (click, tab) selects the text within the current element
 *
 * Example:
 * <div piwik-select-on-focus>my dialog</div>
 */
(function () {
    angular.module('piwikApp.directive').directive('piwikSelectOnFocus', piwikSelectOnFocus);

    function piwikSelectOnFocus(){
        return {
            restrict: 'A',
            link: function(scope, element, attr, ctrl) {

                var focusedElement = null;

                var tagName = (element.prop('tagName') + '').toLowerCase();
                var elementSupportsSelect = tagName === 'textarea';

                function onFocusHandler(event) {
                    if (focusedElement !== this) {
                        focusedElement = this;
                        angular.element(this).select();
                    }
                }

                function onClickHandler(event) {
                    // .select() + focus and blur seems to not work on pre elements
                    var range = document.createRange();
                    range.selectNode(this);
                    window.getSelection().addRange(range);
                }

                function onBlurHandler(event) {
                    focusedElement = null;
                }

                if (elementSupportsSelect) {
                    element.on('focus', onFocusHandler);
                    element.on('blur', onBlurHandler);
                } else {
                    element.on('click', onClickHandler);
                }

                scope.$on('$destroy', function() {
                    if (elementSupportsSelect) {
                        element.off('focus', onFocusHandler);
                        element.off('blur', onBlurHandler);
                    } else {
                        element.off('click', onClickHandler);
                    }
                });
            }
        };
    }
})();