/red/modules/social/twitter.social.js
JavaScript | 154 lines | 86 code | 35 blank | 33 comment | 14 complexity | 120211657293fcc3ccbd73f74fcf328a MD5 | raw file
1// ### Part of the [Rosy Framework](http://github.com/ff0000/rosy) 2/* shell.js */ 3 4// ## Local Namespace 5 6var red = red || {}; 7 8red.module = red.module || {}; 9 10red.module.social = red.module.social || {}; 11 12 13/** 14 * Requires DOM elements: 15 * <link rel="media_url"> 16 * <meta property="og:app_id"> 17 * <div id="fb-root"> 18 * Refer to http://yoast.com/social-buttons/ for more information on social-tracking-events 19 * 20 * add [data-custom-social="twitter"] to automatically fire customTweet() 21 * 22 * EXAMPLE 23 * <a 24 * data-custom-social-="twitter" // REQUIred 25 * data-url="http://example.com" // optional 26 * data-text="Some tweet you are sending" // optional 27 * >Twitter</a> 28 * 29 * Also, you can $.publish() a "custom-twitter-post" event to fire customTweet() 30 * 31 * Example 32 * $.publish("custom-twitter-post", {url:"http://example.com", text : "Some Tweet Message"}) 33 * 34 */ 35 36red.module.social.Twitter = (function () { 37 38 var EVENTS = { 39 POST : "custom-twitter-post", 40 RENDER : "social/render" 41 }; 42 43 return red.Module.extend({ 44 45 _twitter_url : "https://twitter.com/share?", 46 47 48 init : function () { 49 50 this.loadJSDK(); 51 52 $('[data-custom-social="twitter"]').live("click", $.proxy(this.customTweet, this)); 53 $.subscribe(EVENTS.POST, $.proxy(this.customTweet, this)); 54 $.subscribe(EVENTS.RENDER, $.proxy(this.render, this)); 55 }, 56 57 onTwitterInit : function () { 58 if (window.twttr) { 59 window.twttr.events.bind('tweet', $.proxy(this.onTweet, this)); 60 window.twttr.events.bind('follow', $.proxy(this.onFollow, this)); 61 } 62 }, 63 64 querystring : function (url) { // parses query-string 65 var a = /\+/g, // Regex for replacing addition symbol with a space 66 r = /([^&=]+)=?([^&]*)/g, 67 d = function (s) { 68 return decodeURIComponent(s.replace(a, " ")); 69 }, 70 q = url || window.location.search.substring(1), 71 qs = {}, 72 e = r.exec(q); 73 74 while (e) { 75 qs[d(e[1])] = d(e[2]); 76 e = r.exec(q); 77 } 78 79 return qs; 80 }, 81 82 // override this for tracking and such 83 // fires from custom tweet AND from @anywhere plugins 84 onTweet: function (e, eData) { 85 var el = (e.currentTarget) ? $(e.currentTarget) : $(e.target), 86 data = eData || el.data() || {}, 87 url = el.attr("src"), 88 tweeturl = this.querystring(url).url; 89 90 $.publish("track", [{type : "event", category: "twitter", action : "on-tweet", label : tweeturl }]); 91 92 return data; 93 }, 94 95 onFollow: function (e, eData) { 96 var el = $(e.currentTarget), 97 data = eData || el.data(); 98 99 $.publish("track", [{type : "event", category: "twitter", action : "on-follow", label : data.url}]); 100 return data; 101 }, 102 103 customTweet : function (e, eData) { 104 // needs URL and text 105 var el = $(e.currentTarget), 106 data = eData || el.data(), 107 url = this._twitter_url, 108 i; 109 110 data.url = data.url || window.location.href; 111 data.text = data.text || document.title; 112 113 for (i in data) { 114 if (typeof(data[i]) === 'string') { 115 url += "&" + i + "=" + encodeURIComponent(data[i]); 116 } 117 } 118 119 // passing through the click event to avoid blockers 120 window.open(url, 'sharer', 'toolbar=0,status=0,scrollbars=1,width=575,height=338'); 121 122 // fires onTweet 123 this.onTweet(e, eData); 124 125 }, 126 127 render : function () { 128 $.ajax({ url: '//platform.twitter.com/widgets.js', dataType: 'script', cache: true}); 129 }, 130 131 loadJSDK : function () { 132 133 //////////////////////////////////////////////////////////// 134 /////// TWITTER SHARING and tracking 135 // Load G+1 136 var e = document.createElement('script'); 137 e.type = "text/javascript"; 138 e.async = true; 139 e.src = 'http://platform.twitter.com/widgets.js'; 140 document.getElementsByTagName('head')[0].appendChild(e); 141 142 $(e).load($.proxy(function () { 143 this.onTwitterInit(); 144 }, this)); 145 }, 146 147 destroy : function () { 148 $('[data-custom-social="twitter"]').die("click", $.proxy(this.customTweet, this)); 149 $.unsubscribe(EVENTS.POST, $.proxy(this.customTweet, this)); 150 $.unsubscribe(EVENTS.RENDER, $.proxy(this.render, this)); 151 } 152 }, EVENTS); 153 154}.call(red.module.social));