comment.js 3.09 KB
/**
 * 退换货申请
 * @author: yyqing<yanqing.yang@yoho.cn>
 * @date: 2016/3/1
 */
var $ = require('yoho.jquery');

var $comment = $('.comment-table'),
    $remarkBtn = $comment.find('.remark-btn');

var $dialog = $('#comment-dialog-widget'),
    $titleBar = $dialog.find('.dialog-titlebar'),
    $commentArea = $dialog.find('textarea');

var pageW = $(document).width(),
    pageH = $(document).height(),
    winH = $(window).height();

var dialog = {
        canmove: false,
        offset: {},
        maxtop: 0,
        maxleft: 0
    },
    orderInfo = {},
    $optDom;

// 处理订单数据
$remarkBtn.each(function() {
    var $next = $(this).next(),
        data = $next.data(),
        key = data.orderid;

    orderInfo[key] = {};
    orderInfo[key].productSkn = data.productskn;
    orderInfo[key].productId = data.productid;
    orderInfo[key].goodsId = data.goodsid;
    orderInfo[key].orderId = data.orderid;
    orderInfo[key].erpSkuId = data.erpskuid;

    $next.remove();
    $(this).data('code', key);
});

$remarkBtn.click(function() {
    var scrollTop = $(document).scrollTop();

    $optDom = $(this);
    $commentArea.val('');
    $dialog.removeClass('hide').css({
        top: scrollTop + (winH - $dialog.outerHeight()) / 2,
        left: (pageW - $dialog.outerWidth()) / 2
    });
});

// 评论弹窗
$titleBar.bind('mousedown', function(e) {
    if ($(e.target).hasClass('dialog-close-btn')) {
        return;
    }

    dialog.canmove = true;
    dialog.offset = {
        x: e.offsetX,
        y: e.offsetY
    };
    dialog.maxtop = pageH - $dialog.outerHeight();
    dialog.maxleft = pageW - $dialog.outerWidth();
});

$(document).mousemove(function(e) {
    var mtop, mleft;

    if (!dialog.canmove) {
        return;
    }

    mtop = e.pageY - dialog.offset.y;
    mleft = e.pageX - dialog.offset.x;
    mtop = mtop < dialog.maxtop ? mtop : dialog.maxtop;
    mleft = mleft < dialog.maxleft ? mleft : dialog.maxleft;
    $dialog.css({
        top: mtop > 0 ? mtop : 0,
        left: mleft > 0 ? mleft : 0
    });
}).mouseup(function(e) {
    dialog.canmove = false;
});

$dialog.on('click', '.dialog-save-btn', function() {
    var remark = $.trim($commentArea.val()),
        param;

    if (remark === '') {
        alert('请添加评论内容');
    }
    if ($optDom.length) {
        param = orderInfo[$optDom.data().code];
        param.content = remark;

        $.ajax({
            type: 'POST',
            url: '/home/comment/saveComment',
            data: param
        }).then(function(jsonData) {
            var $par = $optDom.parent();

            if (jsonData.code === 200) {
                $par.prev().text(remark);
                $par.html('<span class="remarked">已评论!</span>');
                $optDom.length = 0;
                $dialog.addClass('hide');
            } else {
                alert(jsonData.message);
            }
        });
    }
});

$dialog.on('click', '.dialog-close-btn', function() {
    $optDom.length = 0;
    $dialog.addClass('hide');
});