app/scripts/plugins/share.coffee

EPFL share plugin

Handle social share buttons

requires: jQuery or Zepto DOM library
author: Julien Ramboz
version: 1.0

AMD loader

Try loading as AMD module or fall back to default loading

((plugin) -> if typeof define is "function" and define.amd define ["jslib", "plugin", "toggle"], plugin else plugin @$, AbstractPlugin ).call this, share = ($, AbstractPlugin) -> "use strict"

Plugin

The actual plugin class

class Share extends AbstractPlugin

Default options

@defaultOptions: order: [ "facebook" "twitter" "linkedin" "googleplus" "mail" ] hashtags: ['epfl'] sites: facebook: label: "Facebook" shareLink: "www.facebook.com/sharer/sharer.php" + "?u=%url%" googleplus: label: "Google+" shareLink: "plus.google.com/share" + "?url=%url%" linkedin: label: "LinkedIn" shareLink: "www.linkedin.com/shareArticle" + "?url=%url%&title=%title%" reddit: label: "reddit" shareLink: "www.reddit.com/submit" + "?url=%url%&title=%title%" stumbleupon: label: "StumbleUpon" shareLink: "www.stumbleupon.com/submit" + "?url=%url%&title=%title%" twitter: label: "Twitter" shareLink: "twitter.com/intent/tweet" + "?url=%url%&text=%title%&hashtags=%hashtags%" mail: label: "Email" shareLink: "mailto:?subject=%title%&body=%url%"

Initialisation

Initializer function.

initialize: (options) -> @update $.extend {}, Share.options, options

Prepare the hashtags to share

prepareShareHashtags: (site) -> hashtags = [] fnc = @options.prepareHashtags hashtags .concat(@options.hashtags) .concat($.isFunction(fnc) && fnc(hashtags, site) || [])

Prepare the text to share

prepareShareText: (text, site) -> fnc = @options.prepareText $.isFunction(fnc) && fnc(text, site) || text

Prepare the share link

prepareShareLink: (link, url, title, site) -> preparedHashtags = @prepareShareHashtags site preparedText = @prepareShareText title, site if link.indexOf('%hashtags%') > -1 preparedHashtags = preparedHashtags.join(',') link = link.replace(new RegExp('%hashtags%', 'g'), preparedHashtags) else preparedText += ' ' + preparedHashtags.map( (i) -> '#' + i ).join(' ') escapedUrl = encodeURIComponent url escapedTitle = encodeURIComponent preparedText link .replace(new RegExp('%url%', 'g'), escapedUrl) .replace(new RegExp('%title%', 'g'), escapedTitle)

Update the share menu

update: (options) -> @options = $.extend true, @options, options @element.children(":not([data-role='label'])").remove() for site in @options.order @element.append @createButton site @share

Structure creation

Create elements required by the structure if they are not present

Create the share button

createButton: (site) -> conf = @options.sites[site] return $() if not conf url = window.location title = $(".page-title").clone().children().remove().end().text() || document.title shareLink = conf.shareLink shareLink = "https://" + shareLink unless shareLink.match(/^\w+:/) $button = $(document.createElement "a").attr "class": "share-link share-#{site}" "href": @prepareShareLink shareLink, url, title, site "target": "_blank", "title": conf.label $label = $(document.createElement "span").attr "class": "visuallyhidden" $label.text conf.label $button.append $label $icon = $(document.createElement "span").attr "class": "icon icon-share-#{site}_button" $button.prepend $icon $button

Cleanup

Cleanup the widget and remove remaining references

destructor: () -> @menu.remove() @menu = null super()

Installation

Install the plugin into the JS library

Share.install("Share", () -> $("#tools [role='toolbar']")["#{AbstractPlugin.namespace}share"]() )