Skip to content

Instantly share code, notes, and snippets.

@rezaerami
Created February 15, 2020 09:06
Show Gist options
  • Save rezaerami/0d1b53d7a2a55eb9ccf88b9732d0d17c to your computer and use it in GitHub Desktop.
Save rezaerami/0d1b53d7a2a55eb9ccf88b9732d0d17c to your computer and use it in GitHub Desktop.
logic of updating parent and siblings
class SliderCore {
config = {};
sliderDots = null;
sliderArrow = null;
activeIndex = 0;
constructor(config){
this.setConfig(config);
this.initialize()
}
setConfig = config => {
this.config = config;
}
getConfig = () => {
return this.config
}
setActiveIndex = activeIndex => {
this.activeIndex = activeIndex;
this.update();
}
getActiveIndex = () => {
return this.activeIndex;
}
initialize = () => {
this.sliderDots = new SliderDots({core: this});
this.sliderArrow = new SliderArrow({core: this});
}
update = () => {
this.sliderDots.update();
}
}
class SliderDots {
core = null;
constructor(params){
const {core} = params;
this.setCore(core);
}
setCore = core => {
this.core = core;
}
getCore = () => {
return this.core
}
update = () => {
this.activatePage();
}
paginate = page => {
this.core.setActiveIndex(page * this.core.config.perSlide);
}
activatePage = () => {
console.log(this.core.activeIndex)
}
}
class SliderArrow {
core = null;
constructor(params){
const {core} = params;
this.setCore(core);
}
setCore = core => {
this.core = core;
}
getCore = () => {
return this.core
}
next = () => {
const activeIndex = this.core.activeIndex + 1;
this.goToSlide(activeIndex);
}
prev = () => {
const activeIndex = this.core.activeIndex - 1;
this.goToSlide(activeIndex);
}
goToSlide = activeIndex => {
this.core.setActiveIndex(activeIndex);
}
}
const config = {
perSlide: 3,
}
const sliderCore = new SliderCore(config);
sliderCore.sliderArrow.next()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment