detail.js 2.16 KB
/**
 * PLUS+STAR详情页
 * @author: xuqi<qi.xu@yoho.cn>
 * @date: 2015/10/10
 */

var $ = require('jquery'),
    Hammer = require('yoho.hammer'),
    ellipsis = require('mlellipsis'),
    lazyLoad = require('yoho.lazyload');

var $intro = $('#intro'),
    $imt = $('#intro-more-txt'),
    $infosContainer = $('#related-infos-container');

var info = require('../info');

var tip = require('../../plugin/tip');

var brandId = $('#brand-info').data('id');

var mIntro, aIntro;

var moreHammer, likeHammer;

require('../../plugin/wx-share');

ellipsis.init();

//Init LazyLoad
lazyLoad($('img.lazy'));

//文字介绍文字截取
$intro[0].mlellipsis(3);

//获取截取文字和完整文字
setTimeout(function() {
    mIntro = $intro.text();
    aIntro = $intro.attr('title');
});

info.initInfosEvt($infosContainer);

//文字介绍收起与展开
moreHammer = new Hammer(document.getElementById('more-intro'));
moreHammer.on('tap', function(e) {
    var $this = $(e.target).closest('#more-intro');

    $this.toggleClass('spread');

    if ($this.hasClass('spread')) {

        //显示
        $intro.text(aIntro);
        $imt.text('收起');
    } else {

        //隐藏
        $intro.text(mIntro);
        $imt.text('more');

        $(window).scrollTop(0, 400); //滑动到顶部
    }

    e.preventDefault(); //防止收缩后误点到商品产生跳转
});

//品牌收藏
likeHammer = new Hammer(document.getElementById('brand-like'));
likeHammer.on('tap', function(e) {
    var opt = 'ok',
        $this = $(e.target);

    e.preventDefault();

    if ($this.hasClass('like')) {
        opt = 'cancel';
    }

    $.ajax({
        type: 'POST',
        url: '/guang/opt/favoriteBrand',
        data: {
            id: brandId,
            opt: opt
        },
        success: function(data) {
            if (data.code === 200) {
                $this.toggleClass('like');
            } else if (data.code === 400 || data.code === 412) {
                location.href = data.data; //未登录跳转登录页面
            } else {
                tip.show(data.message);
            }
        },
        error: function() {
            tip.show('网络断开连接了~');
        }
    });
});