2 $('.noscript-action').remove();
5 var lameWidgetReminder = function() {
8 window.onbeforeunload = function () { return widgetsL10n.lamerReminder };
9 $('h2:first').after( '<div class="updated"><p>' + widgetsL10n.lameReminder + '</p></div>' );
10 $('#current-widgets .submit input[name=save-widgets]').css( 'background-color', '#ffffe0' ).click( function() {
11 window.onbeforeunload = null;
18 // Open or close widget control form
19 var toggleWidget = function( li, disableFields ) {
20 var width = li.find('input.widget-width').val();
22 // it seems IE chokes on these animations because of the positioning/floating
23 var widgetAnim = $.browser.msie ? function() {
25 if ( t.is(':visible') ) {
26 if ( disableFields ) { t.find( ':input:enabled' ).not( '[name="widget-id[]"], [name*="[submit]"]' ).attr( 'disabled', 'disabled' ); }
27 li.css( 'marginLeft', 0 );
28 t.siblings('div').children('h4').children('a').text( widgetsL10n.edit );
30 t.find( ':disabled' ).attr( 'disabled', '' ); // always enable on open
32 li.css( 'marginLeft', ( width - 250 ) * -1 );
33 t.siblings('div').children('h4').children('a').text( widgetsL10n.cancel );
39 if ( t.is(':visible') ) {
40 if ( disableFields ) { t.find( ':input:enabled' ).not( '[name="widget-id[]"], [name*="[submit]"]' ).attr( 'disabled', 'disabled' ); }
42 li.animate( { marginLeft: 0 } );
43 t.siblings('div').children('h4').children('a').text( widgetsL10n.edit );
45 t.find( ':disabled' ).attr( 'disabled', '' ); // always enable on open
47 li.animate( { marginLeft: ( width - 250 ) * -1 } );
48 t.siblings('div').children('h4').children('a').text( widgetsL10n.cancel );
50 t.animate( { height: 'toggle' } );
53 return li.children('div.widget-control').each( widgetAnim ).end();
56 // onclick for edit/cancel links
57 var editClick = function() {
58 var q = wpAjax.unserialize( this.href );
59 // if link is in available widgets list, make sure it points to the current sidebar
60 if ( ( q.sidebar && q.sidebar == $('#sidebar').val() ) || q.add ) {
61 var w = q.edit || q.add;
62 toggleWidget( $('#current-sidebar .widget-control-list input[@name^="widget-id"][@value=' + w + ']').parents('li:first'), false ).blur();
64 } else if ( q.sidebar ) { // otherwise, redirect to correct page
68 // If link is in current widgets list, just open the form
69 toggleWidget( $(this).parents('li:first'), true ).blur();
73 // onclick for add links
74 var addClick = function() {
75 var oldLi = $(this).parents('li:first').find('ul.widget-control-info li');
76 var newLi = oldLi.clone();
78 if ( newLi.html().match( /%i%/ ) ) {
79 // supplid form is a template, replace %i% by unique id
80 var i = $('#generated-time').val() + increment.toString();
82 newLi.html( newLi.html().replace( /%i%/g, i ) );
84 $(this).text( widgetsL10n.edit ).unbind().click( editClick );
85 // save form content in textarea so we don't have any conflicting HTML ids
86 oldLi.html( '<textarea>' + oldLi.html() + '</textarea>' );
90 addWidgetControls( newLi );
92 // add widget to sidebar sortable
93 widgetSortable.append( newLi ).SortableAddItem( newLi[0] );
95 // increment widget counter
96 var n = parseInt( $('#widget-count').text(), 10 ) + 1;
97 $('#widget-count').text( n.toString() )
103 // add event handlers to all links found in context
104 var addWidgetControls = function( context ) {
108 $('a.widget-control-edit', context).click( editClick );
110 // onclick for save links
111 $('a.widget-control-save', context).click( function() {
112 lameWidgetReminder();
113 toggleWidget( $(this).parents('li:first'), false ).blur()
117 // onclick for remove links
118 $('a.widget-control-remove', context).click( function() {
119 var w = $(this).parents('li:first').find('input[@name^="widget-id"]').val();
120 $(this).parents('li:first').remove();
121 var t = $('#widget-list ul#widget-control-info-' + w + ' textarea');
122 t.parent().html( t.text() ).parents('li.widget-list-item:first').children( 'h4' ).children('a.widget-action')
123 .show().text( widgetsL10n.add ).unbind().click( addClick );
124 var n = parseInt( $('#widget-count').text(), 10 ) - 1;
125 $('#widget-count').text( n.toString() )
132 $('a.widget-control-add').click( addClick );
135 var widgetSortableInit = function() {
136 try { // a hack to make sortables work in jQuery 1.2+ and IE7
137 $('#current-sidebar .widget-control-list').SortableDestroy();
139 widgetSortable = $('#current-sidebar .widget-control-list').Sortable( {
140 accept: 'widget-sortable',
141 helperclass: 'sorthelper',
142 handle: 'h4.widget-title',
143 onStop: widgetSortableInit
147 // initialize sortable
148 widgetSortableInit();