intro.js 1.62 KB
var $ = require('yoho-jquery');
var videoPlayerTpl = require('hbs/product/video-player.hbs');

// 图片换成懒加载方式
var replacePairs = {
    '<img src=': '<img class="lazy img-responsive" width="720" height="200" ' +
    'src="data:image/gif;base64,R0lGODlhAQABAJEAAAAAAP///93d3f///yH5BAEAAAMALAAAAAABAAEAAAICVAEAOw==" ' +
    'data-original=',
    '<img border="0" src=': '<img border="0" class="lazy img-responsive" width="720" height="200" ' +
    'src="data:image/gif;base64,R0lGODlhAQABAJEAAAAAAP///93d3f///yH5BAEAAAMALAAAAAABAAEAAAICVAEAOw==" ' +
    'data-original=',
    '.jpg"': '.jpg?imageslim"',
    'http://': '//'
};

function _getDetailDataBySizeInfo(intro) {
    var $intro;

    // 详情配图
    $.each(replacePairs, function(key, value) {
        intro = intro.replace(new RegExp(key, 'gm'), value);
    });

    // 视频链接处理
    $intro = $(intro);

    $intro.find('a[href]').each(function() {
        const $el = $(this);

        $el.attr('href', 'javascript:;')
            .attr('target', '')
            .attr('title', '')
            .css('cursor', 'initial');
    });

    $intro.find('.video-placeholder').each(function(idx, ele) {
        var $this = $(ele);
        var videoSrc = $this.find('source').attr('src');
        var $video = $this.find('video');
        var width = $video.attr('width');
        var height = $video.attr('height');

        $this.empty().append(videoPlayerTpl({url: videoSrc, width: width || 750, height: height || 240}));
    });

    return $intro;
}

exports.getHtml = function() {
    var intro = $('#details-intro').html();

    return _getDetailDataBySizeInfo(intro);
};