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 widget.addClass( 'open' );
54 inside.slideDown('fast');
56 inside.slideUp('fast', function() {
57 widget.attr( 'style', '' );
58 widget.removeClass( 'open' );
62 } else if ( target.hasClass('widget-control-save') ) {
63 wpWidgets.save( target.closest('div.widget'), 0, 1, 0 );
65 } else if ( target.hasClass('widget-control-remove') ) {
66 wpWidgets.save( target.closest('div.widget'), 1, 1, 0 );
68 } else if ( target.hasClass('widget-control-close') ) {
69 widget = target.closest('div.widget');
70 widget.removeClass( 'open' );
71 wpWidgets.close( widget );
76 sidebars.children('.widget').each( function() {
79 wpWidgets.appendTitle( this );
81 if ( $this.find( 'p.widget-error' ).length ) {
82 $this.find( 'a.widget-action' ).trigger('click');
86 $('#widget-list').children('.widget').draggable({
87 connectToSortable: 'div.widgets-sortables',
88 handle: '> .widget-top > .widget-title',
92 containment: 'document',
93 start: function( event, ui ) {
94 var chooser = $(this).find('.widgets-chooser');
96 ui.helper.find('div.widget-description').hide();
99 if ( chooser.length ) {
100 // Hide the chooser and move it out of the widget
101 $( '#wpbody-content' ).append( chooser.hide() );
102 // Delete the cloned chooser from the drag helper
103 ui.helper.find('.widgets-chooser').remove();
104 self.clearWidgetSelection();
117 placeholder: 'widget-placeholder',
119 handle: '> .widget-top > .widget-title',
122 containment: 'document',
123 start: function( event, ui ) {
124 var height, $this = $(this),
125 $wrap = $this.parent(),
126 inside = ui.item.children('.widget-inside');
128 if ( inside.css('display') === 'block' ) {
130 $(this).sortable('refreshPositions');
133 if ( ! $wrap.hasClass('closed') ) {
134 // Lock all open sidebars min-height when starting to drag.
135 // Prevents jumping when dragging a widget from an open sidebar to a closed sidebar below.
136 height = ui.item.hasClass('ui-draggable') ? $this.height() : 1 + $this.height();
137 $this.css( 'min-height', height + 'px' );
141 stop: function( event, ui ) {
142 var addNew, widgetNumber, $sidebar, $children, child, item,
146 if ( $widget.hasClass('deleting') ) {
147 wpWidgets.save( $widget, 1, 0, 1 ); // delete widget
152 addNew = $widget.find('input.add_new').val();
153 widgetNumber = $widget.find('input.multi_number').val();
155 $widget.attr( 'style', '' ).removeClass('ui-draggable');
159 if ( 'multi' === addNew ) {
161 $widget.html().replace( /<[^<>]+>/g, function( tag ) {
162 return tag.replace( /__i__|%i%/g, widgetNumber );
166 $widget.attr( 'id', id.replace( '__i__', widgetNumber ) );
169 $( 'div#' + id ).find( 'input.multi_number' ).val( widgetNumber );
170 } else if ( 'single' === addNew ) {
171 $widget.attr( 'id', 'new-' + id );
175 wpWidgets.save( $widget, 0, 0, 1 );
176 $widget.find('input.add_new').val('');
177 $( document ).trigger( 'widget-added', [ $widget ] );
180 $sidebar = $widget.parent();
182 if ( $sidebar.parent().hasClass('closed') ) {
183 $sidebar.parent().removeClass('closed');
184 $children = $sidebar.children('.widget');
186 // Make sure the dropped widget is at the top
187 if ( $children.length > 1 ) {
188 child = $children.get(0);
189 item = $widget.get(0);
191 if ( child.id && item.id && child.id !== item.id ) {
192 $( child ).before( $widget );
198 $widget.find( 'a.widget-action' ).trigger('click');
200 wpWidgets.saveOrder( $sidebar.attr('id') );
204 activate: function() {
205 $(this).parent().addClass( 'widget-hover' );
208 deactivate: function() {
209 // Remove all min-height added on "start"
210 $(this).css( 'min-height', '' ).parent().removeClass( 'widget-hover' );
213 receive: function( event, ui ) {
214 var $sender = $( ui.sender );
216 // Don't add more widgets to orphaned sidebars
217 if ( this.id.indexOf('orphaned_widgets') > -1 ) {
218 $sender.sortable('cancel');
222 // If the last widget was moved out of an orphaned sidebar, close and remove it.
223 if ( $sender.attr('id').indexOf('orphaned_widgets') > -1 && ! $sender.children('.widget').length ) {
224 $sender.parents('.orphan-sidebar').slideUp( 400, function(){ $(this).remove(); } );
227 }).sortable( 'option', 'connectWith', 'div.widgets-sortables' );
229 $('#available-widgets').droppable({
230 tolerance: 'pointer',
232 return $(o).parent().attr('id') !== 'widget-list';
234 drop: function(e,ui) {
235 ui.draggable.addClass('deleting');
236 $('#removing-widget').hide().children('span').html('');
238 over: function(e,ui) {
239 ui.draggable.addClass('deleting');
240 $('div.widget-placeholder').hide();
242 if ( ui.draggable.hasClass('ui-sortable-helper') ) {
243 $('#removing-widget').show().children('span')
244 .html( ui.draggable.find('div.widget-title').children('h4').html() );
247 out: function(e,ui) {
248 ui.draggable.removeClass('deleting');
249 $('div.widget-placeholder').show();
250 $('#removing-widget').hide().children('span').html('');
255 $( '#widgets-right .widgets-holder-wrap' ).each( function( index, element ) {
256 var $element = $( element ),
257 name = $element.find( '.sidebar-name h3' ).text(),
258 id = $element.find( '.widgets-sortables' ).attr( 'id' ),
259 li = $('<li tabindex="0">').text( $.trim( name ) );
262 li.addClass( 'widgets-chooser-selected' );
265 selectSidebar.append( li );
266 li.data( 'sidebarId', id );
269 $( '#available-widgets .widget .widget-title' ).on( 'click.widgets-chooser', function() {
270 var $widget = $(this).closest( '.widget' );
272 if ( $widget.hasClass( 'widget-in-question' ) || $( '#widgets-left' ).hasClass( 'chooser' ) ) {
276 self.clearWidgetSelection();
277 $( '#widgets-left' ).addClass( 'chooser' );
278 $widget.addClass( 'widget-in-question' ).children( '.widget-description' ).after( chooser );
280 chooser.slideDown( 300, function() {
281 selectSidebar.find('.widgets-chooser-selected').focus();
284 selectSidebar.find( 'li' ).on( 'focusin.widgets-chooser', function() {
285 selectSidebar.find('.widgets-chooser-selected').removeClass( 'widgets-chooser-selected' );
286 $(this).addClass( 'widgets-chooser-selected' );
291 // Add event handlers
292 chooser.on( 'click.widgets-chooser', function( event ) {
293 var $target = $( event.target );
295 if ( $target.hasClass('button-primary') ) {
296 self.addWidget( chooser );
298 } else if ( $target.hasClass('button-secondary') ) {
301 }).on( 'keyup.widgets-chooser', function( event ) {
302 if ( event.which === $.ui.keyCode.ENTER ) {
303 if ( $( event.target ).hasClass('button-secondary') ) {
304 // Close instead of adding when pressing Enter on the Cancel button
307 self.addWidget( chooser );
310 } else if ( event.which === $.ui.keyCode.ESCAPE ) {
316 saveOrder : function( sidebarId ) {
318 action: 'widgets-order',
319 savewidgets: $('#_wpnonce_widgets').val(),
324 $( '#' + sidebarId ).find('.spinner:first').css('display', 'inline-block');
327 $('div.widgets-sortables').each( function() {
328 if ( $(this).sortable ) {
329 data['sidebars[' + $(this).attr('id') + ']'] = $(this).sortable('toArray').join(',');
333 $.post( ajaxurl, data, function() {
334 $('.spinner').hide();
338 save : function( widget, del, animate, order ) {
339 var sidebarId = widget.closest('div.widgets-sortables').attr('id'),
340 data = widget.find('form').serialize(), a;
343 $('.spinner', widget).show();
346 action: 'save-widget',
347 savewidgets: $('#_wpnonce_widgets').val(),
355 data += '&' + $.param(a);
357 $.post( ajaxurl, data, function(r) {
361 if ( ! $('input.widget_number', widget).val() ) {
362 id = $('input.widget-id', widget).val();
363 $('#available-widgets').find('input.widget-id').each(function(){
364 if ( $(this).val() === id ) {
365 $(this).closest('div.widget').show();
372 widget.slideUp('fast', function(){
374 wpWidgets.saveOrder();
380 $('.spinner').hide();
381 if ( r && r.length > 2 ) {
382 $( 'div.widget-content', widget ).html( r );
383 wpWidgets.appendTitle( widget );
384 $( document ).trigger( 'widget-updated', [ widget ] );
388 wpWidgets.saveOrder();
393 appendTitle : function(widget) {
394 var title = $('input[id*="-title"]', widget).val() || '';
397 title = ': ' + title.replace(/<[^<>]+>/g, '').replace(/</g, '<').replace(/>/g, '>');
400 $(widget).children('.widget-top').children('.widget-title').children()
401 .children('.in-widget-title').html(title);
405 close : function(widget) {
406 widget.children('.widget-inside').slideUp('fast', function() {
407 widget.attr( 'style', '' );
411 addWidget: function( chooser ) {
412 var widget, widgetId, add, n, viewportTop, viewportBottom, sidebarBounds,
413 sidebarId = chooser.find( '.widgets-chooser-selected' ).data('sidebarId'),
414 sidebar = $( '#' + sidebarId );
416 widget = $('#available-widgets').find('.widget-in-question').clone();
417 widgetId = widget.attr('id');
418 add = widget.find( 'input.add_new' ).val();
419 n = widget.find( 'input.multi_number' ).val();
421 // Remove the cloned chooser from the widget
422 widget.find('.widgets-chooser').remove();
424 if ( 'multi' === add ) {
426 widget.html().replace( /<[^<>]+>/g, function(m) {
427 return m.replace( /__i__|%i%/g, n );
431 widget.attr( 'id', widgetId.replace( '__i__', n ) );
433 $( '#' + widgetId ).find('input.multi_number').val(n);
434 } else if ( 'single' === add ) {
435 widget.attr( 'id', 'new-' + widgetId );
436 $( '#' + widgetId ).hide();
439 // Open the widgets container
440 sidebar.closest( '.widgets-holder-wrap' ).removeClass('closed');
442 sidebar.append( widget );
443 sidebar.sortable('refresh');
445 wpWidgets.save( widget, 0, 0, 1 );
446 // No longer "new" widget
447 widget.find( 'input.add_new' ).val('');
449 $( document ).trigger( 'widget-added', [ widget ] );
452 * Check if any part of the sidebar is visible in the viewport. If it is, don't scroll.
453 * Otherwise, scroll up to so the sidebar is in view.
455 * We do this by comparing the top and bottom, of the sidebar so see if they are within
456 * the bounds of the viewport.
458 viewportTop = $(window).scrollTop();
459 viewportBottom = viewportTop + $(window).height();
460 sidebarBounds = sidebar.offset();
462 sidebarBounds.bottom = sidebarBounds.top + sidebar.outerHeight();
464 if ( viewportTop > sidebarBounds.bottom || viewportBottom < sidebarBounds.top ) {
465 $( 'html, body' ).animate({
466 scrollTop: sidebarBounds.top - 130
470 window.setTimeout( function() {
471 // Cannot use a callback in the animation above as it fires twice,
472 // have to queue this "by hand".
473 widget.find( '.widget-title' ).trigger('click');
477 closeChooser: function() {
480 $( '.widgets-chooser' ).slideUp( 200, function() {
481 $( '#wpbody-content' ).append( this );
482 self.clearWidgetSelection();
486 clearWidgetSelection: function() {
487 $( '#widgets-left' ).removeClass( 'chooser' );
488 $( '.widget-in-question' ).removeClass( 'widget-in-question' );
492 $(document).ready( function(){ wpWidgets.init(); } );