link.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /**
  2. * @output wp-admin/js/link.js
  3. */
  4. /* global postboxes, deleteUserSetting, setUserSetting, getUserSetting */
  5. jQuery(document).ready( function($) {
  6. var newCat, noSyncChecks = false, syncChecks, catAddAfter;
  7. $('#link_name').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').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"]').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').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), val = th.find('input').val(), id = th.find('input')[0].id, name = $.trim( th.text() ), o;
  76. $('#' + id).change( syncChecks );
  77. o = $( '<option value="' + parseInt( val, 10 ) + '"></option>' ).text( name );
  78. } );
  79. } );
  80. };
  81. /*
  82. * Instantiates the list manager.
  83. *
  84. * @see js/_enqueues/lib/lists.js
  85. */
  86. $('#categorychecklist').wpList( {
  87. // CSS class name for alternate styling.
  88. alt: '',
  89. // The type of list.
  90. what: 'link-category',
  91. // ID of the element the parsed Ajax response will be stored in.
  92. response: 'category-ajax-response',
  93. // Callback that's run after an item got added to the list.
  94. addAfter: catAddAfter
  95. } );
  96. // All categories is the default tab, so we delete the user setting.
  97. $('a[href="#categories-all"]').click(function(){deleteUserSetting('cats');});
  98. // Set a preference for the popular categories to cookies.
  99. $('a[href="#categories-pop"]').click(function(){setUserSetting('cats','pop');});
  100. if ( 'pop' == getUserSetting('cats') )
  101. $('a[href="#categories-pop"]').click();
  102. /**
  103. * Adds event handler that shows the interface controls to add a new category.
  104. *
  105. * @ignore
  106. *
  107. * @param {Event} event The event object.
  108. * @returns {boolean} Always returns false to prevent regular link
  109. * functionality.
  110. */
  111. $('#category-add-toggle').click( function() {
  112. $(this).parents('div:first').toggleClass( 'wp-hidden-children' );
  113. $('#category-tabs a[href="#categories-all"]').click();
  114. $('#newcategory').focus();
  115. return false;
  116. } );
  117. $('.categorychecklist :checkbox').change( syncChecks ).filter( ':checked' ).change();
  118. });