From 02205eb116851372662228fdc53baecd78599d2a Mon Sep 17 00:00:00 2001 From: frankpagan Date: Fri, 23 Aug 2024 13:13:08 -0400 Subject: [PATCH] feat: network defined in netorks directory --- src/config/constants.js | 18 ---------- src/functions/getData.js | 41 ---------------------- src/index.js | 58 ++++++++++++++++++++++++++----- src/networks/bufferapp.js | 5 +++ src/networks/delicious.js | 6 ++++ src/networks/digg.js | 5 +++ src/networks/facebook.js | 5 +++ src/networks/google.js | 5 +++ src/networks/hackernews.js | 5 +++ src/{shares => networks}/index.js | 0 src/networks/linkedIn.js | 5 +++ src/networks/pinterest.js | 6 ++++ src/networks/reddit.js | 5 +++ src/networks/stumbleupon.js | 5 +++ src/networks/tumblr.js | 5 +++ src/networks/twitter.js | 5 +++ src/shares/bufferapp.js | 7 ---- src/shares/delicious.js | 8 ----- src/shares/digg.js | 7 ---- src/shares/facebook.js | 7 ---- src/shares/google.js | 7 ---- src/shares/hackernews.js | 7 ---- src/shares/linkedIn.js | 7 ---- src/shares/pinterest.js | 8 ----- src/shares/reddit.js | 7 ---- src/shares/stumbleupon.js | 7 ---- src/shares/tumblr.js | 7 ---- src/shares/twitter.js | 7 ---- 28 files changed, 111 insertions(+), 154 deletions(-) delete mode 100644 src/config/constants.js delete mode 100644 src/functions/getData.js create mode 100644 src/networks/bufferapp.js create mode 100644 src/networks/delicious.js create mode 100644 src/networks/digg.js create mode 100644 src/networks/facebook.js create mode 100644 src/networks/google.js create mode 100644 src/networks/hackernews.js rename src/{shares => networks}/index.js (100%) create mode 100644 src/networks/linkedIn.js create mode 100644 src/networks/pinterest.js create mode 100644 src/networks/reddit.js create mode 100644 src/networks/stumbleupon.js create mode 100644 src/networks/tumblr.js create mode 100644 src/networks/twitter.js delete mode 100644 src/shares/bufferapp.js delete mode 100644 src/shares/delicious.js delete mode 100644 src/shares/digg.js delete mode 100644 src/shares/facebook.js delete mode 100644 src/shares/google.js delete mode 100644 src/shares/hackernews.js delete mode 100644 src/shares/linkedIn.js delete mode 100644 src/shares/pinterest.js delete mode 100644 src/shares/reddit.js delete mode 100644 src/shares/stumbleupon.js delete mode 100644 src/shares/tumblr.js delete mode 100644 src/shares/twitter.js diff --git a/src/config/constants.js b/src/config/constants.js deleted file mode 100644 index f20f05b..0000000 --- a/src/config/constants.js +++ /dev/null @@ -1,18 +0,0 @@ -export const selectorClick = '[share-network]'; -export const selectorDiv = '[share-url], [share-title], [share-text], [share-height], [share-width] [share-media]'; - -export const options = 'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,'; -export const shareLists = { - google: 'https://plus.google.com/share?url=', - facebook: 'https://www.facebook.com/sharer/sharer.php?display=popup&u=', - twitter: 'https://twitter.com/intent/tweet?text=', - tumblr: 'http://www.tumblr.com/share?s=&v=3&u=', - pinterest: 'https://www.pinterest.com/pin/create/button/?description=', - reddit: 'http://www.reddit.com/submit?url=', - hackernews: 'http://news.ycombinator.com/submitlink?u=', - bufferapp: 'http://bufferapp.com/add?text=', - linkedIn: 'https://www.linkedin.com/shareArticle?mini=true&url=', - digg: 'http://digg.com/submit?url=', - stumbleupon: 'http://www.stumbleupon.com/submit?url=', - delicious: 'https://delicious.com/save?v=5&provider=' -}; \ No newline at end of file diff --git a/src/functions/getData.js b/src/functions/getData.js deleted file mode 100644 index e278899..0000000 --- a/src/functions/getData.js +++ /dev/null @@ -1,41 +0,0 @@ -const getData = (el, document, container) => { - - let url = el.getAttribute('share-url') || ''; - if (url == '') url = container.getAttribute('share-url') || ''; - // url = encodeURIComponent((url == '') ? document.URL : url); - - const urlObj = new URL(url || document.URL); - url = encodeURIComponent(urlObj.origin) + urlObj.pathname + urlObj.search + urlObj.hash; - - let text = el.getAttribute('share-text') || ''; - if (text == '') text = container.getAttribute('share-text') || ''; - text = encodeURIComponent(text); - - let title = el.getAttribute('share-title') || ''; - if (title == '') title = container.getAttribute('share-title') || ''; - title = encodeURIComponent((title == '') ? document.title : title); - - let height = el.getAttribute('share-height') || ''; - if (height == '') height = container.getAttribute('share-height') || ''; - height = parseInt((height == '') ? 600 : height); - - let width = el.getAttribute('share-width') || ''; - if (width == '') width = container.getAttribute('share-width') || ''; - width = parseInt((width == '') ? 500 : width); - - let media = el.getAttribute('share-media') || ''; - if (media == '') media = container.getAttribute('share-media') || ''; - media = encodeURIComponent((media == '') ? '' : media); - - return { - width, - height, - url, - text, - title, - media, - source: url - } -}; - -export default getData; \ No newline at end of file diff --git a/src/index.js b/src/index.js index 8756608..946b68e 100644 --- a/src/index.js +++ b/src/index.js @@ -1,37 +1,77 @@ -// import constants -import { selectorClick, selectorDiv, options } from './config/constants'; -// import functions -import getData from './functions/getData'; -import share from './shares/index'; +import networks from './networks/index'; const CoCreateSocialShare = { init: function () { + const selectorClick = '[share-network]'; + let elements = document.querySelectorAll(selectorClick); elements.forEach(el => this.initElement(el)); }, initElement: function (el) { - var that = this; + const self = this; el.addEventListener("click", function (event) { // Select Div element with dataset to share event.preventDefault() + const selectorDiv = '[share-url], [share-title], [share-text], [share-height], [share-width] [share-media]'; const container = el.closest(selectorDiv) // Get data to share from attributes - const data = getData(el, document, container); + const data = self.getData(el, document, container); // Generate share url from data - const network = share(el, data); + const network = networks(el, data); // Popup sub-window to share data - that.popup(network) + self.popup(network) }); }, + getData: function (el, document, container) { + + let url = el.getAttribute('share-url') || ''; + if (url == '') url = container.getAttribute('share-url') || ''; + // url = encodeURIComponent((url == '') ? document.URL : url); + + const urlObj = new URL(url || document.URL); + url = encodeURIComponent(urlObj.origin) + urlObj.pathname + urlObj.search + urlObj.hash; + + let text = el.getAttribute('share-text') || ''; + if (text == '') text = container.getAttribute('share-text') || ''; + text = encodeURIComponent(text); + + let title = el.getAttribute('share-title') || ''; + if (title == '') title = container.getAttribute('share-title') || ''; + title = encodeURIComponent((title == '') ? document.title : title); + + let height = el.getAttribute('share-height') || ''; + if (height == '') height = container.getAttribute('share-height') || ''; + height = parseInt((height == '') ? 600 : height); + + let width = el.getAttribute('share-width') || ''; + if (width == '') width = container.getAttribute('share-width') || ''; + width = parseInt((width == '') ? 500 : width); + + let media = el.getAttribute('share-media') || ''; + if (media == '') media = container.getAttribute('share-media') || ''; + media = encodeURIComponent((media == '') ? '' : media); + + return { + width, + height, + url, + text, + title, + media, + source: url + } + }, + popup: function (network) { + const options = 'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,'; window.open(network["url"], '', options + 'height=' + network["height"] + ',width=' + network["width"]); } diff --git a/src/networks/bufferapp.js b/src/networks/bufferapp.js new file mode 100644 index 0000000..b2600b8 --- /dev/null +++ b/src/networks/bufferapp.js @@ -0,0 +1,5 @@ +const bufferapp = (data) => { + return 'http://bufferapp.com/add?text=' + data.text + '&url=' + data.url; +}; + +export default bufferapp; \ No newline at end of file diff --git a/src/networks/delicious.js b/src/networks/delicious.js new file mode 100644 index 0000000..9e4c0c2 --- /dev/null +++ b/src/networks/delicious.js @@ -0,0 +1,6 @@ +const delicious = (data) => { + return 'https://delicious.com/save?v=5&provider=' + data.provider + '&noui&jump=close&url=' + data.url + + '&title=' + data.title; +}; + +export default delicious; \ No newline at end of file diff --git a/src/networks/digg.js b/src/networks/digg.js new file mode 100644 index 0000000..eb46087 --- /dev/null +++ b/src/networks/digg.js @@ -0,0 +1,5 @@ +const digg = (data) => { + return 'http://digg.com/submit?url=' + '&title=' + data.title; +}; + +export default digg; \ No newline at end of file diff --git a/src/networks/facebook.js b/src/networks/facebook.js new file mode 100644 index 0000000..c125901 --- /dev/null +++ b/src/networks/facebook.js @@ -0,0 +1,5 @@ +const facebook = (data) => { + return 'https://www.facebook.com/sharer/sharer.php?display=popup&u=' + data.url + '&title=' + data.title; +}; + +export default facebook; \ No newline at end of file diff --git a/src/networks/google.js b/src/networks/google.js new file mode 100644 index 0000000..35601d9 --- /dev/null +++ b/src/networks/google.js @@ -0,0 +1,5 @@ +const google = (data) => { + return 'https://plus.google.com/share?url=' + data.url; +}; + +export default google; \ No newline at end of file diff --git a/src/networks/hackernews.js b/src/networks/hackernews.js new file mode 100644 index 0000000..a68eae0 --- /dev/null +++ b/src/networks/hackernews.js @@ -0,0 +1,5 @@ +const hackernews = (data) => { + return 'http://news.ycombinator.com/submitlink?u=' + data.url + '&t=' + data.title; +}; + +export default hackernews; \ No newline at end of file diff --git a/src/shares/index.js b/src/networks/index.js similarity index 100% rename from src/shares/index.js rename to src/networks/index.js diff --git a/src/networks/linkedIn.js b/src/networks/linkedIn.js new file mode 100644 index 0000000..251444d --- /dev/null +++ b/src/networks/linkedIn.js @@ -0,0 +1,5 @@ +const linkedIn = (data) => { + return 'https://www.linkedin.com/shareArticle?mini=true&url=' + data.url + '&title=' + data.title + '&source=' + data.source; +}; + +export default linkedIn; \ No newline at end of file diff --git a/src/networks/pinterest.js b/src/networks/pinterest.js new file mode 100644 index 0000000..992a97c --- /dev/null +++ b/src/networks/pinterest.js @@ -0,0 +1,6 @@ +const pinterest = (data) => { + return 'https://www.pinterest.com/pin/create/button/?description=' + data.text + '&url=' + data.url + '&media=' + + data.media + '&is_video=false'; +}; + +export default pinterest; \ No newline at end of file diff --git a/src/networks/reddit.js b/src/networks/reddit.js new file mode 100644 index 0000000..2d0269e --- /dev/null +++ b/src/networks/reddit.js @@ -0,0 +1,5 @@ +const reddit = (data) => { + return 'http://www.reddit.com/submit?url=' + data.url + '&title=' + data.title; +}; + +export default reddit; \ No newline at end of file diff --git a/src/networks/stumbleupon.js b/src/networks/stumbleupon.js new file mode 100644 index 0000000..aa697a6 --- /dev/null +++ b/src/networks/stumbleupon.js @@ -0,0 +1,5 @@ +const stumbleupon = (data) => { + return 'http://www.stumbleupon.com/submit?url=' + data.url + '&title=' + data.title; +}; + +export default stumbleupon; \ No newline at end of file diff --git a/src/networks/tumblr.js b/src/networks/tumblr.js new file mode 100644 index 0000000..8b2a109 --- /dev/null +++ b/src/networks/tumblr.js @@ -0,0 +1,5 @@ +const tumblr = (data) => { + return 'http://www.tumblr.com/share?s=&v=3&u=' + data.url + '&t=' + data.title; +}; + +export default tumblr; \ No newline at end of file diff --git a/src/networks/twitter.js b/src/networks/twitter.js new file mode 100644 index 0000000..22e071b --- /dev/null +++ b/src/networks/twitter.js @@ -0,0 +1,5 @@ +const twitter = (data) => { + return 'https://twitter.com/intent/tweet?text=' + data.text + '&url=' + data.url; +}; + +export default twitter; \ No newline at end of file diff --git a/src/shares/bufferapp.js b/src/shares/bufferapp.js deleted file mode 100644 index f014b41..0000000 --- a/src/shares/bufferapp.js +++ /dev/null @@ -1,7 +0,0 @@ -import { shareLists } from '../config/constants'; - -const bufferapp = (data) => { - return shareLists.bufferapp + data.text + '&url=' + data.url; -}; - -export default bufferapp; \ No newline at end of file diff --git a/src/shares/delicious.js b/src/shares/delicious.js deleted file mode 100644 index c62d404..0000000 --- a/src/shares/delicious.js +++ /dev/null @@ -1,8 +0,0 @@ -import { shareLists } from '../config/constants'; - -const delicious = (data) => { - return shareLists.delicious + data.provider + '&noui&jump=close&url=' + data.url + - '&title=' + data.title; -}; - -export default delicious; \ No newline at end of file diff --git a/src/shares/digg.js b/src/shares/digg.js deleted file mode 100644 index 73eab45..0000000 --- a/src/shares/digg.js +++ /dev/null @@ -1,7 +0,0 @@ -import { shareLists } from '../config/constants'; - -const digg = (data) => { - return shareLists.digg + data.url + '&title=' + data.title; -}; - -export default digg; \ No newline at end of file diff --git a/src/shares/facebook.js b/src/shares/facebook.js deleted file mode 100644 index 700b077..0000000 --- a/src/shares/facebook.js +++ /dev/null @@ -1,7 +0,0 @@ -import { shareLists } from '../config/constants'; - -const facebook = (data) => { - return shareLists.facebook + data.url + '&title=' + data.title; -}; - -export default facebook; \ No newline at end of file diff --git a/src/shares/google.js b/src/shares/google.js deleted file mode 100644 index 43df59b..0000000 --- a/src/shares/google.js +++ /dev/null @@ -1,7 +0,0 @@ -import { shareLists } from '../config/constants'; - -const google = (data) => { - return shareLists.google + data.url; -}; - -export default google; \ No newline at end of file diff --git a/src/shares/hackernews.js b/src/shares/hackernews.js deleted file mode 100644 index f3437b4..0000000 --- a/src/shares/hackernews.js +++ /dev/null @@ -1,7 +0,0 @@ -import { shareLists } from '../config/constants'; - -const hackernews = (data) => { - return shareLists.hackernews + data.url + '&t=' + data.title; -}; - -export default hackernews; \ No newline at end of file diff --git a/src/shares/linkedIn.js b/src/shares/linkedIn.js deleted file mode 100644 index d1c58d7..0000000 --- a/src/shares/linkedIn.js +++ /dev/null @@ -1,7 +0,0 @@ -import { shareLists } from '../config/constants'; - -const linkedIn = (data) => { - return shareLists.linkedIn + data.url + '&title=' + data.title + '&source=' + data.source; -}; - -export default linkedIn; \ No newline at end of file diff --git a/src/shares/pinterest.js b/src/shares/pinterest.js deleted file mode 100644 index d1b3470..0000000 --- a/src/shares/pinterest.js +++ /dev/null @@ -1,8 +0,0 @@ -import { shareLists } from '../config/constants'; - -const pinterest = (data) => { - return shareLists.pinterest + data.text + '&url=' + data.url + '&media=' + - data.media + '&is_video=false'; -}; - -export default pinterest; \ No newline at end of file diff --git a/src/shares/reddit.js b/src/shares/reddit.js deleted file mode 100644 index c089471..0000000 --- a/src/shares/reddit.js +++ /dev/null @@ -1,7 +0,0 @@ -import { shareLists } from '../config/constants'; - -const reddit = (data) => { - return shareLists.reddit + data.url + '&title=' + data.title; -}; - -export default reddit; \ No newline at end of file diff --git a/src/shares/stumbleupon.js b/src/shares/stumbleupon.js deleted file mode 100644 index ac5d1b9..0000000 --- a/src/shares/stumbleupon.js +++ /dev/null @@ -1,7 +0,0 @@ -import { shareLists } from '../config/constants'; - -const stumbleupon = (data) => { - return shareLists.stumbleupon + data.url + '&title=' + data.title; -}; - -export default stumbleupon; \ No newline at end of file diff --git a/src/shares/tumblr.js b/src/shares/tumblr.js deleted file mode 100644 index 17d6a1a..0000000 --- a/src/shares/tumblr.js +++ /dev/null @@ -1,7 +0,0 @@ -import { shareLists } from '../config/constants'; - -const tumblr = (data) => { - return shareLists.tumblr + data.url + '&t=' + data.title; -}; - -export default tumblr; \ No newline at end of file diff --git a/src/shares/twitter.js b/src/shares/twitter.js deleted file mode 100644 index d12a22b..0000000 --- a/src/shares/twitter.js +++ /dev/null @@ -1,7 +0,0 @@ -import { shareLists } from '../config/constants'; - -const twitter = (data) => { - return shareLists.twitter + data.text + '&url=' + data.url; -}; - -export default twitter; \ No newline at end of file