]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/js/widgets.js
Wordpress 2.7.1
[autoinstalls/wordpress.git] / wp-admin / js / widgets.js
1 jQuery(function($) {
2         $('.noscript-action').remove();
3
4         var reminded = false;
5         var lameWidgetReminder = function() {
6                 if ( reminded )
7                         return;
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;
12                 } );
13                 reminded = true;
14         };
15
16         var increment = 1;
17
18         // Open or close widget control form
19         var toggleWidget = function( li, disableFields ) {
20                 var width = li.find('input.widget-width').val();
21
22                 // it seems IE chokes on these animations because of the positioning/floating
23                 var widgetAnim = $.browser.msie ? function() {
24                         var t = $(this);
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 );
29                         } else {
30                                 t.find( ':disabled' ).attr( 'disabled', '' ); // always enable on open
31                                 if ( width > 250 )
32                                         li.css( 'marginLeft', ( width - 250 ) * -1 );
33                                 t.siblings('div').children('h4').children('a').text( widgetsL10n.cancel );
34                         }
35                         t.toggle();
36                 } : function() {
37                         var t = $(this);
38
39                         if ( t.is(':visible') ) {
40                                 if ( disableFields ) { t.find( ':input:enabled' ).not( '[name="widget-id[]"], [name*="[submit]"]' ).attr( 'disabled', 'disabled' ); }
41                                 if ( width > 250 )
42                                         li.animate( { marginLeft: 0 } );
43                                 t.siblings('div').children('h4').children('a').text( widgetsL10n.edit );
44                         } else {
45                                 t.find( ':disabled' ).attr( 'disabled', '' ); // always enable on open
46                                 if ( width > 250 )
47                                         li.animate( { marginLeft: ( width - 250 ) * -1 } );
48                                 t.siblings('div').children('h4').children('a').text( widgetsL10n.cancel );
49                         }
50                         t.animate( { height: 'toggle' } );
51                 };
52
53                 return li.children('div.widget-control').each( widgetAnim ).end();
54         };
55
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();
63                         return false;
64                 } else if ( q.sidebar ) { // otherwise, redirect to correct page
65                         return true;
66                 }
67
68                 // If link is in current widgets list, just open the form
69                 toggleWidget( $(this).parents('li:first'), true ).blur();
70                 return false;
71         };
72
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();
77
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();
81                         increment++;
82                         newLi.html( newLi.html().replace( /%i%/g, i ) );
83                 } else {
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>' );
87                 }
88
89                 // add event handlers
90                 addWidgetControls( newLi );
91
92                 // add widget to sidebar sortable
93                 widgetSortable.append( newLi ).SortableAddItem( newLi[0] );
94
95                 // increment widget counter
96                 var n = parseInt( $('#widget-count').text(), 10 ) + 1;
97                 $('#widget-count').text( n.toString() )
98
99                 lameWidgetReminder();
100                 return false;
101         };
102
103         // add event handlers to all links found in context
104         var addWidgetControls = function( context ) {
105                 if ( !context )
106                         context = document;
107
108                 $('a.widget-control-edit', context).click( editClick );
109
110                 // onclick for save links
111                 $('a.widget-control-save', context).click( function() {
112                         lameWidgetReminder();
113                         toggleWidget( $(this).parents('li:first'), false ).blur()
114                         return false;
115                 } );
116
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() )
126                         return false;
127                 } );
128         }
129
130         addWidgetControls();
131
132         $('a.widget-control-add').click( addClick );
133
134         var widgetSortable;
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();
138                 } catch(e) {}
139                 widgetSortable = $('#current-sidebar .widget-control-list').Sortable( {
140                         accept: 'widget-sortable',
141                         helperclass: 'sorthelper',
142                         handle: 'h4.widget-title',
143                         onStop: widgetSortableInit
144                 } );
145         }
146
147         // initialize sortable
148         widgetSortableInit();
149
150 });