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

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

/**
 * The given expression will be executed when the user presses either escape or presses something outside
 * of this element
 *
 * Example:
 * <div piwik-focus-anywhere-but-here="closeDialog()">my dialog</div>
 */
(function () {
    angular.module('piwikApp.directive').directive('piwikFocusAnywhereButHere', piwikFocusAnywhereButHere);

    piwikFocusAnywhereButHere.$inject = ['$document'];

    function piwikFocusAnywhereButHere($document){
        return {
            restrict: 'A',
            link: function(scope, element, attr, ctrl) {

                function onClickOutsideElement (event) {
                    if (element.has(event.target).length === 0) {
                        setTimeout(function () {
                            scope.$apply(attr.piwikFocusAnywhereButHere);
                        }, 0);
                    }
                }

                function onEscapeHandler (event) {
                    if (event.which === 27) {
                        setTimeout(function () {
                            scope.$apply(attr.piwikFocusAnywhereButHere);
                        }, 0);
                    }
                }

                $document.on('keyup', onEscapeHandler);
                $document.on('mouseup', onClickOutsideElement);
                scope.$on('$destroy', function() {
                    $document.off('mouseup', onClickOutsideElement);
                    $document.off('keyup', onEscapeHandler);
                });
            }
        };
    }
})();