| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- import { mergeDeep } from "@vci/helper/src/object";
- import { isElement } from "@vci/helper/src/element";
- import EventDispatcher from "@vci/helper/src/EventDispatcher";
- export default class VciAutoplay extends EventDispatcher {
- constructor(option) {
- super();
- this.option = mergeDeep({
- el: null,
- page: 1,
- pages: 0,
- durationInterval: 1e3,
- enableAlternateReverse: false
- }, option);
- this.init();
- }
- init() {
- this.alias();
- this.isPaused = true;
- this.increment = 1;
- this.inter = -1;
- this.el && this.mount(this.el);
- }
- alias() {
- Object.defineProperty(this, "el", {
- get: () => this.option.el,
- set: el => { this.option.el = el; }
- });
- Object.defineProperty(this, "durationInterval", {
- get: () => this.option.durationInterval,
- set: durationInterval => {
- this.adaptModifyProps(() => this.option.durationInterval = durationInterval);
- }
- });
- Object.defineProperty(this, "enableAlternateReverse", {
- get: () => this.option.enableAlternateReverse,
- set: enableAlternateReverse => {
- this.adaptModifyProps(() => this.option.enableAlternateReverse = enableAlternateReverse);
- }
- });
- Object.defineProperty(this, "page", {
- get: () => this.option.page,
- set: page => {
- if(isNaN(+page)) console.warn(`page设置失败, page[${page}]不是一个数字`);
- else this.adaptModifyProps(() => {
- this.option.page = Math.max(Math.min(+page, this.pages), 1);
- this.dispatchEvent("page-change", { page: this.option.page, pages: this.pages });
- });
- }
- });
- Object.defineProperty(this, "pages", {
- get: () => this.option.pages,
- set: pages => {
- if(isNaN(+pages)) console.warn(`pages设置失败, pages[${pages}]不是一个数字`);
- else this.adaptModifyProps(() => {
- this.option.pages = +pages;
- this.option.page = Math.max(Math.min(this.option.page, this.pages), 1);
- this.dispatchEvent("pages-change", { page: this.page, pages: this.pages });
- });
- }
- });
- Object.defineProperty(this, "enabled", {
- get: () => this.option.pages > 1
- });
- }
- mount(el) {
- if(!isElement(el)) throw new Error(`挂载元素时${el}不是一个HTML元素`);
- this.el = el;
- !this._pause && (this._pause = this.pause.bind(this));
- !this._resume && (this._resume = this.resume.bind(this));
- !this._wheel && (this._wheel = this.wheel.bind(this));
- this.el.addEventListener("pointermove", this._pause);
- this.el.addEventListener("pointerleave", this._resume);
- this.el.addEventListener("wheel", this._wheel);
- return this;
- }
- detach() {
- if(this.el) {
- this.el.removeEventListener("pointermove", this._pause);
- this.el.removeEventListener("pointerleave", this._resume);
- this.el.removeEventListener("wheel", this._wheel);
- this.el = null;
- }
- return this;
- }
- wheel(e) {
- clearTimeout(this.interWheel);
- this.interWheel = setTimeout(() => e["wheelDelta"] > 0 ? this.sub() : this.add(), 50);
- }
- pause() {
- this.isPaused = true;
- clearTimeout(this.inter);
- return this;
- }
- resume() {
- this.play();
- return this;
- }
- play() {
- if(!this.enabled) return this;
- this.isPaused = false;
- this.inter = setTimeout(() => {
- if(this.enableAlternateReverse) {
- this.option.page += this.increment;
- if(this.option.page > this.pages) {
- this.increment = -1;
- this.option.page = this.pages - 1;
- }
- if(this.option.page < 1) {
- this.increment = 1;
- this.option.page = 2;
- }
- } else {
- this.option.page++;
- this.option.page > this.pages && (this.option.page = 1);
- }
- this.dispatchEvent("page-change", { page: this.option.page, pages: this.pages });
- this.play();
- }, this.durationInterval);
- return this;
- }
- start() {
- this.play();
- return this;
- }
- stop() {
- this.pause();
- this.option.page = 1;
- this.increment = 1;
- return this;
- }
- adaptModifyProps(handle) {
- const _isPaused = this.isPaused;
- !_isPaused && this.pause();
- handle && handle();
- !_isPaused && this.play();
- }
- adaptVue(vm, keyPage = "page") {
- vm.$watch(keyPage, page => this.option.page = Math.max(Math.min(+page, this.pages), 1));
- vm.$once("hook:beforeDestroy", () => this.destroy());
- return this;
- }
- add() {
- this.option.page++;
- if(this.option.page > this.pages) this.option.page = this.pages;
- this.dispatchEvent("page-change", { page: this.option.page, pages: this.pages });
- }
- sub() {
- this.option.page--;
- if(this.option.page < 1) this.option.page = 1;
- this.dispatchEvent("page-change", { page: this.option.page, pages: this.pages });
- }
- jump(page) {
- this.option.page = Math.min(Math.max(page, 1), this.pages);
- this.dispatchEvent("page-change", { page: this.option.page, pages: this.pages });
- }
- destroy() {
- super.destroy();
- this
- .stop()
- .detach();
- }
- }
|