family-coinMall.page.js 3.44 KB
import 'home/family-coinMall.page.css';
import $ from 'yoho-jquery';
import Page from 'yoho-page';
import tip from 'plugin/tip';
import tabRender from 'home/coin-get-list.hbs';
import lazyLoad from 'yoho-jquery-lazyload';

class IconMall extends Page {
    constructor() {
        super();

        this.selector = {
            $tabItem: $('.tab .tab-item'),
            $defaultTabItem: $('.tab .tab-item:first'),
            $coinMallC: $('.coin-mall-c'),
            $tabFixed: $('.tab-fixed'),
            $yohonowTab: $('#yohonow-tab'),
            $marsTab: $('#mars-tab')
        };

        this.view = {
            tabRender
        };

        this.page = 1;
        this.limit = 10;
        this.type = 'now';
        this.loading = false;
        this.end = false;

        this.init();
    }

    init() {
        this.bindEvents();
        this.defaultChosen();
        this.scroll();
        this.getList();
    }

    scrollHandler() {
        if (($(window).scrollTop() + $(window).height() >= $(document).height() * 0.8)) {
            this.doMore();
        }
    }

    doMore() {
        if (!this.end && !this.loading) {
            this.page++;
            this.getList();
        }
    }

    getList() {
        this.loading = true;
        this.ajax({
            url: '/home/family/coinMall/getList',
            data: {
                type: this.type,
                page: this.page
            }
        }).then(result => {
            if (result && result.list.length > 0) {
                if (this.type === 'now') {
                    this.selector.$yohonowTab.append(this.view.tabRender(result));
                }
                if (this.type === 'mars') {
                    this.selector.$marsTab.append(this.view.tabRender(result));
                }
                this.loading = false;
            } else {
                this.end = true;
                tip.show('没有更多数据了~~~');
            }
            lazyLoad($('img.lazy'));
        }).catch(error => {
            console.error(error);
        });
    }

    scroll() {
        $(window).scroll(() => {
            let $fixTop = this.selector.$tabFixed.offset().top;
            let $scrollTop = $(window).scrollTop();

            if ($scrollTop >= $fixTop) {
                this.selector.$tabFixed.find('.tab').addClass('fixed');
            } else {
                this.selector.$tabFixed.find('.tab').removeClass('fixed');
            }

            $(window).scroll(() => {
                window.requestAnimationFrame(this.scrollHandler.bind(this));
            });
        });
    }

    bindEvents() {
        this.selector.$tabItem.on('click', this.tabItem.bind(this));
    }

    defaultChosen() {
        let defaultId = this.selector.$defaultTabItem.attr('id');

        this.selector.$defaultTabItem.addClass('active');
        this.selector.$coinMallC.attr('id', defaultId);
    }

    tabItem(e) {
        let $this = $(e.currentTarget);
        let $thisId = $this.attr('id');

        $(`#${$thisId}-tab`).show().siblings('.tab-item-c').hide();
        this.selector.$coinMallC.removeAttr('id');
        this.selector.$coinMallC.attr('id', $thisId);
        $this.addClass('active').siblings('div').removeClass('active');
        if ($this.attr('id') === 'yohonow') {
            this.type = 'now';
        }
        if ($this.attr('id') === 'mars') {
            this.type = 'mars';
        }
        this.page = 1;
        this.getList();
    }
}

$(() => {
    new IconMall();
});