/red/modules/social/twitter.social.js

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