]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/js/widgets.js
Wordpress 2.5.1
[autoinstalls/wordpress.git] / wp-admin / js / widgets.js
1 jQuery(function($) {
2         $('.noscript-action').remove();
3
4         var increment = 1;
5
6         // Open or close widget control form
7         var toggleWidget = function( li, disableFields ) {
8                 var width = li.find('input.widget-width').val();
9
10                 // it seems IE chokes on these animations because of the positioning/floating
11                 var widgetAnim = $.browser.msie ? function() {
12                         var t = $(this);
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 );
17                         } else {
18                                 t.find( ':disabled' ).attr( 'disabled', '' ); // always enable on open
19                                 if ( width > 250 )
20                                         li.css( 'marginLeft', ( width - 250 ) * -1 );
21                                 t.siblings('h4').children('a').text( widgetsL10n.cancel );
22                         }
23                         t.toggle();
24                 } : function() {
25                         var t = $(this);
26
27                         if ( t.is(':visible') ) {
28                                 if ( disableFields ) { t.find( ':input:enabled' ).not( '[name="widget-id[]"], [name*="[submit]"]' ).attr( 'disabled', 'disabled' ); }
29                                 if ( width > 250 )
30                                         li.animate( { marginLeft: 0 } );
31                                 t.siblings('h4').children('a').text( widgetsL10n.edit );
32                         } else {
33                                 t.find( ':disabled' ).attr( 'disabled', '' ); // always enable on open
34                                 if ( width > 250 )
35                                         li.animate( { marginLeft: ( width - 250 ) * -1 } );
36                                 t.siblings('h4').children('a').text( widgetsL10n.cancel );
37                         }
38                         t.animate( { height: 'toggle' } );
39                 };
40
41                 return li.children('div.widget-control').each( widgetAnim ).end();
42         };
43
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();
51                         return false;
52                 } else if ( q.sidebar ) { // otherwise, redirect to correct page
53                         return true;
54                 }
55
56                 // If link is in current widgets list, just open the form
57                 toggleWidget( $(this).parents('li:first'), true ).blur();
58                 return false;
59         };
60
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();
65
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();
69                         increment++;
70                         newLi.html( newLi.html().replace( /%i%/g, i ) );
71                 } else {
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>' );
75                 }
76
77                 // add event handlers
78                 addWidgetControls( newLi );
79
80                 // add widget to sidebar sortable
81                 widgetSortable.append( newLi ).SortableAddItem( newLi[0] );
82
83                 // increment widget counter
84                 var n = parseInt( $('#widget-count').text(), 10 ) + 1;
85                 $('#widget-count').text( n.toString() )
86
87                 return false;
88         };
89
90         // add event handlers to all links found in context
91         var addWidgetControls = function( context ) {
92                 if ( !context )
93                         context = document;
94
95                 $('a.widget-control-edit', context).click( editClick );
96
97                 // onclick for save links
98                 $('a.widget-control-save', context).click( function() {
99                         toggleWidget( $(this).parents('li:first'), false ).blur()
100                         return false;
101                 } );
102
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() )
112                         return false;
113                 } );
114         }
115
116         addWidgetControls();
117
118         $('a.widget-control-add').click( addClick );
119
120         var widgetSortable;
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();
124                 } catch(e) {}
125                 widgetSortable = $('#current-sidebar .widget-control-list').Sortable( {
126                         accept: 'widget-sortable',
127                         helperclass: 'sorthelper',
128                         handle: 'h4.widget-title',
129                         onStop: widgetSortableInit
130                 } );
131         }
132
133         // initialize sortable
134         widgetSortableInit();
135
136 });