admin.js
868 lines
| 1 | /** |
| 2 | * Follow Buttons. |
| 3 | * |
| 4 | * @package ShareThisFollowButtons |
| 5 | */ |
| 6 | |
| 7 | /* exported FollowButtons */ |
| 8 | var FollowButtons = ( function( $, wp ) { |
| 9 | 'use strict'; |
| 10 | |
| 11 | return { |
| 12 | /** |
| 13 | * Holds data. |
| 14 | */ |
| 15 | data: {}, |
| 16 | |
| 17 | /** |
| 18 | * Boot plugin. |
| 19 | */ |
| 20 | boot: function( data ) { |
| 21 | this.data = data; |
| 22 | |
| 23 | $( document ).ready( |
| 24 | function() { |
| 25 | this.init(); |
| 26 | }.bind( this ) |
| 27 | ); |
| 28 | }, |
| 29 | |
| 30 | /** |
| 31 | * Initialize plugin. |
| 32 | */ |
| 33 | init: function() { |
| 34 | this.$container = $( '.sharethis-wrap' ); |
| 35 | |
| 36 | // Get and set current accounts platform configurations to global. |
| 37 | this.$config = this.getConfig(); |
| 38 | |
| 39 | this.listen(); |
| 40 | this.createReset(); |
| 41 | |
| 42 | // Check if platform has changed its button config. |
| 43 | this.checkIfChanged(); |
| 44 | |
| 45 | // Check for non WP Share Buttons. |
| 46 | this.shareButtonsExists(); |
| 47 | }, |
| 48 | |
| 49 | /** |
| 50 | * Initiate listeners. |
| 51 | */ |
| 52 | listen: function() { |
| 53 | var self = this, |
| 54 | timer = ''; |
| 55 | |
| 56 | // Enable tool submit. |
| 57 | const enableButtons = document.querySelectorAll( '.enable-tool' ); |
| 58 | |
| 59 | if ( enableButtons ) { |
| 60 | enableButtons.forEach( enableButton => { |
| 61 | enableButton.addEventListener( 'click', ( e ) => { |
| 62 | e.stopPropagation(); |
| 63 | e.preventDefault(); |
| 64 | |
| 65 | self.updateButtons( enableButton.dataset.button, 'On' ); |
| 66 | } ); |
| 67 | } ); |
| 68 | } |
| 69 | |
| 70 | // Disable tool submit. |
| 71 | const disableButtons = document.querySelectorAll( '.disable-tool' ); |
| 72 | |
| 73 | if ( disableButtons ) { |
| 74 | disableButtons.forEach( disableButton => { |
| 75 | disableButton.addEventListener( 'click', ( e ) => { |
| 76 | e.stopPropagation(); |
| 77 | e.preventDefault(); |
| 78 | |
| 79 | self.updateButtons( disableButton.dataset.button, 'Off' ); |
| 80 | } ); |
| 81 | } ); |
| 82 | } |
| 83 | |
| 84 | // On off button events. |
| 85 | this.$container.on( |
| 86 | 'click', |
| 87 | '.share-on, .share-off', |
| 88 | function() { |
| 89 | |
| 90 | // Revert to default color. |
| 91 | $( this ).closest( 'div' ).find( 'div.label-text' ).css( 'color', '#8d8d8d' ); |
| 92 | |
| 93 | // Change the input selected color to white. |
| 94 | $( this ).find( '.label-text' ).css( 'color', '#ffffff' ); |
| 95 | |
| 96 | // If turning on show recommendation notice. |
| 97 | if ( $( this ).hasClass( 'share-on' ) ) { |
| 98 | self.postPageTriggered(); |
| 99 | } |
| 100 | } |
| 101 | ); |
| 102 | |
| 103 | // Copy text from read only input fields. |
| 104 | this.$container.on( |
| 105 | 'click', |
| 106 | '#copy-shortcode, #copy-template', |
| 107 | function() { |
| 108 | self.copyText( $( this ).closest( 'div' ).find( 'input' ) ); |
| 109 | } |
| 110 | ); |
| 111 | |
| 112 | // Open close options and update platform and WP on off status. |
| 113 | this.$container.on( |
| 114 | 'click', |
| 115 | '.enable-buttons .share-on, .enable-buttons .share-off', |
| 116 | function() { |
| 117 | var type = $( this ).find( 'div.label-text' ).html(); |
| 118 | |
| 119 | self.updateButtons( 'inline-follow', type, 'click' ); |
| 120 | } |
| 121 | ); |
| 122 | |
| 123 | // Toggle button menus when arrows are clicked. |
| 124 | this.$container.on( |
| 125 | 'click', |
| 126 | 'span.st-arrow', |
| 127 | function() { |
| 128 | var type = $( this ).html(); |
| 129 | |
| 130 | self.updateButtons( 'inline-follow', type, '' ); |
| 131 | } |
| 132 | ); |
| 133 | |
| 134 | // Click reset buttons. |
| 135 | this.$container.on( |
| 136 | 'click', |
| 137 | 'p.submit #reset', |
| 138 | function() { |
| 139 | var type = $( this ) |
| 140 | .closest( 'p.submit' ) |
| 141 | .prev() |
| 142 | .find( '.enable-buttons' ) |
| 143 | .attr( 'id' ); |
| 144 | |
| 145 | self.setDefaults( type ); |
| 146 | } |
| 147 | ); |
| 148 | |
| 149 | // Toggle margin control buttons. |
| 150 | this.$container.on( |
| 151 | 'click', |
| 152 | 'button.margin-control-button', |
| 153 | function() { |
| 154 | var status = $( this ).hasClass( 'active-margin' ); |
| 155 | |
| 156 | self.activateMargin( this, status ); |
| 157 | } |
| 158 | ); |
| 159 | |
| 160 | // Button alignment. |
| 161 | this.$container.on( |
| 162 | 'click', |
| 163 | '.button-alignment .alignment-button', |
| 164 | function() { |
| 165 | $( '.button-alignment .alignment-button[data-selected="true"]' ) |
| 166 | .attr( 'data-selected', 'false' ); |
| 167 | $( this ).attr( 'data-selected', 'true' ); |
| 168 | |
| 169 | self.loadPreview( '' ); |
| 170 | } |
| 171 | ); |
| 172 | |
| 173 | $( 'body' ).on( |
| 174 | 'click', |
| 175 | '.item label', |
| 176 | function() { |
| 177 | var checked = $( this ).siblings( 'input' ).is( ':checked' ); |
| 178 | |
| 179 | $( '.sharethis-inline-follow-buttons' ).removeClass( 'st-has-labels' ); |
| 180 | |
| 181 | if ( ! checked ) { |
| 182 | $( this ).closest( '.st-radio-config' ).find( '.item' ).each( |
| 183 | function() { |
| 184 | $( this ).find( 'input' ).prop( 'checked', false ); |
| 185 | } |
| 186 | ); |
| 187 | |
| 188 | $( this ).siblings( 'input' ).prop( 'checked', true ); |
| 189 | } |
| 190 | |
| 191 | self.loadPreview( '' ); |
| 192 | } |
| 193 | ); |
| 194 | |
| 195 | // All levers. |
| 196 | this.$container.on( |
| 197 | 'click', |
| 198 | '.item div.switch', |
| 199 | function() { |
| 200 | self.loadPreview( '' ); |
| 201 | } |
| 202 | ); |
| 203 | |
| 204 | // CTA text. |
| 205 | this.$container.on( |
| 206 | 'keyup', |
| 207 | '.cta-text input', |
| 208 | function() { |
| 209 | self.loadPreview( '' ); |
| 210 | } |
| 211 | ); |
| 212 | |
| 213 | // Minimum count. |
| 214 | this.$container.on( |
| 215 | 'change', |
| 216 | '#radius-selector', |
| 217 | function() { |
| 218 | self.loadPreview( '' ); |
| 219 | } |
| 220 | ); |
| 221 | |
| 222 | // Add profile to network. |
| 223 | this.$container.on( |
| 224 | 'keyup', |
| 225 | '#st-network-urls .center-align .profile_link', |
| 226 | function() { |
| 227 | clearTimeout( timer ); |
| 228 | |
| 229 | timer = setTimeout( |
| 230 | function() { |
| 231 | self.loadPreview( '' ); |
| 232 | }.bind( this ), |
| 233 | 1000 |
| 234 | ); |
| 235 | } |
| 236 | ); |
| 237 | |
| 238 | // Select or deselect a network. |
| 239 | this.$container.on( |
| 240 | 'click', |
| 241 | '.follow-buttons .follow-button', |
| 242 | function() { |
| 243 | var selection = $( this ).attr( 'data-selected' ), |
| 244 | follow = $( this ).attr( 'data-network' ); |
| 245 | |
| 246 | if ( 'true' === selection ) { |
| 247 | $( this ).attr( 'data-selected', 'false' ); |
| 248 | $( '.center-align[data-network="' + follow + '"]' ).attr( 'data-selected', 'false' ); |
| 249 | $( '.sharethis-selected-networks > div > div[data-network="' + follow + '"]' ).remove(); |
| 250 | } else { |
| 251 | $( this ).attr( 'data-selected', 'true' ); |
| 252 | $( '.center-align[data-network="' + follow + '"]' ).attr( 'data-selected', 'true' ); |
| 253 | $( '.sharethis-selected-networks > div' ).append( '<div class="st-btn" data-network="' + follow + '" style="display: inline-block;"></div>' ); |
| 254 | } |
| 255 | |
| 256 | self.loadPreview( '' ); |
| 257 | } |
| 258 | ); |
| 259 | |
| 260 | // Add class to preview when scrolled to. |
| 261 | $( window ).on( |
| 262 | 'scroll', |
| 263 | function() { |
| 264 | if ( undefined === $( '.selected-button' ).offset() ) { |
| 265 | return; |
| 266 | } |
| 267 | |
| 268 | var stickyTop = $( '.selected-button' ).offset().top; |
| 269 | |
| 270 | if ( $( window ).scrollTop() >= stickyTop ) { |
| 271 | $( '.sharethis-selected-networks' ).addClass( 'sharethis-prev-stick' ); |
| 272 | } else { |
| 273 | $( '.sharethis-selected-networks' ).removeClass( 'sharethis-prev-stick' ); |
| 274 | } |
| 275 | } |
| 276 | ); |
| 277 | |
| 278 | // Submit configurations. |
| 279 | $( '.sharethis-wrap form' ).submit( |
| 280 | function() { |
| 281 | self.loadPreview( 'submit', 'inline-follow' ); |
| 282 | } |
| 283 | ); |
| 284 | |
| 285 | // Tooltip. |
| 286 | this.$container.on( |
| 287 | 'mouseover', |
| 288 | '.tooltip-icon', |
| 289 | function() { |
| 290 | var tooltip = $( this ).attr( 'data-tooltip' ), |
| 291 | position = $( this ).position(), |
| 292 | leftPos = position.left + 20, |
| 293 | topPos = position.top - 20; |
| 294 | |
| 295 | $( '.tooltip-message-over' ).fadeIn( 500 ).html( tooltip ).css( { 'top': topPos + 'px', 'left': leftPos + 'px' } ); |
| 296 | } |
| 297 | ); |
| 298 | |
| 299 | // Tooltip out. |
| 300 | this.$container.on( |
| 301 | 'mouseleave', |
| 302 | '.tooltip-icon', |
| 303 | function() { |
| 304 | $( '.tooltip-message-over' ).fadeOut( 500 ); |
| 305 | } |
| 306 | ); |
| 307 | |
| 308 | // Close notice. |
| 309 | $( 'body' ).on( |
| 310 | 'click', |
| 311 | '.notice-dismiss', |
| 312 | function() { |
| 313 | $( this ).parent( '.notice.is-dismissible' ).hide(); |
| 314 | } |
| 315 | ); |
| 316 | }, |
| 317 | |
| 318 | /** |
| 319 | * Check the platform has updated the button configs. |
| 320 | */ |
| 321 | checkIfChanged: function() { |
| 322 | var iTs = this.$config[ 'inline-follow-buttons' ], |
| 323 | myITs = this.data.buttonConfig['inline-follow']; |
| 324 | |
| 325 | // Set variables if array exists. |
| 326 | if ( undefined !== iTs ) { |
| 327 | iTs = iTs[ 'updated_at' ]; |
| 328 | |
| 329 | if ( undefined !== iTs ) { |
| 330 | iTs = iTs.toString(); |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | if ( undefined !== myITs ) { |
| 335 | myITs = myITs[ 'updated_at' ]; |
| 336 | } |
| 337 | |
| 338 | // If platform has updated the button config or platform configs are broken use WP config. |
| 339 | if ( iTs !== myITs || undefined === this.data.buttonConfig ) { |
| 340 | this.setConfigFields( 'inline-follow', this.$config[ 'inline-follow-buttons' ], 'platform' ); |
| 341 | } else { |
| 342 | this.loadPreview( 'initial', '' ); |
| 343 | } |
| 344 | }, |
| 345 | |
| 346 | /** |
| 347 | * Show button configuration. |
| 348 | * |
| 349 | * @param button |
| 350 | * @param type |
| 351 | * @param event |
| 352 | */ |
| 353 | updateButtons: function( button, type, event ) { |
| 354 | const self = this; |
| 355 | |
| 356 | // Set option value for button. |
| 357 | wp.ajax.post( |
| 358 | 'update_follow_buttons', |
| 359 | { |
| 360 | type: 'inline-follow', |
| 361 | onoff: type, |
| 362 | nonce: this.data.nonce |
| 363 | } |
| 364 | ).always( |
| 365 | function() { |
| 366 | // If not one of the show types then hide. |
| 367 | if ( 'On' === type ) { |
| 368 | self.loadPreview( 'turnon', button ); |
| 369 | } else { |
| 370 | self.loadPreview( 'turnoff', button ); |
| 371 | } |
| 372 | } |
| 373 | ); |
| 374 | }, |
| 375 | |
| 376 | /** |
| 377 | * Copy text to clipboard |
| 378 | * |
| 379 | * @param copiedText |
| 380 | */ |
| 381 | copyText: function( copiedText ) { |
| 382 | copiedText.select(); |
| 383 | document.execCommand( 'copy' ); |
| 384 | }, |
| 385 | |
| 386 | /** |
| 387 | * Add the reset buttons to share buttons menu |
| 388 | */ |
| 389 | createReset: function() { |
| 390 | var button = '<input type="button" id="reset" class="button button-primary" value="Reset">', |
| 391 | newButtons = $( '.sharethis-wrap form .submit' ).append( button ).clone(); |
| 392 | }, |
| 393 | |
| 394 | /** |
| 395 | * Set to default settings when reset is clicked. |
| 396 | * |
| 397 | * @param type |
| 398 | */ |
| 399 | setDefaults: function( type ) { |
| 400 | wp.ajax.post( |
| 401 | 'set_follow_default_settings', |
| 402 | { |
| 403 | type: type, |
| 404 | nonce: this.data.nonce |
| 405 | } |
| 406 | ).always( |
| 407 | function() { |
| 408 | if ( 'both' !== type ) { |
| 409 | location.href = location.pathname + '?page=sharethis-share-buttons&reset=' + type; |
| 410 | } else { |
| 411 | location.reload(); |
| 412 | } |
| 413 | } |
| 414 | ); |
| 415 | }, |
| 416 | |
| 417 | /** |
| 418 | * Get current config data from user. |
| 419 | */ |
| 420 | getConfig: function() { |
| 421 | var result = null, |
| 422 | callExtra = 'secret=' + this.data.secret; |
| 423 | |
| 424 | if ( 'undefined' === this.data.secret || undefined === this.data.secret ) { |
| 425 | callExtra = 'token=' + this.data.token; |
| 426 | } |
| 427 | |
| 428 | $.ajax( |
| 429 | { |
| 430 | url: 'https://platform-api.sharethis.com/v1.0/property/?' + callExtra + '&id=' + this.data.propertyid, |
| 431 | method: 'GET', |
| 432 | async: false, |
| 433 | contentType: 'application/json; charset=utf-8', |
| 434 | success: function( results ) { |
| 435 | result = results; |
| 436 | } |
| 437 | } |
| 438 | ); |
| 439 | |
| 440 | return result; |
| 441 | }, |
| 442 | |
| 443 | /** |
| 444 | * Activate specified option margin controls and show/hide |
| 445 | * |
| 446 | * @param marginButton |
| 447 | * @param status |
| 448 | */ |
| 449 | activateMargin: function( marginButton, status ) { |
| 450 | if ( ! status ) { |
| 451 | $( marginButton ).addClass( 'active-margin' ).find( 'span.margin-on-off' ).html( 'On' ); |
| 452 | $( marginButton ).siblings( 'div.margin-input-fields' ).show().find( 'input' ).prop( 'disabled', false ); |
| 453 | } else { |
| 454 | $( marginButton ).removeClass( 'active-margin' ).find( 'span.margin-on-off' ).html( 'Off' ); |
| 455 | $( marginButton ).siblings( 'div.margin-input-fields' ).hide().find( 'input' ).prop( 'disabled', true ); |
| 456 | } |
| 457 | }, |
| 458 | |
| 459 | /** |
| 460 | * Set the settings fields for the button configurations. |
| 461 | * |
| 462 | * @param button |
| 463 | */ |
| 464 | setConfigFields: function( button, config, type ) { |
| 465 | var size, |
| 466 | button = 'inline-follow'; |
| 467 | |
| 468 | if ( '' === config ) { |
| 469 | config = this.data.buttonConfig[ button ]; |
| 470 | } |
| 471 | |
| 472 | if ( undefined === config ) { |
| 473 | return; |
| 474 | } |
| 475 | |
| 476 | $( '.follow-buttons .follow-button, #st-network-urls .center-align' ).each( |
| 477 | function() { |
| 478 | $( this ).attr( 'data-selected', false ); |
| 479 | } |
| 480 | ); |
| 481 | |
| 482 | // Follows. |
| 483 | $.each( |
| 484 | config[ 'networks' ], |
| 485 | function( index, value ) { |
| 486 | $( '.follow-buttons .follow-button[data-network="' + value + '"]' ).attr( 'data-selected', 'true' ); |
| 487 | $( '#st-network-urls .center-align[data-network="' + value + '"]' ).attr( 'data-selected', 'true' ); |
| 488 | } |
| 489 | ); |
| 490 | |
| 491 | // Alignment. |
| 492 | $( '.button-alignment .alignment-button[data-selected="true"]' ).attr( 'data-selected', 'false' ); |
| 493 | $( '.button-alignment .alignment-button[data-alignment="' + config[ 'alignment' ] + '"]' ).attr( 'data-selected', 'true' ); |
| 494 | |
| 495 | // Label Position. |
| 496 | $( '.label-position .item input' ).prop( 'checked', false ); |
| 497 | $( '.label-position #' + config['action_pos'] ).siblings( 'input' ).prop( 'checked', true ); |
| 498 | |
| 499 | // Profiles. |
| 500 | $.each( |
| 501 | config.profiles, |
| 502 | function( name, value ) { |
| 503 | $( '#st-network-urls .center-align[data-network="' + name + '"]' ).find( 'input.profile_link' ).val( value ); |
| 504 | } |
| 505 | ); |
| 506 | |
| 507 | // CTA. |
| 508 | $( 'div.call-to-action' ).find( 'input' ).prop( 'checked', ( 'false' !== config['action_enable'] && false !== config['action_enable'] ) ); |
| 509 | $( '.cta-text input' ).val( config.action ); |
| 510 | |
| 511 | // Corners. |
| 512 | if ( parseInt( config.radius.toString().replace( 'px', '' ) ) > $( '#' + button + ' #radius-selector' ).attr( 'max' ) ) { |
| 513 | $( '#' + button + ' #radius-selector' ).attr( 'max', config.radius.toString().replace( 'px', '' ) ); |
| 514 | $( '#' + button + ' #radius-selector' ).val( config.radius.toString().replace( 'px', '' ) ); |
| 515 | } else { |
| 516 | $( '#' + button + ' #radius-selector' ).val( config.radius.toString().replace( 'px', '' ) ); |
| 517 | } |
| 518 | |
| 519 | // Size. |
| 520 | $( '.button-size .item input' ).prop( 'checked', false ); |
| 521 | |
| 522 | if ( '32' === config.size.toString() ) { |
| 523 | size = '#small'; |
| 524 | } |
| 525 | |
| 526 | if ( '40' === config.size.toString() ) { |
| 527 | size = '#medium'; |
| 528 | } |
| 529 | |
| 530 | if ( '48' === config.size.toString() ) { |
| 531 | size = '#large'; |
| 532 | } |
| 533 | |
| 534 | $( '.button-size ' + size ).siblings( 'input' ).prop( 'checked', true ); |
| 535 | |
| 536 | // Extra spacing. |
| 537 | $( 'div.extra-spacing' ).find( 'input' ).prop( 'checked', ( 0 !== config.spacing && '0' !== config.spacing ) ); |
| 538 | |
| 539 | if ( 'platform' === type ) { |
| 540 | this.loadPreview( 'initial-platform', button ); |
| 541 | } |
| 542 | }, |
| 543 | |
| 544 | /** |
| 545 | * Check if share buttons are active and plugin doesn't exist. |
| 546 | */ |
| 547 | shareButtonsExists: function() { |
| 548 | var needPlugin = ( ( undefined !== this.$config[ 'inline-share-buttons' ] || undefined !== this.$config[ 'sticky-share-buttons' ] ) && false === this.data.shareButtons ); |
| 549 | |
| 550 | if ( needPlugin ) { |
| 551 | this.$container.before( |
| 552 | '<div class="notice notice-error is-dismissible">' + |
| 553 | '<p>' + |
| 554 | 'It appears you have share buttons enabled in your account, but do not have the ' + |
| 555 | '<strong>' + |
| 556 | 'ShareThis Share Buttons' + |
| 557 | '</strong>' + |
| 558 | ' WordPress plugin installed or activated!' + |
| 559 | '</p>' + |
| 560 | '<p>' + |
| 561 | 'Please go here: ' + |
| 562 | '<a href="https://wordpress.org/plugins/sharethis-share-buttons/" target="_blank">' + |
| 563 | 'https://wordpress.org/plugins/sharethis-share-buttons/' + |
| 564 | '</a>' + |
| 565 | ' to download our plugin and utilize our Share Buttons with the power of WordPress!' + |
| 566 | '</p>' + |
| 567 | '</div>' |
| 568 | ); |
| 569 | } |
| 570 | }, |
| 571 | |
| 572 | /** |
| 573 | * Check if share buttons are active and plugin doesn't exist. |
| 574 | */ |
| 575 | postPageTriggered: function() { |
| 576 | if ( 0 === $( '.notice.follow-notice' ).length ) { |
| 577 | this.$container.before( |
| 578 | '<div class="notice notice-info is-dismissible follow-notice">' + |
| 579 | '<p>' + |
| 580 | 'We recommending adding Follow Buttons to the header, footer or if available your sidebars if you are also using Share Buttons. You can do this with our' + |
| 581 | ' <a href="' + this.data.url + '/wp-admin/widgets.php">' + |
| 582 | 'Widget' + |
| 583 | '</a>' + |
| 584 | ', Shortcode, or Template code.' + |
| 585 | '</p>' + |
| 586 | '<button type="button" class="notice-dismiss"><span class="screen-reader-text">Dismiss this notice.</span></button>' + |
| 587 | '</div>' |
| 588 | ); |
| 589 | } |
| 590 | }, |
| 591 | |
| 592 | /** |
| 593 | * Load preview buttons. |
| 594 | * |
| 595 | * @param type |
| 596 | * @param button |
| 597 | */ |
| 598 | loadPreview: function( type, button ) { |
| 599 | if ( 'initial' === type ) { |
| 600 | this.setConfigFields( 'inline-follow', '', '' ); |
| 601 | } |
| 602 | |
| 603 | var bAlignment = $( '.button-alignment .alignment-button[data-selected="true"]' ).attr( 'data-alignment' ), |
| 604 | self = this, |
| 605 | bSize = $( '.button-size .item input:checked' ).siblings( 'label' ).html(), |
| 606 | actionCopy = $( '.cta-text input' ).val(), |
| 607 | enableAction = $( '.cta-on-off' ) |
| 608 | .find( 'input' ) |
| 609 | .is( ':checked' ), |
| 610 | extraSpacing = $( '.extra-spacing' ) |
| 611 | .find( 'input' ) |
| 612 | .is( ':checked' ), |
| 613 | bRadius = $( '#radius-selector' ).val() + 'px', |
| 614 | lPosition = $( '.label-position .item input:checked' ).siblings( 'label' ).html(), |
| 615 | follows = [], |
| 616 | size, |
| 617 | spacing = 0, |
| 618 | padding, |
| 619 | fontSize, |
| 620 | config, |
| 621 | beforeConfig, |
| 622 | theFirst = false, |
| 623 | wpConfig, |
| 624 | timer = '', |
| 625 | upConfig, |
| 626 | theData, |
| 627 | enabled = false, |
| 628 | networkName, |
| 629 | networkProfile, |
| 630 | profiles = {}; |
| 631 | |
| 632 | if ( undefined !== lPosition ) { |
| 633 | lPosition = lPosition.toLowerCase(); |
| 634 | } |
| 635 | |
| 636 | $( '#st-network-urls .center-align[data-selected="true"]' ).each( |
| 637 | function( index ) { |
| 638 | networkName = $( this ).attr( 'data-network' ); |
| 639 | networkProfile = $( this ).find( '.profile_link' ).val(); |
| 640 | profiles[ networkName ] = networkProfile; |
| 641 | } |
| 642 | ); |
| 643 | |
| 644 | if ( 'Small' === bSize ) { |
| 645 | size = 32; |
| 646 | fontSize = 11; |
| 647 | padding = 8; |
| 648 | |
| 649 | $( '#radius-selector' ).attr( 'max', 16 ); |
| 650 | } |
| 651 | |
| 652 | if ( 'Medium' === bSize ) { |
| 653 | size = 40; |
| 654 | fontSize = 12; |
| 655 | padding = 10; |
| 656 | |
| 657 | $( '#radius-selector' ).attr( 'max', 20 ); |
| 658 | } |
| 659 | |
| 660 | if ( 'Large' === bSize ) { |
| 661 | size = 48; |
| 662 | fontSize = 16; |
| 663 | padding = 12; |
| 664 | |
| 665 | $( '#radius-selector' ).attr( 'max', 26 ); |
| 666 | } |
| 667 | |
| 668 | if ( extraSpacing ) { |
| 669 | spacing = 8; |
| 670 | } |
| 671 | |
| 672 | if ( 'initial' === type && undefined !== this.data.buttonConfig[ 'networks' ] ) { |
| 673 | follows = this.data.buttonConfig[ 'networks' ]; |
| 674 | } else { |
| 675 | $( '.sharethis-selected-networks > div .st-btn' ).each( |
| 676 | function( index ) { |
| 677 | follows[ index ] = $( this ).attr( 'data-network' ); |
| 678 | } |
| 679 | ); |
| 680 | } |
| 681 | |
| 682 | if ( 'sync-platform' === type && undefined !== this.$config[ 'inline-follow-buttons' ] ) { |
| 683 | follows = this.$config[ 'inline-follow-buttons' ][ 'networks' ]; |
| 684 | } |
| 685 | |
| 686 | // If newly turned on use selected networks. |
| 687 | if ( 'turnon' === type || undefined !== this.data.buttonConfig && undefined === this.data.buttonConfig['networks'] ) { |
| 688 | follows = []; |
| 689 | |
| 690 | $( '.follow-buttons .follow-button[data-selected="true"]' ).each( |
| 691 | function( index ) { |
| 692 | follows[ index ] = $( this ).attr( 'data-network' ); |
| 693 | } |
| 694 | ); |
| 695 | } |
| 696 | |
| 697 | if ( 'submit' === type ) { |
| 698 | follows = []; |
| 699 | |
| 700 | $( '.sharethis-selected-networks > div .st-btn' ).each( |
| 701 | function( index ) { |
| 702 | follows[ index ] = $( this ).attr( 'data-network' ); |
| 703 | } |
| 704 | ); |
| 705 | } |
| 706 | |
| 707 | // If submited or turned on make sure enabled setting is set properly. |
| 708 | if ( undefined !== this.$config && undefined !== this.$config[ 'inline-follow-buttons' ] && undefined !== this.$config[ 'inline-follow-buttons' ][ 'enabled' ] ) { |
| 709 | enabled = 'true' === this.$config[ 'inline-follow-buttons' ][ 'enabled' ] || |
| 710 | true === this.$config[ 'inline-follow-buttons' ][ 'enabled' ] || |
| 711 | true === this.$tempEnable; |
| 712 | } else { |
| 713 | enabled = false; |
| 714 | } |
| 715 | |
| 716 | config = { |
| 717 | action: actionCopy, |
| 718 | action_enable: enableAction, |
| 719 | alignment: bAlignment, |
| 720 | networks: follows, |
| 721 | size: size, |
| 722 | radius: bRadius, |
| 723 | padding: padding, |
| 724 | action_pos: lPosition, |
| 725 | fontsize: fontSize, |
| 726 | spacing: spacing, |
| 727 | enabled: enabled, |
| 728 | profiles: profiles, |
| 729 | fade_in: false |
| 730 | }; |
| 731 | |
| 732 | // Set config for initial post. |
| 733 | beforeConfig = config; |
| 734 | |
| 735 | if ( 'submit' === type || 'initial-platform' === type || 'turnon' === type || 'turnoff' === type ) { |
| 736 | |
| 737 | // If submiting WP keep platform timestamp if exists. |
| 738 | if ( 'submit' === type && undefined !== this.$config[ 'inline-follow-buttons' ] && undefined !== this.$config[ 'inline-follow-buttons' ][ 'updated_at' ] ) { |
| 739 | config[ 'updated_at' ] = this.$config[ 'inline-follow-buttons' ][ 'updated_at' ]; |
| 740 | } |
| 741 | |
| 742 | // If platform different from WP. |
| 743 | if ( 'initial-platform' === type ) { |
| 744 | config = this.$config[ 'inline-follow-buttons' ]; |
| 745 | |
| 746 | if ( undefined === this.data.buttonConfig || true === this.data.buttonConfig ) { |
| 747 | theFirst = 'upgrade'; |
| 748 | } |
| 749 | } |
| 750 | |
| 751 | // If first load ever. |
| 752 | if ( 'initial-platform' === type && undefined !== this.data.buttonConfig[ 'inline-follow' ] && undefined === this.data.buttonConfig[ 'inline-follow' ][ 'updated_at' ] && undefined !== this.$config[ 'inline-follow-buttons' ][ 'updated_at' ] ) { |
| 753 | config = beforeConfig; |
| 754 | config.updated_at = this.$config[ 'inline-follow-buttons' ][ 'updated_at' ]; |
| 755 | config.networks = this.data.buttonConfig[ 'inline-follow' ][ 'networks' ]; |
| 756 | } |
| 757 | |
| 758 | if ( 'turnon' === type ) { |
| 759 | config.enabled = true; |
| 760 | config.networks = [ 'facebook', 'twitter', 'instagram', 'youtube' ]; |
| 761 | |
| 762 | $.each( |
| 763 | config.networks, |
| 764 | function( index, value ) { |
| 765 | $( '.follow-buttons .follow-button[data-selected="' + value + '"]' ).attr( 'data-selected', 'true' ); |
| 766 | } |
| 767 | ); |
| 768 | |
| 769 | // Set temp enable to true. |
| 770 | this.$tempEnable = true; |
| 771 | } |
| 772 | |
| 773 | if ( 'turnoff' === type ) { |
| 774 | config.enabled = false; |
| 775 | |
| 776 | // Set temp enable to false. |
| 777 | this.$tempEnable = false; |
| 778 | } |
| 779 | |
| 780 | if ( 'upgrade' === theFirst ) { |
| 781 | upConfig = { |
| 782 | 'inline-follow': this.$config[ 'inline-follow-buttons' ] |
| 783 | }; |
| 784 | |
| 785 | wp.ajax.post( |
| 786 | 'set_follow_button_config', |
| 787 | { |
| 788 | button: 'platform', |
| 789 | config: upConfig, |
| 790 | first: theFirst, |
| 791 | type: 'login', |
| 792 | nonce: this.data.nonce |
| 793 | } |
| 794 | ).always( |
| 795 | function( results ) { |
| 796 | location.reload(); |
| 797 | }.bind( this ) |
| 798 | ); |
| 799 | } else { |
| 800 | wp.ajax.post( |
| 801 | 'set_follow_button_config', |
| 802 | { |
| 803 | button: 'inline-follow', |
| 804 | config: config, |
| 805 | first: false, |
| 806 | nonce: this.data.nonce |
| 807 | } |
| 808 | ); |
| 809 | |
| 810 | if ( 'initial-platform' !== type || ( |
| 811 | undefined !== this.data.buttonConfig['inline-follow'] && undefined === this.data.buttonConfig['inline-follow'][ 'updated_at' ] |
| 812 | ) ) { |
| 813 | config.enabled = 'true' === config.enabled || true === config.enabled; |
| 814 | |
| 815 | delete config.container; |
| 816 | delete config.id; |
| 817 | delete config[ 'has_spacing' ]; |
| 818 | delete config[ 'fade_in' ]; |
| 819 | delete config[ 'show_mobile_buttons' ]; |
| 820 | |
| 821 | theData = JSON.stringify( |
| 822 | { |
| 823 | 'secret': this.data.secret, |
| 824 | 'id': this.data.propertyid, |
| 825 | 'product': 'inline-follow-buttons', |
| 826 | 'config': config |
| 827 | } |
| 828 | ); |
| 829 | |
| 830 | // Send new button status value. |
| 831 | $.ajax( |
| 832 | { |
| 833 | url: 'https://platform-api.sharethis.com/v1.0/property/product', |
| 834 | method: 'POST', |
| 835 | async: false, |
| 836 | contentType: 'application/json; charset=utf-8', |
| 837 | data: theData, |
| 838 | success: function() { |
| 839 | if ( 'turnon' === type || 'turnoff' === type ) { |
| 840 | location.reload(); |
| 841 | } |
| 842 | } |
| 843 | } |
| 844 | ); |
| 845 | } |
| 846 | } |
| 847 | } |
| 848 | |
| 849 | $( '.sharethis-inline-follow-buttons' ).html( '' ); |
| 850 | |
| 851 | config['enabled'] = true; |
| 852 | |
| 853 | window.__sharethis__.href = 'https://www.sharethis.com/'; |
| 854 | window.__sharethis__.load( 'inline-follow-buttons', config ); |
| 855 | |
| 856 | $( '.sharethis-selected-networks > div .st-btn' ).show(); |
| 857 | |
| 858 | $( '.sharethis-selected-networks > div' ).sortable( |
| 859 | { |
| 860 | stop: function( event, ui ) { |
| 861 | self.loadPreview( '' ); |
| 862 | } |
| 863 | } |
| 864 | ); |
| 865 | } |
| 866 | }; |
| 867 | } )( window.jQuery, window.wp ); |
| 868 |