magazine320.js 5.32 KB
/*
 * @description: 杂志页
 * @author: chenglong.wang@yoho.cn
 * @date: 2015/1/13
 */

var $      = require('jquery');
var swiper = require('yoho.swiper');
var Hammer = require('hammerjs');

var oShare = require("../plugin/share");

exports.init = function () {

    var buySlide=new swiper('#buy-slide',{
        paginationClickable: true,
        slidesPerView: 'auto',
        slideElement: 'li',
        onSlideChangeEnd: fallSwiperItems_buy
    });
    var ruleSlide=new swiper('.rules-slide',{
        wrapperClass:"rules-wrapper",
        paginationClickable: true,
        slidesPerView: 'auto',
        slideElement: 'li',
        onSlideChangeEnd: fallSwiperItems_rule
    });

    function fallSwiperItems_buy(){
        var currentSlide=arguments[0];
        _fallSwiperItems(currentSlide,"buy");
    }

    function fallSwiperItems_rule(){
        var currentSlide=arguments[0];
        _fallSwiperItems(currentSlide,"rule");
    }
    var currentPageNumber=1;
    function _fallSwiperItems(currentSlide,type){
        var url="";
        if(type=="buy") {
            url="/channel/magazine/page?type=1&p=";
        } else if( type=="zine") {
            url="/channel/magazine/page?type=2&p=";
        } else {
            url="/channel/magazine/rules?p=";
        }

        var totalLength=$(currentSlide.container).find(".swiper-slide").length;
        var currentIndex=currentSlide.activeIndex;
        if(totalLength-currentIndex<=6) {
            $.ajax({
                url:YohoConfig.mainUrl+url+(currentPageNumber++),
                cache:false,
                success:function(data){
                    for(var i=0;i<data.data.length;i++) {
                        var cItem=data.data[i];
                        var newAppend=currentSlide.createSlide("<a><img src='"+cItem.cover+"' dynamic='"+cItem.dynamic+"'/></a><time>"+cItem.title+"</time>");
                        newAppend.append();
                    }
                    var oLi=$(currentSlide.container).find(".swiper-slide");
                    var LastSlide = currentSlide.getLastSlide();
                    oLi.css({'width':'131px'});
                    $(LastSlide).css({'width':'121px'});
                    currentSlide.reInit();

                }
            });
        }
    }

    //e-zine
    $.ajax({
        type:'GET',
        url:YohoConfig.mainUrl+'/channel/magazine/ezine',
        success:function(response){
            var data = response.data;
            var len = data.length;
            var item ='';

            for(var i=0;i<len;i++){
                if (data[i].magazineType == 2) {
                    item += '<li class="swiper-slide"><a class="magazine-special" href="javascript:;" title="'+data[i].releaseDate+'"><img src="'+data[i].cover+'" alt="" /></a><time>'+data[i].releaseDate+'</time></li>';
                }else{
                    item += '<li class="swiper-slide"><a href="javascript:;" title="'+data[i].releaseDate+'"><img src="'+data[i].cover+'" alt="" /></a><time>'+data[i].releaseDate+'</time></li>';
                }
            }
            $(".zine-wrapper").html(item);
            var zineSlide=new swiper('.zine-slide',{
                wrapperClass:"zine-wrapper",
                paginationClickable: true,
                slidesPerView: 'auto',
                slideElement: 'li'
            });
        }
    });

    //rules动图弹层
    var mobile = $("#mobile");
    var rules = new Hammer(document.getElementById('rules'));
    rules.on('tap',function (event) {
        if ($(event.target).is($('img'))) {
            var rulesMark = $('<div id="rulesMark"><img src="" class="popImg" /></div>').appendTo(mobile);
            var url = $(event.target).attr('dynamic');
            var popImg = $('.popImg');
            popImg.attr('src',url);
            rulesMark.fadeIn();
        }
    });

    //关闭弹层
    var close = new Hammer(document.getElementById('mobile'));
    close.on('tap',function (event) {
        var $temp = $('#rulesMark');
        if ($(event.target).is($temp)) {
            $temp.fadeOut().remove();
        }
    });

    //分享
    var shareUrl = $("#share").attr("data-link");
    oShare.init({
        shareUrl:shareUrl,
        img:$("#share").attr("cover"),
        sinaText:'随时引领潮流生活,我正在看《YOHO!潮流志》'+$(".magazine-more").find("time").text()+'期点杂志,每月7日,17日,27日出刊哦~',
        fbTextFunction:function(){
            return {
                des:'随时引领潮流生活,我正在看《YOHO!潮流志》'+$(".magazine-more").find("time").text()+'期点杂志,每月7日,17日,27日出刊哦~',
                name:'随时引领潮流生活,我正在看《YOHO!潮流志》'+$(".magazine-more").find("time").text()+'期点杂志,每月7日,17日,27日出刊哦~',
                caption:'随时引领潮流生活,我正在看《YOHO!潮流志》'+$(".magazine-more").find("time").text()+'期点杂志,每月7日,17日,27日出刊哦~',
                url:location.href
            }
        },
        tweetText:'随时引领潮流生活,我正在看《YOHO!潮流志》'+$(".magazine-more").find("time").text()+'期点杂志,每月7日,17日,27日出刊哦~',
        qqText:'随时引领潮流生活,我正在看《YOHO!潮流志》'+$(".magazine-more").find("time").text()+'期点杂志,每月7日,17日,27日出刊哦~'
    });
};