Skip to content

Commit

Permalink
refactor(): scss refactoring to reduce code duplication #144
Browse files Browse the repository at this point in the history
  • Loading branch information
Ks89 committed Aug 12, 2018
1 parent f9af5f8 commit e934d9b
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 139 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,16 @@
SOFTWARE.
*/

// The idea to create a carousel with two Subjects came from ng-bootstrap
// So a big thank you to the ng-bootstrap team for the interesting implementation that I used here with some customizations.

import {
AfterContentInit,
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
HostBinding,
HostListener,
Inject,
Input,
NgZone,
Expand All @@ -37,6 +42,10 @@ import {
SimpleChange,
SimpleChanges
} from '@angular/core';
import { isPlatformBrowser } from '@angular/common';

import { Subject, timer } from 'rxjs';
import { filter, map, switchMap, takeUntil } from 'rxjs/operators';

import { AccessibleComponent } from '../accessible.component';

Expand All @@ -46,32 +55,53 @@ import { InternalLibImage } from '../../model/image-internal.class';
import { Action } from '../../model/action.enum';
import { getIndex } from '../../utils/image.util';
import { NEXT, PREV } from '../../utils/user-input.util';
import { isPlatformBrowser } from '@angular/common';
import { CurrentImageConfig } from '../../model/current-image-config.interface';
import { LoadingConfig, LoadingType } from '../../model/loading-config.interface';
import { Description, DescriptionStrategy, DescriptionStyle } from '../../model/description.interface';
import { DotsConfig } from '../../model/dots-config.interface';
import { SlideConfig } from '../../model/slide-config.interface';
import { Subject, timer } from 'rxjs';
import { filter, map, switchMap, takeUntil } from 'rxjs/operators';

/**
* Component with clickable dots (small circles) to navigate between images inside the modal gallery.
* Component with configurable inline/plain carousel.
*/
@Component({
selector: 'ks-carousel',
styleUrls: ['carousel.scss'],
styleUrls: ['carousel.scss', '../image-arrows.scss'],
templateUrl: 'carousel.html',
host: {
// tabIndex: '0',
'(mouseenter)': 'pauseOnHover && stopCarousel()',
'(mouseleave)': 'pauseOnHover && autoPlay && playCarousel()',
'(keydown.arrowLeft)': 'keyboardNavigation && prevImage()',
'(keydown.arrowRight)': 'keyboardNavigation && nextImage()'
},
changeDetection: ChangeDetectionStrategy.OnPush
})
export class CarouselComponent extends AccessibleComponent implements OnInit, AfterContentInit, OnDestroy, OnChanges {
// @HostBinding('tabindex') tabindex = 0;

@HostListener('mouseenter')
onMouseEnter() {
if (!this.pauseOnHover) {
return;
}
this.stopCarousel();
}
@HostListener('mouseleave')
onMouseLeave() {
if (!this.pauseOnHover || !this.autoPlay) {
return;
}
this.playCarousel();
}
@HostListener('keydown.arrowLeft')
onKeyDownLeft() {
if (!this.keyboardNavigation) {
return;
}
this.prevImage();
}
@HostListener('keydown.arrowRight')
onKeyDownLRight() {
if (!this.keyboardNavigation) {
return;
}
this.nextImage();
}

/**
* Object of type `DotsConfig` to init DotsComponent's features.
* For instance, it contains a param to show/hide this component.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,131 +1,100 @@
// The MIT License (MIT)
//
// Copyright (c) 2017-2018 Stefano Cappa (Ks89)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

$curr-img-margin: 0;
$curr-img-animation-time: .8s;

$dots-bottom-position: 20px;

$nav-animation-time: 1s;
$nav-transition-time: .5s;
$nav-side-margin: 15px;
$nav-right-position: 5px;
$nav-left-position: 5px;

$figcaption-padding: 10px;
$figcaption-position: absolute;
$figcaption-bottom: 0;
$figcaption-left: 0;
$figcaption-right: 0;

#carousel-container {
display: flex;
flex-direction: row;
align-items: center;
justify-content: space-between;
}

#carousel-container #current-figure {
animation: fadein-visible 0.8s;
text-align: center;
}

#carousel-container #current-figure {
margin: 0;
position: relative;
}

#carousel-container #current-figure img {
max-width: 100%;
height: auto;
display: block;
}

#carousel-container #current-figure figcaption {
padding: 10px;
position: absolute;
bottom: 0;
left: 0;
right: 0;
}

#carousel-container #current-figure figcaption .description {
font-weight: bold;
text-align: center;
}

#carousel-container .nav-left {
position: absolute;
left: 5px;
top: 50%;
}

#carousel-container .nav-right {
position: absolute;
right: 5px;
top: 50%;
}

.arrow-image, .right-arrow-image, .left-arrow-image, .empty-arrow-image {
width: 30px;
height: 30px;
background-size: 30px;
}

.empty-arrow-image {
background: black;
opacity: 0;
}

.left-arrow-image {
background-image: url("data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iaXNvLTg4NTktMSI/PjxzdmcgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB4bWxuczp4bGluaz0iaHR0cDovL3d3dy53My5vcmcvMTk5OS94bGluayIgdmVyc2lvbj0iMS4xIiBpZD0iQ2FwYV8xIiB4PSIwcHgiIHk9IjBweCIgdmlld0JveD0iMCAwIDQ3Ny4xNzUgNDc3LjE3NSIgc3R5bGU9ImVuYWJsZS1iYWNrZ3JvdW5kOm5ldyAwIDAgNDc3LjE3NSA0NzcuMTc1OyIgeG1sOnNwYWNlPSJwcmVzZXJ2ZSIgd2lkdGg9IjUxMnB4IiBoZWlnaHQ9IjUxMnB4Ij48Zz48cGF0aCBkPSJNMTQ1LjE4OCwyMzguNTc1bDIxNS41LTIxNS41YzUuMy01LjMsNS4zLTEzLjgsMC0xOS4xcy0xMy44LTUuMy0xOS4xLDBsLTIyNS4xLDIyNS4xYy01LjMsNS4zLTUuMywxMy44LDAsMTkuMWwyMjUuMSwyMjUgICBjMi42LDIuNiw2LjEsNCw5LjUsNHM2LjktMS4zLDkuNS00YzUuMy01LjMsNS4zLTEzLjgsMC0xOS4xTDE0NS4xODgsMjM4LjU3NXoiIGZpbGw9IiNGRkZGRkYiLz48L2c+PGc+PC9nPjxnPjwvZz48Zz48L2c+PGc+PC9nPjxnPjwvZz48Zz48L2c+PGc+PC9nPjxnPjwvZz48Zz48L2c+PGc+PC9nPjxnPjwvZz48Zz48L2c+PGc+PC9nPjxnPjwvZz48Zz48L2c+PC9zdmc+");
opacity: 0.8;
transition: all 0.5s;
}

.left-arrow-image:hover {
transform: scale(1.2);
}

.right-arrow-image {
background-image: url("data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iaXNvLTg4NTktMSI/PjxzdmcgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB4bWxuczp4bGluaz0iaHR0cDovL3d3dy53My5vcmcvMTk5OS94bGluayIgdmVyc2lvbj0iMS4xIiBpZD0iQ2FwYV8xIiB4PSIwcHgiIHk9IjBweCIgdmlld0JveD0iMCAwIDQ3Ny4xNzUgNDc3LjE3NSIgc3R5bGU9ImVuYWJsZS1iYWNrZ3JvdW5kOm5ldyAwIDAgNDc3LjE3NSA0NzcuMTc1OyIgeG1sOnNwYWNlPSJwcmVzZXJ2ZSIgd2lkdGg9IjUxMnB4IiBoZWlnaHQ9IjUxMnB4Ij48Zz48cGF0aCBkPSJNMzYwLjczMSwyMjkuMDc1bC0yMjUuMS0yMjUuMWMtNS4zLTUuMy0xMy44LTUuMy0xOS4xLDBzLTUuMywxMy44LDAsMTkuMWwyMTUuNSwyMTUuNWwtMjE1LjUsMjE1LjUgICBjLTUuMyw1LjMtNS4zLDEzLjgsMCwxOS4xYzIuNiwyLjYsNi4xLDQsOS41LDRjMy40LDAsNi45LTEuMyw5LjUtNGwyMjUuMS0yMjUuMUMzNjUuOTMxLDI0Mi44NzUsMzY1LjkzMSwyMzQuMjc1LDM2MC43MzEsMjI5LjA3NXogICAiIGZpbGw9IiNGRkZGRkYiLz48L2c+PGc+PC9nPjxnPjwvZz48Zz48L2c+PGc+PC9nPjxnPjwvZz48Zz48L2c+PGc+PC9nPjxnPjwvZz48Zz48L2c+PGc+PC9nPjxnPjwvZz48Zz48L2c+PGc+PC9nPjxnPjwvZz48Zz48L2c+PC9zdmc+");
opacity: 0.8;
transition: all 0.5s;
}

.right-arrow-image:hover {
transform: scale(1.2);
}

#dots {
position: absolute;
bottom: 20px;
width: 100%;
}

.dots-container {
display: flex;
flex-direction: row;
justify-content: center;
margin-bottom: 30px;
}

.dots-container > .dot {
background: #fff;
border-radius: 5px;
height: 10px;
margin-left: 4px;
margin-right: 4px;
width: 10px;
cursor: pointer;
opacity: 0.5;
}

.dots-container > .dot:hover {
opacity: 0.9;
transition: all 0.5s ease;
transition-property: opacity;
}

.dots-container > .dot.active {
cursor: pointer;
opacity: 0.9;
}

@keyframes fadein-semi-visible05 {
from {
opacity: 0;
}
to {
opacity: 0.5;
}
}

@keyframes fadein-semi-visible09 {
from {
opacity: 0;
}
to {
opacity: 0.9;
> #current-figure {
animation: fadein-visible $curr-img-animation-time;
text-align: center;
margin: $curr-img-margin;
position: relative;

.nav {
animation: animatezoom $nav-animation-time;
cursor: pointer;
transition: all $nav-transition-time;
top: 50%;
position: absolute;

&:hover {
transform: scale(1.1);
}
}

> .nav-left {
@extend .nav;
left: $nav-left-position;

}

> .nav-right {
@extend .nav;
right: $nav-right-position;
}

> img {
max-width: 100%;
height: auto;
display: block;
}

> figcaption {
padding: $figcaption-padding;
position: $figcaption-position;
bottom: $figcaption-bottom;
left: $figcaption-left;
right: $figcaption-right;

& .description {
font-weight: bold;
text-align: center;
}
}

> #dots {
position: absolute;
bottom: $dots-bottom-position;
width: 100%;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export interface ImageLoadEvent {
*/
@Component({
selector: 'ks-current-image',
styleUrls: ['current-image.scss', 'current-image-arrows.scss', 'current-image-previews.scss'],
styleUrls: ['current-image.scss', '../image-arrows.scss', 'current-image-previews.scss'],
templateUrl: 'current-image.html',
changeDetection: ChangeDetectionStrategy.OnPush
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ $figcaption-bottom: 0;
$figcaption-left: 0;
$figcaption-right: 0;

$description-color: white;

:host {
display: flex;
flex-direction: column;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import { getIndex } from '../../utils/image.util';
*/
@Component({
selector: 'ks-previews',
styleUrls: ['previews.scss', 'previews-arrows.scss'],
styleUrls: ['previews.scss', '../previews-arrows.scss'],
templateUrl: 'previews.html',
changeDetection: ChangeDetectionStrategy.OnPush
})
Expand Down

0 comments on commit e934d9b

Please sign in to comment.