saunter-home.js 4.8 KB
var $ = require('yoho.jquery'),
    lazyLoad = require('yoho.lazyload'),
    Mustache = require('yoho.mustache'),
    Mlellipsis = require('mlellipsis'),
    Swiper = require('yoho.iswiper');

var $nav = $('#saunter-nav'),
    $msgList = $('#msg-list'),
    $loadMore = $('#load-more-info'),
    $noMore = $loadMore.children('.no-more'),
    $loading = $loadMore.children('.loading'),
    loadH = $loadMore.height(),
    end = false,
    page = 1,
    loading = false, //正在ajax标志
    winH = $(window).height(),

    query = $('#query').val(), //无author时
    gender = $('#gender').val(),
    clientType = $('#client-type').val(),

    udid = $('#client-ip').val(),

    tpl,
    swiper;

//判断是否微信内置浏览器
function isWeixin() {
    var ua = navigator.userAgent.toLowerCase();
    var mm = ua.match(/MicroMessenger/i);

    if (mm && mm.toString() === 'micromessenger') {
        return true;
    } else {
        return false;
    }
}

//微信浏览器隐藏头部显示
if (isWeixin()) {
    $('.yoho-header').addClass('hide');
}

require('./wxshare')();
require('yoho.pjax');

Mlellipsis.init();

//init banner swiper
swiper = new Swiper('.swiper-container', {
    pagination: '.swiper-pagination',
    lazyLoading: true,
    lazyLoadingInPrevNext: true,
    autoplayDisableOnInteraction: false
});

lazyLoad();

//相关文章截取文字
function mleTxt() {
    $('.tag-title, .tag-text').each(function() {
        this.mlellipsis(2);
    });
}

setTimeout(function() {
    mleTxt();
}, 0);

//读取模板
$.get('/common/tagtpl', function (data) {
    tpl = '{{#msgs}}' + data + '{{/msgs}}';
    Mustache.parse(tpl);
});

//下拉加载数据
function loadMore() {
    var $focusNav = $nav.find('li.focus'),
        type = $focusNav.data('type'),
        num = $msgList.find('.tag-content').length;

    //-30,for chrome计算差
    if (loading || end || $(window).scrollTop() + $(window).height() < $(document).height() - loadH - 30) {
        return;
    }

    loading = true;
    $loading.removeClass('hide');
    $noMore.addClass('hide');

    $.ajax({
        type: 'GET',
        url: '/tags/get',
        data: {
            max_sort_id: type,
            page: page + 1,
            query: query,
            gender: gender,
            client_type: clientType
        }
    }).then(function (data) {
        var res, msgs, html,
            $newContent;

        if (data.code === 200) {
            res = data.data;
            if (res.end) {
                end = true;
                $loading.addClass('hide');
                $noMore.removeClass('hide');
            }

            msgs = res.infos;
            html = Mustache.render(tpl, {
                msgs: msgs
            });
            $msgList.append(html);

            page++;

            //lazyload & mlellipsis the items appended
            $newContent = $msgList.children('.tag-content:gt(' + (num - 1) + ')');
            lazyLoad($newContent.find('img.lazy'));
            $newContent.find('.tag-title, .tag-text').each(function() {
                this.mlellipsis(2);
            });
        }
        loading = false;
    });
}

$(window).scroll(loadMore);

//资讯收藏和取消收藏
function bindPriseTap() {
    $('.msg-list').delegate('.like-btn', 'touchstart', function (e) {
        var $cur = $(e.currentTarget),
            $info = $cur.closest('.tag-content'),
            id = $info.data('id'),
            url = '/guang/info/collect',
            isLogin = $('#is-login').val(),
            $loginTip = $('#login-tip'),
            type = 'fav';

        isLogin = isLogin ? isLogin : 'N';

        if (isLogin === 'Y') {

            if ($cur.hasClass('like')) {
                type = 'cancel';
            }

            $.ajax({
                type: 'GET',
                url: url,
                data: {
                    id: id,
                    type: type
                }
            }).then(function (data) {
                if (data.code === 200) {

                    //更新点赞数
                    //$cur.next('span.like-count').text(data.data);

                    //收藏与取消收藏
                    $cur.toggleClass('like');
                } else if (data.code === 400) {

                    //提示登录信息
                    $loginTip.fadeIn(500, function () {
                        setTimeout(function () {
                            $loginTip.fadeOut(500);
                        }, 1000);
                    });
                }
            });
        }
    }).bind('click', function(e) {
        if (isLogin === 'Y') {

            //阻止链接跳转
            e.preventDefault();
        }
    });
}

bindPriseTap();

//pjax
// $(document).pjax('.pjax-link', '#pjax-container');
// $(document).on('pjax:end', function() {
//     lazyLoad();
//     mleTxt();
//     //重新绑定prise
//     bindPriseTap();
// });