1 /*global ajaxurl, isRtl */
10 chooser = $('.widgets-chooser'),
11 selectSidebar = chooser.find('.widgets-chooser-sidebars'),
12 sidebars = $('div.widgets-sortables'),
13 isRTL = !! ( 'undefined' !== typeof isRtl && isRtl );
15 $('#widgets-right .sidebar-name').click( function() {
17 $wrap = $this.closest('.widgets-holder-wrap');
19 if ( $wrap.hasClass('closed') ) {
20 $wrap.removeClass('closed');
21 $this.parent().sortable('refresh');
23 $wrap.addClass('closed');
27 $('#widgets-left .sidebar-name').click( function() {
28 $(this).closest('.widgets-holder-wrap').toggleClass('closed');
31 $(document.body).bind('click.widgets-toggle', function(e) {
32 var target = $(e.target),
33 css = { 'z-index': 100 },
34 widget, inside, targetWidth, widgetWidth, margin;
36 if ( target.parents('.widget-top').length && ! target.parents('#available-widgets').length ) {
37 widget = target.closest('div.widget');
38 inside = widget.children('.widget-inside');
39 targetWidth = parseInt( widget.find('input.widget-width').val(), 10 ),
40 widgetWidth = widget.parent().width();
42 if ( inside.is(':hidden') ) {
43 if ( targetWidth > 250 && ( targetWidth + 30 > widgetWidth ) && widget.closest('div.widgets-sortables').length ) {
44 if ( widget.closest('div.widget-liquid-right').length ) {
45 margin = isRTL ? 'margin-right' : 'margin-left';
47 margin = isRTL ? 'margin-left' : 'margin-right';
50 css[ margin ] = widgetWidth - ( targetWidth + 30 ) + 'px';
53 inside.slideDown('fast');
55 inside.slideUp('fast', function() {
56 widget.attr( 'style', '' );
60 } else if ( target.hasClass('widget-control-save') ) {
61 wpWidgets.save( target.closest('div.widget'), 0, 1, 0 );
63 } else if ( target.hasClass('widget-control-remove') ) {
64 wpWidgets.save( target.closest('div.widget'), 1, 1, 0 );
66 } else if ( target.hasClass('widget-control-close') ) {
67 wpWidgets.close( target.closest('div.widget') );
72 sidebars.children('.widget').each( function() {
75 wpWidgets.appendTitle( this );
77 if ( $this.find( 'p.widget-error' ).length ) {
78 $this.find( 'a.widget-action' ).trigger('click');
82 $('#widget-list').children('.widget').draggable({
83 connectToSortable: 'div.widgets-sortables',
84 handle: '> .widget-top > .widget-title',
88 containment: 'document',
89 start: function( event, ui ) {
90 var chooser = $(this).find('.widgets-chooser');
92 ui.helper.find('div.widget-description').hide();
95 if ( chooser.length ) {
96 // Hide the chooser and move it out of the widget
97 $( '#wpbody-content' ).append( chooser.hide() );
98 // Delete the cloned chooser from the drag helper
99 ui.helper.find('.widgets-chooser').remove();
100 self.clearWidgetSelection();
113 placeholder: 'widget-placeholder',
115 handle: '> .widget-top > .widget-title',
118 containment: 'document',
119 start: function( event, ui ) {
120 var height, $this = $(this),
121 $wrap = $this.parent(),
122 inside = ui.item.children('.widget-inside');
124 if ( inside.css('display') === 'block' ) {
126 $(this).sortable('refreshPositions');
129 if ( ! $wrap.hasClass('closed') ) {
130 // Lock all open sidebars min-height when starting to drag.
131 // Prevents jumping when dragging a widget from an open sidebar to a closed sidebar below.
132 height = ui.item.hasClass('ui-draggable') ? $this.height() : 1 + $this.height();
133 $this.css( 'min-height', height + 'px' );
137 stop: function( event, ui ) {
138 var addNew, widgetNumber, $sidebar, $children, child, item,
142 if ( $widget.hasClass('deleting') ) {
143 wpWidgets.save( $widget, 1, 0, 1 ); // delete widget
148 addNew = $widget.find('input.add_new').val();
149 widgetNumber = $widget.find('input.multi_number').val();
151 $widget.attr( 'style', '' ).removeClass('ui-draggable');
155 if ( 'multi' === addNew ) {
157 $widget.html().replace( /<[^<>]+>/g, function( tag ) {
158 return tag.replace( /__i__|%i%/g, widgetNumber );
162 $widget.attr( 'id', id.replace( '__i__', widgetNumber ) );
165 $( 'div#' + id ).find( 'input.multi_number' ).val( widgetNumber );
166 } else if ( 'single' === addNew ) {
167 $widget.attr( 'id', 'new-' + id );
171 wpWidgets.save( $widget, 0, 0, 1 );
172 $widget.find('input.add_new').val('');
173 $( document ).trigger( 'widget-added', [ $widget ] );
176 $sidebar = $widget.parent();
178 if ( $sidebar.parent().hasClass('closed') ) {
179 $sidebar.parent().removeClass('closed');
180 $children = $sidebar.children('.widget');
182 // Make sure the dropped widget is at the top
183 if ( $children.length > 1 ) {
184 child = $children.get(0);
185 item = $widget.get(0);
187 if ( child.id && item.id && child.id !== item.id ) {
188 $( child ).before( $widget );
194 $widget.find( 'a.widget-action' ).trigger('click');
196 wpWidgets.saveOrder( $sidebar.attr('id') );
200 activate: function() {
201 $(this).parent().addClass( 'widget-hover' );
204 deactivate: function() {
205 // Remove all min-height added on "start"
206 $(this).css( 'min-height', '' ).parent().removeClass( 'widget-hover' );
209 receive: function( event, ui ) {
210 var $sender = $( ui.sender );
212 // Don't add more widgets to orphaned sidebars
213 if ( this.id.indexOf('orphaned_widgets') > -1 ) {
214 $sender.sortable('cancel');
218 // If the last widget was moved out of an orphaned sidebar, close and remove it.
219 if ( $sender.attr('id').indexOf('orphaned_widgets') > -1 && ! $sender.children('.widget').length ) {
220 $sender.parents('.orphan-sidebar').slideUp( 400, function(){ $(this).remove(); } );
223 }).sortable( 'option', 'connectWith', 'div.widgets-sortables' );
225 $('#available-widgets').droppable({
226 tolerance: 'pointer',
228 return $(o).parent().attr('id') !== 'widget-list';
230 drop: function(e,ui) {
231 ui.draggable.addClass('deleting');
232 $('#removing-widget').hide().children('span').html('');
234 over: function(e,ui) {
235 ui.draggable.addClass('deleting');
236 $('div.widget-placeholder').hide();
238 if ( ui.draggable.hasClass('ui-sortable-helper') ) {
239 $('#removing-widget').show().children('span')
240 .html( ui.draggable.find('div.widget-title').children('h4').html() );
243 out: function(e,ui) {
244 ui.draggable.removeClass('deleting');
245 $('div.widget-placeholder').show();
246 $('#removing-widget').hide().children('span').html('');
251 $( '#widgets-right .widgets-holder-wrap' ).each( function( index, element ) {
252 var $element = $( element ),
253 name = $element.find( '.sidebar-name h3' ).text(),
254 id = $element.find( '.widgets-sortables' ).attr( 'id' ),
255 li = $('<li tabindex="0">').text( $.trim( name ) );
258 li.addClass( 'widgets-chooser-selected' );
261 selectSidebar.append( li );
262 li.data( 'sidebarId', id );
265 $( '#available-widgets .widget .widget-title' ).on( 'click.widgets-chooser', function() {
266 var $widget = $(this).closest( '.widget' );
268 if ( $widget.hasClass( 'widget-in-question' ) || $( '#widgets-left' ).hasClass( 'chooser' ) ) {
272 self.clearWidgetSelection();
273 $( '#widgets-left' ).addClass( 'chooser' );
274 $widget.addClass( 'widget-in-question' ).children( '.widget-description' ).after( chooser );
276 chooser.slideDown( 300, function() {
277 selectSidebar.find('.widgets-chooser-selected').focus();
280 selectSidebar.find( 'li' ).on( 'focusin.widgets-chooser', function() {
281 selectSidebar.find('.widgets-chooser-selected').removeClass( 'widgets-chooser-selected' );
282 $(this).addClass( 'widgets-chooser-selected' );
287 // Add event handlers
288 chooser.on( 'click.widgets-chooser', function( event ) {
289 var $target = $( event.target );
291 if ( $target.hasClass('button-primary') ) {
292 self.addWidget( chooser );
294 } else if ( $target.hasClass('button-secondary') ) {
297 }).on( 'keyup.widgets-chooser', function( event ) {
298 if ( event.which === $.ui.keyCode.ENTER ) {
299 if ( $( event.target ).hasClass('button-secondary') ) {
300 // Close instead of adding when pressing Enter on the Cancel button
303 self.addWidget( chooser );
306 } else if ( event.which === $.ui.keyCode.ESCAPE ) {
312 saveOrder : function( sidebarId ) {
314 action: 'widgets-order',
315 savewidgets: $('#_wpnonce_widgets').val(),
320 $( '#' + sidebarId ).find('.spinner:first').css('display', 'inline-block');
323 $('div.widgets-sortables').each( function() {
324 if ( $(this).sortable ) {
325 data['sidebars[' + $(this).attr('id') + ']'] = $(this).sortable('toArray').join(',');
329 $.post( ajaxurl, data, function() {
330 $('.spinner').hide();
334 save : function( widget, del, animate, order ) {
335 var sidebarId = widget.closest('div.widgets-sortables').attr('id'),
336 data = widget.find('form').serialize(), a;
339 $('.spinner', widget).show();
342 action: 'save-widget',
343 savewidgets: $('#_wpnonce_widgets').val(),
351 data += '&' + $.param(a);
353 $.post( ajaxurl, data, function(r) {
357 if ( ! $('input.widget_number', widget).val() ) {
358 id = $('input.widget-id', widget).val();
359 $('#available-widgets').find('input.widget-id').each(function(){
360 if ( $(this).val() === id ) {
361 $(this).closest('div.widget').show();
368 widget.slideUp('fast', function(){
370 wpWidgets.saveOrder();
376 $('.spinner').hide();
377 if ( r && r.length > 2 ) {
378 $( 'div.widget-content', widget ).html( r );
379 wpWidgets.appendTitle( widget );
380 $( document ).trigger( 'widget-updated', [ widget ] );
384 wpWidgets.saveOrder();
389 appendTitle : function(widget) {
390 var title = $('input[id*="-title"]', widget).val() || '';
393 title = ': ' + title.replace(/<[^<>]+>/g, '').replace(/</g, '<').replace(/>/g, '>');
396 $(widget).children('.widget-top').children('.widget-title').children()
397 .children('.in-widget-title').html(title);
401 close : function(widget) {
402 widget.children('.widget-inside').slideUp('fast', function() {
403 widget.attr( 'style', '' );
407 addWidget: function( chooser ) {
408 var widget, widgetId, add, n, viewportTop, viewportBottom, sidebarBounds,
409 sidebarId = chooser.find( '.widgets-chooser-selected' ).data('sidebarId'),
410 sidebar = $( '#' + sidebarId );
412 widget = $('#available-widgets').find('.widget-in-question').clone();
413 widgetId = widget.attr('id');
414 add = widget.find( 'input.add_new' ).val();
415 n = widget.find( 'input.multi_number' ).val();
417 // Remove the cloned chooser from the widget
418 widget.find('.widgets-chooser').remove();
420 if ( 'multi' === add ) {
422 widget.html().replace( /<[^<>]+>/g, function(m) {
423 return m.replace( /__i__|%i%/g, n );
427 widget.attr( 'id', widgetId.replace( '__i__', n ) );
429 $( '#' + widgetId ).find('input.multi_number').val(n);
430 } else if ( 'single' === add ) {
431 widget.attr( 'id', 'new-' + widgetId );
432 $( '#' + widgetId ).hide();
435 // Open the widgets container
436 sidebar.closest( '.widgets-holder-wrap' ).removeClass('closed');
438 sidebar.append( widget );
439 sidebar.sortable('refresh');
441 wpWidgets.save( widget, 0, 0, 1 );
442 // No longer "new" widget
443 widget.find( 'input.add_new' ).val('');
445 $( document ).trigger( 'widget-added', [ widget ] );
448 * Check if any part of the sidebar is visible in the viewport. If it is, don't scroll.
449 * Otherwise, scroll up to so the sidebar is in view.
451 * We do this by comparing the top and bottom, of the sidebar so see if they are within
452 * the bounds of the viewport.
454 viewportTop = $(window).scrollTop();
455 viewportBottom = viewportTop + $(window).height();
456 sidebarBounds = sidebar.offset();
458 sidebarBounds.bottom = sidebarBounds.top + sidebar.outerHeight();
460 if ( viewportTop > sidebarBounds.bottom || viewportBottom < sidebarBounds.top ) {
461 $( 'html, body' ).animate({
462 scrollTop: sidebarBounds.top - 130
466 window.setTimeout( function() {
467 // Cannot use a callback in the animation above as it fires twice,
468 // have to queue this "by hand".
469 widget.find( '.widget-title' ).trigger('click');
473 closeChooser: function() {
476 $( '.widgets-chooser' ).slideUp( 200, function() {
477 $( '#wpbody-content' ).append( this );
478 self.clearWidgetSelection();
482 clearWidgetSelection: function() {
483 $( '#widgets-left' ).removeClass( 'chooser' );
484 $( '.widget-in-question' ).removeClass( 'widget-in-question' );
488 $(document).ready( function(){ wpWidgets.init(); } );