publicize.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /* jshint onevar: false, smarttabs: true */
  2. /* global tb_show, tb_pathToImage, ajaxurl, tb_init, tb_remove */
  3. /* global myblogsResponse, publicizeConnTestStart:true, publicizeConnTestComplete:true, publicizeConnRefreshClick:true */
  4. var showOptionsPage;
  5. jQuery( function( $ ) {
  6. showOptionsPage = function( service, nonce, connection, blogId ) {
  7. tb_show( null, null, null );
  8. $('body').append( '<div id=\'TB_load\'><img src=\'' + tb_pathToImage + '\' /></div>' );
  9. $('#TB_load').show();
  10. var query = '';
  11. if ( connection ) {
  12. query += '&connection=' + encodeURIComponent( connection );
  13. }
  14. if ( 'undefined' !== typeof( blogId ) && blogId ) {
  15. query += '&blog_id=' + Number( blogId );
  16. }
  17. $.post( ajaxurl, 'action=publicize_' + service + '_options_page&_wpnonce=' + nonce + query, function( response ) {
  18. $('#TB_load').remove();
  19. try {
  20. var obj = jQuery.parseJSON( response );
  21. if ( obj && 'object' === typeof( obj ) ) {
  22. if ( obj.hasOwnProperty( 'fb_redirect' ) ) {
  23. location.href = obj.fb_redirect + '&redirect_uri=' + encodeURIComponent( location.href );
  24. return;
  25. }
  26. }
  27. } catch (err) {
  28. // Do nothing and move on
  29. }
  30. if ( response ) {
  31. var blogID = $( 'input[name=wpas_ajax_blog_id]' ).val();
  32. var message = $( '<div id="wpas-ajax-' + blogID + '" class="wrap"></div>' ).append( response );
  33. message.append( '<a href="#TB_inline?thickbox&height=420&width=555&inlineId=wpas-ajax-' + blogID + '" id="wpas-click-' + blogID + '" class="new-thickbox" style="display: none;"></a>' );
  34. $('#wpas-message').html( message );
  35. tb_init( 'a.new-thickbox' );
  36. $('#wpas-click-' + blogID).click();
  37. var tb_height = parseInt( $('#TB_ajaxContent').css('height'), 10 );
  38. var content_height = $('#thickbox-content').height();
  39. if ( content_height < tb_height ) {
  40. var new_height = content_height + 15;
  41. $('#TB_ajaxContent').css( 'height', new_height );
  42. var new_margin = parseInt( $('#TB_window').css( 'margin-top'), 10 ) + (tb_height - new_height) / 2 + 'px';
  43. $('#TB_window').css( 'margin-top', new_margin);
  44. }
  45. $('.save-options').unbind('click').click( function() {
  46. var sel = $( 'input[name=\'option\']:checked' );
  47. var global = $( 'input[name=\'global\']:checked' );
  48. var connection = $(this).data('connection');
  49. var token = encodeURIComponent( sel.val() );
  50. var id = encodeURIComponent( sel.attr( 'id' ) );
  51. var type = encodeURIComponent( sel.attr( 'data-type' ) );
  52. var nonce = $(this).attr('rel');
  53. var global_conn = 'off';
  54. var global_nonce = '';
  55. if ( global.length ) {
  56. global_conn = 'on';
  57. global_nonce = global.val();
  58. }
  59. $.post( ajaxurl, 'action=publicize_'+ service + '_options_save&connection=' + connection + '&selected_id=' + id + '&token=' + token + '&type=' + type + '&_wpnonce=' + nonce + '&global=' + global_conn + '&global_nonce=' + global_nonce, function( /*response*/ ) {
  60. var frameNonce;
  61. tb_remove();
  62. frameNonce = document.location.search.match( /frame-nonce=([^&]+)/ );
  63. if ( /inside-newdash=1/.test( document.location.search ) && frameNonce ) {
  64. document.location = 'options-general.php?page=sharing&inside-newdash=1&frame-nonce' + frameNonce[1];
  65. } else {
  66. top.location = 'options-general.php?page=sharing';
  67. }
  68. } );
  69. } );
  70. }
  71. }, 'html' );
  72. };
  73. $( 'body' ).append( '<div id="wpas-message" style="display: none"></div>' );
  74. $( '.wpas-posts' ).change( function() {
  75. var inputs = $(this).parents( 'td:first' ).find( ':input' );
  76. var _this = this;
  77. var blogID = inputs.filter( '[name=wpas_ajax_blog_id]' ).val();
  78. $( '#waiting_' + blogID ).show();
  79. $.post( ajaxurl, inputs.serialize() + '&action=wpas_post', function( response ) { myblogsResponse.call( _this, blogID, response ); }, 'html' );
  80. } );
  81. $( '.options' ).unbind('click').bind( 'click', function(e) {
  82. e.preventDefault();
  83. e.stopPropagation();
  84. var service = $(this).attr('class').replace( 'options ', '' );
  85. var blogId = null;
  86. if ( 'undefined' !== typeof( $(this).attr('id') ) ) {
  87. blogId = parseInt( $(this).attr('id').replace( 'options-', '' ), 10 );
  88. }
  89. var nonce = $(this).attr('href').replace( '#nonce=', '' );
  90. var connection = $(this).data( 'connection' );
  91. showOptionsPage.call( this, service, nonce, connection, blogId );
  92. });
  93. /**
  94. * Kicks off tests for all connections
  95. */
  96. publicizeConnTestStart = function() {
  97. $( '.pub-connection-test' )
  98. .addClass( 'test-in-progress' );
  99. $.post( ajaxurl, { action: 'test_publicize_conns' }, publicizeConnTestComplete );
  100. };
  101. publicizeConnRefreshClick = function( event ) {
  102. event.preventDefault();
  103. var popupURL = event.currentTarget.href;
  104. var popupTitle = event.currentTarget.title;
  105. // open a popup window
  106. // when it is closed, kick off the tests again
  107. var popupWin = window.open( popupURL, popupTitle, '' );
  108. var popupWinTimer= window.setInterval( function() {
  109. if ( popupWin.closed !== false ) {
  110. window.clearInterval( popupWinTimer );
  111. publicizeConnTestStart();
  112. }
  113. }, 500 );
  114. };
  115. publicizeConnTestComplete = function( response ) {
  116. $( '.pub-connection-test' ).removeClass( 'test-in-progress' );
  117. $.each( response.data, function( index, testResult ) {
  118. // find the li for this connection
  119. var testSelector = '#pub-connection-test-' + testResult.connectionID;
  120. if ( testResult.connectionTestPassed ) {
  121. $( testSelector )
  122. .addClass( 'test-passed' )
  123. .html( '' )
  124. .removeClass( 'test-failed' );
  125. } else {
  126. $( testSelector )
  127. .addClass( 'test-failed' )
  128. .html( '<p><span class="pub-connection-error">' + testResult.connectionTestMessage + '</span></p>' )
  129. .removeClass( 'test-passed' );
  130. if ( testResult.userCanRefresh ) {
  131. $( testSelector )
  132. .append( '<br/>' );
  133. $( '<a/>', {
  134. 'class' : 'pub-refresh-button button',
  135. 'title' : testResult.refreshText,
  136. 'href' : testResult.refreshURL,
  137. 'text' : testResult.refreshText,
  138. 'target' : '_refresh_' + testResult.serviceName
  139. } )
  140. .appendTo( testSelector )
  141. .click( publicizeConnRefreshClick );
  142. }
  143. }
  144. } );
  145. };
  146. $( document ).ready( function() {
  147. // If we have at least one .pub-connection-test div present, kick off the connection test
  148. if ( $( '.pub-connection-test' ).length ) {
  149. publicizeConnTestStart();
  150. }
  151. } );
  152. } );