info.js 4.74 KB
/**
 * 资讯相关API
 * @author: xuqi<qi.xu@yoho.cn>
 * @date: 2015/10/10
 */

var $ = require('jquery'),
        Hammer = require('yoho.hammer'),
        ellipsis = require('mlellipsis'),
        lazyLoad = require('yoho.lazyload');

var tip = require('../plugin/tip');
var loading = require('../plugin/loading');

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

var searching = false;

ellipsis.init();

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

/**
 * 设置指定资讯项的Lazyload和文字截取
 * @params $infos 资讯项
 */
function setLazyLoadAndMellipsis($infos) {
    lazyLoad($infos.find('img.lazy'));

    $infos.each(function() {
        var $this = $(this),
                $title = $this.find('.info-title'),
                $text = $this.find('.info-text');

        $title[0].mlellipsis(2);
        $text[0].mlellipsis(2);
    });
}

/**
 * 初始化资讯列表事件绑定
 * @params $container 逛资讯列表容器
 */
function initInfosEvt($container) {
    var cHammer = new Hammer($container[0]);

    cHammer.on('tap', function(e) {
        var $this = $(e.target),
                opt = 'ok',
                $likeBtn,
                $collectBtn,
                $info;

        $likeBtn = $this.closest('.like-btn');
        if ($likeBtn.length > 0) {
            if ($likeBtn.hasClass('like')) {
                opt = 'cancel';
            }

            $info = $this.closest('.guang-info');
            $.ajax({
                type: 'POST',
                url: '/guang/opt/praiseArticle',
                data: {
                    id: $info.data('id'),
                    opt: opt
                },
                success: function(data) {
                    var code = data.code;

                    if (code === 200) {
                        $likeBtn.next('.like-count').text(data.data);

                        //切换点赞状态
                        $likeBtn.toggleClass('like');
                    }
                },
                error: function() {
                    tip.show('网络断开连接了~');
                }
            });
        }

        // 2015/11/03 hf: 增加APP里的收藏资讯功能. 写得不对的地方还请大神修改!
        $collectBtn = $this.closest('.collect-btn');
        if ($collectBtn.length > 0) {
            if ($collectBtn.hasClass('collected')) {
                opt = 'cancel';
            }

            $info = $this.closest('.guang-info');
            $.ajax({
                type: 'POST',
                url: '/guang/opt/collectArticle',
                data: {
                    id: $info.data('id'),
                    opt: opt
                },
                success: function(data) {
                    if (data.code && data.code === 200) {

                        //切换收藏状态
                        $collectBtn.toggleClass('collected');
                    }
                },
                error: function() {
                    tip.show('网络断开连接了~');
                }
            });
        }
    });

    setLazyLoadAndMellipsis($container.find('.guang-info'));
}

/**
 * 资讯LoadMore
 */
function loadMore($container, opt) {
    var num;

    if (searching) {
        return;
    }

    if (opt.end) {
        return;
    }

    if (opt.page === 1) {

        //显示loading
        loading.showLoadingMask();
    }

    num = $container.find('.guang-info').length;
    searching = true;
    $.ajax({
        type: 'GET',
        url: ' /guang/index/page',
        data: opt,
        success: function(data) {
            var $newItems;

            if (data === ' ') {
                opt.end = true;
                searching = false;

                //
                $loading.addClass('hide');
                $noMore.removeClass('hide');

                return;
            }

            $container.append(data);

            if (num > 0) {
                $newItems = $container.find('.guang-info:gt(' + (num - 1) + ')');
            } else {
                $newItems = $container.find('.guang-info');
            }

            setLazyLoadAndMellipsis($newItems);

            if (opt.page === 1) {
                loading.hideLoadingMask();

                $loading.removeClass('hide');//显示空屏加载时hide的隐藏

                window.rePosFooter();//插入内容后重新计算底部位置
            }

            opt.page++;

            searching = false;
        },
        error: function() {
            tip.show('网络断开连接了~');
            searching = false;
        }
    });
}

exports.initInfosEvt = initInfosEvt;
exports.setLazyLoadAndMellipsis = setLazyLoadAndMellipsis;
exports.loadMore = loadMore;