Açıklama Yok

admin-sharing.js 14KB

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