suggest.js 3.38 KB
/**
 * 个人中心--意见反馈
 * @author: chenglong<chenglong.wang@yoho.cn>
 * @date: 2015/11/12
 */


var $ = require('jquery'),
    Hammer = require('yoho.hammer'),
    Handlebars = require('yoho.handlebars');

var diaLog = require('./dialog');

var $uploadImgList = $('.upload-img-list'),
    headerNavHammer,
    formHammer,
    imgTpl,
    imgTemplate,
    $likeBtn = $('.suggest-item .like-btn'),
    $disLikeBtn = $('.suggest-item .dislike-btn'),
    $imgAdd = $('.img-add'),
    imgStr = '';

require('./jquery.uploadifive');

imgTpl = '{{# imgList}}' +
    '<li>' +
        '<img src="{{imgUrl}}" />' +
        '<span class="upload-img-remove"></span>' +
    '</li>' +
    '{{/ imgList}}';

imgTemplate = Handlebars.compile(imgTpl);

$('#upload-img').uploadifive({
    auto: true,
    fileType: 'image*/*',
    uploadScript: '/home/suggestimgUpload',
    fileObjName: 'fileData',
    fileSizeLimit: 1024,
    height: '100%',
    width: '100%',
    queueSizeLimit: 1,

    onAddQueueItem: function(files) {

        //TODO
    },
    onUploadComplete: function(file, data) {
        imgStr = JSON.parse(data).imgList[0].imgRelUrl;
        $uploadImgList.html(imgTemplate(JSON.parse(data)));
        $imgAdd.hide();
    }
});

headerNavHammer = new Hammer(document.getElementById('yoho-header'));

headerNavHammer.on('tap', function(e) {
    var suggestText = $('#suggest-textarea').val();


    if ($(e.target).hasClass('nav-btn')) {

        $.ajax({
            method: 'post',
            url: '/home/savesuggest',
            data: {
                content: suggestText,
                image: imgStr
            }
        }).then(function(data) {
            if (data.code === 200) {
                diaLog.showDialog({
                    autoHide: true,
                    dialogText: '提交成功'
                });
            }
        }).fail(function() {

            //TODO
        });
    }
});

if (document.getElementById('img-form') !== null) {
    formHammer = new Hammer(document.getElementById('img-form'));

    formHammer.on('tap', function(e) {
        if ($(e.target).hasClass('upload-img-remove')) {
            $uploadImgList.html('');
            imgStr = '';
            setTimeout(function() {
                $imgAdd.show();
            }, 50);

        }
    });
}

// 点赞与取消点赞
$likeBtn.bind('click', function() {

    var id = $(this).closest('.suggest-item').attr('data-id'),
        $that = $(this);

    $.ajax({
        method: 'post',
        url: '/home/upAndDown',
        data: {
            suggest_id: id
        }
    }).then(function(data) {
        if (data.code === 200) {
            $that.closest('.suggest-type').removeClass('show');
            $('.suggest-good').addClass('show');
        }
    }).fail(function(data) {

        //TODO
    });
});

$disLikeBtn.bind('click', function() {

    var id = $(this).closest('.suggest-item').attr('data-id'),
        $that = $(this);

    $.ajax({
        method: 'post',
        url: '/home/upAndDown',
        data: {
            suggest_id: id
        }
    }).then(function(data) {
        if (data.code === 200) {
            $that.closest('.suggest-type').removeClass('show');
            $('.suggest-bad').addClass('show');
        }
    }).fail(function(data) {

        //TODO
    });
});