index.js 6.6 KB
/**
 * @fileOverview 弹出各种分享的组件
 * @author:王威
 * @date:2014-09-19
 * yohoo-share-button-sina
 * yohoo-share-button-facebook
 * yohoo-share-button-tweet
 *     	fbAppId:YohoConfig.girl.fbAppId,'657464311006064',
    	fbUrl:YohoConfig.girl.mainUrl+'/channel/yohood/closedialog',
 */

var yohoShare=function(info){
		var $=require("jquery");
		var defaults={
			width:800,
			height:500,
			img:"",
			imgFunction:null,
			sinaText:"",
			sinaTextFunction:null,
			tweetText:"",
			tweetTextFunction:null,
			fbCaption:"",
			fbDescription:"",
			fbName:"",
			fbTextFunction:null,
			shareUrl:"",
			shareUrlFunction:null,
			fbAppId:"",
			fbUrl:"",
            qqText:""
		};
		var options=$.extend(defaults,info);

		

		
		$(".yohoo-share-button-sina").unbind().on("click", function()
        {
        	if(options.sinaTextFunction!=null)
			{
				options.sinaText=(function(){
					return options.sinaTextFunction.apply($(".yohoo-share-button-sina"));
				})();
			}
			options.sinaText=options.sinaText.replace(/[ ]/g, "%20")
                            .replace(/[&]/g,"%26")
                            .replace(/[#]/g,"%23")
                            .replace(/[+]/g,"%2B");
			
            window.sharetitle = options.sinaText;
            if(options.imgFunction!=null)
            {
            	options.img=(function(){
					return options.imgFunction.apply($(".yohoo-share-button-sina"));
				})();
            }
            window.shareUrl = options.img;
            share();
        });
        if(options.shareUrlFunction!=null)
        {
        	options.shareUrl=(function(){
        		return options.shareUrlFunction.apply({sina:$(".yohoo-share-button-sina"),
        												facebook:$(".yohoo-share-button-facebook"),
        												tweet:$(".yohoo-share-button-tweet")});
        	})();
        }
        else
        {
        	options.shareUrl=options.shareUrl==""?document.location.href:options.shareUrl;
        }
        
        function share()
        {
            (function(s, d, e) {
                try {
                } catch (e) {
                }
                var f = 'http://v.t.sina.com.cn/share/share.php?', u = options.shareUrl, p = ['url=', e(u), '&title=', e(window.sharetitle+' @YOHO潮流志'), '&appkey=3910025296', '&pic=', e(window.shareUrl)].join('');
                function a() {
                    if (!window.open([f, p].join(''), 'mb', ['toolbar=0,status=0,resizable=1,width=620,height=450,left=', (s.width - 620) / 2, ',top=', (s.height - 450) / 2].join('')))
                        u.href = [f, p].join('');
                };
                if (/Firefox/.test(navigator.userAgent)) {
                    setTimeout(a, 0);
                } else {
                    a();
                }
            })(screen, document, encodeURIComponent);
        }

        var iTop = (window.screen.availHeight - 30 - options.height) / 2; //获得窗口的垂直位置;
        var iLeft = (window.screen.availWidth - 10 - options.width) / 2; //获得窗口的水平位置;
        $(".yohoo-share-button-facebook").unbind().on("click",function() {
            if(options.imgFunction!=null)
            {
                options.img=(function(){
                    return options.imgFunction.apply($(".yohoo-share-button-facebook"));
                })();
            }
            var tmpResult={des:"",name:"",caption:""};
            if(options.fbTextFunction!=null)
            {
                tmpResult=(function(){
                    return options.fbTextFunction.apply($(".yohoo-share-button-facebook"));
                })();
                if (!tmpResult.url)
                {
                    options.fbCaption=tmpResult.caption;
                    options.fbDescription=tmpResult.des;
                    options.fbName=tmpResult.name;
                }
                else
                {
                    options.fbUrl = tmpResult.url;
                }
            }
                        
            if (options.fbUrl)
            {
                window.open("https://www.facebook.com/sharer/sharer.php?u=" + options.fbUrl, "",
                            "height="+options.height+",width="+options.width+",left=" + iLeft + ",top=" + iTop); 
            }
            else
            {
		options.fbCaption=options.fbCaption.replace(/[ ]/g, "%20")
							.replace(/[&]/g,"%26")
                            .replace(/[#]/g,"%23")
                            .replace(/[+]/g,"%2B");
		options.fbDescription=tmpResult.des.replace(/[ ]/g, "%20")
							.replace(/[&]/g,"%26")
                            .replace(/[#]/g,"%23")
                            .replace(/[+]/g,"%2B");
		options.fbName=tmpResult.name.replace(/[ ]/g, "%20")
							.replace(/[&]/g,"%26")
                            .replace(/[#]/g,"%23")
                            .replace(/[+]/g,"%2B");
                window.open("http://www.facebook.com/dialog/feed?app_id=1431491300468916&redirect_uri=http://www.yohoboys.com/channel/yohood/closedialog&" +
                    "link=" + options.shareUrl +
                    "&picture=" + options.img+
                    "&caption=" +options.fbName+
                    "&description="+options.fbDescription+(options.fbDescription==""?"":"(分享自 @yohoboy)")+
                    "&name="+options.fbCaption,
                    "",
                    "height="+options.height+",width="+options.width+",left=" + iLeft + ",top=" + iTop);
            }
        });

        $(".yohoo-share-button-tweet").unbind().on("click",function() {
        	if(options.tweetTextFunction!=null)
			{
				options.tweetText=(function(){
					return options.tweetTextFunction.apply($(".yohoo-share-button-tweet"));
				})();
			}
			options.tweetText=options.tweetText.replace(/[ ]/g, "%20")
							.replace(/[&]/g,"%26")
                            .replace(/[#]/g,"%23")
                            .replace(/[+]/g,"%2B");
            window.open("http://twitter.com/intent/tweet?url=" + options.shareUrl + 
                    "&text="+options.tweetText,
                    "",
                    "height="+options.height+",width="+options.width+",left=" + iLeft + ",top=" + iTop);
        });

        $(".yohoo-share-button-qq").unbind().on("click",function(){
            window.open('http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url='+ options.shareUrl+ 
                        '&title='+options.qqText+'&summary= '+
                        '&pics='+options.img,'_blank','height='+options.height+',width='+options.width+',left='+iLeft+',top='+iTop);
        });

        
  	
  	
  	//////////////////////////////////////////////////////////////////////////
  	
};

module.exports = {init:yohoShare};