File tree 2 files changed +58
-0
lines changed
2 files changed +58
-0
lines changed Original file line number Diff line number Diff line change
1
+ /*global angular */
2
+
3
+ /**
4
+ * Directive that executes an expression when the element it is applied to gets
5
+ * an `escape` keydown event.
6
+ */
7
+ angular
8
+ . module ( 'todomvc' )
9
+ . directive ( 'todoEscape' , function ( ) {
10
+ 'use strict' ;
11
+
12
+ var ESCAPE_KEY = 27 ;
13
+
14
+ return function ( scope , elem , attrs ) {
15
+ elem . bind ( 'keydown' , function ( event ) {
16
+ if ( event . keyCode === ESCAPE_KEY ) {
17
+ scope . $apply ( attrs . todoEscape ) ;
18
+ }
19
+ } ) ;
20
+
21
+ scope . $on ( '$destroy' , function ( ) {
22
+ elem . unbind ( 'keydown' ) ;
23
+ } ) ;
24
+ } ;
25
+ } ) ;
Original file line number Diff line number Diff line change
1
+ describe ( 'todo-escape' , function ( ) {
2
+ var ESCAPE_KEY = 27 ;
3
+ var OTHER_KEY = 32 ;
4
+ var template = '<input todo-escape="escapePressed=true" />' ;
5
+ var scope , compile ;
6
+
7
+ beforeEach ( module ( 'todomvc' ) ) ;
8
+
9
+ beforeEach ( inject ( function ( $rootScope , $compile ) {
10
+ scope = $rootScope . $new ( ) ;
11
+ compile = $compile ;
12
+ scope . escapePressed = false ;
13
+ } ) ) ;
14
+
15
+ it ( 'should evaluate the given statement when escape key pressed' , function ( ) {
16
+ var el = compile ( template ) ( scope ) ;
17
+ el . triggerHandler ( { type : 'keydown' , keyCode : ESCAPE_KEY } ) ;
18
+ expect ( scope . escapePressed ) . toBe ( true ) ;
19
+ } ) ;
20
+
21
+ it ( 'should evaluate the given statement when escape key pressed' , function ( ) {
22
+ var el = compile ( template ) ( scope ) ;
23
+ el . triggerHandler ( { type : 'keydown' , keyCode : OTHER_KEY } ) ;
24
+ expect ( scope . escapePressed ) . toBe ( false ) ;
25
+ } ) ;
26
+
27
+ it ( 'should unbind the handler when the scope is destroyed' , function ( ) {
28
+ var el = compile ( template ) ( scope ) ;
29
+ scope . $destroy ( ) ;
30
+ el . triggerHandler ( { type : 'keydown' , keyCode : ESCAPE_KEY } ) ;
31
+ expect ( scope . escapePressed ) . toBe ( false ) ;
32
+ } ) ;
33
+ } ) ;
You can’t perform that action at this time.
0 commit comments