/** * ShareThis Share Buttons * * @package ShareThisShareButtons */ /* exported ShareButtons */ var ShareButtons = ( function( $, wp ) { 'use strict'; return { /** * Holds data. */ data: {}, /** * Boot plugin. */ boot: function( data ) { this.data = data; $( document ).ready( function() { this.init(); }.bind( this ) ); }, /** * Initialize plugin. */ init: function() { this.$container = $( '.sharethis-wrap' ); this.$resultWrapper = $( '#category-result-wrapper' ); // Set temp enables. this.$tempEnable = { 'inline': false, 'sticky': false, 'gdpr': false }; // Get and set current accounts platform configurations to global. this.$config = this.getConfig(); this.listen(); this.createReset(); // Check if platform has changed its button config. this.checkIfChanged(); // Check if buttons are enabled or disabled on both ends. this.markSelected(); // Check if adblock is enabled. Shows notice if so. this.checkAdBlock(); }, /** * Initiate listeners. */ listen: function() { var self = this, timer = ''; // Scroll to anchor in vendor list. // Send user input to category search AFTER they stop typing. $('body').on( 'keyup', '.vendor-search input', function( e ) { clearTimeout( timer ); timer = setTimeout( function() { self.scrollToAnchor($(this).val()); }.bind( this ), 500 ); } ); // Toggle button menus when arrows are clicked. $( 'body' ).on( 'click', '.accor-wrap .accor-tab', function() { var type = $( this ).find( 'span.accor-arrow' ); self.updateAccors( type.html(), type ); } ); // New color select. this.$container.on('click', "#sharethis-form-color .color", function() { $('#sharethis-form-color .color').removeClass('selected'); $(this).addClass('selected'); }); // clear or show choices. this.$container.on('click', '#clear-choices', function(e) { e.preventDefault(); e.stopPropagation(); $( '.purpose-item input' ).prop( 'checked', false ); }); // clear or show choices. this.$container.on('click', '#see-st-choices', function(e) { e.preventDefault(); e.stopPropagation(); $('.purpose-item input[name="purposes[1]"]').prop('checked', true); $('.purpose-item input[name="purposes[3]"][value="consent"]').prop('checked', true); $('.purpose-item input[name="purposes[5]"][value="consent"]').prop('checked', true); $('.purpose-item input[name="purposes[6]"][value="consent"]').prop('checked', true); $('.purpose-item input[name="purposes[9]"][value="legitimate"]').prop('checked', true); $('.purpose-item input[name="purposes[10]"][value="legitimate"]').prop('checked', true); }); // Uncheck radio if click on selected box. this.$container.on( 'click', '.gdpr-platform .lever', ( e ) => { e.preventDefault(); e.stopPropagation(); const theInput = $( e.currentTarget ).siblings( 'input' ); if ( 'select-purpose' === theInput.attr( 'name' ) && !theInput.is( ':checked' ) ) { if ( 'consent' === theInput.val() ) { $( '.purpose-item input' ).prop( 'checked', false ); $( '.empty-choices' ).addClass( 'engage' ); } else { $( '.purpose-item input[name="purposes[1]"]' ).prop( 'checked', true ); $( '.purpose-item input[name="purposes[3]"][value="consent"]' ).prop( 'checked', true ); $( '.purpose-item input[name="purposes[5]"][value="consent"]' ).prop( 'checked', true ); $( '.purpose-item input[name="purposes[6]"][value="consent"]' ).prop( 'checked', true ); $( '.purpose-item input[name="purposes[9]"][value="legitimate"]' ).prop( 'checked', true ); $( '.purpose-item input[name="purposes[10]"][value="legitimate"]' ).prop( 'checked', true ); } theInput.prop( 'checked', true ) $( '.back-next' ).addClass( 'engage' ); return; } if ( theInput.is( ':checked' ) ) { $( `input[name="${theInput.attr( 'name' )}"]` ).prop( 'checked', false ) } else { theInput.prop( 'checked', true ) } } ); // On off button events. this.$container.on( 'click', '.share-on, .share-off', function() { // Revert to default color. $( this ).closest( 'div' ).find( 'div.label-text' ).css( 'color', '#8d8d8d' ); // Change the input selected color to white. $( this ).find( '.label-text' ).css( 'color', '#ffffff' ); } ); // Copy text from read only input fields. this.$container.on( 'click', '#copy-shortcode, #copy-template', function() { self.copyText( $( this ).closest( 'div' ).find( 'input' ) ); } ); // Open close options and update platform and WP on off status. this.$container.on( 'click', '.enable-buttons .share-on, .enable-buttons .share-off', function() { var button = $( this ).closest( 'div' ).attr( 'id' ), type = $( this ).find( 'div.label-text' ).html(); self.updateButtons( button, type, 'click' ); } ); // Toggle button menus when arrows are clicked. this.$container.on( 'click', 'span.st-arrow', function() { var button = $( this ).attr( 'id' ), type = $( this ).html(); self.updateButtons( button, type, '' ); } ); // Click reset buttons. this.$container.on( 'click', 'p.submit #reset', function() { var type = $( this ) .closest( 'p.submit' ) .prev() .find( '.enable-buttons' ) .attr( 'id' ); self.setDefaults( type ); } ); // Send user input to category search AFTER they stop typing. this.$container.on( 'keyup', 'input#category-ta, input#page-ta', function( e ) { var type = $( this ).siblings( '.search-st-icon' ).attr( 'id' ), result = '#' + type + '-result-wrapper'; clearTimeout( timer ); timer = setTimeout( function() { self.returnResults( $( this ).val(), type, result ); }.bind( this ), 500 ); } ); // Force search when search icon is clicked. this.$container.on( 'click', '.search-st-icon', function() { var type = $( this ).attr( 'id' ), key = $( this ).siblings( 'input' ).val(), result = '#' + type + '-result-wrapper'; self.returnResults( key, type, result ); } ); // Select an item to exclude. Add it to list. this.$container.on( 'click', '.ta-page-item, .ta-category-item', function() { var type = $( this ) .closest( '.list-wrapper' ) .find( 'span.search-st-icon' ) .attr( 'id' ); self.updateOmit( $( this ), type ); } ); // Remove excluded item from list. this.$container.on( 'click', '.remove-omit', function() { $( this ).closest( 'li.omit-item' ).remove(); } ); // Toggle margin control buttons. this.$container.on( 'click', 'button.margin-control-button', function() { var status = $( this ).hasClass( 'active-margin' ); self.activateMargin( this, status ); } ); $( 'body' ).on( 'click', '.item #cta, .item #counts, .item #none, .item #medium, .item #small, .item #large', function() { var checked = $( this ).siblings( 'input' ).is( ':checked' ), button = $( this ).closest( '.selected-button' ).attr( 'id' ); $( '.sharethis-inline-share-buttons' ).removeClass( 'st-has-labels' ); if ( ! checked ) { $( this ).closest( '.st-radio-config' ).find( '.item' ).each( function() { $( this ).find( 'input' ).prop( 'checked', false ); } ); $( this ).siblings( 'input' ).prop( 'checked', true ); } self.loadPreview( '', button ); } ); // All levers. this.$container.on( 'click', '.item div.switch', function() { var button = $( this ).closest( '.selected-button' ).attr( 'id' ); self.loadPreview( '', button ); } ); // Minimum count. this.$container.on( 'change', 'input.minimum-count, #radius-selector, .vertical-alignment, .mobile-breakpoint', function() { var button = $( this ).closest( '.selected-button' ).attr( 'id' ); self.loadPreview( '', button ); } ); // Languages. this.$container.on( 'change', '#st-language-inline, #st-language-sticky', function() { var id = $( this ).attr( 'id' ), language = $( this ).find( 'option:selected' ).val(); if ( 'st-language-inline' === id ) { $( '#st-language-sticky option[value="' + language + '"]' ).prop( 'selected', true ); } else { $( '#st-language-inline option[value="' + language + '"]' ).prop( 'selected', true ); } self.loadPreview( '', 'sticky' ); self.loadPreview( '', 'inline' ); } ); // Button alignment. this.$container.on( 'click', '.button-alignment .alignment-button', function() { var button = $( this ).closest( '.selected-button' ).attr( 'id' ); $( '.sharethis-inline-share-buttons' ).removeClass( 'st-justified' ); $( '.button-alignment .alignment-button[data-selected="true"]' ) .attr( 'data-selected', 'false' ); $( this ).attr( 'data-selected', 'true' ); self.loadPreview( '', button ); } ); // Select or deselect a network. this.$container.on( 'click', '.share-buttons .share-button', function() { var selection = $( this ).attr( 'data-selected' ), button = $( this ).closest( '.selected-button' ).attr( 'id' ), network = $( this ).attr( 'data-network' ); if ( 'true' === selection ) { $( this ).attr( 'data-selected', 'false' ); $( '.' + button + '-platform .sharethis-selected-networks > div > div div[data-network="' + network + '"]' ).remove(); } else { $( this ).attr( 'data-selected', 'true' ); $( '.' + button + '-platform .sharethis-selected-networks > div > div' ).append( '
' ); } self.loadPreview( '', button ); } ); // Submit configurations. $( '.sharethis-wrap form' ).submit( function(e) { self.loadPreview( 'submit', 'inline' ); self.loadPreview( 'submit', 'sticky' ); self.loadPreview( 'submit', 'gdpr' ); } ); // Add class to preview when scrolled to. $( window ).on( 'scroll load', function() { var vTop = $( '.vertical-alignment' ).val() + 'px', stickyTop, stopPoint, gdprTop = parseInt( $( 'form .form-table:last-of-type' ).offset().top ) - 100, inlineTop = $( '.inline-platform' ).offset().top; // Start fixed inline preview when top of browser reached. if ( $( window ).scrollTop() >= inlineTop && $( window ).scrollTop() <= gdprTop ) { $( '.sharethis-inline-share-buttons' ).addClass( 'sharethis-prev-stick' ); } else { $( '.sharethis-inline-share-buttons' ).removeClass( 'sharethis-prev-stick' ); } // Stop the sticky preview when top of sticky config reached. stickyTop = parseInt( $( 'form .form-table:nth-of-type(2)' ).offset().top ) - 100; stopPoint = stickyTop + parseInt( vTop ); if ( $( window ).scrollTop() >= stickyTop && $( window ).scrollTop() <= gdprTop ) { $( '.sharethis-sticky-share-buttons .st-sticky-share-buttons' ) .css( { 'display':'block', 'position': 'fixed', 'top': vTop } ); $( '.sharethis-sticky-share-buttons .st-sticky-share-buttons.st-left' ) .removeClass( 'stuck-buttons' ); $( '.sharethis-inline-share-buttons' ).removeClass( 'sharethis-prev-stick' ); } else { $( '.sharethis-sticky-share-buttons .st-sticky-share-buttons' ) .css( { 'display': 'none' } ); $( '.sharethis-sticky-share-buttons .st-sticky-share-buttons.st-left' ) .addClass( 'stuck-buttons' ); } } ); }, /** * Change font color of selected buttons. * Also decide whether to update WP enable / disable status or just show / hide menu options. */ markSelected: function() { var sConfigSet = null !== this.$config && undefined !== this.$config['sticky-share-buttons'], iConfigSet = null !== this.$config && undefined !== this.$config['inline-share-buttons'], gConfigSet = null !== this.$config && undefined !== this.$config['gdpr-compliance-tool-v2'], iturnOn, iturnOff, sturnOn, sturnOff, stickyEnable, inlineEnable, gturnOn, gturnOff, gdprEnable; // Check if api call is successful and if sticky buttons are enabled. Use WP data base if not. if ( sConfigSet ) { stickyEnable = this.$config['sticky-share-buttons']['enabled']; // Dot notation cannot be used due to dashes in name. } else { if ( undefined !== this.data.buttonConfig['sticky'] ) { stickyEnable = this.data.buttonConfig['sticky']['enabled']; } } // Check if api call is successful and if inline buttons are enabled. Use WP data base if not. if ( iConfigSet ) { inlineEnable = this.$config['inline-share-buttons']['enabled']; // Dot notation cannot be used due to dashes in name. } else { if ( undefined !== this.data.buttonConfig['inline'] ) { inlineEnable = this.data.buttonConfig['inline']['enabled']; } } // Check if api call is successful and if gdpr is enabled. Use WP data base if not. if ( gConfigSet ) { gdprEnable = this.$config['gdpr-compliance-tool-v2']['enabled']; // Dot notation cannot be used due to dashes in name. } else { if ( undefined !== this.data.buttonConfig['gdpr'] ) { gdprEnable = this.data.buttonConfig['gdpr']['enabled']; } } // Decide whether to update WP database or just show / hide menu options. if ( ! iConfigSet || ( undefined !== this.data.buttonConfig['inline'] && this.data.buttonConfig['inline']['enabled'] === this.$config['inline-share-buttons']['enabled'] ) ) { // Dot notation cannot be used due to dashes in name. iturnOn = 'show'; iturnOff = 'hide'; } else { iturnOn = 'On'; iturnOff = 'Off'; } // If enabled show button configuration. if ( 'true' === inlineEnable || true === inlineEnable ) { $( '.inline-platform' ).css( 'display', 'table-footer-group' ); this.updateButtons( 'Inline', iturnOn ); $( '#Inline label.share-on input' ).prop( 'checked', true ); } else { $( '.inline-platform' ).hide(); this.updateButtons( 'Inline', iturnOff ); $( '#Inline label.share-off input' ).prop( 'checked', true ); } // Decide whether to update WP database or just show / hide menu options. if ( ! sConfigSet || ( undefined !== this.data.buttonConfig['sticky'] && this.data.buttonConfig['sticky']['enabled'] === this.$config['sticky-share-buttons']['enabled'] ) ) { // Dot notation cannot be used due to dashes in name. sturnOn = 'show'; sturnOff = 'hide'; } else { sturnOn = 'On'; sturnOff = 'Off'; } // If enabled show sticky options. if ( 'true' === stickyEnable || true === stickyEnable ) { $( '.sticky-platform' ).css( 'display', 'table-footer-group' ); this.updateButtons( 'Sticky', sturnOn ); $( '#Sticky label.share-on input' ).prop( 'checked', true ); } else { $( '.sticky-platform' ).hide(); this.updateButtons( 'Sticky', sturnOff ); $( '#Sticky label.share-off input' ).prop( 'checked', true ); } // Decide whether to update WP database or just show / hide menu options. if ( ! gConfigSet || ( undefined !== this.data.buttonConfig['gdpr'] && this.data.buttonConfig['gdpr']['enabled'] === this.$config['gdpr-compliance-tool-v2']['enabled'] ) ) { // Dot notation cannot be used due to dashes in name. gturnOn = 'show'; gturnOff = 'hide'; } else { gturnOn = 'On'; gturnOff = 'Off'; } // If enabled show gdpr options. if ( 'true' === gdprEnable || true === gdprEnable ) { $( '.gdpr-platform' ).css( 'display', 'table-footer-group' ); this.updateButtons( 'Gdpr', gturnOn ); $( '#Gdpr label.share-on input' ).prop( 'checked', true ); } else { $( '.gdpr-platform' ).hide(); this.updateButtons( 'Gdpr', gturnOff ); $( '#Gdpr label.share-off input' ).prop( 'checked', true ); } // Change button font color based on status. $( '.share-on input:checked, .share-off input:checked' ).closest( 'label' ).find( 'span.label-text' ).css( 'color', '#ffffff' ); }, /** * Check the platform has updated the button configs. */ checkIfChanged: function() { var iTs = this.$config['inline-share-buttons'], sTs = this.$config['sticky-share-buttons'], gTs = this.$config['gdpr-compliance-tool-v2'], myITs = this.data.buttonConfig['inline'], mySTs = this.data.buttonConfig['sticky'], myGTs = this.data.buttonConfig['gdpr'], theConfig; // Set variables if array exists. if ( undefined !== iTs ) { iTs = iTs['updated_at']; if ( undefined !== iTs ) { iTs = iTs.toString(); } } if ( undefined !== sTs ) { sTs = sTs['updated_at']; if ( undefined !== sTs ) { sTs = sTs.toString(); } } if ( undefined !== gTs ) { gTs = gTs['updated_at']; if ( undefined !== gTs ) { gTs = gTs.toString(); } } if ( undefined !== myITs ) { myITs = myITs['updated_at']; } if ( undefined !== mySTs ) { mySTs = mySTs['updated_at']; } if ( undefined !== myGTs ) { myGTs = myGTs['updated_at']; } // If platform has updated the button config or platform configs are broken use WP config. if ( iTs !== myITs || undefined === this.data.buttonConfig ) { this.setConfigFields( 'inline', this.$config['inline-share-buttons'], 'platform' ); } else { this.loadPreview( 'initial', 'inline' ); } if ( sTs !== mySTs || undefined === this.data.buttonConfig ) { this.setConfigFields( 'sticky', this.$config['sticky-share-buttons'], 'platform' ); } else { this.loadPreview( 'initial', 'sticky' ); } if ( gTs !== myGTs || undefined === this.data.buttonConfig ) { this.setConfigFields( 'gdpr', this.$config['gdpr-compliance-tool-v2'], 'platform' ); } else { this.loadPreview( 'initial', 'gdpr' ); } }, /** * Show button configuration. * * @param button * @param type * @param event */ updateButtons: function( button, type, event ) { var when = 'last-of-type', pTypes = [ 'show', 'On', '►', 'true' ], aTypes = [ 'show', 'hide', '►', '▼' ], timer = ''; // Determine which style. if ( 'Inline' === button ) { when = 'first-of-type'; } // If Sticky. if ( 'Sticky' === button ) { when = 'nth-of-type(2)'; } // If not one of the show types then hide. if ( -1 !== $.inArray( type, pTypes ) ) { // Show the button configs. $( '.sharethis-wrap form .form-table:' + when + ' tr' ).not( ':eq(0)' ).show(); // Show the submit / reset buttons. $( '.sharethis-wrap form .submit:' + when ).show(); // Change the icon next to title. $( '.sharethis-wrap h2:' + when + ' span' ).html( '▼' ); // Platform config. $( '.' + button.toLowerCase() + '-platform' ).css( 'display', 'table-footer-group' ); if ( 'click' === event ) { this.loadPreview( 'turnon', button ); } // Set option value for button. wp.ajax.post( 'update_buttons', { type: button.toLowerCase(), onoff: 'On', nonce: this.data.nonce } ).always( function() { } ); } else { // Hide the button configs. $( '.sharethis-wrap form .form-table:' + when + ' tr' ).not( ':eq(0)' ).hide(); // Hide the submit / reset buttons. $( '.sharethis-wrap form .submit:' + when ).hide(); // Change the icon next to title. $( '.sharethis-wrap h2:' + when + ' span' ).html( '►' ); // Platform config. $( '.' + button.toLowerCase() + '-platform' ).hide(); if ( 'click' === event ) { this.loadPreview( 'turnoff', button ); } // Set option value for button. wp.ajax.post( 'update_buttons', { type: button.toLowerCase(), onoff: 'Off', nonce: this.data.nonce } ).always( function() { } ); } }, /** * Update buttons on platform * * @param button * @param type */ updatePlatform: function( button, type ) { var status, buttonConfig, self = this, button = button.toLowerCase(); // Set status variable to bool. if ( 'On' === type ) { status = true; } else { status = false; } // Default button config with enable. if ( 'inline' === button ) { buttonConfig = { "enabled": ( true === status ), "alignment" : "center", "font_size" : 12, "has_spacing" : true, "labels" : "cta", "min_count" : 10, "networks" : [ "facebook", "twitter", "pinterest", "email", "sms", "sharethis" ], "num_networks" : 6, "padding" : 10, "radius" : 4, "show_total" : true, "size" : 40, "size_label" : "medium", "spacing" : 8 } } else { buttonConfig = { "enabled": ( true === status ), "alignment" : "left", "labels" : "cta", "min_count" : 10, "mobile_breakpoint" : 1024, "networks" : [ "facebook", "twitter", "pinterest", "email", "sms", "sharethis" ], "num_networks" : 6, "padding" : 12, "radius" : 4, "show_mobile" : true, "show_toggle" : true, "show_total" : true, "size" : 48, "top" : 160 } } }, /** * Copy text to clipboard * * @param copiedText */ copyText: function( copiedText ) { copiedText.select(); document.execCommand( 'copy' ); }, /** * Add the reset buttons to share buttons menu */ createReset: function() { var button = '', newButtons = $( '.sharethis-wrap form .submit' ).append( button ).clone(), newButtons2 = $( '.sharethis-wrap form .submit' ).clone(); // Add new cloned reset button to inline menu list. $( '.sharethis-wrap form .form-table:first-of-type' ).after( newButtons ); $( newButtons ).find( '#submit:first-of-type' ).addClass( 'st-submit-2' ); $( '.sharethis-wrap form .form-table:nth-of-type(2)' ).after( newButtons2 ); $( newButtons2 ).find( '#submit:nth-of-type(2)' ).addClass( 'st-submit-3' ); }, /** * Set to default settings when reset is clicked. * * @param type */ setDefaults: function( type ) { wp.ajax.post( 'set_default_settings', { type: type, nonce: this.data.nonce } ).always( function() { if ( 'both' !== type ) { location.href = location.pathname + '?page=sharethis-share-buttons&reset=' + type; } else { location.reload(); } } ); }, /** * Send input value and return LIKE categories/pages. * * @param key * @param type * @param result */ returnResults: function( key, type, result ) { wp.ajax.post( 'return_omit', { key: key, type: type, nonce: this.data.nonce } ).always( function( results ) { if ( '' !== results ) { $( result ).show().html( results ); } else { $( result ).hide(); } }.bind( this ) ); }, /** * Add / remove selected omit item to omit list. * * @param value * @param type */ updateOmit: function( value, type ) { var result = '#' + type + '-current-omit', wrapper = '#' + type + '-result-wrapper'; // Hide the results when item is selected and add it to list. $( wrapper ).hide(); $( result ).append( '