]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/js/widgets.dev.js
Wordpress 2.8-scripts
[autoinstalls/wordpress.git] / wp-admin / js / widgets.dev.js
1
2 var wpWidgets;
3 (function($) {
4
5 wpWidgets = {
6         init : function() {
7         var rem;
8
9                 if ( $('body').hasClass('widgets_access') ) {
10                         return;
11                 }
12
13                 $('#widgets-right div.sidebar-name').click(function(){
14             var c = $(this).siblings('.widgets-sortables');
15                         if ( c.is(':visible') ) {
16                                 c.sortable('disable');
17                                 $(this).parent().addClass('closed');
18                         } else {
19                                 $(this).parent().removeClass('closed');
20                                 c.sortable('enable').sortable('refresh');
21                         }
22         });
23
24         $('#widgets-left div.sidebar-name').click(function(){
25                         if ( $(this).siblings('.widget-holder').is(':visible') ) {
26                                 $(this).parent().addClass('closed');
27                         } else {
28                                 $(this).parent().removeClass('closed');
29                         }
30         });
31
32                 $('#widgets-right .widget, #wp_inactive_widgets .widget').each(function(){
33                         wpWidgets.appendTitle(this);
34                 });
35
36                 this.addEvents();
37         $('.widget-error').parents('.widget').find('a.widget-action').click();
38
39                 $('#available-widgets').droppable({
40                         tolerance: 'pointer',
41                         accept: function(o){
42                                 return $(o).parent().attr('id') != 'widget-list';
43                         },
44                         drop: function(e,ui) {
45                                 ui.draggable.addClass('deleting');
46                                 $('#removing-widget').hide().children('span').html('');
47                         },
48                         over: function(e,ui) {
49                                 ui.draggable.addClass('deleting');
50                                 $('.widget-placeholder').hide();
51
52                                 if ( ui.draggable.hasClass('ui-sortable-helper') )
53                                         $('#removing-widget').show().children('span').html( ui.draggable.find('.widget-title h4').html() );
54                         },
55                         out: function(e,ui) {
56                                 ui.draggable.removeClass('deleting');
57                                 $('.widget-placeholder').show();
58                                 $('#removing-widget').hide().children('span').html('');
59                         }
60                 });
61
62                 $('#widget-list .widget').draggable({
63                         connectToSortable: '.widgets-sortables',
64                         handle: '.widget-title',
65                         distance: 2,
66                         helper: 'clone',
67                         zIndex: 5,
68                         containment: 'document',
69                         start: function(e,ui) {
70                                 wpWidgets.fixWebkit(1);
71                                 ui.helper.find('.widget-description').hide();
72                         },
73                         stop: function(e,ui) {
74                                 if ( rem )
75                                         $(rem).hide();
76                                 rem = '';
77                                 wpWidgets.fixWebkit();
78                         }
79                 });
80
81         $('.widgets-sortables').sortable({
82                         placeholder: 'widget-placeholder',
83                         connectWith: '.widgets-sortables',
84                         items: '.widget',
85                         handle: '.widget-title',
86                         cursor: 'move',
87                         distance: 2,
88                         containment: 'document',
89                         start: function(e,ui) {
90                                 wpWidgets.fixWebkit(1);
91                                 ui.item.find('.widget-inside').hide();
92                                 ui.item.css({'marginLeft':'','width':''});
93                         },
94                         stop: function(e,ui) {
95                                 if ( ui.item.hasClass('ui-draggable') )
96                                         ui.item.draggable('destroy');
97
98                                 if ( ui.item.hasClass('deleting') ) {
99                                         wpWidgets.save( ui.item, 1, 0, 1 ); // delete widget
100                                         ui.item.remove();
101                                         return;
102                                 }
103
104                                 var add = ui.item.find('input.add_new').val(), n = ui.item.find('input.multi_number').val(), id = ui.item.attr('id'), sb = $(this).attr('id');
105
106                                 ui.item.css({'marginLeft':'','width':''});
107                                 wpWidgets.fixWebkit();
108                                 if ( add ) {
109                                         if ( 'multi' == add ) {
110                                                 ui.item.html( ui.item.html().replace(/<[^<>]+>/g, function(m){ return m.replace(/__i__|%i%/g, n); }) );
111                                                 ui.item.attr( 'id', id.replace(/__i__|%i%/g, n) );
112                                                 n++;
113                                                 $('div#' + id).find('input.multi_number').val(n);
114                                         } else if ( 'single' == add ) {
115                                                 ui.item.attr( 'id', 'new-' + id );
116                                                 rem = 'div#' + id;
117                                         }
118                                         wpWidgets.addEvents(ui.item);
119                                         wpWidgets.save( ui.item, 0, 0, 1 );
120                                         ui.item.find('input.add_new').val('');
121                                         ui.item.find('a.widget-action').click();
122                                         return;
123                                 }
124                                 wpWidgets.saveOrder(sb);
125                         },
126                         receive: function(e,ui) {
127                                 if ( !$(this).is(':visible') )
128                                         $(this).sortable('cancel');
129                         }
130                 }).not(':visible').sortable('disable');
131                 wpWidgets.resize();
132                 wpWidgets.fixLabels();
133         },
134
135         saveOrder : function(sb) {
136                 if ( sb )
137                         $('#' + sb).parents('.widgets-holder-wrap').find('.ajax-feedback').css('visibility', 'visible');
138
139                 var a = {
140                         action: 'widgets-order',
141                         savewidgets: $('#_wpnonce_widgets').val(),
142                         sidebars: []
143                 };
144
145                 $('.widgets-sortables').each( function() {
146                         a['sidebars[' + $(this).attr('id') + ']'] = $(this).sortable('toArray').join(',');
147                 });
148
149                 $.post( ajaxurl, a, function() {
150                         $('.ajax-feedback').css('visibility', 'hidden');
151                 });
152                 this.resize();
153         },
154
155         save : function(widget, del, animate, order) {
156                 var sb = widget.parents('.widgets-sortables').attr('id'), data = widget.find('form').serialize(), a;
157                 widget = $(widget);
158                 widget.find('.ajax-feedback').css('visibility', 'visible');
159
160                 a = {
161                         action: 'save-widget',
162                         savewidgets: $('#_wpnonce_widgets').val(),
163                         sidebar: sb
164                 };
165
166                 if ( del )
167                         a['delete_widget'] = 1;
168
169                 data += '&' + $.param(a);
170
171                 $.post( ajaxurl, data, function(r){
172                         var id;
173
174                         if ( del ) {
175                                 if ( !$('.widget_number', widget).val() ) {
176                                         id = $('.widget-id', widget).val();
177                                         $('#available-widgets .widget-id').each(function(){
178                                                 if ( $(this).val() == id )
179                                                         $(this).parents('.widget').show();
180                                         });
181                                 }
182
183                                 if ( animate ) {
184                                         order = 0;
185                                         widget.slideUp('fast', function(){
186                                                 $(this).remove();
187                                                 wpWidgets.saveOrder();
188                                         });
189                                 } else {
190                                         widget.remove();
191                                         wpWidgets.resize();
192                                 }
193                         } else {
194                                 $('.ajax-feedback').css('visibility', 'hidden');
195                                 if ( r && r.length > 2 ) {
196                                         $('.widget-content', widget).html(r);
197                                         wpWidgets.appendTitle(widget);
198                                         wpWidgets.fixLabels(widget);
199                                 }
200                         }
201                         if ( order )
202                                 wpWidgets.saveOrder();
203                 });
204         },
205
206         appendTitle : function(widget) {
207                 $('input[type="text"]', widget).each(function(){
208                         var title;
209                         if ( this.id.indexOf('title') != -1 ) {
210                                 title = $(this).val().replace(/<[^<>]+>/g, '').replace(/</g, '&lt;').replace(/>/g, '&gt;');
211                                 if ( title )
212                                         $('.widget-title .in-widget-title', widget).html(': ' + title);
213                                 return false;
214                         }
215                 });
216         },
217
218         resize : function() {
219                 $('.widgets-sortables').not('#wp_inactive_widgets').each(function(){
220                         var h = 50, H = $('.widget', this).length;
221                         h = h + parseInt(H * 48, 10);
222                         $(this).css( 'minHeight', h + 'px' );
223                 });
224         },
225
226     fixWebkit : function(n) {
227         n = n ? 'none' : '';
228         $('body').css({
229                         WebkitUserSelect: n,
230                         KhtmlUserSelect: n
231                 });
232     },
233     
234     fixLabels : function(sc) {
235                 sc = sc || document;
236
237                 $('.widget-inside label', sc).each(function(){
238                         var f = $(this).attr('for');
239
240                         if ( f && f == $('input', this).attr('id') )
241                                 $(this).removeAttr('for');
242                 });
243         },
244     
245     close : function(widget) {
246                 widget.find('.widget-inside').slideUp('fast', function(){
247                         widget.css({'width':'','marginLeft':''});
248                 });
249         },
250
251     addEvents : function(sc) {
252                 sc = sc || document;
253                 $('a.widget-action', sc).click(function(){
254             var w = parseInt( $(this).parents('.widget').find('.widget-width').val(), 10 ), css = {}, inside = $(this).parents('.widget-top').siblings('.widget-inside');
255                         if ( inside.is(':hidden') ) {
256                                 if ( w > 250 && inside.parents('.widgets-sortables').length ) {
257                                         css['width'] = w + 30 + 'px';
258                                         if ( inside.parents('.widget-liquid-right').length )
259                                                 css['marginLeft'] = 235 - w + 'px';
260                                         inside.parents('.widget').css(css);
261                                 }
262                                 inside.slideDown('fast');
263                         } else {
264                                 inside.slideUp('fast', function(){ inside.parents('.widget').css({'width':'','marginLeft':''}); });
265                         }
266             return false;
267         });
268         $('.widget-control-save', sc).click(function(){
269                         wpWidgets.save( $(this).parents('.widget'), 0, 1, 0 );
270                         return false;
271                 });
272                 $('.widget-control-remove', sc).click(function(){
273                         wpWidgets.save( $(this).parents('.widget'), 1, 1, 0 );
274                         return false;
275                 });
276                 $('.widget-control-close', sc).click(function(){
277                         wpWidgets.close( $(this).parents('.widget') );
278                         return false;
279                 });
280         }
281 };
282 $(document).ready(function(){wpWidgets.init();});
283
284 })(jQuery);