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