From 7132e9a9789694d5b559b6e2f7486075a0a055e4 Mon Sep 17 00:00:00 2001 From: Bruce Hubbard Date: Tue, 3 Mar 2015 15:30:41 -0500 Subject: [PATCH] Fixing bug where having no data context would break sharing --- client/views/facebook/facebook.coffee | 20 +++++++++---------- client/views/googleplus/googleplus.coffee | 12 +++++------- client/views/pinterest/pinterest.coffee | 14 ++++++------- client/views/twitter/twitter.coffee | 24 +++++++++++------------ shareit.coffee | 15 +++++++++----- 5 files changed, 41 insertions(+), 44 deletions(-) diff --git a/client/views/facebook/facebook.coffee b/client/views/facebook/facebook.coffee index 5a8a5cf..ccf0066 100644 --- a/client/views/facebook/facebook.coffee +++ b/client/views/facebook/facebook.coffee @@ -1,10 +1,8 @@ Template.shareit_facebook.rendered = -> - return unless @data - @autorun -> - template = Template.instance() - data = Template.currentData() - + template = Template.instance() + data = Template.currentData() || ShareIt.defaultDataContext() + $('meta[property^="og:"]').remove() # # OpenGraph tags @@ -17,7 +15,7 @@ Template.shareit_facebook.rendered = -> $('', { property: 'og:url', content: url }).appendTo 'head' $('', { property: 'og:title', content: title }).appendTo 'head' $('', { property: 'og:description', content: description }).appendTo 'head' - + if data.thumbnail if typeof data.thumbnail == "function" img = data.thumbnail() @@ -26,9 +24,9 @@ Template.shareit_facebook.rendered = -> if img if not /^http(s?):\/\/+/.test(img) img = location.origin + img - + $('', { property: 'og:image', content: img }).appendTo 'head' - + if ShareIt.settings.sites.facebook.appId? template.$('.fb-share').click (e) -> e.preventDefault() @@ -45,9 +43,9 @@ Template.shareit_facebook.rendered = -> href = base + "?s=100&p[url]=" + url + "&p[title]=" + title + "&p[summary]=" + summary if img href += "&p[images][0]=" + encodeURIComponent img - + template.$(".fb-share").attr "href", href - + return - + Template.shareit_facebook.helpers(ShareIt.helpers) diff --git a/client/views/googleplus/googleplus.coffee b/client/views/googleplus/googleplus.coffee index 95b4665..e47c98d 100644 --- a/client/views/googleplus/googleplus.coffee +++ b/client/views/googleplus/googleplus.coffee @@ -1,11 +1,9 @@ Template.shareit_googleplus.rendered = () -> - return unless @data - @autorun -> template = Template.instance() - data = Template.currentData() + data = Template.currentData() || ShareIt.defaultDataContext() $('meta[itemscope]').remove() - + # # Schema tags # @@ -17,7 +15,7 @@ Template.shareit_googleplus.rendered = () -> $('', { itemprop: 'name', content: location.hostname }).appendTo 'head' $('', { itemprop: 'url', content: url }).appendTo 'head' $('', { itemprop: 'description', content: description }).appendTo 'head' - + if data.thumbnail if typeof data.thumbnail == "function" img = data.thumbnail() @@ -26,12 +24,12 @@ Template.shareit_googleplus.rendered = () -> if img if not /^http(s?):\/\/+/.test(img) img = location.origin + img - + $('', { itemprop: 'image', content: img }).appendTo 'head' # # Google share button # - + href = "https://plus.google.com/share?url=#{url}" template.$(".googleplus-share").attr "href", href diff --git a/client/views/pinterest/pinterest.coffee b/client/views/pinterest/pinterest.coffee index 8771643..e788977 100644 --- a/client/views/pinterest/pinterest.coffee +++ b/client/views/pinterest/pinterest.coffee @@ -1,21 +1,19 @@ Template.shareit_pinterest.rendered = -> - return unless @data - @autorun -> template = Template.instance() - data = Template.currentData() - + data = Template.currentData() || ShareIt.defaultDataContext() + preferred_url = data.url || location.origin + location.pathname url = encodeURIComponent preferred_url description = encodeURIComponent data.pinterest?.description || data.description - + href = "http://www.pinterest.com/pin/create/button/?url=#{url}&media=#{data.media}&description=#{description}" - + template.$('.pinterest-share').attr 'href', href - + Template.shareit_pinterest.events 'click a': (event, template) -> event.preventDefault() window.open $(template.find('.pinterest-share')).attr('href'), 'pinterest_window', 'width=750, height=650' -Template.shareit_pinterest.helpers(ShareIt.helpers) \ No newline at end of file +Template.shareit_pinterest.helpers(ShareIt.helpers) diff --git a/client/views/twitter/twitter.coffee b/client/views/twitter/twitter.coffee index 7e91f6d..78bd572 100644 --- a/client/views/twitter/twitter.coffee +++ b/client/views/twitter/twitter.coffee @@ -1,11 +1,9 @@ Template.shareit_twitter.rendered = -> - return unless @data - @autorun -> template = Template.instance() - data = Template.currentData() + data = Template.currentData() || ShareIt.defaultDataContext() $('meta[property^="twitter:"]').remove() - + if data.thumbnail if typeof data.thumbnail == "function" img = data.thumbnail() @@ -14,38 +12,38 @@ Template.shareit_twitter.rendered = -> if img if not /^http(s?):\/\/+/.test(img) img = location.origin + img - + # # Twitter cards # - + $('', { property: 'twitter:card', content: 'summary' }).appendTo 'head' # What should go here? #$('', { property: 'twitter:site', content: '' }).appendTo 'head' - + if data.author $('', { property: 'twitter:creator', content: data.author }).appendTo 'head' - + description = data.twitter?.description || data.excerpt || data.description || data.summary $('', { property: 'twitter:url', content: location.origin + location.pathname }).appendTo 'head' $('', { property: 'twitter:title', content: "#{data.title}" }).appendTo 'head' $('', { property: 'twitter:description', content: description }).appendTo 'head' $('', { property: 'twitter:image', content: img }).appendTo 'head' - + # # Twitter share button # - + preferred_url = data.url || location.origin + location.pathname url = encodeURIComponent preferred_url - + base = "https://twitter.com/intent/tweet" text = encodeURIComponent data.twitter?.title || data.title href = base + "?url=" + url + "&text=" + text - + if data.author href += "&via=" + data.author - + template.$(".tw-share").attr "href", href diff --git a/shareit.coffee b/shareit.coffee index bc8a4c4..5984cd3 100644 --- a/shareit.coffee +++ b/shareit.coffee @@ -2,7 +2,7 @@ ShareIt = { settings: autoInit: true buttons: 'responsive' - sites: + sites: 'facebook': 'appId': null 'version': 'v2.1' @@ -24,7 +24,13 @@ ShareIt = { configure: (hash) -> @settings = $.extend(true, @settings, hash) - + + defaultDataContext: () -> + { + url: location.origin + location.pathname, + title: document.title + } + helpers: { classes: () -> ShareIt.settings.classes @@ -46,7 +52,7 @@ ShareIt = { ShareIt.init = (hash) -> @settings = $.extend(true, @settings, hash) - + # Twitter window.twttr = do (d = document, s = 'script', id = 'twitter-wjs') -> t = undefined @@ -69,7 +75,7 @@ ShareIt.init = (hash) -> if ShareIt.settings.autoInit window.fbAsyncInit = -> FB.init(ShareIt.settings.sites.facebook) - + ((d, s, id) -> js = undefined fjs = d.getElementsByTagName(s)[0] @@ -81,4 +87,3 @@ ShareIt.init = (hash) -> fjs.parentNode.insertBefore js, fjs return ) document, 'script', 'facebook-jssdk' - \ No newline at end of file