detail.js 22.2 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 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 155 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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
/*
 * detail页js
 * @author: yue.liu@yoho.cn
 * @date: 2015-04-16
 */

var $ = require('jquery'),
    share = require('../plugin/share'),
    box = require('../plugin/box'),
    imgZoom = require('../plugin/imgZoom'),
    tools = require('yoho-tools'),
    mustache = require('mustache'),
    //Handlebars = require('handlebars'),
    mulLine = require('mlellipsis'),
    flip = require("../plugin/flip"),
    slide = require("../plugin/photoslide"),
    jsonp = require('yoho-jsonp');


require('lazyload');
require('../plugin/slider');
require('../plugin/comment');
require('../plugin/content-paging');

exports.init = function() {
    var shareUrl = $(".share").attr("data-link"),
        shareCover = $(".share").attr("cover-url"),
        detailTitle = $(".detail-title").find("h1"),
        detailSubtitle = $(".detail-title").find("h2"),
        ajaxParam,
        i,
        data,
        cidNum;

    //边栏数据相关变量
    var adItem = '', //边栏广告
        sideVideoTpl, //边栏视频模板
        sideVideoData, //边栏视频数据
        sideVideoHtml, //边栏视频渲染的html
        sidePostTpl, //边栏relatedpost模板
        sidePostData, //边栏relatedpost数据
        sidePostHtml; //边栏relatedpost渲染的html

    //related-post截行相关变量
    var relatedPost,
        postList, //post列表
        totalLineNum, //总行数
        titleLineNum, //title行数
        subTitleLineNum; //subtitle行数

    //滑出边栏小视频side-video播放区域暂停相关变量
    var relatedPostsOffsetTop,
        relatedPostsTop,
        relatedPostsMarginLeft;

    //页面滑动热播位置变化相关变量
    var detailBodyOffsetTop,
        relatedPostsOffsetHeightTop,
        distance,
        eleTop,
        detailBodyHeight;

    //底部数据相关变量
    var bottomPostTpl, //底部relatedpost模板
        bottomPostData, //底部relatedpost数据
        bottomPostHtml; //底部relatedpost渲染的html

    //态度设置相关变量
    var exType = 0,
        isCancel = 0,
        attitudeData,
        attitudeDataWowIcon,
        attitudeWowNum,
        attitudeDataWtfIcon,
        attitudeWtfNum,
        attitudeDataZzzIcon,
        attitudeZzzNum;

    //兼容视频,IE等相关变量
    var video,
        ieVideo,
        ipadVideo,
        browser,
        b_version,
        version,
        trim_Version;

    //初始化截行插件
    mulLine.init();

    //分享
    share.init({
        shareUrl: shareUrl,
        img: shareCover,
        sinaText: '【Yoho!Girls】' + detailTitle.text() + '\r\n' + detailSubtitle.text() + '(来自Yoho!GIRL)',
        fbTextFunction: function() {
            return {
                des: '【Yoho!Girls】' + detailTitle.text() + '\r\n' + detailSubtitle.text() + '#Yoho!GIRL',
                name: '【Yoho!Girls】' + detailTitle.text() + '\r\n' + detailSubtitle.text() + '#Yoho!GIRL',
                caption: '【Yoho!Girls】' + detailTitle.text() + '\r\n' + detailSubtitle.text() + '#Yoho!GIRL',
                url: location.href
            }
        },
        tweetText: '【Yoho!Girls】' + detailTitle.text() + '\r\n' + detailSubtitle.text(),
        qqText: '【Yoho!Girls】' + detailTitle.text() + ' ' + detailSubtitle.text() + '(来自Yoho!GIRL)'
    });

    //懒加载
    $(".detail-body").find("img.lazy").lazyload({
        placeholder: "data:image/gif;base64,R0lGODlhAQABAJEAAAAAAP///93d3f///yH5BAEAAAMALAAAAAABAAEAAAICVAEAOw=="
    });

    //订阅
    $(".focus-us .textbox a").click(function() {
        var input = $(this).prev();
        if ($.trim(input.val()) === "") {
            //box.alert(tips.getTips("emailEmpty"),5);
            box.alert(11);
            return;
        }
        if (!tools.IYOHO_isEmail(input.val())) {
            //box.alert(tips.getTips("emailError"),5);
            box.alert(22);
            return;
        }
        window.open('/passport/subscribe/index?email=' + input.val());
    });

    $(".focus-us .textbox input").keydown(function(event) {
        if (event.keyCode === 13) {
            $(this).next().trigger("click");
        }
    });

    //图片轮播
    if ($(".detail-slide-body").size() > 0) {
        $(".detail-slide-ctrl-tabs").find("img.lazy").lazyload();
        $(".detail-slide-body").imgZoom({
            styleClass: 'detail-zoom'
        });

        $(".detail-slide-piclist").slider({
            pageCtrl: $(".detail-slide-ctrl-tabs"),
            pageCtrlAnimate: true
        });
    };

    //调用评论插件
    $(".stats-btn").on("click", ".comments", function() {
        cidNum = $(".comments-box").attr("cid");
        if ($(".comments-box").find(".comment-textarea").size() > 0) {
            if ($(".comments-box").css("display") === "none") {
                $(".comments-box").show();
            } else {
                $(".comments-box").hide();
            }
        } else {
            $(".comments-box").comment({
                cid: cidNum,
                thirdLogin: true,
                commentPageUrl: '/comment/default/list',
                publishUrl: '/comment/default/publish',
                deleteUrl: '/comment/default/delete'
            });
        }
    });

    //ajax参数
    ajaxParam = {
        id: $(".comments-box").attr("cid"),
        type: $(".comments-box").attr("ctype"),
        c: $(".comments-box").attr("cc"),
        twoc: $(".comments-box").attr("ctwoc")
    };

    //右侧side的数据获取
    jsonp({
        url: YohoConfig.mainUrl + '/channel/detail/getright?id=' + ajaxParam.id + '&type=' + ajaxParam.type + '&c=' + ajaxParam.c + '&twoc=' + ajaxParam.twoc,
        success: function(response) {
            data = response.data;
            //广告
            for (i = 0; i < data.banners.length; i++) {
                adItem += '<a href="' + data.banners[i].link + '" target="_blank"><img src="' + data.banners[i].img + '" alt="" /></a>'
            }
            $(".detail-ad").html(adItem);

            //没有广告去掉margin
            if (data.banners.length == 0) {
                $(".detail-side .app-download").css("margin", 0);
            }

            //视频
            sideVideoTpl = $('#side-video-tpl').html();
            sideVideoData = {
                video: data.video
            }

            sideVideoHtml = mustache.render(sideVideoTpl, sideVideoData);
            $('.detail-side').append($(sideVideoHtml));

            if (navigator.userAgent.indexOf('iPad') !== -1) {
                // 封面视频播放器替换为H5的VIDEO标签
                if (!!$('.video-area').find('object').size()) {
                    $('.video-area').find('object').hide();
                    $('.side-video').find('video').show();
                }
            }

            //边栏related-post
            if (data.mosts.length <= 0) return;
            sidePostTpl = $('#side-related-posts').html();
            sidePostData = {
                relatedPost: {
                    postList: data.mosts
                }
            }
            sidePostHtml = mustache.render(sidePostTpl, sidePostData);
            $('.detail-side').append($(sidePostHtml));

            //边栏related-post根据行数截取字符
            relatedPost = $('.side-related-posts');
            postList = relatedPost.find("li");
            if (postList.length > 0) {
                totalLineNum = 3;
                postList.each(function(index, obj) {
                    titleLineNum = $(obj).find('h3').getTextLineNumber('title');
                    subTitleLineNum = $(obj).find('p').getTextLineNumber('subtitle');
                    if (titleLineNum > 2) {
                        $(obj).find('h3').mlellipsis(2);
                        titleLineNum = 2;
                    }
                    if (subTitleLineNum > totalLineNum - titleLineNum) {
                        $(obj).find('p').mlellipsis(totalLineNum - titleLineNum, 'detail');
                    }
                });
            }

            //滑出小视频播放区域暂停小视频
            $(window).resize(function() {
                relatedPostsOffsetTop = relatedPost.offset().top - $('#minEnterprise').outerHeight();
                relatedPostsTop = $('#minEnterprise').outerHeight();
                relatedPostsMarginLeft = $('.detail-container').width() / 2 - relatedPost.outerWidth();
            }).resize();
            $(window).bind('scroll', function() {
                function thisMovie(movieName) {
                    if (navigator.appName.indexOf('Microsoft') != -1) {
                        return window[movieName];
                    } else {
                        return document[movieName];
                    }
                }
                if ($('.side-video').size() > 0) {
                    if ($(window).scrollTop() >= $('.side-video').offset().top || $(window).scrollTop() < $('.side-video').offset().top - $(window).height()) {

                        try {
                            if (navigator.appName.indexOf('Microsoft') != -1) {
                                thisMovie('newestvideo').StopMovie();
                            } else if (navigator.userAgent.indexOf('iPad') !== -1) {
                                var videoId = document.getElementById('side-video-ipad');
                                videoId.pause();
                            } else {
                                $('.side-video').find('embed').get(0).StopMovie();
                            }

                        } catch (e) {}
                    }
                }

                if ($(window).scrollTop() >= relatedPostsOffsetTop && relatedPost.css('display') !== 'none') {
                    if (navigator.userAgent.indexOf('iPad') !== -1) return;
                    //right_tab_toped=true;
                    relatedPost.css({
                        'position': 'fixed',
                        'left': '50%',
                        'margin-left': relatedPostsMarginLeft,
                        'top': relatedPostsTop
                    });

                    //热播位置
                    detailBodyOffsetTop = $('.detail-body').offset().top;
                    detailBodyHeight = $('.detail-body').outerHeight();
                    relatedPostsOffsetHeightTop = relatedPost.offset().top + relatedPost.outerHeight();
                    distance = detailBodyHeight + detailBodyOffsetTop;
                    eleTop = distance - relatedPost.outerHeight();
                    if (relatedPostsOffsetHeightTop >= distance) {
                        relatedPost.css({
                            'position': 'absolute',
                            'top': eleTop - relatedPostsTop
                        });
                    }
                    if (relatedPost.offset().top - $(window).scrollTop() > relatedPostsTop) {
                        relatedPost.css({
                            'position': 'fixed',
                            'top': relatedPostsTop
                        });
                    }
                } else if ($(window).scrollTop() < relatedPostsOffsetTop && relatedPost.css('display') !== 'none') {
                    //right_tab_toped=false;
                    relatedPost.removeAttr('style').show();
                }


            });
        }
    });

    //底部数据的获取
    jsonp({
        url: YohoConfig.mainUrl + '/channel/detail/getbottom?id=' + ajaxParam.id + '&type=' + ajaxParam.type + '&c=' + ajaxParam.c + '&twoc=' + ajaxParam.twoc,
        success: function(response) {
            data = response.data;

            var relatedPost = data.related,
                prevnext = data.prevnext,
                attitude = data.attitude,
                commentnum = data.commentnum;

            //底部related-post
            if (data.related.length > 0) {
                bottomPostTpl = $('#bottom-related-posts').html();
                bottomPostData = {
                    relatedPost: {
                        postList: data.related
                    }
                };
                // console.log(bottomPostData);
                // console.log(bottomPostTpl);

                //console.log(Handlebars);
                /*var tpl = Handlebars.compile(bottomPostTpl);
                var template = tpl(bottomPostData);*/
                bottomPostHtml = mustache.render(bottomPostTpl, bottomPostData);
                //console.log(tpl)
                $('.detail-body').append($(bottomPostHtml));

                postList = $('.related-posts').find("li");
                postList.eq(5).nextAll().hide();

                //底部related-post根据行数截取字符
                if (postList.length > 0) {
                    postList.each(function(index, obj) {
                        totalLineNum = 3;
                        titleLineNum = $(obj).find('h3').getTextLineNumber('title');
                        subTitleLineNum = $(obj).find('p').getTextLineNumber('subtitle');
                        if (titleLineNum > 2) {
                            $(obj).find('h3').mlellipsis(2);
                            titleLineNum = 2;
                        }
                        if (subTitleLineNum > totalLineNum - titleLineNum) {
                            $(obj).find('p').mlellipsis(totalLineNum - titleLineNum, 'detail');
                        }
                    });
                }
            }

            //上一篇下一篇
            if (data.prevnext.prev.title) {
                $(".news-next-prev .prev").show();
                $(".news-next-prev .prev").find("a").attr("href", prevnext.prev.link).attr("title", prevnext.prev.title).attr("onclick", "prevOrNextClickEvent();");
            }
            if (data.prevnext.next.title) {
                $(".news-next-prev .next").show();
                $(".news-next-prev .next").find("a").attr("href", prevnext.next.link).attr("title", prevnext.next.title).attr("onclick", "prevOrNextClickEvent();");
            }

            //态度
            if (data.attitude.userWow > 0) {
                $(".stats-btn .wow").addClass("current");
            };
            if (data.attitude.userWtf > 0) {
                $(".stats-btn .wtf").addClass("current");
            };
            if (data.attitude.userZzz > 0) {
                $(".stats-btn .zzz").addClass("current");
            };
            setattitude(data.attitude.wowCount, data.attitude.zzzCount, data.attitude.wtfCount);

            if (data.commentnum > 0) {
                $(".comment-num").text(data.commentnum).addClass('hasNum');
            } else {
                $(".comment-num").removeClass('hasNum');
            }
        }
    });

    //态度设置
    $(".stats-btn").on("click", "li", function() {
        if ($(this).index() == 3) return;
        exType = $(this).index() + 1;
        isCancel = 0;

        if ($(this).hasClass("current")) {
            isCancel = 1;
        }

        attitudeData = {
            id: ajaxParam.id,
            exType: exType,
            isCancel: isCancel
        };

        $.ajax({
            url: YohoConfig.mainUrl + '/channel/detail/setattitude',
            data: attitudeData,
            dataType: 'json',
            success: function(response) {
                $(".stats-btn").find("li").removeClass("current");

                if (response.data.userWow > 0) {
                    $(".stats-btn .wow").addClass("current");
                };
                if (response.data.userWtf > 0) {
                    $(".stats-btn .wtf").addClass("current");
                };
                if (response.data.userZzz > 0) {
                    $(".stats-btn .zzz").addClass("current");
                };
                setattitude(response.data.wowCount, response.data.zzzCount, response.data.wtfCount);
            }
        });
    });

    /*
     * 态度设置函数
     * @param:三种态度的数值
     */
    function setattitude(wow, zzz, wtf) {
        attitudeWowNum = $('.stats-btn .wow').find('span');
        attitudeZzzNum = $('.stats-btn .zzz').find('span');
        attitudeWtfNum = $('.stats-btn .wtf').find('span');
        if (wow == 0) {
            attitudeWowNum.removeClass('hasNum');
        } else {
            attitudeWowNum.addClass('hasNum');
            attitudeWowNum.text(wow);
        };

        if (zzz == 0) {
            attitudeZzzNum.removeClass('hasNum');
        } else {
            attitudeZzzNum.addClass('hasNum');
            attitudeZzzNum.text(zzz);
        };

        if (wtf == 0) {
            attitudeWtfNum.removeClass('hasNum');
        } else {
            attitudeWtfNum.addClass('hasNum');
            attitudeWtfNum.text(wtf);
        };
    }

    //relatedpost的loadmore事件
    $('.detail-body').on('click.loadMore', '.load-more', function() {
        $(this).fadeOut(500, function() {
            $('.related-posts').find('li').show();
        });
    })

    //翻转
    flip.init({
        wrapClass: ".yohoboy-flip",
        operate: "mouse"
    });

    //hr分页
    $(".detail-body").contentPaging();

    //兼容以前视频视频的大小
    video = $(".text-body").find("embed");
    ieVideo = $(".text-body").find("object");
    ipadVideo = $(".text-body").find("video");
    if ($(window).width() >= 1200) {
        video.css({
            "width": "738px",
            "height": "415px"
        });
        ieVideo.css({
            "width": "738px",
            "height": "415px"
        })
    } else if ($(window).width() <= 768) {
        ipadVideo.css({
            "width": "460px",
            "height": "259px"
        });
        video.css({
            "width": "460px",
            "height": "259px"
        });
        ieVideo.css({
            "width": "460px",
            "height": "259px"
        });
    } else {
        video.css({
            "width": "630px",
            "height": "354px"
        });
        ieVideo.css({
            "width": "630px",
            "height": "354px"
        });
    };

    //检测ie版本,ie7/8采用1024分辨率
    browser = navigator.appName;
    b_version = navigator.appVersion;
    version = b_version.split(";");
    if (version.length > 1) {
        var trim_Version = version[1].replace(/[ ]/g, "");
        if (browser == "Microsoft Internet Explorer" && trim_Version == "MSIE8.0" || browser == "Microsoft Internet Explorer" && trim_Version == "MSIE7.0") {
            ieVideo.css({
                "width": "630px",
                "height": "354px"
            })
        }
    }

    //ipad横竖屏切换
    $(window).resize(function() {
        clearTimeout(timer);
        var timer = setTimeout(function() {
            $(".detail-slide-piclist").slider('resize');
        }, 0)
    });
    slide.initSlide();

    //视频内容右下角浮层小视频同步播放
    exports.videoMiniPlayer = function() {
        var videoPlace = $(".detail-video"),
            videoWrap = $('.video-wrap'),
            video = videoWrap.find("object"),
            videoIpad = videoWrap.find("video"),
            videoHeight,
            videoOffsetHeight,
            headerNavHeight,
            scrollTop,
            smallVideoMarginLeft;

        if (video.size() > 0) {
            $(window).on("resize", function() {
                $(".detail-video").css({
                    "width": videoIpad.outerWidth(),
                    "height": videoIpad.outerHeight()
                })
            }).resize();
            videoWrap.addClass("suspend");

            videoHeight = videoWrap.outerHeight();
            videoOffsetHeight = videoWrap.offset().top;
            headerNavHeight = $("#minEnterprise").height();
            scrollTop = videoHeight + videoOffsetHeight - headerNavHeight;


            $(window).on("scroll", function() {
                if ($(window).scrollTop() > scrollTop && !videoWrap.hasClass("current") && !videoWrap.hasClass("close") && !videoWrap.hasClass("suspend") && !videoWrap.hasClass("stop")) {
                    videoWrap.css("opacity", 0);
                    videoWrap.addClass("current");

                    smallVideoMarginLeft = $(".detail-container").width() / 2 - videoWrap.outerWidth();
                    videoWrap.css("margin-left", smallVideoMarginLeft);
                    videoWrap.animate({
                        "opacity": 1
                    }, 1000);
                } else if ($(window).scrollTop() < scrollTop) {
                    videoWrap.css("opacity", 1);
                    videoWrap.removeClass("close").removeClass("current").removeAttr("style");
                }
            });

            //小视频关闭按钮
            $(".video-close-btn").on("click", function() {
                videoWrap.animate({
                    "opacity": 0
                }, 500, function() {
                    videoWrap.addClass("close").removeClass("current");
                });

            })
        }
    };


}

window.updateState = function() {
    seajs.use("channel/detail", function(detail) {
        detail.updateLogin();
    });
};


window.picLimited = function() {
    seajs.use("jquery/1.8.3/jquery.js", function($) {
        if ($(".text-body") <= 0) return;
        $(".text-body").find("img").css("max-width", "700px");
    });
};

window.normarlPlayer = function() {
    seajs.use("jquery/1.8.3/jquery.js", function($) {
        $(".video-wrap").removeClass("current");
    });
};

window.stopPlayer = function() {
    seajs.use('jquery/1.8.3/jquery.js', function($) {
        if ($(".video-wrap").hasClass("current")) {
            $(".video-wrap").animate({
                "opacity": 0
            }, 500, function() {
                $(".video-wrap").addClass("stop").removeClass("current");
            });
        } else {
            $(".video-wrap").addClass("stop").removeClass("current");
        }
    });
};

window.suspendPlayer = function() {
    seajs.use("jquery/1.8.3/jquery.js", function($) {
        $(".video-wrap").addClass("suspend");
    });
};

window.startPlayer = function() {
    seajs.use("jquery/1.8.3/jquery.js", function($) {
        $(".video-wrap").removeClass("stop").removeClass("suspend");
    });
};

//视频不能同时播放
window.thisMovie = function(movieName) {
    if (navigator.appName.indexOf("Microsoft") != -1) {
        return window[movieName];
    } else {
        return document[movieName];
    }
};

window.StopAnotherMovie = function(id) {
    seajs.use("jquery/1.8.3/jquery.js", function($) {
        var allVideo = $("body").find("embed");
        allVideo.each(function() {
            window.StopMovie();
        });
    });
};