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


var $ = require('jquery'),
    Hammer = require('yoho.hammer'),
    lazyLoad = require('yoho.lazyload'),
    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 = '',
    uploadImgNum = 0;

require('./jquery.uploadifive');

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

imgTemplate = Handlebars.compile(imgTpl);

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

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

lazyLoad();

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: '提交成功'
                });
                setTimeout(function() {
                    location.pathname = 'home/suggest';
                }, 2000);
            }
        }).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 = '';
            uploadImgNum--;
            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');
            $that.closest('.suggest-item').find('.suggest-good').addClass('show');
        }
    }).fail(function(data) {

        //TODO

        diaLog.showDialog({
            autoHide: true,
            dialogText: '网络错误~'
        });
    });
});

$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');
            $that.closest('.suggest-item').find('.suggest-bad').addClass('show');
        }
    }).fail(function(data) {

        //TODO

        diaLog.showDialog({
            autoHide: true,
            dialogText: '网络错误~'
        });
    });
});