1/**
2 * @output wp-admin/js/link.js
3 */
4
5/* global postboxes, deleteUserSetting, setUserSetting, getUserSetting */
6
7jQuery( function($) {
8
9 var newCat, noSyncChecks = false, syncChecks, catAddAfter;
10
11 $('#link_name').trigger( 'focus' );
12 // Postboxes.
13 postboxes.add_postbox_toggles('link');
14
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 */
22 $('#category-tabs a').on( 'click', function(){
23 var t = $(this).attr('href');
24 $(this).parent().addClass('tabs').siblings('li').removeClass('tabs');
25 $('.tabs-panel').hide();
26 $(t).show();
27 if ( '#categories-all' == t )
28 deleteUserSetting('cats');
29 else
30 setUserSetting('cats','pop');
31 return false;
32 });
33 if ( getUserSetting('cats') )
34 $('#category-tabs a[href="#categories-pop"]').trigger( 'click' );
35
36 // Ajax Cat.
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 */
44 $('#link-category-add-submit').on( '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 */
56 syncChecks = function() {
57 if ( noSyncChecks )
58 return;
59 noSyncChecks = true;
60 var th = $(this), c = th.is(':checked'), id = th.val().toString();
61 $('#in-link-category-' + id + ', #in-popular-link_category-' + id).prop( 'checked', c );
62 noSyncChecks = false;
63 };
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 */
80 catAddAfter = function( r, s ) {
81 $(s.what + ' response_data', r).each( function() {
82 var t = $($(this).text());
83 t.find( 'label' ).each( function() {
84 var th = $(this),
85 val = th.find('input').val(),
86 id = th.find('input')[0].id,
87 name = th.text().trim(),
88 o;
89 $('#' + id).on( 'change', syncChecks );
90 o = $( '<option value="' + parseInt( val, 10 ) + '"></option>' ).text( name );
91 } );
92 } );
93 };
94
95 /*
96 * Instantiates the list manager.
97 *
98 * @see js/_enqueues/lib/lists.js
99 */
100 $('#categorychecklist').wpList( {
101 // CSS class name for alternate styling.
102 alt: '',
103
104 // The type of list.
105 what: 'link-category',
106
107 // ID of the element the parsed Ajax response will be stored in.
108 response: 'category-ajax-response',
109
110 // Callback that's run after an item got added to the list.
111 addAfter: catAddAfter
112 } );
113
114 // All categories is the default tab, so we delete the user setting.
115 $('a[href="#categories-all"]').on( 'click', function(){deleteUserSetting('cats');});
116
117 // Set a preference for the popular categories to cookies.
118 $('a[href="#categories-pop"]').on( 'click', function(){setUserSetting('cats','pop');});
119
120 if ( 'pop' == getUserSetting('cats') )
121 $('a[href="#categories-pop"]').trigger( 'click' );
122
123 /**
124 * Adds event handler that shows the interface controls to add a new category.
125 *
126 * @ignore
127 *
128 * @param {Event} event The event object.
129 * @return {boolean} Always returns false to prevent regular link
130 * functionality.
131 */
132 $('#category-add-toggle').on( 'click', function() {
133 $(this).parents('div:first').toggleClass( 'wp-hidden-children' );
134 $('#category-tabs a[href="#categories-all"]').trigger( 'click' );
135 $('#newcategory').trigger( 'focus' );
136 return false;
137 } );
138
139 $('.categorychecklist :checkbox').on( 'change', syncChecks ).filter( ':checked' ).trigger( 'change' );
140});
141