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

var $ = require('jquery'),
    Mustache = require('mustache'),
    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;

        //获取相关资讯模板
         $.get('/ps/readTpl', function(data) {
            if (data.success) {
                tpl = data.data;
                Mustache.parse(tpl); //pre-compile and cache template
            }
        });

        //文字截取
        ellipsis.init();
        setTimeout(function() {
            $intro.mlellipsis(4); //品牌介绍
            $('.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', 'touchstart', function(e) {
            e.preventDefault();
            showMoreIntro();
        }).delegate('.more', 'click', function() {
            showMoreIntro();
        });

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

            if (!canScroll) {
                return;
            }

            if ($(window).scrollTop() + winH >= $(document).height() - loadMoreH) {
                //loadData
                if (!dataEnd) {
                    count = $infoContent.children('.info-block').length;
                    canScroll = false;
                    $.ajax({
                        type: 'GET',
                        url: '/ps/loadInfo',
                        data: {
                            start: count
                        }
                    }).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:not([src])').lazyload({
                                    container: $infoContent
                                });
                            }
                            canScroll = true;
                        }
                    });
                }
            }
        });
    });
};