1
+ angular . module ( 'ui.bootstrap.modal' , [ ] )
2
+ . constant ( 'modalConfig' , {
3
+ backdrop : true ,
4
+ escape : true
5
+ } )
6
+ . directive ( 'modal' , [ '$parse' , 'modalConfig' , function ( $parse , modalConfig ) {
7
+ var backdropEl ;
8
+ var body = angular . element ( document . getElementsByTagName ( 'body' ) [ 0 ] ) ;
9
+ return {
10
+ restrict : 'EA' ,
11
+ link : function ( scope , elm , attrs ) {
12
+ var opts = angular . extend ( { } , modalConfig , scope . $eval ( attrs . uiOptions || attrs . bsOptions || attrs . options ) ) ;
13
+ var shownExpr = attrs . modal || attrs . show ;
14
+ var setClosed ;
15
+ if ( attrs . close ) {
16
+ setClosed = function ( ) {
17
+ scope . $apply ( attrs . close ) ;
18
+ } ;
19
+ } else {
20
+ setClosed = function ( ) {
21
+ scope . $apply ( function ( ) {
22
+ $parse ( shownExpr ) . assign ( scope , false ) ;
23
+ } ) ;
24
+ } ;
25
+ }
26
+ elm . addClass ( 'modal' ) ;
27
+ if ( opts . backdrop && ! backdropEl ) {
28
+ backdropEl = angular . element ( '<div class="modal-backdrop"></div>' ) ;
29
+ backdropEl . css ( 'display' , 'none' ) ;
30
+ body . append ( backdropEl ) ;
31
+ }
32
+ function setShown ( shown ) {
33
+ scope . $apply ( function ( ) {
34
+ model . assign ( scope , shown ) ;
35
+ } ) ;
36
+ }
37
+ function escapeClose ( evt ) {
38
+ if ( evt . which === 27 ) { setClosed ( ) ; }
39
+ }
40
+ function clickClose ( ) {
41
+ setClosed ( ) ;
42
+ }
43
+ function close ( ) {
44
+ if ( opts . escape ) { body . unbind ( 'keyup' , escapeClose ) ; }
45
+ if ( opts . backdrop ) {
46
+ backdropEl . css ( 'display' , 'none' ) . removeClass ( 'in' ) ;
47
+ backdropEl . unbind ( 'click' , clickClose ) ;
48
+ }
49
+ elm . css ( 'display' , 'none' ) . removeClass ( 'in' ) ;
50
+ body . removeClass ( 'modal-open' ) ;
51
+ }
52
+ function open ( ) {
53
+ if ( opts . escape ) { body . bind ( 'keyup' , escapeClose ) ; }
54
+ if ( opts . backdrop ) {
55
+ backdropEl . css ( 'display' , 'block' ) . addClass ( 'in' ) ;
56
+ if ( opts . backdrop != "static" ) {
57
+ backdropEl . bind ( 'click' , clickClose ) ;
58
+ }
59
+ }
60
+ elm . css ( 'display' , 'block' ) . addClass ( 'in' ) ;
61
+ body . addClass ( 'modal-open' ) ;
62
+ }
63
+ scope . $watch ( shownExpr , function ( isShown , oldShown ) {
64
+ if ( isShown ) {
65
+ open ( ) ;
66
+ } else {
67
+ close ( ) ;
68
+ }
69
+ } ) ;
70
+ }
71
+ } ;
72
+ } ] ) ;
0 commit comments