Slideshow.js 4.5 KB
//*
// 图片轮播
// */

import Loger from "../Loger";
import Ape from "./Ape";
import $ from "jquery";
import ClassDataProxy from "proxy/ClassDataProxy";
import MessageTypes from "../MessageTypes";

const SlideList = `
<li class="slidesMap"><img class="slidesImg" src="{_imgList}" data="{_id}" alt=""></li>
`;
let loger = Loger.getLoger('PC-Slideshow');
class Slideshow extends Ape {
    constructor() {
        super();
        this._timer = null;
        this.defaultList = ['images/xdybg.png',
            'images/bg.jpg',
            'images/xdybg.png',
            'images/bg.jpg',
            'images/xdybg.png'
        ];
        this.dataList = [];
        this.dataLength = 0;
        this.isLocalStorage();
        this.addEvent();
        this.init();
    }

    init() {
        $(window).on('resize',this._imgReload.bind(this));

        $(".slidesImg").on('load', this._onLoadImage.bind(this));
        $('#dots').on('mouseenter',this._mouseenterHandler.bind(this));
        $('#dots').on('mouseleave',this._mouseleaveHandler.bind(this));
        $('#motionMapBox').on('click','.slidesMap',this._slidesMapHandler.bind(this));
    }

    addEvent() {

    }
    isLocalStorage(){

        let that = this;
        this.detailPage(function (_data) {
            if(_data){
                let _dataList = _data.returnData.data;
                let motionMapBox = $('#motionMapBox');
                if(_dataList){

                    motionMapBox.empty();
                    for(let i in _dataList){
                        let _adminList = that._format(SlideList,{
                            _imgList:_dataList[i].path,
                            _id:_dataList[i].businessId
                        })
                        motionMapBox.append(_adminList)
                    }
                    that.dataLength = _dataList.length;
                }else{
                    motionMapBox.empty();
                    for(let i = 0;i< that.defaultList.length;i++){
                        let _adminList = that._format(SlideList,{
                            _imgList:_dataList[i]
                        })
                        motionMapBox.append(_adminList)
                    }
                    that.dataLength = that.defaultList.length;
                }
                that.slideImg();
                that._imgReload();
            }
        });

    }
    detailPage(callback){
        let _url = ClassDataProxy.locationProtocol + ClassDataProxy.locationProt + '/upload/getImgBySiteId';
        let that = this;
        $.ajax({
            type: "POST",
            url: _url,
            data:{siteId:ClassDataProxy.siteId},
            headers: {
                siteId:ClassDataProxy.siteId,
                'token':ClassDataProxy.token
            },
            success:function(_data){
               /* if(_data && _data.code == 200){
                    loger.log('获取首页图片信息',_data)
                }*/
                if(_data && _data.code == 200){
                    if(callback){
                        callback(_data);
                    }
                }else{
                    if(callback){
                        callback(null);
                    }
                }

            },
            error:function(error){
               // console.log(error,'获取首页图片信息失败')
                if(callback){
                    callback(null);
                }
            }

        })
    }
    _onLoadImage(){
      //  this._imgReload();
    }
    slideImg(){
        let _num = 0;
        let that = this;
        clearInterval(this._timer);
        this._timer = setInterval(function(){
            $('.slidesMap').eq(_num).show().siblings().hide();
            _num++;
            if(_num > that.dataLength - 1){
                _num = 0;
            }
        },3000);//切换时间
    }
    _mouseenterHandler(){
        clearInterval(this._timer);
    }
    _mouseleaveHandler(){
      //  this.slideImg();
    }
    _slidesMapHandler(evt){
        let _id = $(evt.currentTarget).find('.slidesImg').attr('data');
        this._emit(MessageTypes.CLICK_IMGINFO,{id:_id});
    }
    _imgReload(){
        let _windowWidth = $("body").width();

        $('.slidesMap').css({width:_windowWidth + "px"});
        $('.slidesImg').css({"background-size":'auto 100%'});

        $('#motionMapBox').width($(".slidesMap").length * _windowWidth)
    }
    //工具类
    _format(str, obj) {
        return str.replace(/\{(\w+)\}/g, function (match, group, index) {
            return obj[group];
        });
    };
}
export default Slideshow;