collocation-list.js 3.35 KB
/**
 * 星潮教室-星搭配分页加载
 * @author: wsl<shuiling.wang@yoho.cn>
 * @date: 2016/4/12
 */

var $ = require('jquery'),
    tip = require('../plugin/tip'),
    loading = require('../plugin/loading'),
    lazyLoad = require('yoho.lazyload'),
    ellipsis = require('yoho.mlellipsis'),
    stopLoading = false;

var page = 1;

ellipsis.init();

$('body').addClass('star-class-body');

function massageAJAX(page) {
    var $this, $title, $cont;

    loading.showLoadingMask();
    $.ajax({
        type: 'GET',
        url: '/guang/starclass/ajaxCollocation',
        data: {
            page: page
        },
        dataType: 'html',
        success: function(data) {
            stopLoading = false;

            if (data === ' ') {
                stopLoading = true;
                tip.show('没有更多内容了');
            }

            $('.collocation-list').append(data);

            // 限制标题字数
            $('.cont-area').each(function() {
                $this = $(this);
                $title = $this.find('.title');
                $cont = $this.find('.cont-txt');

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

            loading.hideLoadingMask();
            lazyLoad($('img.lazy'));
        },
        error: function() {
            tip.show('网络断开连接了~');
        }
    });
}

function scrollHandler() {
    if (!stopLoading && ($(window).scrollTop() + $(window).height() > $('body').height() - 100)) {
        stopLoading = true;
        page++;
        massageAJAX(page);
    }
}

// 分享成功
window.successShare = function() {
    $.ajax({
        type: 'POST',
        url: '/guang/starclass/forward',
        success: function(data) {
            var code = data.code;

            if (code === 200 && data.data > 0) {
                tip.show('分享成功,亲密度+10');
            }
        },
        error: function() {
            tip.show('网络断开连接了~');
        }
    });
};

$(window).scroll(function() {
    scrollHandler();
});

if ($('.collocation-list').find('li').length === 0) {
    massageAJAX(1);
}

// 星搭配收藏请求
$(document).on('touchstart', '.collection', function(event) {
    var $that = $(this),
        $icon = $that.find('.collected-ico');

    var type;

    event.stopPropagation();

    if ($icon.hasClass('collected')) {
        type = 'del';
    } else {
        type = 'fav';
    }


    $.ajax({
        type: 'POST',
        url: '/guang/starclass/setFavorite',
        data: {
            articleId: $that.parents('li').attr('articleId'),
            type: type
        },
        success: function(data) {
            var code = data.code;

            if (code === 200) {
                if ($icon.hasClass('collected')) {
                    $icon.removeClass('collected');
                } else {
                    $icon.addClass('collected');
                }

            }

            if (code === 201) {
                if ($('#collocation-link').length <= 0) {
                    $('body').append('<a href=\'' + data.data + '\' style="display:none;" id="collocation-link">' +
                    '<span class="collocation-link"></span></a>');
                }

                $('.collocation-link').click();
            }
        },
        error: function() {
            tip.show('网络断开连接了~');
        }
    });
});