comment.page.js 5.97 KB
/**
 * 我的评论
 * @author: yyqing<yanqing.yang@yoho.cn>
 * @date: 2016/3/1
 */
var $ = require('yoho-jquery');
var ImgPreview = require('../common/img-preview');

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(),
    preview = new ImgPreview();

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


require('../plugins/jquery.qupload');

// 处理订单数据
$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() {
    dialog.canmove = false;
});

$('.img-preview').on('click', 'i.view', function() {

    var url = $(this).closest('.img-preview').find('img').attr('src');

    preview.preview(url);
});

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

    if (remark === '') {
        alert('请添加评论内容');
        return;
    }
    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');
});

$('.comment-add input[type="file"]').each(function(i, it) {
    var $this = $(it),
        $par = $this.closest('.problem-description');

    if (!window.location.origin) {
        window.location.origin = window.location.protocol + '//' +
                                window.location.hostname + (window.location.port ? ':' + window.location.port : '');
    }
    $this.qupload({
        button_image_url: '',
        upload_url: window.location.origin + '/home/returns/imgUpload',
        file_post_name: 'fileData',
        button_text: '<span class="btn_upload_text">上传图片</span>',
        button_text_style: '.btn_upload_text{color: #ffffff;}',
        button_width: 60,
        button_height: 60,
        button_text_left_padding: 32,
        button_text_top_padding: 8,
        button_action: window.SWFUpload.BUTTON_ACTION.SELECT_FILE,
        file_size_limit: '10240',
        file_types: '*.jpg;*.jpeg;*.png;*.bmp',
        uploadSuccessed: function(data) {
            var $imgList = $par.find('li'),
                isShow = false,
                img, _html;

            img = JSON.parse(data).imgList[0];
            _html = '<span class="btn-del" title="删除"></span>' +
                '<img src="' + img[0] + '" width="126" height="126">' +
                '<input type="hidden" name="imgs" value="' + img.imgRelUrl + '">';
            $imgList.each(function() {
                if (!isShow && !$(this).find('img').length) {
                    isShow = true;
                    $(this).html(_html);
                }
            });
        }
    });
});

// comment form
$('.comment-table').on('click', '[data-role="showCommentForm"]', function() {
    // $('[data-role="showCommentForm"]).
    var $ntr = $(this).closest('tr').next('tr');

    if ($ntr.attr('data-role') === 'commentAddForm') {
        $ntr.toggle();
    }
});


// star
$('.comment-table').on('click', '.comment-star.editable > span', function() {
    $(this).parent().find('span').removeClass('active');
    $(this).addClass('active');
});

// btn group
$('.comment-add').on('click', ' .btn-group .btn', function() {
    $(this).siblings('.btn').removeClass('active');
    $(this).addClass('active');
});

// goods comment textarea
$('.comment-table').on('focus', '[data-role="goodsCommentWrap"] textarea', function() {
    $(this).parent().find('.text-tip').hide();
});
$('.comment-table').on('blur', '[data-role="goodsCommentWrap"] textarea', function() {
    if (!$(this).val()) {
        $(this).parent().find('.text-tip').show();
    }
});
$('.comment-table').on('click', '[data-role="goodsCommentWrap"] .text-tip', function() {
    $(this).parent().find('textarea').focus();
});
$('.comment-table').find('[data-role="goodsCommentWrap"] textarea').trigger('blur');