forked from stryju/angular-escape
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
101 lines (74 loc) · 2.11 KB
/
index.js
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/* global define:false, console */
(function ( factory ) {
'use strict';
/* istanbul ignore next */
if ( typeof define === 'function' && define.amd ) {
return define( [ 'angular' ], factory );
}
/* istanbul ignore next */
if ( typeof exports === 'object' ) {
require( 'angular' );
return ( module.exports = factory( window.angular ) );
}
return factory( window.angular );
}( function ( angular ) {
'use strict';
function service( $document ) {
// jshint validthis:true
// jshint latedef:false
this.key = key;
this.click = click;
// ---
function key( cb, $scope ) {
var handler = function ( ev ) {
if ( ( ev.which || ev.keyCode ) !== 27 ) {
return ev;
}
// should allow on buttons?
// should allow for input[type=button|reset|submit]?
// if ( /input|textarea|select/i.test( ev.target.nodeName ) ) {
if ( /input|textarea|select/i.test( ev.target.nodeName ) ) {
return ev;
}
cb( ev );
$document.off( 'keydown', handler );
};
return register( 'keydown', handler, $scope );
}
function click( cb, element, $scope ) {
element = element && element[ 0 ] ? element[ 0 ] : element;
var handler = function ( ev ) {
if ( element ) {
var t = ev.target;
if ( t === element ) {
return ev;
}
while ( ( t = t.parentNode ) ) {
if ( t === element ) {
return ev;
}
}
}
cb( ev );
$document.off( 'click', handler );
};
return register( 'click', handler, $scope );
}
function register( event, handler, $scope ) {
var unregister = function () {
$document.off( event, handler );
};
$document.on( event, handler );
if ( $scope ) {
$scope.$on( '$destroy', unregister );
}
return unregister;
}
}
service.$inject = [
'$document'
];
var ngModule = angular.module( 'str.angular-escape', [ 'ng' ] )
.service( 'esc', service );
return ngModule.name;
}));