@@ -5,6 +5,7 @@ import { Subscription } from 'rxjs';
5
5
import { sidebarCssClasses , validBreakpoints , checkBreakpoint } from '../shared' ;
6
6
import { ClassToggler } from '../shared/toggle-classes' ;
7
7
import { SidebarService } from './sidebar.service' ;
8
+ import { OutClickService } from './out-click.service' ;
8
9
9
10
interface SidebarState {
10
11
minimized : boolean ;
@@ -14,7 +15,7 @@ interface SidebarState {
14
15
15
16
@Component ( {
16
17
selector : 'cui-sidebar' ,
17
- template : `<ng-content></ng-content>` ,
18
+ template : `<ng-content></ng-content>`
18
19
} )
19
20
export class SidebarComponent implements OnInit , OnDestroy {
20
21
@Input ( ) compact : boolean ;
@@ -24,7 +25,8 @@ export class SidebarComponent implements OnInit, OnDestroy {
24
25
@Input ( ) opened : boolean ;
25
26
@Input ( ) offCanvas : boolean ;
26
27
27
- private subscription : Subscription ;
28
+ private stateToggleSubscription : Subscription ;
29
+ private outClickSubscription : Subscription ;
28
30
private body : HTMLBodyElement ;
29
31
30
32
state : SidebarState = {
@@ -38,7 +40,8 @@ export class SidebarComponent implements OnInit, OnDestroy {
38
40
private renderer : Renderer2 ,
39
41
private hostElement : ElementRef ,
40
42
private classToggler : ClassToggler ,
41
- private sidebarService : SidebarService
43
+ private sidebarService : SidebarService ,
44
+ private outClickService : OutClickService
42
45
) {
43
46
renderer . addClass ( hostElement . nativeElement , 'sidebar' ) ;
44
47
}
@@ -54,20 +57,15 @@ export class SidebarComponent implements OnInit, OnDestroy {
54
57
this . isOpened ( this . opened ) ;
55
58
this . setState ( ) ;
56
59
57
- this . subscription = this . sidebarService . sidebarState$ . subscribe ( ( state ) => {
58
- if ( 'minimize' in state ) {
59
- this . minimize ( state . minimize ) ;
60
- }
61
- if ( 'open' in state ) {
62
- this . open ( state ) ;
63
- }
64
- } ) ;
60
+ this . stateToggleSubscribe ( ) ;
61
+ this . outClickSubscribe ( ) ;
65
62
}
66
63
67
64
ngOnDestroy ( ) : void {
68
65
this . renderer . removeClass ( this . body , 'sidebar-fixed' ) ;
69
66
70
- this . subscription . unsubscribe ( ) ;
67
+ this . stateToggleSubscribe ( false ) ;
68
+ this . outClickSubscribe ( false ) ;
71
69
}
72
70
73
71
isCompact ( compact : boolean = this . compact ) : void {
@@ -98,8 +96,7 @@ export class SidebarComponent implements OnInit, OnDestroy {
98
96
99
97
displayBreakpoint ( display : any = this . display ) : void {
100
98
if ( display !== false ) {
101
- const cssClass = Boolean ( display ) && checkBreakpoint ( display , validBreakpoints ) ? `sidebar-${ display } -show` : sidebarCssClasses [ 0 ] ;
102
- this . renderer . addClass ( this . body , cssClass ) ;
99
+ this . sidebarService . toggle ( { open : true , breakpoint : display } ) ;
103
100
}
104
101
}
105
102
@@ -120,14 +117,58 @@ export class SidebarComponent implements OnInit, OnDestroy {
120
117
const cssClass = Boolean ( state . breakpoint ) && checkBreakpoint ( state . breakpoint , validBreakpoints ) ?
121
118
`sidebar-${ state . breakpoint } -show` :
122
119
sidebarCssClasses [ 0 ] ;
120
+ const mobile = cssClass === sidebarCssClasses [ 0 ] ;
123
121
const opened = this . body . classList . contains ( cssClass ) ;
124
122
const open = toggle ? ! opened : state . open ;
125
123
if ( open ) {
126
124
this . renderer . addClass ( this . body , cssClass ) ;
127
125
} else {
128
126
this . renderer . removeClass ( this . body , cssClass ) ;
129
127
}
128
+ if ( mobile ) {
129
+ if ( open && ( ! this . outClickSubscription || this . outClickSubscription . closed ) ) {
130
+ this . outClickSubscribe ( ) ;
131
+ }
132
+ if ( ! open && this . outClickSubscription && ! this . outClickSubscription . closed ) {
133
+ this . outClickSubscribe ( false ) ;
134
+ }
135
+ }
130
136
this . state . opened = < boolean > open ;
131
137
return < boolean > open ;
132
138
}
139
+
140
+ private stateToggleSubscribe ( subscribe : Boolean = true ) {
141
+ if ( subscribe ) {
142
+ this . stateToggleSubscription = this . sidebarService . sidebarState$ . subscribe ( ( state ) => {
143
+ if ( 'minimize' in state ) {
144
+ this . minimize ( state . minimize ) ;
145
+ }
146
+ if ( 'open' in state ) {
147
+ this . open ( state ) ;
148
+ }
149
+ } ) ;
150
+ } else {
151
+ this . stateToggleSubscription . unsubscribe ( ) ;
152
+ }
153
+ }
154
+
155
+ private outClickSubscribe ( subscribe : Boolean = true ) {
156
+ if ( subscribe ) {
157
+ this . outClickSubscription = this . outClickService . outClick$ . subscribe ( message => {
158
+ if ( message . event ) {
159
+ this . hideMobile ( message . event ) ;
160
+ }
161
+ } ) ;
162
+ } else {
163
+ this . outClickSubscription . unsubscribe ( ) ;
164
+ }
165
+ }
166
+
167
+ public hideMobile ( e ) {
168
+ if ( this . state . opened ) {
169
+ if ( ! e . target . closest ( '[cuisidebartoggle]' ) ) {
170
+ this . sidebarService . toggle ( { open : false , breakpoint : '' } ) ;
171
+ }
172
+ }
173
+ }
133
174
}
0 commit comments