7 var rem, sidebars = $('div.widgets-sortables'), isRTL = !! ( 'undefined' != typeof isRtl && isRtl ),
8 margin = ( isRtl ? 'marginRight' : 'marginLeft' );
10 $('#widgets-right').children('.widgets-holder-wrap').children('.sidebar-name').click(function(){
11 var c = $(this).siblings('.widgets-sortables'), p = $(this).parent();
12 if ( !p.hasClass('closed') ) {
13 c.sortable('disable');
16 p.removeClass('closed');
17 c.sortable('enable').sortable('refresh');
21 $('#widgets-left').children('.widgets-holder-wrap').children('.sidebar-name').click(function() {
22 $(this).siblings('.widget-holder').parent().toggleClass('closed');
25 sidebars.not('#wp_inactive_widgets').each(function(){
26 var h = 50, H = $(this).children('.widget').length;
27 h = h + parseInt(H * 48, 10);
28 $(this).css( 'minHeight', h + 'px' );
31 $('a.widget-action').live('click', function(){
32 var css = {}, widget = $(this).closest('div.widget'), inside = widget.children('.widget-inside'), w = parseInt( widget.find('input.widget-width').val(), 10 );
34 if ( inside.is(':hidden') ) {
35 if ( w > 250 && inside.closest('div.widgets-sortables').length ) {
36 css['width'] = w + 30 + 'px';
37 if ( inside.closest('div.widget-liquid-right').length )
38 css[margin] = 235 - w + 'px';
41 wpWidgets.fixLabels(widget);
42 inside.slideDown('fast');
44 inside.slideUp('fast', function() {
45 widget.css({'width':'', margin:''});
51 $('input.widget-control-save').live('click', function(){
52 wpWidgets.save( $(this).closest('div.widget'), 0, 1, 0 );
56 $('a.widget-control-remove').live('click', function(){
57 wpWidgets.save( $(this).closest('div.widget'), 1, 1, 0 );
61 $('a.widget-control-close').live('click', function(){
62 wpWidgets.close( $(this).closest('div.widget') );
66 sidebars.children('.widget').each(function() {
67 wpWidgets.appendTitle(this);
68 if ( $('p.widget-error', this).length )
69 $('a.widget-action', this).click();
72 $('#widget-list').children('.widget').draggable({
73 connectToSortable: 'div.widgets-sortables',
74 handle: '> .widget-top > .widget-title',
78 containment: 'document',
79 start: function(e,ui) {
80 wpWidgets.fixWebkit(1);
81 ui.helper.find('div.widget-description').hide();
83 stop: function(e,ui) {
87 wpWidgets.fixWebkit();
92 placeholder: 'widget-placeholder',
94 handle: '> .widget-top > .widget-title',
97 containment: 'document',
98 start: function(e,ui) {
99 wpWidgets.fixWebkit(1);
100 ui.item.children('.widget-inside').hide();
101 ui.item.css({margin:'', 'width':''});
103 stop: function(e,ui) {
104 if ( ui.item.hasClass('ui-draggable') && ui.item.data('draggable') )
105 ui.item.draggable('destroy');
107 if ( ui.item.hasClass('deleting') ) {
108 wpWidgets.save( ui.item, 1, 0, 1 ); // delete widget
113 var add = ui.item.find('input.add_new').val(),
114 n = ui.item.find('input.multi_number').val(),
115 id = ui.item.attr('id'),
116 sb = $(this).attr('id');
118 ui.item.css({margin:'', 'width':''});
119 wpWidgets.fixWebkit();
121 if ( 'multi' == add ) {
122 ui.item.html( ui.item.html().replace(/<[^<>]+>/g, function(m){ return m.replace(/__i__|%i%/g, n); }) );
123 ui.item.attr( 'id', id.replace(/__i__|%i%/g, n) );
125 $('div#' + id).find('input.multi_number').val(n);
126 } else if ( 'single' == add ) {
127 ui.item.attr( 'id', 'new-' + id );
130 wpWidgets.save( ui.item, 0, 0, 1 );
131 ui.item.find('input.add_new').val('');
132 ui.item.find('a.widget-action').click();
135 wpWidgets.saveOrder(sb);
137 receive: function(e,ui) {
138 if ( !$(this).is(':visible') )
139 $(this).sortable('cancel');
141 }).sortable('option', 'connectWith', 'div.widgets-sortables').parent().filter('.closed').children('.widgets-sortables').sortable('disable');
143 $('#available-widgets').droppable({
144 tolerance: 'pointer',
146 return $(o).parent().attr('id') != 'widget-list';
148 drop: function(e,ui) {
149 ui.draggable.addClass('deleting');
150 $('#removing-widget').hide().children('span').html('');
152 over: function(e,ui) {
153 ui.draggable.addClass('deleting');
154 $('div.widget-placeholder').hide();
156 if ( ui.draggable.hasClass('ui-sortable-helper') )
157 $('#removing-widget').show().children('span')
158 .html( ui.draggable.find('div.widget-title').children('h4').html() );
160 out: function(e,ui) {
161 ui.draggable.removeClass('deleting');
162 $('div.widget-placeholder').show();
163 $('#removing-widget').hide().children('span').html('');
168 saveOrder : function(sb) {
170 $('#' + sb).closest('div.widgets-holder-wrap').find('img.ajax-feedback').css('visibility', 'visible');
173 action: 'widgets-order',
174 savewidgets: $('#_wpnonce_widgets').val(),
178 $('div.widgets-sortables').each( function() {
179 a['sidebars[' + $(this).attr('id') + ']'] = $(this).sortable('toArray').join(',');
182 $.post( ajaxurl, a, function() {
183 $('img.ajax-feedback').css('visibility', 'hidden');
189 save : function(widget, del, animate, order) {
190 var sb = widget.closest('div.widgets-sortables').attr('id'), data = widget.find('form').serialize(), a;
192 $('.ajax-feedback', widget).css('visibility', 'visible');
195 action: 'save-widget',
196 savewidgets: $('#_wpnonce_widgets').val(),
201 a['delete_widget'] = 1;
203 data += '&' + $.param(a);
205 $.post( ajaxurl, data, function(r){
209 if ( !$('input.widget_number', widget).val() ) {
210 id = $('input.widget-id', widget).val();
211 $('#available-widgets').find('input.widget-id').each(function(){
212 if ( $(this).val() == id )
213 $(this).closest('div.widget').show();
219 widget.slideUp('fast', function(){
221 wpWidgets.saveOrder();
228 $('.ajax-feedback').css('visibility', 'hidden');
229 if ( r && r.length > 2 ) {
230 $('div.widget-content', widget).html(r);
231 wpWidgets.appendTitle(widget);
232 wpWidgets.fixLabels(widget);
236 wpWidgets.saveOrder();
240 appendTitle : function(widget) {
241 var title = $('input[id*="-title"]', widget);
242 if ( title = title.val() ) {
243 title = title.replace(/<[^<>]+>/g, '').replace(/</g, '<').replace(/>/g, '>');
244 $(widget).children('.widget-top').children('.widget-title').children()
245 .children('.in-widget-title').html(': ' + title);
249 resize : function() {
250 $('div.widgets-sortables').not('#wp_inactive_widgets').each(function(){
251 var h = 50, H = $(this).children('.widget').length;
252 h = h + parseInt(H * 48, 10);
253 $(this).css( 'minHeight', h + 'px' );
257 fixWebkit : function(n) {
265 fixLabels : function(widget) {
266 widget.children('.widget-inside').find('label').each(function(){
267 var f = $(this).attr('for');
268 if ( f && f == $('input', this).attr('id') )
269 $(this).removeAttr('for');
273 close : function(widget) {
274 widget.children('.widget-inside').slideUp('fast', function(){
275 widget.css({'width':'', margin:''});
280 $(document).ready(function($){ wpWidgets.init(); });