1- 'use strict'
1+ 'use strict' ;
22
3- const CheckNodelist = ( arr ) => {
4- return ( arr && arr . length !== 0 ) ;
5- }
6-
7- const SetLinks = ( _arr , link ) => {
8- for ( let elem of _arr ) {
9- elem . setAttribute ( "href" , link ) ;
10- elem . setAttribute ( "target" , '_blank' ) ;
3+ class ShareLink {
4+ constructor ( element ) {
5+ this . element = element ;
6+ this . platform = element . getAttribute ( 'wt-share-element' ) ;
7+ this . title = document . title ;
8+ this . url = window . location . href ;
9+ this . encoded = this . encodeURIParams ( ) ;
10+ this . initializeShareLinks ( ) ;
1111 }
12- }
1312
14- const SetShareLinks = ( ) => {
15- let title = document . title ;
16- let url = window . location . href ;
17- let unencodedURI = `${ url } &title='${ title } '&description='${ title } '` ;
18- let encoded = encodeURI ( unencodedURI ) ;
19-
20- let social_fb = document . querySelectorAll ( '[wt-share-element="facebook"]' ) ;
21- let social_tw = document . querySelectorAll ( '[wt-share-element="twitter"]' ) ;
22- let social_ln = document . querySelectorAll ( '[wt-share-element="linkedin"]' ) ;
23- let social_wp = document . querySelectorAll ( '[wt-share-element="whatsapp"]' ) ;
24- let social_pi = document . querySelectorAll ( '[wt-share-element="pinterest"]' ) ;
25- let social_red = document . querySelectorAll ( '[wt-share-element="reddit"]' ) ;
26- let social_cp = document . querySelectorAll ( '[wt-share-element="copy"]' ) ;
27-
28- if ( CheckNodelist ( social_fb ) ) {
29- SetLinks ( social_fb , `https://www.facebook.com/sharer/sharer.php?u=${ encoded } ` ) ;
30- }
31- if ( CheckNodelist ( social_tw ) ) {
32- SetLinks ( social_tw , `https://twitter.com/share?url=${ encoded } ` ) ;
33- }
34- if ( CheckNodelist ( social_ln ) ) {
35- SetLinks ( social_ln , `https://www.linkedin.com/shareArticle?mini=true&url='${ encoded } ` ) ;
36- }
37- if ( CheckNodelist ( social_wp ) ) {
38- SetLinks ( social_wp , `https://wa.me/?text=${ encoded } ` ) ;
39- }
40- if ( CheckNodelist ( social_pi ) ) {
41- SetLinks ( social_pi , `http://pinterest.com/pin/create/button/?url=${ encoded } ` ) ;
13+ encodeURIParams ( ) {
14+ let unencodedURI = `${ this . url } &title='${ this . title } '&description='${ this . title } '` ;
15+ return encodeURI ( unencodedURI ) ;
4216 }
43- if ( CheckNodelist ( social_red ) ) {
44- SetLinks ( social_red , `http://www.reddit.com/submit?url=${ encoded } ` ) ;
45- }
46- if ( CheckNodelist ( social_cp ) ) {
47- for ( let btn of social_cp ) {
48- btn . addEventListener ( 'click' , ( ) => navigator . clipboard . writeText ( `${ url } ` ) ) ;
17+
18+ initializeShareLinks ( ) {
19+ const socialSelectors = {
20+ facebook : `https://www.facebook.com/sharer/sharer.php?u=${ this . encoded } ` ,
21+ twitter : `https://twitter.com/share?url=${ this . encoded } ` ,
22+ linkedin : `https://www.linkedin.com/shareArticle?mini=true&url='${ this . encoded } ` ,
23+ whatsapp : `https://wa.me/?text=${ this . encoded } ` ,
24+ pinterest : `http://pinterest.com/pin/create/button/?url=${ this . encoded } ` ,
25+ reddit : `http://www.reddit.com/submit?url=${ this . encoded } `
26+ } ;
27+
28+ let _link = socialSelectors [ this . platform ] ;
29+ if ( ! _link ) return ; // handle errors if the platform has issues
30+
31+ if ( _link === 'copy' ) {
32+ this . element . addEventListener ( 'click' , ( ) => navigator . clipboard . writeText ( `${ this . url } ` ) ) ;
33+ }
34+ else {
35+ this . element . setAttribute ( "href" , _link ) ;
36+ this . element . setAttribute ( "target" , '_blank' ) ;
4937 }
5038 }
5139}
5240
53- window . addEventListener ( 'DOMContentLoaded' , SetShareLinks ( ) ) ;
41+ const InitializeShareLink = ( ) => {
42+ window . trickeries = window . trickeries || [ ] ;
43+ let links = document . querySelectorAll ( "[wt-share-element]" ) ;
44+ if ( ! links || links . length === 0 ) return ;
45+
46+ links . forEach ( link => {
47+ let instance = new ShareLink ( link ) ;
48+ window . trickeries . push ( { 'ShareLink' : instance } ) ;
49+ } ) ;
50+ } ;
51+
52+ // Execute InitializeShareLink when the DOM is fully loaded
53+ if ( / c o m p l e t e | i n t e r a c t i v e | l o a d e d / . test ( document . readyState ) ) {
54+ InitializeShareLink ( ) ;
55+ } else {
56+ window . addEventListener ( 'DOMContentLoaded' , InitializeShareLink ) ;
57+ }
0 commit comments