plustar-detail-render.js 3.83 KB
let $ = require('yoho-jquery');
let dbClass = 'data-bind',
    isApp = $('input[name="isApp"]').val(),
    uid = $('input[name="uid"]').val();

module.exports = function(callback) {
    let brandId = $('#brandId').val();

    if (brandId && $('#related-infos-demote').length) {
        $.ajax({
            type: 'POST',
            url: '/guang/plustar/brandinfoAsync',
            data: {
                brand_id: brandId,
                isApp: isApp,
                uid: uid
            },
            success: function(data) {
                render(data); // eslint-disable-line
                callback && callback(data);
            }
        });
    }
};
function render(data) {
    if (data.isLike) {
        $('.brand-islike').addClass('like');
    }
    if (data.infos && data.infos.length) {
        let guangInfoEl = $('#related-infos-container>.guang-info').remove();

        for (let i = 0; i < data.infos.length; i++) {
            let info = data.infos[i];
            let 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);
            let 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);
            let timeEl = item.find('.time-view-like-share');

            timeEl.find('.publish-time').text(info.publishTime);
            timeEl.find('.page-view').text(info.pageView);
            let 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);
            }
            let 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();
}