Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
7
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
EIT-SWE
ux
utils
Commits
fa71fa98
Commit
fa71fa98
authored
Feb 27, 2020
by
Shane Eckenrode
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add responsive directive
parent
0fdd15b7
Pipeline
#85018
passed with stages
in 3 minutes and 58 seconds
Changes
4
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
168 additions
and
0 deletions
+168
-0
libs/utils/responsive/src/index.ts
libs/utils/responsive/src/index.ts
+1
-0
libs/utils/responsive/src/lib/responsive.directive.spec.ts
libs/utils/responsive/src/lib/responsive.directive.spec.ts
+93
-0
libs/utils/responsive/src/lib/responsive.directive.ts
libs/utils/responsive/src/lib/responsive.directive.ts
+64
-0
libs/utils/responsive/src/lib/responsive.module.ts
libs/utils/responsive/src/lib/responsive.module.ts
+10
-0
No files found.
libs/utils/responsive/src/index.ts
View file @
fa71fa98
export
{
ResponsiveModule
}
from
'
./lib/responsive.module
'
;
export
{
ResponsiveService
}
from
'
./lib/responsive.service
'
;
export
{
ScreenSize
}
from
'
./lib/screen-size.model
'
;
libs/utils/responsive/src/lib/responsive.directive.spec.ts
0 → 100644
View file @
fa71fa98
import
{
CommonModule
}
from
'
@angular/common
'
;
import
{
Component
,
NO_ERRORS_SCHEMA
}
from
'
@angular/core
'
;
import
{
ComponentFixture
,
TestBed
}
from
'
@angular/core/testing
'
;
import
{
By
}
from
'
@angular/platform-browser
'
;
import
{
Observable
,
Subject
}
from
'
rxjs
'
;
import
{
ResponsiveModule
}
from
'
./responsive.module
'
;
import
{
ResponsiveService
}
from
'
./responsive.service
'
;
import
{
ScreenSize
}
from
'
./screen-size.model
'
;
const
o
=
new
Subject
<
ScreenSize
>
();
class
ResponsiveServiceStub
{
currentScreenSize$
:
Observable
<
ScreenSize
>
=
o
.
asObservable
();
}
@
Component
({
selector
:
'
ut-test-comp
'
,
template
:
`
<button utResponsive></button>
`
})
class
TestComponent
{}
describe
(
'
ResponsiveDirective
'
,
()
=>
{
let
fixture
:
ComponentFixture
<
TestComponent
>
;
let
classes
:
any
;
beforeEach
(()
=>
{
TestBed
.
configureTestingModule
({
imports
:
[
CommonModule
,
ResponsiveModule
],
declarations
:
[
TestComponent
],
providers
:
[{
provide
:
ResponsiveService
,
useClass
:
ResponsiveServiceStub
}],
schemas
:
[
NO_ERRORS_SCHEMA
]
}).
compileComponents
();
fixture
=
TestBed
.
createComponent
(
TestComponent
);
fixture
.
detectChanges
();
classes
=
fixture
.
debugElement
.
query
(
By
.
css
(
'
button
'
)).
classes
;
});
describe
(
'
when screen size is DESKTOP
'
,
()
=>
{
beforeEach
(()
=>
{
o
.
next
(
ScreenSize
.
DESKTOP
);
fixture
.
detectChanges
();
});
it
(
'
should only have desktop class
'
,
()
=>
{
expect
(
classes
.
desktop
).
toBeTruthy
();
expect
(
classes
.
tablet
).
toBeFalsy
();
expect
(
classes
.
mobile
).
toBeFalsy
();
});
});
describe
(
'
when screen size is TABLET_SM
'
,
()
=>
{
beforeEach
(()
=>
{
o
.
next
(
ScreenSize
.
TABLET_SM
);
fixture
.
detectChanges
();
});
it
(
'
should have tablet-sm class
'
,
()
=>
{
expect
(
classes
.
desktop
).
toBeFalsy
();
expect
(
classes
.
tablet
).
toBeFalsy
();
expect
(
classes
[
'
tablet-sm
'
]).
toBeTruthy
();
expect
(
classes
.
mobile
).
toBeFalsy
();
});
});
describe
(
'
when screen size is TABLET
'
,
()
=>
{
beforeEach
(()
=>
{
o
.
next
(
ScreenSize
.
TABLET
);
fixture
.
detectChanges
();
});
it
(
'
should have tablet class
'
,
()
=>
{
expect
(
classes
.
desktop
).
toBeFalsy
();
expect
(
classes
.
tablet
).
toBeTruthy
();
expect
(
classes
[
'
tablet-sm
'
]).
toBeFalsy
();
expect
(
classes
.
mobile
).
toBeFalsy
();
});
});
describe
(
'
when screen size is MOBILE
'
,
()
=>
{
beforeEach
(()
=>
{
o
.
next
(
ScreenSize
.
MOBILE
);
fixture
.
detectChanges
();
});
it
(
'
should only have mobile class
'
,
()
=>
{
expect
(
classes
.
desktop
).
toBeFalsy
();
expect
(
classes
.
tablet
).
toBeFalsy
();
expect
(
classes
.
mobile
).
toBeTruthy
();
});
});
});
libs/utils/responsive/src/lib/responsive.directive.ts
0 → 100644
View file @
fa71fa98
import
{
Directive
,
HostBinding
,
OnDestroy
,
OnInit
}
from
'
@angular/core
'
;
import
{
Subject
}
from
'
rxjs
'
;
import
{
filter
,
takeUntil
}
from
'
rxjs/operators
'
;
import
{
ResponsiveService
}
from
'
./responsive.service
'
;
import
{
ScreenSize
}
from
'
./screen-size.model
'
;
@
Directive
({
selector
:
'
[utResponsive]
'
})
export
class
ResponsiveDirective
implements
OnInit
,
OnDestroy
{
@
HostBinding
(
'
class.desktop
'
)
public
isDesktop
=
false
;
@
HostBinding
(
'
class.tablet
'
)
public
isTablet
=
false
;
@
HostBinding
(
'
class.tablet-sm
'
)
public
isSmallTablet
=
false
;
@
HostBinding
(
'
class.mobile
'
)
public
isMobile
=
false
;
private
destroyed$
=
new
Subject
<
any
>
();
constructor
(
private
responsiveService
:
ResponsiveService
)
{}
ngOnInit
():
void
{
this
.
responsiveService
.
currentScreenSize$
.
pipe
(
filter
(
size
=>
!!
size
),
takeUntil
(
this
.
destroyed$
)
)
.
subscribe
((
size
:
ScreenSize
)
=>
{
switch
(
size
)
{
case
ScreenSize
.
DESKTOP
:
this
.
isDesktop
=
true
;
this
.
isTablet
=
false
;
this
.
isSmallTablet
=
false
;
this
.
isMobile
=
false
;
break
;
case
ScreenSize
.
TABLET
:
this
.
isDesktop
=
false
;
this
.
isTablet
=
true
;
this
.
isSmallTablet
=
false
;
this
.
isMobile
=
false
;
break
;
case
ScreenSize
.
TABLET_SM
:
this
.
isDesktop
=
false
;
this
.
isTablet
=
false
;
this
.
isSmallTablet
=
true
;
this
.
isMobile
=
false
;
break
;
case
ScreenSize
.
MOBILE
:
this
.
isDesktop
=
false
;
this
.
isTablet
=
false
;
this
.
isSmallTablet
=
false
;
this
.
isMobile
=
true
;
break
;
}
});
}
public
ngOnDestroy
():
void
{
this
.
destroyed$
.
next
();
this
.
destroyed$
.
complete
();
}
}
libs/utils/responsive/src/lib/responsive.module.ts
0 → 100644
View file @
fa71fa98
import
{
CommonModule
}
from
'
@angular/common
'
;
import
{
NgModule
}
from
'
@angular/core
'
;
import
{
ResponsiveDirective
}
from
'
./responsive.directive
'
;
@
NgModule
({
imports
:
[
CommonModule
],
declarations
:
[
ResponsiveDirective
],
exports
:
[
ResponsiveDirective
]
})
export
class
ResponsiveModule
{}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment