Blame view

public/js/home/suggest.page.js 4.49 KB
zhangxiaoru authored
1 2 3 4 5 6 7 8 9 10
/**
 * 个人中心--意见反馈
 */

var $ = require('yoho-jquery'),
    Hammer = require('yoho-hammer'),
    lazyLoad = require('yoho-jquery-lazyload'),
    Handlebars = require('yoho-handlebars');

var diaLog = require('../plugin/dialog');
zhangxiaoru authored
11
var saveImage = require('./save-image');
zhangxiaoru authored
12 13 14 15 16 17 18 19 20 21 22 23 24

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.upload');
zhangxiaoru authored
25
require('../common');
zhangxiaoru authored
26
zhangxiaoru authored
27 28 29 30 31 32 33 34 35
imgTpl = '{{# imgList}}' +
    '<li>' +
        '{{# imgUrl}}<img src="{{.}}" />' +
        '<span class="upload-img-remove"></span>{{/ imgUrl}}' +
    '</li>' +
    '{{/ imgList}}';

imgTemplate = Handlebars.compile(imgTpl);
zhangxiaoru authored
36
zhangxiaoru authored
37 38 39
$('#upload-img').upload({
    auto: true,
    fileType: 'image/*',
zhangxiaoru authored
40
    uploadScript: '/api/upload/image',
zhangxiaoru authored
41
    fileObjName: 'filename',
zhangxiaoru authored
42 43 44 45
    fileSizeLimit: 300,
    height: '100%',
    width: '100%',
    multi: false,
zhangxiaoru authored
46
    formData: {
zhangxiaoru authored
47
        bucket: 'suggest'
zhangxiaoru authored
48
    },
zhangxiaoru authored
49 50
    onAddQueueItem: function(files) {
zhangxiaoru authored
51
        // TODO
zhangxiaoru authored
52 53 54 55 56
        $uploadImgList.html(imgTemplate({
            imgList: true
        }));
    },
    onUploadComplete: function(file, data) {
zhangxiaoru authored
57
        data = saveImage.saveImage(data);
zhangxiaoru authored
58
zhangxiaoru authored
59
        $uploadImgList.html('');
zhangxiaoru authored
60 61
        imgStr = data.imgList[0].imgRelUrl;
        $uploadImgList.html(imgTemplate(data));
zhangxiaoru authored
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
        $imgAdd.hide();
        uploadImgNum++;
    }
});

lazyLoad();

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

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


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

        if (!textReg.test(suggestText)) {
            diaLog.showDialog({
                autoHide: true,
                dialogText: '意见不能为空'
            });

            return;
        }

        $.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);
            } else {
                diaLog.showDialog({
                    autoHide: true,
                    dialogText: '提交失败~'
                });
            }
        }).fail(function() {

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

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,
            reliable: 1
        }
    }).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) {
zhangxiaoru authored
155
        // TODO
zhangxiaoru authored
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182

        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,
            reliable: 2
        }
    }).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) {
zhangxiaoru authored
183
        // TODO
zhangxiaoru authored
184 185 186 187 188 189

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