link.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /**
  2. * @output wp-admin/js/link.js
  3. */
  4. /* global postboxes, deleteUserSetting, setUserSetting, getUserSetting */
  5. jQuery( function($) {
  6. var newCat, noSyncChecks = false, syncChecks, catAddAfter;
  7. $('#link_name').trigger( 'focus' );
  8. // Postboxes.
  9. postboxes.add_postbox_toggles('link');
  10. /**
  11. * Adds event that opens a particular category tab.
  12. *
  13. * @ignore
  14. *
  15. * @return {boolean} Always returns false to prevent the default behavior.
  16. */
  17. $('#category-tabs a').on( 'click', function(){
  18. var t = $(this).attr('href');
  19. $(this).parent().addClass('tabs').siblings('li').removeClass('tabs');
  20. $('.tabs-panel').hide();
  21. $(t).show();
  22. if ( '#categories-all' == t )
  23. deleteUserSetting('cats');
  24. else
  25. setUserSetting('cats','pop');
  26. return false;
  27. });
  28. if ( getUserSetting('cats') )
  29. $('#category-tabs a[href="#categories-pop"]').trigger( 'click' );
  30. // Ajax Cat.
  31. newCat = $('#newcat').one( 'focus', function() { $(this).val( '' ).removeClass( 'form-input-tip' ); } );
  32. /**
  33. * After adding a new category, focus on the category add input field.
  34. *
  35. * @return {void}
  36. */
  37. $('#link-category-add-submit').on( 'click', function() { newCat.focus(); } );
  38. /**
  39. * Synchronize category checkboxes.
  40. *
  41. * This function makes sure that the checkboxes are synced between the all
  42. * categories tab and the most used categories tab.
  43. *
  44. * @since 2.5.0
  45. *
  46. * @return {void}
  47. */
  48. syncChecks = function() {
  49. if ( noSyncChecks )
  50. return;
  51. noSyncChecks = true;
  52. var th = $(this), c = th.is(':checked'), id = th.val().toString();
  53. $('#in-link-category-' + id + ', #in-popular-link_category-' + id).prop( 'checked', c );
  54. noSyncChecks = false;
  55. };
  56. /**
  57. * Adds event listeners to an added category.
  58. *
  59. * This is run on the addAfter event to make sure the correct event listeners
  60. * are bound to the DOM elements.
  61. *
  62. * @since 2.5.0
  63. *
  64. * @param {string} r Raw XML response returned from the server after adding a
  65. * category.
  66. * @param {Object} s List manager configuration object; settings for the Ajax
  67. * request.
  68. *
  69. * @return {void}
  70. */
  71. catAddAfter = function( r, s ) {
  72. $(s.what + ' response_data', r).each( function() {
  73. var t = $($(this).text());
  74. t.find( 'label' ).each( function() {
  75. var th = $(this),
  76. val = th.find('input').val(),
  77. id = th.find('input')[0].id,
  78. name = th.text().trim(),
  79. o;
  80. $('#' + id).on( 'change', syncChecks );
  81. o = $( '<option value="' + parseInt( val, 10 ) + '"></option>' ).text( name );
  82. } );
  83. } );
  84. };
  85. /*
  86. * Instantiates the list manager.
  87. *
  88. * @see js/_enqueues/lib/lists.js
  89. */
  90. $('#categorychecklist').wpList( {
  91. // CSS class name for alternate styling.
  92. alt: '',
  93. // The type of list.
  94. what: 'link-category',
  95. // ID of the element the parsed Ajax response will be stored in.
  96. response: 'category-ajax-response',
  97. // Callback that's run after an item got added to the list.
  98. addAfter: catAddAfter
  99. } );
  100. // All categories is the default tab, so we delete the user setting.
  101. $('a[href="#categories-all"]').on( 'click', function(){deleteUserSetting('cats');});
  102. // Set a preference for the popular categories to cookies.
  103. $('a[href="#categories-pop"]').on( 'click', function(){setUserSetting('cats','pop');});
  104. if ( 'pop' == getUserSetting('cats') )
  105. $('a[href="#categories-pop"]').trigger( 'click' );
  106. /**
  107. * Adds event handler that shows the interface controls to add a new category.
  108. *
  109. * @ignore
  110. *
  111. * @param {Event} event The event object.
  112. * @return {boolean} Always returns false to prevent regular link
  113. * functionality.
  114. */
  115. $('#category-add-toggle').on( 'click', function() {
  116. $(this).parents('div:first').toggleClass( 'wp-hidden-children' );
  117. $('#category-tabs a[href="#categories-all"]').trigger( 'click' );
  118. $('#newcategory').trigger( 'focus' );
  119. return false;
  120. } );
  121. $('.categorychecklist :checkbox').on( 'change', syncChecks ).filter( ':checked' ).trigger( 'change' );
  122. });