index.page.js 2.92 KB
/**
 * 逛首页
 * @author: xuqi<qi.xu@yoho.cn>
 * @date: 2015/10/10
 */
require('guang/index.page.css');



let $ = require('yoho-jquery');

let info = require('./info');
let loadMore = info.loadMore;

let $loadMoreInfo = $('#load-more-info');
let $loading = $(''),
    $noMore = $('');

let winH = $(window).height();

let $infoList = $('#info-list'),
    $infos = $infoList.children('.info-list'),
    $nav = $('#guang-nav'),
    $curNav = $nav.children('.focus'),
    curType = $curNav.data('type');

let getDynamicData = require('./list-dynamic');

let state = {};

require('../common');

if ($loadMoreInfo.length > 0) {
    $loading = $loadMoreInfo.children('.loading');
    $noMore = $loadMoreInfo.children('.no-more');
}

info.initSwiper(curType);

info.initInfosEvt($infoList);

// 初始化各Nav下资讯加载的状态
(function() {
    let gender = $('#gender').val();

    $nav.children('.guang-nav-item').each(function() {
        let type = $(this).data('type'),
            focus = $(this).hasClass('focus');

        state[type] = {
            page: focus ? 2 : 1,
            gender: gender,
            type: type,
            end: false
        };
    });

    getDynamicData.getDynamicData();
}());
$nav.bind('contextmenu', function() {
    return false;
});
$nav.on('touchend touchcancel', function(e) {

    let $this = $(e.target).closest('.guang-nav-item'),
        $content,
        index;

    if ($this.hasClass('focus')) {
        return;
    }

    e.preventDefault();
    index = $this.index();

    $this.addClass('focus');
    $curNav.removeClass('focus');

    $content = $infos.eq(index);

    $curNav = $this;
    curType = $this.data('type');

    // 当未加载数据时去请求数据
    if (state[curType].page === 1) {

        // 无数据时隐藏正在加载和没有更多字样
        $loading.addClass('hide');
        $noMore.addClass('hide');

        state[curType].isTab = true;
        loadMore($content, state[curType]);
    } else {

        // 重置当前Tab的load-more
        if (state[curType].end) {
            $loading.addClass('hide');
            $noMore.removeClass('hide');
        } else {
            $loading.removeClass('hide');
            $noMore.addClass('hide');
        }
    }

    $infos.not('.hide').addClass('hide');
    $content.removeClass('hide');

    if (state[curType].page === 1) {
        window.rePosFooter();// 进入空内容时重新定位footer位置
    }
});

function scrollHandler() {
    let $c = $infos.not('.hide');

    if ($(window).scrollTop() + winH >= $(document).height() - 0.25 * $c.height()) {
        loadMore($c, state[curType]);
    }
}

// srcoll to load more
$(document).scroll(function() {
    window.requestAnimationFrame(scrollHandler);
});

$nav.on('touchstart', function(e) {
    let target = e.target || e.srcElement;

    target.className = 'bytouch ' + target.className;
}).on('touchend touchcancel', function() {
    $nav.find('li').removeClass('bytouch');
});