ps.js 7.03 KB
/**
 * plus+star页js
 * @author: xuqi(qi.xu@yoho.cn)
 * @date;2015/4/16
 */

var $ = require('jquery'),
    Mustache = require('mustache'),
    Fastclick = require('fastclick'),
    ellipsis = require('mlellipsis');

require('lazyload');

/**
 * 初始化页面功能
 */
exports.init = function() {
    $(function() {
        var $intro = $('#intro'),
            $infoContent = $('#info-content'),
            $loadMore = $('#load-more-info'),
            $loadStatus = $loadMore.children('.status'),
            $loading = $loadStatus.filter('.loading'),
            $noMore = $loadStatus.filter('.no-more'),
            loadMoreH = $loadMore.height(),
            winH = $(window).height(),
            dataEnd = false,
            canScroll = true,
            tpl;
        //登录提示变量
        var $loginTip = $('#login-tip'),
    		winW,
    		tipH,
    		tipW;
        //资讯加载
        var infoPage = 1,
            //从页面获取数据
            brandName = $('#brand-name').text(),
            gender = $('#gender').val(),
            clientType = $('#client-type').val();
        //fastclick
        Fastclick(document.body);
        
        //获取相关资讯模板
         $.get('/common/articletpl', function(data) {
            tpl = data.data;
            Mustache.parse(tpl); //pre-compile and cache template
        });

        //文字截取
        ellipsis.init();
        setTimeout(function() {
            $intro.mlellipsis(3); //品牌介绍
            $('.info-block-content').each(function() { //相关文章
                $(this).mlellipsis(2);
            });
        }, 0);

        //lazyload
        $('img.lazy').lazyload();

        //显示品牌介绍所有文字
        function showMoreIntro() {
            var intro = $intro.attr('_title');

            $intro.text(intro);

            $('#more-intro .more').css('visibility', 'hidden');
        }

        $('#more-intro').delegate('.more', 'click', function() {
            showMoreIntro();
        });

        //下拉加载更多
        $(window).scroll(function() {
            var setting;

            if (!canScroll) {
                return;
            }

            if ($(window).scrollTop() + winH >= $(document).height() - loadMoreH) {
                //loadData
                if (!dataEnd) {
                    setting = {
                        brandName: brandName,
                        gender: gender,
                        clientType: clientType,
                        start: infoPage++ //页数
                    };
                    
                    canScroll = false;
                    $.ajax({
                        type: 'GET',
                        url: '/plustar/loadinfo',
                        data: setting
                    }).then(function(data) {
                        var html = '',
                            res,
                            i;
                        if (data.success) {
                            if (data.end) {
                                dataEnd = true;

                                $loading.addClass('hide');
                                $noMore.removeClass('hide');
                            }
                            res = data.data;
                            for (i = 0; i < res.length; i++) {
                                html += Mustache.render(tpl, res[i]);
                            }
                            if (html !== '') {
                                $infoContent.append(html);

                                //lazyload 不包含src即未加载的图片
                                $('img.lazy').lazyload({
                                    container: $infoContent
                                });
                            }
                            canScroll = true;
                        }
                    });
                }
            }
        });
        
        //提示信息位置
    	winW = $(window).width();
    	
    	tipH = $loginTip.height();
    	tipW = $loginTip.width();
    	
    	$loginTip.css({
    		top: (winH - tipH) / 2,
    		left: (winW - tipW) / 2
    	});
        
        //绑定商品信息的收藏和取消收藏事件(相关推荐和搭配模块)
        $('.new-arrival-content').delegate('.good-islike', 'touchstart', function(e) {
            var $cur = $(e.currentTarget),
                $good = $cur.closest('.good-info'),
                id = $good.data('id');
            
            $.ajax({
                type: 'GET',
                url: '/favorite/product',
                data: {
                    st: 0,
                    product_skn: id
                }
            }).then(function(data) {
                if (data.code === 200) {
                    $cur.toggleClass('good-like');
                } else if (data.code === 400) {
                    //提示登录信息
                    $('#login-tip').fadeIn(500, function() {
                        setTimeout(function() {
                           $('#login-tip').fadeOut(500);
                        }, 1000);
                    });
                }
            });
        });
        
        //文章点赞
        $('#info-content').delegate('.like-btn', 'touchstart', function(e) {
            var $cur = $(e.currentTarget),
                $info = $cur.closest('.info-block'),
                id = $info.data('id');
                
            $.ajax({
                type: 'GET',
                url: '/favorite/praise',
                data: {
                    id: id
                }
            }).then(function(data) {
                if (data.code === 200) {
                    $cur.toggleClass('like');
                    //更新点赞数
                    $cur.next('span.like-count').text(data.data);
                } else if (data.code === 400) {
                    //提示登录信息
                    $('#login-tip').fadeIn(500, function() {
                        setTimeout(function() {
                           $('#login-tip').fadeOut(500);
                        }, 1000);
                    });
                }
            });
        });
        
        $('#brand-like').bind('touchstart', function() {
            var $cur = $(this),
                id = $(this).closest('.brand-info').data('id');
            
            $.ajax({
                type: 'GET',
                url: '/favorite/brand',
                data: {
                    brand_id: id
                }
            }).then(function(data) {
                if (data.code === 200) {
                    $cur.toggleClass('like');
                } else if (data.code === 400) {
                    $('#login-tip').fadeIn(500, function() {
                        setTimeout(function() {
                           $('#login-tip').fadeOut(500);
                        }, 1000);
                    });
                }
            });
        });
    });
};