mp-wp_genesis 1 jQuery(function($) {
mp-wp_genesis 2 $('.noscript-action').remove();
mp-wp_genesis 3
mp-wp_genesis 4 var reminded = false;
mp-wp_genesis 5 var lameWidgetReminder = function() {
mp-wp_genesis 6 if ( reminded )
mp-wp_genesis 7 return;
mp-wp_genesis 8 window.onbeforeunload = function () { return widgetsL10n.lamerReminder };
mp-wp_genesis 9 $('h2:first').after( '<div class="updated"><p>' + widgetsL10n.lameReminder + '</p></div>' );
mp-wp_genesis 10 $('#current-widgets .submit input[name=save-widgets]').css( 'background-color', '#ffffe0' ).click( function() {
mp-wp_genesis 11 window.onbeforeunload = null;
mp-wp_genesis 12 } );
mp-wp_genesis 13 reminded = true;
mp-wp_genesis 14 };
mp-wp_genesis 15
mp-wp_genesis 16 var increment = 1;
mp-wp_genesis 17
mp-wp_genesis 18 // Open or close widget control form
mp-wp_genesis 19 var toggleWidget = function( li, disableFields ) {
mp-wp_genesis 20 var width = li.find('input.widget-width').val();
mp-wp_genesis 21
mp-wp_genesis 22 // it seems IE chokes on these animations because of the positioning/floating
mp-wp_genesis 23 var widgetAnim = $.browser.msie ? function() {
mp-wp_genesis 24 var t = $(this);
mp-wp_genesis 25 if ( t.is(':visible') ) {
mp-wp_genesis 26 if ( disableFields ) { t.find( ':input:enabled' ).not( '[name="widget-id[]"], [name*="[submit]"]' ).attr( 'disabled', 'disabled' ); }
mp-wp_genesis 27 li.css( 'marginLeft', 0 );
mp-wp_genesis 28 t.siblings('div').children('h4').children('a').text( widgetsL10n.edit );
mp-wp_genesis 29 } else {
mp-wp_genesis 30 t.find( ':disabled' ).attr( 'disabled', '' ); // always enable on open
mp-wp_genesis 31 if ( width > 250 )
mp-wp_genesis 32 li.css( 'marginLeft', ( width - 250 ) * -1 );
mp-wp_genesis 33 t.siblings('div').children('h4').children('a').text( widgetsL10n.cancel );
mp-wp_genesis 34 }
mp-wp_genesis 35 t.toggle();
mp-wp_genesis 36 } : function() {
mp-wp_genesis 37 var t = $(this);
mp-wp_genesis 38
mp-wp_genesis 39 if ( t.is(':visible') ) {
mp-wp_genesis 40 if ( disableFields ) { t.find( ':input:enabled' ).not( '[name="widget-id[]"], [name*="[submit]"]' ).attr( 'disabled', 'disabled' ); }
mp-wp_genesis 41 if ( width > 250 )
mp-wp_genesis 42 li.animate( { marginLeft: 0 } );
mp-wp_genesis 43 t.siblings('div').children('h4').children('a').text( widgetsL10n.edit );
mp-wp_genesis 44 } else {
mp-wp_genesis 45 t.find( ':disabled' ).attr( 'disabled', '' ); // always enable on open
mp-wp_genesis 46 if ( width > 250 )
mp-wp_genesis 47 li.animate( { marginLeft: ( width - 250 ) * -1 } );
mp-wp_genesis 48 t.siblings('div').children('h4').children('a').text( widgetsL10n.cancel );
mp-wp_genesis 49 }
mp-wp_genesis 50 t.animate( { height: 'toggle' } );
mp-wp_genesis 51 };
mp-wp_genesis 52
mp-wp_genesis 53 return li.children('div.widget-control').each( widgetAnim ).end();
mp-wp_genesis 54 };
mp-wp_genesis 55
mp-wp_genesis 56 // onclick for edit/cancel links
mp-wp_genesis 57 var editClick = function() {
mp-wp_genesis 58 var q = wpAjax.unserialize( this.href );
mp-wp_genesis 59 // if link is in available widgets list, make sure it points to the current sidebar
mp-wp_genesis 60 if ( ( q.sidebar && q.sidebar == $('#sidebar').val() ) || q.add ) {
mp-wp_genesis 61 var w = q.edit || q.add;
mp-wp_genesis 62 toggleWidget( $('#current-sidebar .widget-control-list input[@name^="widget-id"][@value=' + w + ']').parents('li:first'), false ).blur();
mp-wp_genesis 63 return false;
mp-wp_genesis 64 } else if ( q.sidebar ) { // otherwise, redirect to correct page
mp-wp_genesis 65 return true;
mp-wp_genesis 66 }
mp-wp_genesis 67
mp-wp_genesis 68 // If link is in current widgets list, just open the form
mp-wp_genesis 69 toggleWidget( $(this).parents('li:first'), true ).blur();
mp-wp_genesis 70 return false;
mp-wp_genesis 71 };
mp-wp_genesis 72
mp-wp_genesis 73 // onclick for add links
mp-wp_genesis 74 var addClick = function() {
mp-wp_genesis 75 var oldLi = $(this).parents('li:first').find('ul.widget-control-info li');
mp-wp_genesis 76 var newLi = oldLi.clone();
mp-wp_genesis 77
mp-wp_genesis 78 if ( newLi.html().match( /%i%/ ) ) {
mp-wp_genesis 79 // supplid form is a template, replace %i% by unique id
mp-wp_genesis 80 var i = $('#generated-time').val() + increment.toString();
mp-wp_genesis 81 increment++;
mp-wp_genesis 82 newLi.html( newLi.html().replace( /%i%/g, i ) );
mp-wp_genesis 83 } else {
mp-wp_genesis 84 $(this).text( widgetsL10n.edit ).unbind().click( editClick );
mp-wp_genesis 85 // save form content in textarea so we don't have any conflicting HTML ids
mp-wp_genesis 86 oldLi.html( '<textarea>' + oldLi.html() + '</textarea>' );
mp-wp_genesis 87 }
mp-wp_genesis 88
mp-wp_genesis 89 // add event handlers
mp-wp_genesis 90 addWidgetControls( newLi );
mp-wp_genesis 91
mp-wp_genesis 92 // add widget to sidebar sortable
mp-wp_genesis 93 widgetSortable.append( newLi ).SortableAddItem( newLi[0] );
mp-wp_genesis 94
mp-wp_genesis 95 // increment widget counter
mp-wp_genesis 96 var n = parseInt( $('#widget-count').text(), 10 ) + 1;
mp-wp_genesis 97 $('#widget-count').text( n.toString() )
mp-wp_genesis 98
mp-wp_genesis 99 lameWidgetReminder();
mp-wp_genesis 100 return false;
mp-wp_genesis 101 };
mp-wp_genesis 102
mp-wp_genesis 103 // add event handlers to all links found in context
mp-wp_genesis 104 var addWidgetControls = function( context ) {
mp-wp_genesis 105 if ( !context )
mp-wp_genesis 106 context = document;
mp-wp_genesis 107
mp-wp_genesis 108 $('a.widget-control-edit', context).click( editClick );
mp-wp_genesis 109
mp-wp_genesis 110 // onclick for save links
mp-wp_genesis 111 $('a.widget-control-save', context).click( function() {
mp-wp_genesis 112 lameWidgetReminder();
mp-wp_genesis 113 toggleWidget( $(this).parents('li:first'), false ).blur()
mp-wp_genesis 114 return false;
mp-wp_genesis 115 } );
mp-wp_genesis 116
mp-wp_genesis 117 // onclick for remove links
mp-wp_genesis 118 $('a.widget-control-remove', context).click( function() {
mp-wp_genesis 119 var w = $(this).parents('li:first').find('input[@name^="widget-id"]').val();
mp-wp_genesis 120 $(this).parents('li:first').remove();
mp-wp_genesis 121 var t = $('#widget-list ul#widget-control-info-' + w + ' textarea');
mp-wp_genesis 122 t.parent().html( t.text() ).parents('li.widget-list-item:first').children( 'h4' ).children('a.widget-action')
mp-wp_genesis 123 .show().text( widgetsL10n.add ).unbind().click( addClick );
mp-wp_genesis 124 var n = parseInt( $('#widget-count').text(), 10 ) - 1;
mp-wp_genesis 125 $('#widget-count').text( n.toString() )
mp-wp_genesis 126 return false;
mp-wp_genesis 127 } );
mp-wp_genesis 128 }
mp-wp_genesis 129
mp-wp_genesis 130 addWidgetControls();
mp-wp_genesis 131
mp-wp_genesis 132 $('a.widget-control-add').click( addClick );
mp-wp_genesis 133
mp-wp_genesis 134 var widgetSortable;
mp-wp_genesis 135 var widgetSortableInit = function() {
mp-wp_genesis 136 try { // a hack to make sortables work in jQuery 1.2+ and IE7
mp-wp_genesis 137 $('#current-sidebar .widget-control-list').SortableDestroy();
mp-wp_genesis 138 } catch(e) {}
mp-wp_genesis 139 widgetSortable = $('#current-sidebar .widget-control-list').Sortable( {
mp-wp_genesis 140 accept: 'widget-sortable',
mp-wp_genesis 141 helperclass: 'sorthelper',
mp-wp_genesis 142 handle: 'h4.widget-title',
mp-wp_genesis 143 onStop: widgetSortableInit
mp-wp_genesis 144 } );
mp-wp_genesis 145 }
mp-wp_genesis 146
mp-wp_genesis 147 // initialize sortable
mp-wp_genesis 148 widgetSortableInit();
mp-wp_genesis 149
mp-wp_genesis 150 });