2 $('.noscript-action').remove();
6 // Open or close widget control form
7 var toggleWidget = function( li, disableFields ) {
8 var width = li.find('input.widget-width').val();
10 // it seems IE chokes on these animations because of the positioning/floating
11 var widgetAnim = $.browser.msie ? function() {
13 if ( t.is(':visible') ) {
14 if ( disableFields ) { t.find( ':input:enabled' ).not( '[name="widget-id[]"], [name*="[submit]"]' ).attr( 'disabled', 'disabled' ); }
15 li.css( 'marginLeft', 0 );
16 t.siblings('h4').children('a').text( widgetsL10n.edit );
18 t.find( ':disabled' ).attr( 'disabled', '' ); // always enable on open
20 li.css( 'marginLeft', ( width - 250 ) * -1 );
21 t.siblings('h4').children('a').text( widgetsL10n.cancel );
27 if ( t.is(':visible') ) {
28 if ( disableFields ) { t.find( ':input:enabled' ).not( '[name="widget-id[]"], [name*="[submit]"]' ).attr( 'disabled', 'disabled' ); }
30 li.animate( { marginLeft: 0 } );
31 t.siblings('h4').children('a').text( widgetsL10n.edit );
33 t.find( ':disabled' ).attr( 'disabled', '' ); // always enable on open
35 li.animate( { marginLeft: ( width - 250 ) * -1 } );
36 t.siblings('h4').children('a').text( widgetsL10n.cancel );
38 t.animate( { height: 'toggle' } );
41 return li.children('div.widget-control').each( widgetAnim ).end();
44 // onclick for edit/cancel links
45 var editClick = function() {
46 var q = wpAjax.unserialize( this.href );
47 // if link is in available widgets list, make sure it points to the current sidebar
48 if ( ( q.sidebar && q.sidebar == $('#sidebar').val() ) || q.add ) {
49 var w = q.edit || q.add;
50 toggleWidget( $('#current-sidebar .widget-control-list input[@name^="widget-id"][@value=' + w + ']').parents('li:first'), false ).blur();
52 } else if ( q.sidebar ) { // otherwise, redirect to correct page
56 // If link is in current widgets list, just open the form
57 toggleWidget( $(this).parents('li:first'), true ).blur();
61 // onclick for add links
62 var addClick = function() {
63 var oldLi = $(this).parents('li:first').find('ul.widget-control-info li');
64 var newLi = oldLi.clone();
66 if ( newLi.html().match( /%i%/ ) ) {
67 // supplid form is a template, replace %i% by unique id
68 var i = $('#generated-time').val() + increment.toString();
70 newLi.html( newLi.html().replace( /%i%/g, i ) );
72 $(this).text( widgetsL10n.edit ).unbind().click( editClick );
73 // save form content in textarea so we don't have any conflicting HTML ids
74 oldLi.html( '<textarea>' + oldLi.html() + '</textarea>' );
78 addWidgetControls( newLi );
80 // add widget to sidebar sortable
81 widgetSortable.append( newLi ).SortableAddItem( newLi[0] );
83 // increment widget counter
84 var n = parseInt( $('#widget-count').text(), 10 ) + 1;
85 $('#widget-count').text( n.toString() )
90 // add event handlers to all links found in context
91 var addWidgetControls = function( context ) {
95 $('a.widget-control-edit', context).click( editClick );
97 // onclick for save links
98 $('a.widget-control-save', context).click( function() {
99 toggleWidget( $(this).parents('li:first'), false ).blur()
103 // onclick for remove links
104 $('a.widget-control-remove', context).click( function() {
105 var w = $(this).parents('li:first').find('input[@name^="widget-id"]').val();
106 $(this).parents('li:first').remove();
107 var t = $('#widget-list ul#widget-control-info-' + w + ' textarea');
108 t.parent().html( t.text() ).parents('li.widget-list-item:first').children( 'h4' ).children('a.widget-action')
109 .show().text( widgetsL10n.add ).unbind().click( addClick );
110 var n = parseInt( $('#widget-count').text(), 10 ) - 1;
111 $('#widget-count').text( n.toString() )
118 $('a.widget-control-add').click( addClick );
121 var widgetSortableInit = function() {
122 try { // a hack to make sortables work in jQuery 1.2+ and IE7
123 $('#current-sidebar .widget-control-list').SortableDestroy();
125 widgetSortable = $('#current-sidebar .widget-control-list').Sortable( {
126 accept: 'widget-sortable',
127 helperclass: 'sorthelper',
128 handle: 'h4.widget-title',
129 onStop: widgetSortableInit
133 // initialize sortable
134 widgetSortableInit();