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('');
175 $sidebar = $widget.parent();
177 if ( $sidebar.parent().hasClass('closed') ) {
178 $sidebar.parent().removeClass('closed');
179 $children = $sidebar.children('.widget');
181 // Make sure the dropped widget is at the top
182 if ( $children.length > 1 ) {
183 child = $children.get(0);
184 item = $widget.get(0);
186 if ( child.id && item.id && child.id !== item.id ) {
187 $( child ).before( $widget );
193 $widget.find( 'a.widget-action' ).trigger('click');
195 wpWidgets.saveOrder( $sidebar.attr('id') );
199 activate: function() {
200 $(this).parent().addClass( 'widget-hover' );
203 deactivate: function() {
204 // Remove all min-height added on "start"
205 $(this).css( 'min-height', '' ).parent().removeClass( 'widget-hover' );
208 receive: function( event, ui ) {
209 var $sender = $( ui.sender );
211 // Don't add more widgets to orphaned sidebars
212 if ( this.id.indexOf('orphaned_widgets') > -1 ) {
213 $sender.sortable('cancel');
217 // If the last widget was moved out of an orphaned sidebar, close and remove it.
218 if ( $sender.attr('id').indexOf('orphaned_widgets') > -1 && ! $sender.children('.widget').length ) {
219 $sender.parents('.orphan-sidebar').slideUp( 400, function(){ $(this).remove(); } );
222 }).sortable( 'option', 'connectWith', 'div.widgets-sortables' );
224 $('#available-widgets').droppable({
225 tolerance: 'pointer',
227 return $(o).parent().attr('id') !== 'widget-list';
229 drop: function(e,ui) {
230 ui.draggable.addClass('deleting');
231 $('#removing-widget').hide().children('span').html('');
233 over: function(e,ui) {
234 ui.draggable.addClass('deleting');
235 $('div.widget-placeholder').hide();
237 if ( ui.draggable.hasClass('ui-sortable-helper') ) {
238 $('#removing-widget').show().children('span')
239 .html( ui.draggable.find('div.widget-title').children('h4').html() );
242 out: function(e,ui) {
243 ui.draggable.removeClass('deleting');
244 $('div.widget-placeholder').show();
245 $('#removing-widget').hide().children('span').html('');
250 $( '#widgets-right .widgets-holder-wrap' ).each( function( index, element ) {
251 var $element = $( element ),
252 name = $element.find( '.sidebar-name h3' ).text(),
253 id = $element.find( '.widgets-sortables' ).attr( 'id' ),
254 li = $('<li tabindex="0">').text( $.trim( name ) );
257 li.addClass( 'widgets-chooser-selected' );
260 selectSidebar.append( li );
261 li.data( 'sidebarId', id );
264 $( '#available-widgets .widget .widget-title' ).on( 'click.widgets-chooser', function() {
265 var $widget = $(this).closest( '.widget' );
267 if ( $widget.hasClass( 'widget-in-question' ) || $( '#widgets-left' ).hasClass( 'chooser' ) ) {
271 self.clearWidgetSelection();
272 $( '#widgets-left' ).addClass( 'chooser' );
273 $widget.addClass( 'widget-in-question' ).children( '.widget-description' ).after( chooser );
275 chooser.slideDown( 300, function() {
276 selectSidebar.find('.widgets-chooser-selected').focus();
279 selectSidebar.find( 'li' ).on( 'focusin.widgets-chooser', function() {
280 selectSidebar.find('.widgets-chooser-selected').removeClass( 'widgets-chooser-selected' );
281 $(this).addClass( 'widgets-chooser-selected' );
286 // Add event handlers
287 chooser.on( 'click.widgets-chooser', function( event ) {
288 var $target = $( event.target );
290 if ( $target.hasClass('button-primary') ) {
291 self.addWidget( chooser );
293 } else if ( $target.hasClass('button-secondary') ) {
296 }).on( 'keyup.widgets-chooser', function( event ) {
297 if ( event.which === $.ui.keyCode.ENTER ) {
298 if ( $( event.target ).hasClass('button-secondary') ) {
299 // Close instead of adding when pressing Enter on the Cancel button
302 self.addWidget( chooser );
305 } else if ( event.which === $.ui.keyCode.ESCAPE ) {
311 saveOrder : function( sidebarId ) {
313 action: 'widgets-order',
314 savewidgets: $('#_wpnonce_widgets').val(),
319 $( '#' + sidebarId ).find('.spinner:first').css('display', 'inline-block');
322 $('div.widgets-sortables').each( function() {
323 if ( $(this).sortable ) {
324 data['sidebars[' + $(this).attr('id') + ']'] = $(this).sortable('toArray').join(',');
328 $.post( ajaxurl, data, function() {
329 $('.spinner').hide();
333 save : function( widget, del, animate, order ) {
334 var sidebarId = widget.closest('div.widgets-sortables').attr('id'),
335 data = widget.find('form').serialize(), a;
338 $('.spinner', widget).show();
341 action: 'save-widget',
342 savewidgets: $('#_wpnonce_widgets').val(),
350 data += '&' + $.param(a);
352 $.post( ajaxurl, data, function(r) {
356 if ( ! $('input.widget_number', widget).val() ) {
357 id = $('input.widget-id', widget).val();
358 $('#available-widgets').find('input.widget-id').each(function(){
359 if ( $(this).val() === id ) {
360 $(this).closest('div.widget').show();
367 widget.slideUp('fast', function(){
369 wpWidgets.saveOrder();
375 $('.spinner').hide();
376 if ( r && r.length > 2 ) {
377 $( 'div.widget-content', widget ).html(r);
378 wpWidgets.appendTitle( widget );
382 wpWidgets.saveOrder();
387 appendTitle : function(widget) {
388 var title = $('input[id*="-title"]', widget).val() || '';
391 title = ': ' + title.replace(/<[^<>]+>/g, '').replace(/</g, '<').replace(/>/g, '>');
394 $(widget).children('.widget-top').children('.widget-title').children()
395 .children('.in-widget-title').html(title);
399 close : function(widget) {
400 widget.children('.widget-inside').slideUp('fast', function() {
401 widget.attr( 'style', '' );
405 addWidget: function( chooser ) {
406 var widget, widgetId, add, n, viewportTop, viewportBottom, sidebarBounds,
407 sidebarId = chooser.find( '.widgets-chooser-selected' ).data('sidebarId'),
408 sidebar = $( '#' + sidebarId );
410 widget = $('#available-widgets').find('.widget-in-question').clone();
411 widgetId = widget.attr('id');
412 add = widget.find( 'input.add_new' ).val();
413 n = widget.find( 'input.multi_number' ).val();
415 // Remove the cloned chooser from the widget
416 widget.find('.widgets-chooser').remove();
418 if ( 'multi' === add ) {
420 widget.html().replace( /<[^<>]+>/g, function(m) {
421 return m.replace( /__i__|%i%/g, n );
425 widget.attr( 'id', widgetId.replace( '__i__', n ) );
427 $( '#' + widgetId ).find('input.multi_number').val(n);
428 } else if ( 'single' === add ) {
429 widget.attr( 'id', 'new-' + widgetId );
430 $( '#' + widgetId ).hide();
433 // Open the widgets container
434 sidebar.closest( '.widgets-holder-wrap' ).removeClass('closed');
436 sidebar.append( widget );
437 sidebar.sortable('refresh');
439 wpWidgets.save( widget, 0, 0, 1 );
440 // No longer "new" widget
441 widget.find( 'input.add_new' ).val('');
444 * Check if any part of the sidebar is visible in the viewport. If it is, don't scroll.
445 * Otherwise, scroll up to so the sidebar is in view.
447 * We do this by comparing the top and bottom, of the sidebar so see if they are within
448 * the bounds of the viewport.
450 viewportTop = $(window).scrollTop();
451 viewportBottom = viewportTop + $(window).height();
452 sidebarBounds = sidebar.offset();
454 sidebarBounds.bottom = sidebarBounds.top + sidebar.outerHeight();
456 if ( viewportTop > sidebarBounds.bottom || viewportBottom < sidebarBounds.top ) {
457 $( 'html, body' ).animate({
458 scrollTop: sidebarBounds.top - 130
462 window.setTimeout( function() {
463 // Cannot use a callback in the animation above as it fires twice,
464 // have to queue this "by hand".
465 widget.find( '.widget-title' ).trigger('click');
469 closeChooser: function() {
472 $( '.widgets-chooser' ).slideUp( 200, function() {
473 $( '#wpbody-content' ).append( this );
474 self.clearWidgetSelection();
478 clearWidgetSelection: function() {
479 $( '#widgets-left' ).removeClass( 'chooser' );
480 $( '.widget-in-question' ).removeClass( 'widget-in-question' );
484 $(document).ready( function(){ wpWidgets.init(); } );