plustar-detail-render.js 3.75 KB

var $ = require('yoho-jquery'),
    dbClass = 'data-bind';

module.exports = function(callback) {
    var brandId = $('#brandId').val();
    var clientType = $('#clientType').val();
    if (brandId && $('#related-infos-demote').length) {
        $.ajax({
            type: 'POST',
            url: '/guang/plustar/brandinfoAsync',
            data: {
                client_type: clientType,
                brand_id: brandId
            },
            success: function(data) {
                render(data);
                callback && callback(data);
            }
        });
    }
};
function render(data) {
    if (data.isLike) {
        $('.brand-islike').addClass('like');
    }
    if (data.infos && data.infos.length) {
        var guangInfoEl = $('#related-infos-container>.guang-info').remove();
        for (var i = 0; i < data.infos.length; i++) {
            var info = data.infos[i];
            var item = guangInfoEl.clone().attr('data-id', info.id);
            item.find('.info-img-link')
            .attr('href', info.url)
            .find('img')
            .attr('data-original', info.img)
            .attr('alt', info.alt);
            var depsEl = item.find('.info-deps');
            depsEl.find('.info-title-container').attr('href', info.url).find('.info-title').text(info.title);
            depsEl.find('.info-text').text(info.text);
            var timeEl = item.find('.time-view-like-share');
            timeEl.find('.publish-time').text(info.publishTime);
            timeEl.find('.page-view').text(info.pageView);
            var likeEl = timeEl.find('.like-share-container');
            if (info.like) {
                likeEl.find('.like-btn').removeClass(dbClass);
                if (info.like.isLiked) {
                    likeEl.find('.like-btn').addClass('like');
                }
                likeEl.find('.like-count').text(info.like.count).removeClass(dbClass);
            }
            if (info.collect) {
                likeEl.find('.collect-link').attr('href', info.collect.url).removeClass(dbClass);
                if (info.collect.isCollected) {
                    likeEl.find('.collect-link').find('.collect-btn').addClass('collected');
                }
            }
            if (info.share) {
                likeEl.find('.share-btn').attr('href', info.share).removeClass(dbClass);
            }
            if (info.author) {
                item.find('.info-author')
                .attr('href', info.author.url)
                .find('.avatar')
                .attr('data-original', info.author.avatar)
                .parent()
                .find('.name')
                .text(info.author.name)
                .removeClass(dbClass);
            }
            var infoImgEl = item.find('.info-img');
            if (info.showTags) {
                infoImgEl.find('.data-bind').removeClass(dbClass);
                if (info.isTip) {
                    infoImgEl.find('.is-tips').removeClass(dbClass);
                }
                if (info.isCollocation) {
                    infoImgEl.find('.is-collocation').removeClass(dbClass);
                }
                if (info.isFashionMan) {
                    infoImgEl.find('.is-fashionMan').removeClass(dbClass);
                }
                if (info.isFashionGood) {
                    infoImgEl.find('.is-fashion-good').removeClass(dbClass);
                }
                if (info.isTopic) {
                    infoImgEl.find('.is-topic').removeClass(dbClass);
                }
                if (info.isSpecialTopic) {
                    infoImgEl.find('.is-special-topic').removeClass(dbClass);
                }
            }
            $('#related-infos-container').append(item);
        }
        $('.related-infos').removeClass(dbClass);
    }

    $('.' + dbClass).remove();
}