Blame view

public/js/guang/detail.page.js 7.05 KB
刘传洋 authored
1 2 3 4 5 6 7
/**
 * 逛详情页
 * @author: liuyue<yue.liu@yoho.cn>
 * @date: 2015/12/16
 */

var $ = require('yoho-jquery'),
yyq authored
8 9 10
    lazyLoad = require('yoho-jquery-lazyload');

var $commentArea = $('#comment-area'),
刘传洋 authored
11 12 13 14
    articleId = $('.detail-body').data('id');

var $commentList = $commentArea.find('.comments-wrap'),
    $commentNum = $('#article-comment > .comment-num'),
刘传洋 authored
15 16
    commenting = false, // 评论请求尚未返回变量
    locating = false, // 评论页面正在跳转
刘传洋 authored
17 18
    $commentBtn = $('#comment-btn'),
    MAX_COMMENTS_WORDS = 100,
刘传洋 authored
19 20
    $wordCountTip = $('#word-count-tip'),
    commentLoaded = false;
刘传洋 authored
21
22
require('../common');
刘传洋 authored
23 24 25 26
require('./right-side');
require('./img-blink');

require('../common/share');
刘传洋 authored
27
require('./detail-dynamic');
刘传洋 authored
28
刘传洋 authored
29
// Pjax
刘传洋 authored
30 31 32 33 34 35
require('yoho-jquery-pjax');

lazyLoad({
    failure_limit: 50
});
刘传洋 authored
36
// 点击评论滑到评论区
刘传洋 authored
37 38 39 40 41 42
// $('#article-comment').click(function() {
//     $('html, body').animate({
//         scrollTop: $commentArea.offset().top - 20
//     }, 800);
// });
刘传洋 authored
43
// 文章点赞与取消点赞
刘传洋 authored
44 45 46 47 48 49 50 51 52 53 54
$('#prise-btn').click(function() {
    var prising = false,
        url,
        $this = $(this);

    if (prising) {
        return;
    }
    $this.toggleClass('liked');
    if ($this.hasClass('liked')) {
刘传洋 authored
55
        // 点赞
刘传洋 authored
56 57 58
        url = '/guang/info/praise';
    } else {
刘传洋 authored
59
        // 取消点赞
刘传洋 authored
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
        url = '/guang/info/cancelPraise';
    }
    prising = true;
    $.ajax({
        type: 'GET',
        url: url,
        data: {
            id: articleId
        }
    }).then(function(data) {
        if (data.code === 200) {
            $this.find('.like-num').html(data.data);
        }
        prising = false;
    });
}).bind('mouseenter mouseleave', function() {
    $(this).toggleClass('hover');
});
刘传洋 authored
79
// 文章收藏与取消收藏
刘传洋 authored
80 81 82 83 84 85 86 87 88 89 90
$('#collect-btn').click(function() {
    var collecting = false,
        url,
        col,
        $this = $(this);

    if (collecting) {
        return;
    }
    if ($this.hasClass('collected')) {
刘传洋 authored
91
        // 取消收藏
刘传洋 authored
92 93 94 95
        url = '/guang/info/cancelcollect';
        col = 0;
    } else {
刘传洋 authored
96
        // 收藏
刘传洋 authored
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
        url = '/guang/info/collect';
        col = 1;
    }
    collecting = true;
    $.ajax({
        type: 'GET',
        url: url,
        data: {
            id: articleId
        }
    }).then(function(data) {
        var hrefUrl;

        switch (data.code) {
            case 401:
刘传洋 authored
113
                // 防止从已有col的页面再次进行跳转后出错的情况
114 115 116
                if (/(\?|&)col=(1|0)/.test(location.href)) {
                    // hrefUrl = location.href.replace(/\?col=(1|0)/, '?col=' + col);
                    hrefUrl = location.href.replace(/(\?|&)col=(1|0)/, '$1col=x');
刘传洋 authored
117
                } else {
118 119 120 121 122
                    if (location.href.indexOf('?') >= 0) {
                        hrefUrl = location.href + '&col=' + col;
                    } else {
                        hrefUrl = location.href + '?col=' + col;
                    }
刘传洋 authored
123 124 125 126 127 128 129 130
                }
                location.href = '//www.yohobuy.com/signin.html?refer=' + encodeURI(hrefUrl);
                break;
            case 400:

                // alert(data.message);
                break;
            case 200:
131
                if (/(\?|&)col=(1|0)/.test(location.href)) {
刘传洋 authored
132
刘传洋 authored
133
                    // 如果页面url中含有col,为了防止页面刷新时收藏或者取消收藏会根据col来的问题,进行页面跳转拿掉参数
134
                    location.href = location.href.replace(/(\?|&)col=(1|0)/, '');
刘传洋 authored
135 136 137 138
                } else {
                    $this.toggleClass('collected');
                }
                break;
yyq authored
139 140
            default:
                break;
刘传洋 authored
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
        }
        collecting = false;
    });
}).bind('mouseenter mouseleave', function() {
    $(this).toggleClass('hover');
});

function showComment() {
    var commonlist = $('.comments-list').find('li').length;

    if (commonlist === 0) {
        $('.commnets-resultwrapper').hide();
        $('.comments-empty').show();
    } else {
        $('.commnets-resultwrapper').show();
        $('.comments-empty').hide();
    }
}
showComment();
刘传洋 authored
161
// 评论
刘传洋 authored
162 163 164 165
function comment(id) {
    var commentInfo = $('#comment-info').val();

    if (commentInfo === '') {
yyq authored
166
        alert('评论不能为空'); // eslint-disable-line
刘传洋 authored
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
        return false;
    }
    commenting = true;
    $.ajax({
        url: '/guang/info/comment',
        data: {
            id: id,
            comment: commentInfo
        },
        type: 'post',
        success: function(data) {
            switch (data.code) {
                case 401:
                    locating = true;
                    location.href = '//www.yohobuy.com/signin.html?refer=' +
                                    window.escape(location.href + '#comment-info');
                    break;
                case 400:
yyq authored
185
                    alert(data.message); // eslint-disable-line
刘传洋 authored
186 187 188 189 190 191 192
                    break;
                case 200:
                    if (data.data) {
                        $commentList.html(data.data.content);
                        $commentNum.html(data.data.count);
                        showComment();
刘传洋 authored
193
                        // clear comment-text
刘传洋 authored
194 195 196
                        $('#comment-info').val('').keyup();
                    }
                    break;
yyq authored
197 198
                default:
                    break;
刘传洋 authored
199 200 201 202 203 204 205 206
            }
            commenting = false;
        }
    });
}

$commentBtn.click(function(e) {
刘传洋 authored
207
    // 页面正在跳转或者正在AJAX请求时评论无效
刘传洋 authored
208 209 210 211
    if (locating || commenting) {
        return false;
    }
刘传洋 authored
212
    // 字数不符合要求
刘传洋 authored
213 214 215 216 217 218 219 220
    if ($('#comment-info').val().length - MAX_COMMENTS_WORDS > 0) {
        return;
    }
    e.preventDefault();
    comment(articleId);
});

刘传洋 authored
221
// comment pager pjax
刘传洋 authored
222
$(document).pjax('.comment-pager a, [data-role="comment-first-url"]', '#pjax-container', {
刘传洋 authored
223 224 225
    timeout: 5000
});
刘传洋 authored
226
if ($('#comment-area').length) {
刘传洋 authored
227
刘传洋 authored
228 229
    $(document).on('scroll', function() {
        if (!commentLoaded && ($(document).scrollTop() + $(window).height()) > $('#comment-area').offset().top) {
刘传洋 authored
230 231 232 233 234 235 236
            commentLoaded = true;
            $('[data-role="comment-first-url"]').click();
        }
    });
}

刘传洋 authored
237
// 分页后移动到评论框的位置
刘传洋 authored
238 239 240
$(document).on('pjax:end', function() {
    showComment();
    $('html,body').scrollTop($('#comment-info').offset().top);
刘传洋 authored
241 242 243

    // 设置头部评论数
    $('#article-comment .comment-num').text($('#comment-area .comment-num').text());
刘传洋 authored
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
});

$('#comment-info').keyup(function() {
    var len = $(this).val().length,
        showTxt;

    if (len === 0) {
        $wordCountTip.html('');
        $commentBtn.addClass('disable');
    } else {
        if (len - MAX_COMMENTS_WORDS <= 0) {
            showTxt = '还可以输入' + (MAX_COMMENTS_WORDS - len) + '字';
            $commentBtn.removeClass('disable');
        } else {
            showTxt = '已超过<span class="exceed-count">' + (len - MAX_COMMENTS_WORDS) + '</span>字';
            $commentBtn.addClass('disable');
        }
    }
    $wordCountTip.html(showTxt);
});
刘传洋 authored
265
// init
刘传洋 authored
266
$('#comment-info').trigger('keyup');
267 268 269 270

$(function() {
    $('.guang-detail-page .article-main').find('a.a-anchor').attr({target: '_blank'});
});