PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/blog/wp-content/plugins/jetpack/modules/sharedaddy/admin-sharing.js

https://gitlab.com/relacilia/cakra
JavaScript | 440 lines | 303 code | 84 blank | 53 comment | 39 complexity | d4e7cd628254f1bc215fce69e6aec375 MD5 | raw file
  1. /* jshint onevar: false, smarttabs: true */
  2. /* global sharing_loading_icon */
  3. (function($) {
  4. $( document ).ready(function() {
  5. function enable_share_button() {
  6. $( '.preview a.sharing-anchor' ).unbind( 'mouseenter mouseenter' ).hover( function() {
  7. if ( $( this ).data( 'hasappeared' ) !== true ) {
  8. var item = $( '.sharing-hidden .inner' );
  9. var original = $( this ).parents( 'li' );
  10. // Create a timer to make the area appear if the mouse hovers for a period
  11. var timer = setTimeout( function() {
  12. $( item ).css( {
  13. left: $( original ).position().left + 'px',
  14. top: $( original ).position().top + $( original ).height() + 3 + 'px'
  15. } ).slideDown( 200, function() {
  16. // Mark the item as have being appeared by the hover
  17. $( original ).data( 'hasappeared', true ).data( 'hasoriginal', true ).data( 'hasitem', false );
  18. // Remove all special handlers
  19. $( item ).mouseleave( handler_item_leave ).mouseenter( handler_item_enter );
  20. $( original ).mouseleave( handler_original_leave ).mouseenter( handler_original_enter );
  21. // Add a special handler to quickly close the item
  22. $( original ).click( close_it );
  23. } );
  24. // The following handlers take care of the mouseenter/mouseleave for the share button and the share area - if both are left then we close the share area
  25. var handler_item_leave = function() {
  26. $( original ).data( 'hasitem', false );
  27. if ( $( original ).data( 'hasoriginal' ) === false ) {
  28. var timer = setTimeout( close_it, 800 );
  29. $( original ).data( 'timer2', timer );
  30. }
  31. };
  32. var handler_item_enter = function() {
  33. $( original ).data( 'hasitem', true );
  34. clearTimeout( $( original ).data( 'timer2' ) );
  35. };
  36. var handler_original_leave = function() {
  37. $( original ).data( 'hasoriginal', false );
  38. if ( $( original ).data( 'hasitem' ) === false ) {
  39. var timer = setTimeout( close_it, 800 );
  40. $( original ).data( 'timer2', timer );
  41. }
  42. };
  43. var handler_original_enter = function() {
  44. $( original ).data( 'hasoriginal', true );
  45. clearTimeout( $( original ).data( 'timer2' ) );
  46. };
  47. var close_it = function() {
  48. item.slideUp( 200 );
  49. // Clear all hooks
  50. $( original ).unbind( 'mouseleave', handler_original_leave ).unbind( 'mouseenter', handler_original_enter );
  51. $( item ).unbind( 'mouseleave', handler_item_leave ).unbind( 'mouseenter', handler_item_leave );
  52. $( original ).data( 'hasappeared', false );
  53. $( original ).unbind( 'click', close_it );
  54. return false;
  55. };
  56. }, 200 );
  57. // Remember the timer so we can detect it on the mouseout
  58. $( this ).data( 'timer', timer );
  59. }
  60. }, function() {
  61. // Mouse out - remove any timer
  62. clearTimeout( $( this ).data( 'timer' ) );
  63. $( this ).data( 'timer', false );
  64. } );
  65. }
  66. function update_preview() {
  67. var button_style = $( '#button_style' ).val();
  68. // Clear the live preview
  69. $( '#live-preview ul.preview li' ).remove();
  70. // Add label
  71. if ( $( '#save-enabled-shares input[name=visible]' ).val() || $( '#save-enabled-shares input[name=hidden]' ).val() ) {
  72. $( '#live-preview ul.preview' ).append( $( '#live-preview ul.archive .sharing-label' ).clone() );
  73. }
  74. // Re-insert all the enabled items
  75. $( 'ul.services-enabled li' ).each( function() {
  76. if ( $( this ).hasClass( 'service' ) ) {
  77. var service = $( this ).attr( 'id' );
  78. $( '#live-preview ul.preview' ).append( $( '#live-preview ul.archive li.preview-' + service ).clone() );
  79. }
  80. } );
  81. // Add any hidden items
  82. if ( $( '#save-enabled-shares input[name=hidden]' ).val() ) {
  83. // Add share button
  84. $( '#live-preview ul.preview' ).append( $( '#live-preview ul.archive .share-more' ).parent().clone() );
  85. $( '.sharing-hidden ul li' ).remove();
  86. // Add hidden items into the inner panel
  87. $( 'ul.services-hidden li' ).each( function( /*pos, item*/ ) {
  88. if ( $( this ).hasClass( 'service' ) ) {
  89. var service = $( this ).attr( 'id' );
  90. $( '.sharing-hidden .inner ul' ).append( $( '#live-preview ul.archive .preview-' + service ).clone() );
  91. }
  92. } );
  93. enable_share_button();
  94. }
  95. $( '#live-preview div.sharedaddy' ).removeClass( 'sd-social-icon' );
  96. $( '#live-preview li.advanced' ).removeClass( 'no-icon' );
  97. // Button style
  98. if ( 'icon' === button_style ) {
  99. $( '#live-preview ul.preview div span, .sharing-hidden .inner ul div span' ).html( ' ' ).parent().addClass( 'no-text' );
  100. $( '#live-preview div.sharedaddy' ).addClass( 'sd-social-icon' );
  101. } else if ( 'official' === button_style ) {
  102. $( '#live-preview ul.preview .advanced, .sharing-hidden .inner ul .advanced' ).each( function( /*i*/ ) {
  103. if ( !$( this ).hasClass( 'preview-press-this' ) && !$( this ).hasClass( 'preview-email' ) && !$( this ).hasClass( 'preview-print' ) && !$( this ).hasClass( 'share-custom' ) ) {
  104. $( this ).find( '.option a span' ).html( '' ).parent().removeClass( 'sd-button' ).parent().attr( 'class', 'option option-smart-on' );
  105. }
  106. } );
  107. } else if ( 'text' === button_style ) {
  108. $( '#live-preview li.advanced' ).addClass( 'no-icon' );
  109. }
  110. }
  111. window.sharing_option_changed = function() {
  112. var item = this;
  113. // Loading icon
  114. $( this ).parents( 'li:first' ).css( 'backgroundImage', 'url("' + sharing_loading_icon + '")' );
  115. // Save
  116. $( this ).parents( 'form' ).ajaxSubmit( function( response ) {
  117. if ( response.indexOf( '<!---' ) >= 0 ) {
  118. var button = response.substring( 0, response.indexOf( '<!--->' ) );
  119. var preview = response.substring( response.indexOf( '<!--->' ) + 6 );
  120. if ( $( item ).is( ':submit' ) === true ) {
  121. // Update the DOM using a bit of cut/paste technology
  122. $( item ).parents( 'li:first' ).replaceWith( button );
  123. }
  124. $( '#live-preview ul.archive li.preview-' + $( item ).parents( 'form' ).find( 'input[name=service]' ).val() ).replaceWith( preview );
  125. }
  126. // Update preview
  127. update_preview();
  128. // Restore the icon
  129. $( item ).parents( 'li:first' ).removeAttr( 'style' );
  130. } );
  131. if ( $( item ).is( ':submit' ) === true ) {
  132. return false;
  133. }
  134. return true;
  135. };
  136. function showExtraOptions( service ) {
  137. jQuery( '.' + service + '-extra-options' ).css( { backgroundColor: '#ffffcc' } ).fadeIn();
  138. }
  139. function hideExtraOptions( service ) {
  140. jQuery( '.' + service + '-extra-options' ).fadeOut( 'slow' );
  141. }
  142. function save_services() {
  143. $( '#enabled-services h3 img' ).show();
  144. // Toggle various dividers/help texts
  145. if ( $( '#enabled-services ul.services-enabled li.service' ).length > 0 ) {
  146. $( '#drag-instructions' ).hide();
  147. }
  148. else {
  149. $( '#drag-instructions' ).show();
  150. }
  151. if ( $( '#enabled-services li.service' ).length > 0 ) {
  152. $( '#live-preview .services h2' ).hide();
  153. }
  154. else {
  155. $( '#live-preview .services h2' ).show();
  156. }
  157. // Gather the modules
  158. var visible = [], hidden = [];
  159. $( 'ul.services-enabled li' ).each( function() {
  160. if ( $( this ).hasClass( 'service' ) ) {
  161. // Ready for saving
  162. visible[visible.length] = $( this ).attr( 'id' );
  163. showExtraOptions( $( this ).attr( 'id' ) );
  164. }
  165. } );
  166. $( 'ul.services-available li' ).each( function() {
  167. if ( $( this ).hasClass( 'service' ) ) {
  168. hideExtraOptions( $( this ).attr( 'id' ) );
  169. }
  170. } );
  171. $( 'ul.services-hidden li' ).each( function() {
  172. if ( $( this ).hasClass( 'service' ) ) {
  173. // Ready for saving
  174. hidden[hidden.length] = $( this ).attr( 'id' );
  175. showExtraOptions( $( this ).attr( 'id' ) );
  176. }
  177. } );
  178. // Set the hidden element values
  179. $( '#save-enabled-shares input[name=visible]' ).val( visible.join( ',' ) );
  180. $( '#save-enabled-shares input[name=hidden]' ).val( hidden.join( ',' ) );
  181. update_preview();
  182. // Save it
  183. $( '#save-enabled-shares' ).ajaxSubmit( function() {
  184. $( '#enabled-services h3 img' ).hide();
  185. } );
  186. }
  187. $( '#enabled-services .services ul' ).sortable( {
  188. receive: function( /*event, ui*/ ) {
  189. save_services();
  190. },
  191. stop: function() {
  192. save_services();
  193. $( 'li.service' ).enableSelection(); // Fixes a problem with Chrome
  194. },
  195. over: function( /*event, ui*/ ) {
  196. $( this ).find( 'ul' ).addClass( 'dropping' );
  197. // Ensure the 'end-fix' is at the end
  198. $( '#enabled-services li.end-fix' ).remove();
  199. $( '#enabled-services ul' ).append( '<li class="end-fix"></li>' );
  200. },
  201. out: function( /*event, ui*/ ) {
  202. $( this ).find( 'ul' ).removeClass( 'dropping' );
  203. // Ensure the 'end-fix' is at the end
  204. $( '#enabled-services li.end-fix' ).remove();
  205. $( '#enabled-services ul' ).append( '<li class="end-fix"></li>' );
  206. },
  207. helper: function( event, ui ) {
  208. ui.find( '.advanced-form' ).hide();
  209. return ui.clone();
  210. },
  211. start: function( /*event, ui*/ ) {
  212. // Make sure that the advanced section is closed
  213. $( '.advanced-form' ).hide();
  214. $( 'li.service' ).disableSelection(); // Fixes a problem with Chrome
  215. },
  216. placeholder: 'dropzone',
  217. opacity: 0.8,
  218. delay: 150,
  219. forcePlaceholderSize: true,
  220. items: 'li',
  221. connectWith: '#available-services ul, #enabled-services .services ul',
  222. cancel: '.advanced-form'
  223. } );
  224. $( '#available-services ul' ).sortable( {
  225. opacity: 0.8,
  226. delay: 150,
  227. cursor: 'move',
  228. connectWith: '#enabled-services .services ul',
  229. placeholder: 'dropzone',
  230. forcePlaceholderSize: true,
  231. start: function() {
  232. $( '.advanced-form' ).hide();
  233. }
  234. } );
  235. // Accessibility keyboard shortcurts
  236. $( '.service' ).on( 'keydown', function ( e ) {
  237. // Reposition if one of the directional keys is pressed
  238. switch ( e.keyCode ) {
  239. case 13: keyboardDragDrop( $( this ) ); break; // Enter
  240. case 32: keyboardDragDrop( $( this ) ); break; // Space
  241. case 37: keyboardChangeOrder( $( this ), 'left' ); break; // Left
  242. case 39: keyboardChangeOrder( $( this ), 'right' ); break; // Right
  243. default: return true; // Exit and bubble
  244. }
  245. e.preventDefault();
  246. });
  247. function keyboardChangeOrder( $this, dir ) {
  248. var thisParent = $this.parent(),
  249. thisParentsChildren = thisParent.find( 'li' ),
  250. thisPosition = thisParentsChildren.index( $this ) + 1,
  251. totalChildren = thisParentsChildren.length - 1,
  252. thisService;
  253. // No need to be able to sort order for the "Available Services" section
  254. if ( thisParent.hasClass( 'services-available' ) ) {
  255. return;
  256. }
  257. if ( 'left' === dir ) {
  258. if ( 1 === thisPosition ) {
  259. return;
  260. }
  261. // Find service to left
  262. var prevSibling = $this.prev();
  263. // Detach this service from DOM
  264. thisService = $this.detach();
  265. // Move it to the appropriate area and add focus back to service
  266. prevSibling.before( thisService );
  267. // Add focus
  268. prevSibling.prev().focus();
  269. }
  270. if ( 'right' === dir ) {
  271. if ( thisPosition === totalChildren ) {
  272. return;
  273. }
  274. // Find service to left
  275. var nextSibling = $this.next();
  276. // Detach this service from DOM
  277. thisService = $this.detach();
  278. // Move it to the appropriate area and add focus back to service
  279. nextSibling.after( thisService );
  280. // Add focus
  281. nextSibling.next().focus();
  282. }
  283. //Save changes
  284. save_services();
  285. }
  286. function keyboardDragDrop( $this ) {
  287. var dropzone,
  288. thisParent = $this.parent();
  289. // Rotate through 3 available dropzones
  290. if ( thisParent.hasClass( 'services-available' ) ) {
  291. dropzone = 'services-enabled';
  292. } else if ( thisParent.hasClass( 'services-enabled' ) ) {
  293. dropzone = 'services-hidden';
  294. } else {
  295. dropzone = 'services-available';
  296. }
  297. // Detach this service from DOM
  298. var thisService = $this.detach();
  299. // Move it to the appropriate area and add focus back to service
  300. $( '.' + dropzone ).prepend( thisService ).find( 'li:first-child' ).focus();
  301. //Save changes
  302. save_services();
  303. }
  304. // Live preview 'hidden' button
  305. $( '.preview-hidden a' ).click( function() {
  306. $( this ).parent().find( '.preview' ).toggle();
  307. return false;
  308. } );
  309. // Add service
  310. $( '#new-service form' ).ajaxForm( {
  311. beforeSubmit: function() {
  312. $( '#new-service-form .error' ).hide();
  313. $( '#new-service-form img' ).show();
  314. $( '#new-service-form input[type=submit]' ).prop( 'disabled', true );
  315. },
  316. success: function( response ) {
  317. $( '#new-service-form img' ).hide();
  318. if ( ( '' + response ) === '1' ) {
  319. $( '#new-service-form .inerror' ).removeClass( 'inerror' ).addClass( 'error' );
  320. $( '#new-service-form .error' ).show();
  321. $( '#new-service-form input[type=submit]' ).prop( 'disabled', false );
  322. }
  323. else {
  324. document.location = document.location.href.replace( /&create_new_service=true/i, '' );
  325. }
  326. }
  327. }
  328. );
  329. function init_handlers() {
  330. $( '#services-config a.remove' ).unbind( 'click' ).click( function() {
  331. var form = $( this ).parent().next();
  332. // Loading icon
  333. $( this ).parents( 'li:first' ).css( 'backgroundImage', 'url("' + sharing_loading_icon + '")' );
  334. // Save
  335. form.ajaxSubmit( function( /*response*/ ) {
  336. // Remove the item
  337. form.parents( 'li:first' ).fadeOut( function() {
  338. $( this ).remove();
  339. // Update preview
  340. update_preview();
  341. } );
  342. } );
  343. return false;
  344. } );
  345. }
  346. $( '#button_style' ).change( function() {
  347. update_preview();
  348. return true;
  349. } ).change();
  350. $( 'input[name=sharing_label]' ).blur( function() {
  351. $('#live-preview h3.sd-title').text( $( '<div/>' ).text( $( this ).val() ).html() );
  352. } );
  353. init_handlers();
  354. enable_share_button();
  355. } );
  356. })( jQuery );