From 837d442e88f7c70c1070605ed9b8cdff88060ee5 Mon Sep 17 00:00:00 2001 From: nicolasbeauvais Date: Sun, 3 Dec 2017 21:28:51 +0100 Subject: [PATCH] Possibility to add custom networks or override existing ones --- CHANGELOG.md | 4 ++ README.md | 35 ++++++++++ bower.json | 2 +- dist/vue-social-sharing.common.js | 104 +++++++++++++++++----------- dist/vue-social-sharing.js | 110 ++++++++++++++++++------------ dist/vue-social-sharing.min.js | 4 +- examples/vue2-example.html | 15 +++- package.json | 2 +- src/index.js | 2 +- src/social-sharing-network.js | 6 +- src/social-sharing.js | 31 ++++++++- test/unit/test.js | 2 +- 12 files changed, 223 insertions(+), 94 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3dd0626..17d3d46 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ + +# [2.3.1](https://github.com/nicolasbeauvais/vue-social-sharing/compare/2.2.11...2.3.1) (2017-12-03) +- Possibility to add custom networks or override existing ones + # [2.2.11](https://github.com/nicolasbeauvais/vue-social-sharing/compare/2.2.10...2.2.11) (2017-12-03) - Merge #61 - Add local events diff --git a/README.md b/README.md index c0d65cf..e17fa03 100644 --- a/README.md +++ b/README.md @@ -159,6 +159,41 @@ You can listen to a `vue-social-sharing` local event by using the following code > Note that the `social_shares_close` event is not fired for the Whatsapp, SMS and Email sharers. +## Extending the network list + +Since version `2.3.1` you can extend and override the list of available networks. You can see a working example of the feature in the `examples/vue2-example.html` file: + +```html + +
+ + Custom network + +
+
+ + +``` +There are two available network types: + +Type | Effect +---------------------- | -------------------------- +`popup` | Open the sharing link in a new popup +`direct` | Open directly the sharing link (suitable for mobile apps sharing, emails, sms, ...) + ## Feature request Feel free to open an issue to ask for a new social network support. diff --git a/bower.json b/bower.json index e30b154..72aacce 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "vue-social-sharing", - "version": "2.2.11", + "version": "2.3.1", "homepage": "https://github.com/nicolasbeauvais/vue-social-sharing", "authors": [ "nicolasbeauvais " diff --git a/dist/vue-social-sharing.common.js b/dist/vue-social-sharing.common.js index 39c1a27..7fe9736 100644 --- a/dist/vue-social-sharing.common.js +++ b/dist/vue-social-sharing.common.js @@ -1,42 +1,13 @@ /*! - * vue-social-sharing v2.2.11 + * vue-social-sharing v2.3.1 * (c) 2017 nicolasbeauvais * Released under the MIT License. */ 'use strict'; -var email = {"sharer":"mailto:?subject=@title&body=@url%0D%0A%0D%0A@description","type":"direct"}; -var facebook = {"sharer":"https://www.facebook.com/sharer/sharer.php?u=@url&title=@title&description=@description"e=@quote","type":"popup"}; -var googleplus = {"sharer":"https://plus.google.com/share?url=@url","type":"popup"}; -var line = {"sharer":"http://line.me/R/msg/text/?@description%0D%0A@url","type":"popup"}; -var linkedin = {"sharer":"https://www.linkedin.com/shareArticle?mini=true&url=@url&title=@title&summary=@description","type":"popup"}; -var odnoklassniki = {"sharer":"https://connect.ok.ru/dk?st.cmd=WidgetSharePreview&st.shareUrl=@url&st.comments=@description","type":"popup"}; -var pinterest = {"sharer":"https://pinterest.com/pin/create/button/?url=@url&media=@media&description=@title","type":"popup"}; -var reddit = {"sharer":"https://www.reddit.com/submit?url=@url&title=@title","type":"popup"}; -var skype = {"sharer":"https://web.skype.com/share?url=@description%0D%0A@url","type":"popup"}; -var telegram = {"sharer":"https://t.me/share/url?url=@url&text=@description","type":"popup"}; -var twitter = {"sharer":"https://twitter.com/intent/tweet?text=@title&url=@url&hashtags=@hashtags@twitteruser","type":"popup"}; -var vk = {"sharer":"https://vk.com/share.php?url=@url&title=@title&description=@description&image=@media&noparse=true","type":"popup"}; -var weibo = {"sharer":"http://service.weibo.com/share/share.php?url=@url&title=@title","type":"popup"}; -var whatsapp = {"sharer":"whatsapp://send?text=@description%0D%0A@url","type":"direct","action":"share/whatsapp/share"}; -var sms = {"sharer":"sms:?body=@url%20@description","type":"direct"}; -var Networks = { - email: email, - facebook: facebook, - googleplus: googleplus, - line: line, - linkedin: linkedin, - odnoklassniki: odnoklassniki, - pinterest: pinterest, - reddit: reddit, - skype: skype, - telegram: telegram, - twitter: twitter, - vk: vk, - weibo: weibo, - whatsapp: whatsapp, - sms: sms -}; +function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } + +var Vue = _interopDefault(require('vue')); var SocialSharingNetwork = { functional: true, @@ -48,8 +19,8 @@ var SocialSharingNetwork = { } }, - render: function render (createElement, context) { - var network = Networks[context.props.network]; + render: function (createElement, context) { + var network = context.parent._data.baseNetworks[context.props.network]; if (!network) { return console.warn(("Network " + (context.props.network) + " does not exist")); @@ -78,6 +49,39 @@ var SocialSharingNetwork = { } }; +var email = {"sharer":"mailto:?subject=@title&body=@url%0D%0A%0D%0A@description","type":"direct"}; +var facebook = {"sharer":"https://www.facebook.com/sharer/sharer.php?u=@url&title=@title&description=@description"e=@quote","type":"popup"}; +var googleplus = {"sharer":"https://plus.google.com/share?url=@url","type":"popup"}; +var line = {"sharer":"http://line.me/R/msg/text/?@description%0D%0A@url","type":"popup"}; +var linkedin = {"sharer":"https://www.linkedin.com/shareArticle?mini=true&url=@url&title=@title&summary=@description","type":"popup"}; +var odnoklassniki = {"sharer":"https://connect.ok.ru/dk?st.cmd=WidgetSharePreview&st.shareUrl=@url&st.comments=@description","type":"popup"}; +var pinterest = {"sharer":"https://pinterest.com/pin/create/button/?url=@url&media=@media&description=@title","type":"popup"}; +var reddit = {"sharer":"https://www.reddit.com/submit?url=@url&title=@title","type":"popup"}; +var skype = {"sharer":"https://web.skype.com/share?url=@description%0D%0A@url","type":"popup"}; +var telegram = {"sharer":"https://t.me/share/url?url=@url&text=@description","type":"popup"}; +var twitter = {"sharer":"https://twitter.com/intent/tweet?text=@title&url=@url&hashtags=@hashtags@twitteruser","type":"popup"}; +var vk = {"sharer":"https://vk.com/share.php?url=@url&title=@title&description=@description&image=@media&noparse=true","type":"popup"}; +var weibo = {"sharer":"http://service.weibo.com/share/share.php?url=@url&title=@title","type":"popup"}; +var whatsapp = {"sharer":"whatsapp://send?text=@description%0D%0A@url","type":"direct","action":"share/whatsapp/share"}; +var sms = {"sharer":"sms:?body=@url%20@description","type":"direct"}; +var BaseNetworks = { + email: email, + facebook: facebook, + googleplus: googleplus, + line: line, + linkedin: linkedin, + odnoklassniki: odnoklassniki, + pinterest: pinterest, + reddit: reddit, + skype: skype, + telegram: telegram, + twitter: twitter, + vk: vk, + weibo: weibo, + whatsapp: whatsapp, + sms: sms +}; + var inBrowser = typeof window !== 'undefined'; var $window = inBrowser ? window : null; @@ -172,6 +176,17 @@ var SocialSharing = { networkTag: { type: String, default: 'span' + }, + + /** + * Additional or overridden networks. + * Default to BaseNetworks + */ + networks: { + type: Object, + default: function () { + return {}; + } } }, @@ -181,7 +196,7 @@ var SocialSharing = { * Available sharing networks. * @param object */ - networks: Networks, + baseNetworks: BaseNetworks, /** * Popup settings. @@ -212,7 +227,7 @@ var SocialSharing = { * @param network Social network key. */ createSharingUrl: function createSharingUrl (network) { - return this.networks[network].sharer + return this.baseNetworks[network].sharer .replace(/@url/g, encodeURIComponent(this.url)) .replace(/@title/g, encodeURIComponent(this.title)) .replace(/@description/g, encodeURIComponent(this.description)) @@ -229,6 +244,7 @@ var SocialSharing = { */ share: function share (network) { this.openSharer(network, this.createSharingUrl(network)); + this.$root.$emit('social_shares_open', network, this.url); this.$emit('open', network, this.url); }, @@ -240,6 +256,7 @@ var SocialSharing = { */ touch: function touch (network) { window.open(this.createSharingUrl(network), '_self'); + this.$root.$emit('social_shares_open', network, this.url); this.$emit('open', network, this.url); }, @@ -255,7 +272,9 @@ var SocialSharing = { // If a popup window already exist it will be replaced, trigger a close event. if (this.popup.window && this.popup.interval) { clearInterval(this.popup.interval); + this.popup.window.close();// Force close (for Facebook) + this.$root.$emit('social_shares_change', network, this.url); this.$emit('change', network, this.url); } @@ -284,7 +303,9 @@ var SocialSharing = { this.popup.interval = setInterval(function () { if (this$1.popup.window.closed) { clearInterval(this$1.popup.interval); + this$1.popup.window = undefined; + this$1.$root.$emit('social_shares_close', network, this$1.url); this$1.$emit('close', network, this$1.url); } @@ -292,6 +313,13 @@ var SocialSharing = { } }, + /** + * Merge base networks list with user's list + */ + beforeMount: function beforeMount () { + this.baseNetworks = Vue.util.extend(this.baseNetworks, this.networks); + }, + /** * Sets popup default dimensions. */ @@ -322,7 +350,7 @@ var SocialSharing = { } }; -SocialSharing.version = '2.2.11'; +SocialSharing.version = '2.3.1'; SocialSharing.install = function (Vue) { Vue.component('social-sharing', SocialSharing); diff --git a/dist/vue-social-sharing.js b/dist/vue-social-sharing.js index 9615a8d..a4065a8 100644 --- a/dist/vue-social-sharing.js +++ b/dist/vue-social-sharing.js @@ -1,46 +1,15 @@ /*! - * vue-social-sharing v2.2.11 + * vue-social-sharing v2.3.1 * (c) 2017 nicolasbeauvais * Released under the MIT License. */ (function (global, factory) { - typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : - typeof define === 'function' && define.amd ? define(factory) : - (global.VueSocialSharing = factory()); -}(this, (function () { 'use strict'; + typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('vue')) : + typeof define === 'function' && define.amd ? define(['vue'], factory) : + (global.VueSocialSharing = factory(global.Vue)); +}(this, (function (Vue) { 'use strict'; -var email = {"sharer":"mailto:?subject=@title&body=@url%0D%0A%0D%0A@description","type":"direct"}; -var facebook = {"sharer":"https://www.facebook.com/sharer/sharer.php?u=@url&title=@title&description=@description"e=@quote","type":"popup"}; -var googleplus = {"sharer":"https://plus.google.com/share?url=@url","type":"popup"}; -var line = {"sharer":"http://line.me/R/msg/text/?@description%0D%0A@url","type":"popup"}; -var linkedin = {"sharer":"https://www.linkedin.com/shareArticle?mini=true&url=@url&title=@title&summary=@description","type":"popup"}; -var odnoklassniki = {"sharer":"https://connect.ok.ru/dk?st.cmd=WidgetSharePreview&st.shareUrl=@url&st.comments=@description","type":"popup"}; -var pinterest = {"sharer":"https://pinterest.com/pin/create/button/?url=@url&media=@media&description=@title","type":"popup"}; -var reddit = {"sharer":"https://www.reddit.com/submit?url=@url&title=@title","type":"popup"}; -var skype = {"sharer":"https://web.skype.com/share?url=@description%0D%0A@url","type":"popup"}; -var telegram = {"sharer":"https://t.me/share/url?url=@url&text=@description","type":"popup"}; -var twitter = {"sharer":"https://twitter.com/intent/tweet?text=@title&url=@url&hashtags=@hashtags@twitteruser","type":"popup"}; -var vk = {"sharer":"https://vk.com/share.php?url=@url&title=@title&description=@description&image=@media&noparse=true","type":"popup"}; -var weibo = {"sharer":"http://service.weibo.com/share/share.php?url=@url&title=@title","type":"popup"}; -var whatsapp = {"sharer":"whatsapp://send?text=@description%0D%0A@url","type":"direct","action":"share/whatsapp/share"}; -var sms = {"sharer":"sms:?body=@url%20@description","type":"direct"}; -var Networks = { - email: email, - facebook: facebook, - googleplus: googleplus, - line: line, - linkedin: linkedin, - odnoklassniki: odnoklassniki, - pinterest: pinterest, - reddit: reddit, - skype: skype, - telegram: telegram, - twitter: twitter, - vk: vk, - weibo: weibo, - whatsapp: whatsapp, - sms: sms -}; +Vue = 'default' in Vue ? Vue['default'] : Vue; var SocialSharingNetwork = { functional: true, @@ -52,8 +21,8 @@ var SocialSharingNetwork = { } }, - render: function render (createElement, context) { - var network = Networks[context.props.network]; + render: function (createElement, context) { + var network = context.parent._data.baseNetworks[context.props.network]; if (!network) { return console.warn(("Network " + (context.props.network) + " does not exist")); @@ -82,6 +51,39 @@ var SocialSharingNetwork = { } }; +var email = {"sharer":"mailto:?subject=@title&body=@url%0D%0A%0D%0A@description","type":"direct"}; +var facebook = {"sharer":"https://www.facebook.com/sharer/sharer.php?u=@url&title=@title&description=@description"e=@quote","type":"popup"}; +var googleplus = {"sharer":"https://plus.google.com/share?url=@url","type":"popup"}; +var line = {"sharer":"http://line.me/R/msg/text/?@description%0D%0A@url","type":"popup"}; +var linkedin = {"sharer":"https://www.linkedin.com/shareArticle?mini=true&url=@url&title=@title&summary=@description","type":"popup"}; +var odnoklassniki = {"sharer":"https://connect.ok.ru/dk?st.cmd=WidgetSharePreview&st.shareUrl=@url&st.comments=@description","type":"popup"}; +var pinterest = {"sharer":"https://pinterest.com/pin/create/button/?url=@url&media=@media&description=@title","type":"popup"}; +var reddit = {"sharer":"https://www.reddit.com/submit?url=@url&title=@title","type":"popup"}; +var skype = {"sharer":"https://web.skype.com/share?url=@description%0D%0A@url","type":"popup"}; +var telegram = {"sharer":"https://t.me/share/url?url=@url&text=@description","type":"popup"}; +var twitter = {"sharer":"https://twitter.com/intent/tweet?text=@title&url=@url&hashtags=@hashtags@twitteruser","type":"popup"}; +var vk = {"sharer":"https://vk.com/share.php?url=@url&title=@title&description=@description&image=@media&noparse=true","type":"popup"}; +var weibo = {"sharer":"http://service.weibo.com/share/share.php?url=@url&title=@title","type":"popup"}; +var whatsapp = {"sharer":"whatsapp://send?text=@description%0D%0A@url","type":"direct","action":"share/whatsapp/share"}; +var sms = {"sharer":"sms:?body=@url%20@description","type":"direct"}; +var BaseNetworks = { + email: email, + facebook: facebook, + googleplus: googleplus, + line: line, + linkedin: linkedin, + odnoklassniki: odnoklassniki, + pinterest: pinterest, + reddit: reddit, + skype: skype, + telegram: telegram, + twitter: twitter, + vk: vk, + weibo: weibo, + whatsapp: whatsapp, + sms: sms +}; + var inBrowser = typeof window !== 'undefined'; var $window = inBrowser ? window : null; @@ -176,6 +178,17 @@ var SocialSharing = { networkTag: { type: String, default: 'span' + }, + + /** + * Additional or overridden networks. + * Default to BaseNetworks + */ + networks: { + type: Object, + default: function () { + return {}; + } } }, @@ -185,7 +198,7 @@ var SocialSharing = { * Available sharing networks. * @param object */ - networks: Networks, + baseNetworks: BaseNetworks, /** * Popup settings. @@ -216,7 +229,7 @@ var SocialSharing = { * @param network Social network key. */ createSharingUrl: function createSharingUrl (network) { - return this.networks[network].sharer + return this.baseNetworks[network].sharer .replace(/@url/g, encodeURIComponent(this.url)) .replace(/@title/g, encodeURIComponent(this.title)) .replace(/@description/g, encodeURIComponent(this.description)) @@ -233,6 +246,7 @@ var SocialSharing = { */ share: function share (network) { this.openSharer(network, this.createSharingUrl(network)); + this.$root.$emit('social_shares_open', network, this.url); this.$emit('open', network, this.url); }, @@ -244,6 +258,7 @@ var SocialSharing = { */ touch: function touch (network) { window.open(this.createSharingUrl(network), '_self'); + this.$root.$emit('social_shares_open', network, this.url); this.$emit('open', network, this.url); }, @@ -259,7 +274,9 @@ var SocialSharing = { // If a popup window already exist it will be replaced, trigger a close event. if (this.popup.window && this.popup.interval) { clearInterval(this.popup.interval); + this.popup.window.close();// Force close (for Facebook) + this.$root.$emit('social_shares_change', network, this.url); this.$emit('change', network, this.url); } @@ -288,7 +305,9 @@ var SocialSharing = { this.popup.interval = setInterval(function () { if (this$1.popup.window.closed) { clearInterval(this$1.popup.interval); + this$1.popup.window = undefined; + this$1.$root.$emit('social_shares_close', network, this$1.url); this$1.$emit('close', network, this$1.url); } @@ -296,6 +315,13 @@ var SocialSharing = { } }, + /** + * Merge base networks list with user's list + */ + beforeMount: function beforeMount () { + this.baseNetworks = Vue.util.extend(this.baseNetworks, this.networks); + }, + /** * Sets popup default dimensions. */ @@ -326,7 +352,7 @@ var SocialSharing = { } }; -SocialSharing.version = '2.2.11'; +SocialSharing.version = '2.3.1'; SocialSharing.install = function (Vue) { Vue.component('social-sharing', SocialSharing); diff --git a/dist/vue-social-sharing.min.js b/dist/vue-social-sharing.min.js index bc03c87..8316db4 100644 --- a/dist/vue-social-sharing.min.js +++ b/dist/vue-social-sharing.min.js @@ -1,6 +1,6 @@ /*! - * vue-social-sharing v2.2.11 + * vue-social-sharing v2.3.1 * (c) 2017 nicolasbeauvais * Released under the MIT License. */ -!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):t.VueSocialSharing=e()}(this,function(){"use strict";var t={sharer:"mailto:?subject=@title&body=@url%0D%0A%0D%0A@description",type:"direct"},e={sharer:"https://www.facebook.com/sharer/sharer.php?u=@url&title=@title&description=@description"e=@quote",type:"popup"},i={sharer:"https://plus.google.com/share?url=@url",type:"popup"},r={sharer:"http://line.me/R/msg/text/?@description%0D%0A@url",type:"popup"},o={sharer:"https://www.linkedin.com/shareArticle?mini=true&url=@url&title=@title&summary=@description",type:"popup"},p={sharer:"https://connect.ok.ru/dk?st.cmd=WidgetSharePreview&st.shareUrl=@url&st.comments=@description",type:"popup"},s={sharer:"https://pinterest.com/pin/create/button/?url=@url&media=@media&description=@title",type:"popup"},n={sharer:"https://www.reddit.com/submit?url=@url&title=@title",type:"popup"},a={sharer:"https://web.skype.com/share?url=@description%0D%0A@url",type:"popup"},l={sharer:"https://t.me/share/url?url=@url&text=@description",type:"popup"},u={sharer:"https://twitter.com/intent/tweet?text=@title&url=@url&hashtags=@hashtags@twitteruser",type:"popup"},h={sharer:"https://vk.com/share.php?url=@url&title=@title&description=@description&image=@media&noparse=true",type:"popup"},c={sharer:"http://service.weibo.com/share/share.php?url=@url&title=@title",type:"popup"},d={sharer:"whatsapp://send?text=@description%0D%0A@url",type:"direct",action:"share/whatsapp/share"},w={sharer:"sms:?body=@url%20@description",type:"direct"},m={email:t,facebook:e,googleplus:i,line:r,linkedin:o,odnoklassniki:p,pinterest:s,reddit:n,skype:a,telegram:l,twitter:u,vk:h,weibo:c,whatsapp:d,sms:w},g={functional:!0,props:{network:{type:String,default:""}},render:function(t,e){var i=m[e.props.network];return i?t(e.parent.networkTag,{staticClass:e.data.staticClass||null,staticStyle:e.data.staticStyle||null,class:e.data.class||null,style:e.data.style||null,attrs:{id:e.data.attrs.id||null,"data-link":"popup"===i.type?"#share-"+e.props.network:e.parent.createSharingUrl(e.props.network),"data-action":"popup"===i.type?null:i.action},on:{click:"popup"===i.type?function(){e.parent.share(e.props.network)}:function(){e.parent.touch(e.props.network)}}},e.children):console.warn("Network "+e.props.network+" does not exist")}},f="undefined"!=typeof window,y=f?window:null,k={props:{url:{type:String,default:f?window.location.href:""},title:{type:String,default:""},description:{type:String,default:""},quote:{type:String,default:""},hashtags:{type:String,default:""},twitterUser:{type:String,default:""},withCounts:{type:[String,Boolean],default:!1},googleKey:{type:String,default:void 0},media:{type:String,default:""},networkTag:{type:String,default:"span"}},data:function(){return{networks:m,popup:{status:!1,resizable:!0,toolbar:!1,menubar:!1,scrollbars:!1,location:!1,directories:!1,width:626,height:436,top:0,left:0,window:void 0,interval:null}}},methods:{createSharingUrl:function(t){return this.networks[t].sharer.replace(/@url/g,encodeURIComponent(this.url)).replace(/@title/g,encodeURIComponent(this.title)).replace(/@description/g,encodeURIComponent(this.description)).replace(/@quote/g,encodeURIComponent(this.quote)).replace(/@hashtags/g,this.hashtags).replace(/@media/g,this.media).replace(/@twitteruser/g,this.twitterUser?"&via="+this.twitterUser:"")},share:function(t){this.openSharer(t,this.createSharingUrl(t)),this.$root.$emit("social_shares_open",t,this.url),this.$emit("open",t,this.url)},touch:function(t){window.open(this.createSharingUrl(t),"_self"),this.$root.$emit("social_shares_open",t,this.url),this.$emit("open",t,this.url)},openSharer:function(t,e){var i=this;this.popup.window&&this.popup.interval&&(clearInterval(this.popup.interval),this.popup.window.close(),this.$root.$emit("social_shares_change",t,this.url),this.$emit("change",t,this.url)),this.popup.window=window.open(e,"sharer","status="+(this.popup.status?"yes":"no")+",height="+this.popup.height+",width="+this.popup.width+",resizable="+(this.popup.resizable?"yes":"no")+",left="+this.popup.left+",top="+this.popup.top+",screenX="+this.popup.left+",screenY="+this.popup.top+",toolbar="+(this.popup.toolbar?"yes":"no")+",menubar="+(this.popup.menubar?"yes":"no")+",scrollbars="+(this.popup.scrollbars?"yes":"no")+",location="+(this.popup.location?"yes":"no")+",directories="+(this.popup.directories?"yes":"no")),this.popup.window.focus(),this.popup.interval=setInterval(function(){i.popup.window.closed&&(clearInterval(i.popup.interval),i.popup.window=void 0,i.$root.$emit("social_shares_close",t,i.url),i.$emit("close",t,i.url))},500)}},mounted:function(){if(f){var t=void 0!==y.screenLeft?y.screenLeft:screen.left,e=void 0!==y.screenTop?y.screenTop:screen.top,i=y.innerWidth?y.innerWidth:document.documentElement.clientWidth?document.documentElement.clientWidth:screen.width,r=y.innerHeight?y.innerHeight:document.documentElement.clientHeight?document.documentElement.clientHeight:screen.height;this.popup.left=i/2-this.popup.width/2+t,this.popup.top=r/2-this.popup.height/2+e}},components:{network:g}};return k.version="2.2.11",k.install=function(t){t.component("social-sharing",k)},"undefined"!=typeof window&&(window.SocialSharing=k),k}); \ No newline at end of file +!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e(require("vue")):"function"==typeof define&&define.amd?define(["vue"],e):t.VueSocialSharing=e(t.Vue)}(this,function(t){"use strict";t="default"in t?t.default:t;var e={functional:!0,props:{network:{type:String,default:""}},render:function(t,e){var i=e.parent._data.baseNetworks[e.props.network];return i?t(e.parent.networkTag,{staticClass:e.data.staticClass||null,staticStyle:e.data.staticStyle||null,class:e.data.class||null,style:e.data.style||null,attrs:{id:e.data.attrs.id||null,"data-link":"popup"===i.type?"#share-"+e.props.network:e.parent.createSharingUrl(e.props.network),"data-action":"popup"===i.type?null:i.action},on:{click:"popup"===i.type?function(){e.parent.share(e.props.network)}:function(){e.parent.touch(e.props.network)}}},e.children):console.warn("Network "+e.props.network+" does not exist")}},i={sharer:"mailto:?subject=@title&body=@url%0D%0A%0D%0A@description",type:"direct"},r={sharer:"https://www.facebook.com/sharer/sharer.php?u=@url&title=@title&description=@description"e=@quote",type:"popup"},o={sharer:"https://plus.google.com/share?url=@url",type:"popup"},p={sharer:"http://line.me/R/msg/text/?@description%0D%0A@url",type:"popup"},s={sharer:"https://www.linkedin.com/shareArticle?mini=true&url=@url&title=@title&summary=@description",type:"popup"},n={sharer:"https://connect.ok.ru/dk?st.cmd=WidgetSharePreview&st.shareUrl=@url&st.comments=@description",type:"popup"},a={sharer:"https://pinterest.com/pin/create/button/?url=@url&media=@media&description=@title",type:"popup"},l={sharer:"https://www.reddit.com/submit?url=@url&title=@title",type:"popup"},u={sharer:"https://web.skype.com/share?url=@description%0D%0A@url",type:"popup"},h={sharer:"https://t.me/share/url?url=@url&text=@description",type:"popup"},c={sharer:"https://twitter.com/intent/tweet?text=@title&url=@url&hashtags=@hashtags@twitteruser",type:"popup"},d={sharer:"https://vk.com/share.php?url=@url&title=@title&description=@description&image=@media&noparse=true",type:"popup"},w={sharer:"http://service.weibo.com/share/share.php?url=@url&title=@title",type:"popup"},m={sharer:"whatsapp://send?text=@description%0D%0A@url",type:"direct",action:"share/whatsapp/share"},f={sharer:"sms:?body=@url%20@description",type:"direct"},g={email:i,facebook:r,googleplus:o,line:p,linkedin:s,odnoklassniki:n,pinterest:a,reddit:l,skype:u,telegram:h,twitter:c,vk:d,weibo:w,whatsapp:m,sms:f},y="undefined"!=typeof window,k=y?window:null,b={props:{url:{type:String,default:y?window.location.href:""},title:{type:String,default:""},description:{type:String,default:""},quote:{type:String,default:""},hashtags:{type:String,default:""},twitterUser:{type:String,default:""},withCounts:{type:[String,Boolean],default:!1},googleKey:{type:String,default:void 0},media:{type:String,default:""},networkTag:{type:String,default:"span"},networks:{type:Object,default:function(){return{}}}},data:function(){return{baseNetworks:g,popup:{status:!1,resizable:!0,toolbar:!1,menubar:!1,scrollbars:!1,location:!1,directories:!1,width:626,height:436,top:0,left:0,window:void 0,interval:null}}},methods:{createSharingUrl:function(t){return this.baseNetworks[t].sharer.replace(/@url/g,encodeURIComponent(this.url)).replace(/@title/g,encodeURIComponent(this.title)).replace(/@description/g,encodeURIComponent(this.description)).replace(/@quote/g,encodeURIComponent(this.quote)).replace(/@hashtags/g,this.hashtags).replace(/@media/g,this.media).replace(/@twitteruser/g,this.twitterUser?"&via="+this.twitterUser:"")},share:function(t){this.openSharer(t,this.createSharingUrl(t)),this.$root.$emit("social_shares_open",t,this.url),this.$emit("open",t,this.url)},touch:function(t){window.open(this.createSharingUrl(t),"_self"),this.$root.$emit("social_shares_open",t,this.url),this.$emit("open",t,this.url)},openSharer:function(t,e){var i=this;this.popup.window&&this.popup.interval&&(clearInterval(this.popup.interval),this.popup.window.close(),this.$root.$emit("social_shares_change",t,this.url),this.$emit("change",t,this.url)),this.popup.window=window.open(e,"sharer","status="+(this.popup.status?"yes":"no")+",height="+this.popup.height+",width="+this.popup.width+",resizable="+(this.popup.resizable?"yes":"no")+",left="+this.popup.left+",top="+this.popup.top+",screenX="+this.popup.left+",screenY="+this.popup.top+",toolbar="+(this.popup.toolbar?"yes":"no")+",menubar="+(this.popup.menubar?"yes":"no")+",scrollbars="+(this.popup.scrollbars?"yes":"no")+",location="+(this.popup.location?"yes":"no")+",directories="+(this.popup.directories?"yes":"no")),this.popup.window.focus(),this.popup.interval=setInterval(function(){i.popup.window.closed&&(clearInterval(i.popup.interval),i.popup.window=void 0,i.$root.$emit("social_shares_close",t,i.url),i.$emit("close",t,i.url))},500)}},beforeMount:function(){this.baseNetworks=t.util.extend(this.baseNetworks,this.networks)},mounted:function(){if(y){var t=void 0!==k.screenLeft?k.screenLeft:screen.left,e=void 0!==k.screenTop?k.screenTop:screen.top,i=k.innerWidth?k.innerWidth:document.documentElement.clientWidth?document.documentElement.clientWidth:screen.width,r=k.innerHeight?k.innerHeight:document.documentElement.clientHeight?document.documentElement.clientHeight:screen.height;this.popup.left=i/2-this.popup.width/2+t,this.popup.top=r/2-this.popup.height/2+e}},components:{network:e}};return b.version="2.3.1",b.install=function(t){t.component("social-sharing",b)},"undefined"!=typeof window&&(window.SocialSharing=b),b}); \ No newline at end of file diff --git a/examples/vue2-example.html b/examples/vue2-example.html index 23c9202..0e3fc10 100644 --- a/examples/vue2-example.html +++ b/examples/vue2-example.html @@ -27,6 +27,7 @@ quote="Vue is a progressive framework for building user interfaces." hashtags="vuejs,javascript,framework" twitter-user="vuejs" + :networks="overriddenNetworks" v-cloak inline-template>
    @@ -100,6 +101,11 @@ Ok +
  • + + Test custom network + +
@@ -109,7 +115,14 @@ new Vue({ el: '#app', - data: {} + data: { + overriddenNetworks: { + "test": { + "sharer": "https://google.com", + "type": "popup" + }, + } + } }); diff --git a/package.json b/package.json index 29a970b..008b9d9 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "vue-social-sharing", "description": "A Vue.js component for sharing links to social networks", - "version": "2.2.11", + "version": "2.3.1", "author": { "name": "nicolasbeauvais", "email": "nicolasbeauvais1@gmail.com" diff --git a/src/index.js b/src/index.js index ad8ea1b..84d7fa0 100644 --- a/src/index.js +++ b/src/index.js @@ -1,6 +1,6 @@ import SocialSharing from './social-sharing'; -SocialSharing.version = '2.2.11'; +SocialSharing.version = '2.3.1'; SocialSharing.install = (Vue) => { Vue.component('social-sharing', SocialSharing); diff --git a/src/social-sharing-network.js b/src/social-sharing-network.js index 2ce3406..0d03a4b 100644 --- a/src/social-sharing-network.js +++ b/src/social-sharing-network.js @@ -1,5 +1,3 @@ -import Networks from './networks.json'; - export default { functional: true, @@ -10,8 +8,8 @@ export default { } }, - render (createElement, context) { - const network = Networks[context.props.network]; + render: (createElement, context) => { + const network = context.parent._data.baseNetworks[context.props.network]; if (!network) { return console.warn(`Network ${context.props.network} does not exist`); diff --git a/src/social-sharing.js b/src/social-sharing.js index 5fdef84..22acc63 100644 --- a/src/social-sharing.js +++ b/src/social-sharing.js @@ -1,5 +1,6 @@ import SocialSharingNetwork from './social-sharing-network'; -import Networks from './networks.json'; +import BaseNetworks from './networks.json'; +import Vue from 'vue'; const inBrowser = typeof window !== 'undefined'; var $window = inBrowser ? window : null; @@ -99,6 +100,17 @@ export default { networkTag: { type: String, default: 'span' + }, + + /** + * Additional or overridden networks. + * Default to BaseNetworks + */ + networks: { + type: Object, + default: function () { + return {}; + } } }, @@ -108,7 +120,7 @@ export default { * Available sharing networks. * @param object */ - networks: Networks, + baseNetworks: BaseNetworks, /** * Popup settings. @@ -139,7 +151,7 @@ export default { * @param network Social network key. */ createSharingUrl (network) { - return this.networks[network].sharer + return this.baseNetworks[network].sharer .replace(/@url/g, encodeURIComponent(this.url)) .replace(/@title/g, encodeURIComponent(this.title)) .replace(/@description/g, encodeURIComponent(this.description)) @@ -156,6 +168,7 @@ export default { */ share (network) { this.openSharer(network, this.createSharingUrl(network)); + this.$root.$emit('social_shares_open', network, this.url); this.$emit('open', network, this.url); }, @@ -167,6 +180,7 @@ export default { */ touch (network) { window.open(this.createSharingUrl(network), '_self'); + this.$root.$emit('social_shares_open', network, this.url); this.$emit('open', network, this.url); }, @@ -180,7 +194,9 @@ export default { // If a popup window already exist it will be replaced, trigger a close event. if (this.popup.window && this.popup.interval) { clearInterval(this.popup.interval); + this.popup.window.close();// Force close (for Facebook) + this.$root.$emit('social_shares_change', network, this.url); this.$emit('change', network, this.url); } @@ -209,7 +225,9 @@ export default { this.popup.interval = setInterval(() => { if (this.popup.window.closed) { clearInterval(this.popup.interval); + this.popup.window = undefined; + this.$root.$emit('social_shares_close', network, this.url); this.$emit('close', network, this.url); } @@ -217,6 +235,13 @@ export default { } }, + /** + * Merge base networks list with user's list + */ + beforeMount () { + this.baseNetworks = Vue.util.extend(this.baseNetworks, this.networks); + }, + /** * Sets popup default dimensions. */ diff --git a/test/unit/test.js b/test/unit/test.js index 8864d2a..260fa52 100644 --- a/test/unit/test.js +++ b/test/unit/test.js @@ -80,7 +80,7 @@ describe('SocialSharing', () => { it('sets the correct default data', () => { expect(typeof SocialSharing.data).toBe('function'); const defaultData = SocialSharing.data(); - expect(defaultData.networks).toBe(Networks); + expect(defaultData.baseNetworks).toBe(Networks); }); // Calculates correct position of popup