(() => {
'use strict';
class SwitchImage {
#constProperties = [
{prop: 'stage', value: document.getElementById('switchPanelStage')},
{prop: 'layer0', value: document.createElement('div')},
{prop: 'layer1', value: document.createElement('div')},
{prop: 'layerNav', value: document.createElement('div')},
{prop: 'markPause', value: './image/panel_pause.svg'},
{prop: 'markPlay', value: './image/panel_play.svg'},
{prop: 'markNavOn', value: './image/panel_ball_on.svg'},
{prop: 'markNavOff', value: './image/panel_ball_off.svg'},
{prop: 'buttonPause', value: document.createElement('span')},
{prop: 'buttonPlay', value: document.createElement('span')},
{prop: 'imageChangeInterval', value: 4000},
{prop: 'slideWidth', value: 50},
{prop: 'slideTime', value: 600},
{prop: 'swipeShreshold', value: 50},
];
#loopTimeoutId = null;
#isBusy = false;
#pausePlayFlag = true;
#focusFlag = true;
#valueIndex = 0;
#slideDirection = 1;
#swipeStartX = 0;
#isSwiping = false;
#values = [];
constructor(values) {
this.#values = values;
this.#constProperties.forEach((obj) => Object.defineProperty(this, obj.prop, {value:obj.value, writable:false}));
this.#init();
}
#init() {
this.stage.appendChild(this.layer0);
this.stage.appendChild(this.layer1);
this.stage.appendChild(this.layerNav);
this.layer0.innerHTML = this.#values[0];
this.layer1.innerHTML = this.#values[0];
this.layer1.animate(
[{opacity: 0}],
{delay:0,duration:0, easing:'linear',fill:'forwards', },
);
this.#preload();
this.#initNav();
this.#initSwipe();
this.#navOn();
this.#autoSlide();
}
#shuffle(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
return array;
}
#preload() {
for (let i = this.#values.length - 1; i >= 0; i--) {
setTimeout(() => {
this.layer1.innerHTML = this.#values[i];
},10);
}
}
#initNav() {
this.#values.map((x,index) => {
const span = document.createElement('span');
span.addEventListener('click',() => {
if (this.#isBusy) {
return;
}
this.#slideAction(index);
});
span.innerHTML = '<img src="' + this.markNavOff + '"><img src="' + this.markNavOn + '">';
this.layerNav.appendChild(span);
});
const pauseImg = new Image();
pauseImg.src = this.markPause;
this.buttonPause.appendChild(pauseImg);
this.layerNav.appendChild(this.buttonPause);
const playImg = new Image();
playImg.src = this.markPlay;
this.buttonPlay.appendChild(playImg);
this.layerNav.appendChild(this.buttonPlay);
this.buttonPlay.style.display = 'none';
this.buttonPause.addEventListener('click',() => this.#setPause());
this.buttonPlay.addEventListener('click',() => this.#setPlay());
}
#initSwipe() {
this.layer1.addEventListener('click', (e) => { e.preventDefault();});
this.layer1.addEventListener('mousedown', (e) => { this.#swipeStart(e.clientX); });
this.layer1.addEventListener('mouseup', (e) => { this.#swipeEnd(e.target, e.clientX); });
this.layer1.addEventListener('touchstart', (e) => { this.#swipeStart(e.touches[0].clientX); });
this.layer1.addEventListener('touchend', (e) => { this.#swipeEnd(e.target, e.changedTouches[0].clientX); });
}
#swipeStart(positionX) {
if (this.#isBusy) {
return;
}
this.#swipeStartX = positionX;
this.layer1.style.cursor = 'grabbing';
this.#isSwiping = true;
}
#swipeEnd(targetElm,positionX) {
if (this.#isSwiping == false) {
return;
}
if (this.#swipeStartX - positionX > this.swipeShreshold) {
this.#slideAction(this.#withinRange(this.#valueIndex + 1));
} else if (this.#swipeStartX - positionX < -this.swipeShreshold) {
this.#slideDirection = -1;
this.#slideAction(this.#withinRange(this.#valueIndex - 1));
} else {
let attrLink = null;
let attrTarget = null;
let targetElm2 = targetElm;
for (let i = 0; i < 9; i++) {
targetElm2 = targetElm2.parentNode;
if (targetElm2 == this.layer1) {
break;
}
attrLink = targetElm2.getAttribute('href');
if (attrLink) {
attrTarget = targetElm2.getAttribute('target');
break;
}
}
if (attrLink) {
if (attrTarget) {
window.open(attrLink, attrTarget);
} else {
window.location.href = attrLink;
}
return;
}
}
this.layer1.style.cursor = '';
this.#isSwiping = false;
}
#setPause() {
this.#pausePlayFlag = false;
this.buttonPause.style.display = 'none';
this.buttonPlay.style.display = 'block';
}
#setPlay() {
this.#pausePlayFlag = true;
this.buttonPause.style.display = 'block';
this.buttonPlay.style.display = 'none';
}
#slideForward() {
if (this.#isBusy) {
return;
}
this.#isBusy = true;
this.#navOn();
this.layer1.innerHTML = this.#values[this.#valueIndex];
this.layer1.animate(
[
{opacity: 0,transform:'translateX(' + (this.slideWidth * this.#slideDirection) + 'px)'},
{opacity: 1,transform:'translateX(0px)'},
],
{delay:0,duration:this.slideTime, easing:'ease-out',fill:'forwards', },
);
setTimeout(() => {
this.layer0.innerHTML = this.#values[this.#valueIndex];
this.layer1.animate(
[{opacity: 0}],
{delay:100,duration:0, easing:'linear',fill:'forwards', },
);
this.#slideDirection = 1;
setTimeout(() => {
this.#isBusy = false;
},100);
},this.slideTime);
}
#autoSlide() {
this.#loopTimeoutId = setTimeout(() => {
if (this.#pausePlayFlag && this.#focusFlag) {
this.#slideAction();
} else {
this.#autoSlide();
}
}, this.imageChangeInterval - this.slideTime);
}
#slideAction(num = this.#valueIndex + 1) {
clearTimeout(this.#loopTimeoutId);
this.#valueIndex = this.#withinRange(num);
this.#slideForward();
this.#autoSlide();
}
#navOn() {
const spans = this.layerNav.querySelectorAll('span');
spans.forEach((elm) => elm.classList.remove('on'));
spans[this.#valueIndex].classList.add('on');
}
#withinRange(nextNumber) {
const num = (nextNumber >= 0) ? nextNumber : this.#values.length - 1;
return num % this.#values.length;
}
}
new SwitchImage(
[
'<article><picture><source srcset="./image/panel01b.webp" media="(width < 700px)"><img src="./image/panel01.webp" draggable="false"></picture><a class="button1" href="https://ao-system.net/" target="_blank"><img src="./image/button1.svg"></a></article>',
'<article><picture><source srcset="./image/panel02b.webp" media="(width < 700px)"><img src="./image/panel02.webp" draggable="false"></picture><a class="button2" href="https://www.aosystem.co.jp/" target="_blank"><img src="./image/button2.svg"></a></article>',
'<article><picture><source srcset="./image/panel03b.webp" media="(width < 700px)"><img src="./image/panel03.webp" draggable="false"></picture><a class="button3" href="https://www.aosystem.co.jp/" target="_blank"><img src="./image/button2.svg"></a></article>',
'<article><picture><source srcset="./image/panel04b.webp" media="(width < 700px)"><img src="./image/panel04.webp" draggable="false"></picture><a class="button1" href="https://www.google.com/" target="_blank"><img src="./image/button3.svg"></a></article>',
'<article><picture><source srcset="./image/panel05b.webp" media="(width < 700px)"><img src="./image/panel05.webp" draggable="false"></picture></article>',
'<article><picture><source srcset="./image/panel06b.webp" media="(width < 700px)"><img src="./image/panel06.webp" draggable="false"></picture></article>',
]
);
})();