1 /*global ajaxurl, isRtl */
4 var $document = $( document );
11 chooser = $('.widgets-chooser'),
12 selectSidebar = chooser.find('.widgets-chooser-sidebars'),
13 sidebars = $('div.widgets-sortables'),
14 isRTL = !! ( 'undefined' !== typeof isRtl && isRtl );
16 $('#widgets-right .sidebar-name').click( function() {
18 $wrap = $this.closest('.widgets-holder-wrap');
20 if ( $wrap.hasClass('closed') ) {
21 $wrap.removeClass('closed');
22 $this.parent().sortable('refresh');
24 $wrap.addClass('closed');
27 $document.triggerHandler( 'wp-pin-menu' );
30 $('#widgets-left .sidebar-name').click( function() {
31 $(this).closest('.widgets-holder-wrap').toggleClass('closed');
32 $document.triggerHandler( 'wp-pin-menu' );
35 $(document.body).bind('click.widgets-toggle', function(e) {
36 var target = $(e.target),
37 css = { 'z-index': 100 },
38 widget, inside, targetWidth, widgetWidth, margin;
40 if ( target.parents('.widget-top').length && ! target.parents('#available-widgets').length ) {
41 widget = target.closest('div.widget');
42 inside = widget.children('.widget-inside');
43 targetWidth = parseInt( widget.find('input.widget-width').val(), 10 ),
44 widgetWidth = widget.parent().width();
46 if ( inside.is(':hidden') ) {
47 if ( targetWidth > 250 && ( targetWidth + 30 > widgetWidth ) && widget.closest('div.widgets-sortables').length ) {
48 if ( widget.closest('div.widget-liquid-right').length ) {
49 margin = isRTL ? 'margin-right' : 'margin-left';
51 margin = isRTL ? 'margin-left' : 'margin-right';
54 css[ margin ] = widgetWidth - ( targetWidth + 30 ) + 'px';
57 widget.addClass( 'open' );
58 inside.slideDown('fast');
60 inside.slideUp('fast', function() {
61 widget.attr( 'style', '' );
62 widget.removeClass( 'open' );
66 } else if ( target.hasClass('widget-control-save') ) {
67 wpWidgets.save( target.closest('div.widget'), 0, 1, 0 );
69 } else if ( target.hasClass('widget-control-remove') ) {
70 wpWidgets.save( target.closest('div.widget'), 1, 1, 0 );
72 } else if ( target.hasClass('widget-control-close') ) {
73 widget = target.closest('div.widget');
74 widget.removeClass( 'open' );
75 wpWidgets.close( widget );
80 sidebars.children('.widget').each( function() {
83 wpWidgets.appendTitle( this );
85 if ( $this.find( 'p.widget-error' ).length ) {
86 $this.find( 'a.widget-action' ).trigger('click');
90 $('#widget-list').children('.widget').draggable({
91 connectToSortable: 'div.widgets-sortables',
92 handle: '> .widget-top > .widget-title',
96 containment: '#wpwrap',
97 start: function( event, ui ) {
98 var chooser = $(this).find('.widgets-chooser');
100 ui.helper.find('div.widget-description').hide();
103 if ( chooser.length ) {
104 // Hide the chooser and move it out of the widget
105 $( '#wpbody-content' ).append( chooser.hide() );
106 // Delete the cloned chooser from the drag helper
107 ui.helper.find('.widgets-chooser').remove();
108 self.clearWidgetSelection();
121 placeholder: 'widget-placeholder',
123 handle: '> .widget-top > .widget-title',
126 containment: '#wpwrap',
127 start: function( event, ui ) {
128 var height, $this = $(this),
129 $wrap = $this.parent(),
130 inside = ui.item.children('.widget-inside');
132 if ( inside.css('display') === 'block' ) {
134 $(this).sortable('refreshPositions');
137 if ( ! $wrap.hasClass('closed') ) {
138 // Lock all open sidebars min-height when starting to drag.
139 // Prevents jumping when dragging a widget from an open sidebar to a closed sidebar below.
140 height = ui.item.hasClass('ui-draggable') ? $this.height() : 1 + $this.height();
141 $this.css( 'min-height', height + 'px' );
145 stop: function( event, ui ) {
146 var addNew, widgetNumber, $sidebar, $children, child, item,
150 if ( $widget.hasClass('deleting') ) {
151 wpWidgets.save( $widget, 1, 0, 1 ); // delete widget
156 addNew = $widget.find('input.add_new').val();
157 widgetNumber = $widget.find('input.multi_number').val();
159 $widget.attr( 'style', '' ).removeClass('ui-draggable');
163 if ( 'multi' === addNew ) {
165 $widget.html().replace( /<[^<>]+>/g, function( tag ) {
166 return tag.replace( /__i__|%i%/g, widgetNumber );
170 $widget.attr( 'id', id.replace( '__i__', widgetNumber ) );
173 $( 'div#' + id ).find( 'input.multi_number' ).val( widgetNumber );
174 } else if ( 'single' === addNew ) {
175 $widget.attr( 'id', 'new-' + id );
179 wpWidgets.save( $widget, 0, 0, 1 );
180 $widget.find('input.add_new').val('');
181 $document.trigger( 'widget-added', [ $widget ] );
184 $sidebar = $widget.parent();
186 if ( $sidebar.parent().hasClass('closed') ) {
187 $sidebar.parent().removeClass('closed');
188 $children = $sidebar.children('.widget');
190 // Make sure the dropped widget is at the top
191 if ( $children.length > 1 ) {
192 child = $children.get(0);
193 item = $widget.get(0);
195 if ( child.id && item.id && child.id !== item.id ) {
196 $( child ).before( $widget );
202 $widget.find( 'a.widget-action' ).trigger('click');
204 wpWidgets.saveOrder( $sidebar.attr('id') );
208 activate: function() {
209 $(this).parent().addClass( 'widget-hover' );
212 deactivate: function() {
213 // Remove all min-height added on "start"
214 $(this).css( 'min-height', '' ).parent().removeClass( 'widget-hover' );
217 receive: function( event, ui ) {
218 var $sender = $( ui.sender );
220 // Don't add more widgets to orphaned sidebars
221 if ( this.id.indexOf('orphaned_widgets') > -1 ) {
222 $sender.sortable('cancel');
226 // If the last widget was moved out of an orphaned sidebar, close and remove it.
227 if ( $sender.attr('id').indexOf('orphaned_widgets') > -1 && ! $sender.children('.widget').length ) {
228 $sender.parents('.orphan-sidebar').slideUp( 400, function(){ $(this).remove(); } );
231 }).sortable( 'option', 'connectWith', 'div.widgets-sortables' );
233 $('#available-widgets').droppable({
234 tolerance: 'pointer',
236 return $(o).parent().attr('id') !== 'widget-list';
238 drop: function(e,ui) {
239 ui.draggable.addClass('deleting');
240 $('#removing-widget').hide().children('span').empty();
242 over: function(e,ui) {
243 ui.draggable.addClass('deleting');
244 $('div.widget-placeholder').hide();
246 if ( ui.draggable.hasClass('ui-sortable-helper') ) {
247 $('#removing-widget').show().children('span')
248 .html( ui.draggable.find('div.widget-title').children('h4').html() );
251 out: function(e,ui) {
252 ui.draggable.removeClass('deleting');
253 $('div.widget-placeholder').show();
254 $('#removing-widget').hide().children('span').empty();
259 $( '#widgets-right .widgets-holder-wrap' ).each( function( index, element ) {
260 var $element = $( element ),
261 name = $element.find( '.sidebar-name h3' ).text(),
262 id = $element.find( '.widgets-sortables' ).attr( 'id' ),
263 li = $('<li tabindex="0">').text( $.trim( name ) );
266 li.addClass( 'widgets-chooser-selected' );
269 selectSidebar.append( li );
270 li.data( 'sidebarId', id );
273 $( '#available-widgets .widget .widget-title' ).on( 'click.widgets-chooser', function() {
274 var $widget = $(this).closest( '.widget' );
276 if ( $widget.hasClass( 'widget-in-question' ) || $( '#widgets-left' ).hasClass( 'chooser' ) ) {
280 self.clearWidgetSelection();
281 $( '#widgets-left' ).addClass( 'chooser' );
282 $widget.addClass( 'widget-in-question' ).children( '.widget-description' ).after( chooser );
284 chooser.slideDown( 300, function() {
285 selectSidebar.find('.widgets-chooser-selected').focus();
288 selectSidebar.find( 'li' ).on( 'focusin.widgets-chooser', function() {
289 selectSidebar.find('.widgets-chooser-selected').removeClass( 'widgets-chooser-selected' );
290 $(this).addClass( 'widgets-chooser-selected' );
295 // Add event handlers
296 chooser.on( 'click.widgets-chooser', function( event ) {
297 var $target = $( event.target );
299 if ( $target.hasClass('button-primary') ) {
300 self.addWidget( chooser );
302 } else if ( $target.hasClass('button-secondary') ) {
305 }).on( 'keyup.widgets-chooser', function( event ) {
306 if ( event.which === $.ui.keyCode.ENTER ) {
307 if ( $( event.target ).hasClass('button-secondary') ) {
308 // Close instead of adding when pressing Enter on the Cancel button
311 self.addWidget( chooser );
314 } else if ( event.which === $.ui.keyCode.ESCAPE ) {
320 saveOrder : function( sidebarId ) {
322 action: 'widgets-order',
323 savewidgets: $('#_wpnonce_widgets').val(),
328 $( '#' + sidebarId ).find( '.spinner:first' ).addClass( 'is-active' );
331 $('div.widgets-sortables').each( function() {
332 if ( $(this).sortable ) {
333 data['sidebars[' + $(this).attr('id') + ']'] = $(this).sortable('toArray').join(',');
337 $.post( ajaxurl, data, function() {
338 $( '.spinner' ).removeClass( 'is-active' );
342 save : function( widget, del, animate, order ) {
343 var sidebarId = widget.closest('div.widgets-sortables').attr('id'),
344 data = widget.find('form').serialize(), a;
347 $( '.spinner', widget ).addClass( 'is-active' );
350 action: 'save-widget',
351 savewidgets: $('#_wpnonce_widgets').val(),
359 data += '&' + $.param(a);
361 $.post( ajaxurl, data, function(r) {
365 if ( ! $('input.widget_number', widget).val() ) {
366 id = $('input.widget-id', widget).val();
367 $('#available-widgets').find('input.widget-id').each(function(){
368 if ( $(this).val() === id ) {
369 $(this).closest('div.widget').show();
376 widget.slideUp('fast', function(){
378 wpWidgets.saveOrder();
384 $( '.spinner' ).removeClass( 'is-active' );
385 if ( r && r.length > 2 ) {
386 $( 'div.widget-content', widget ).html( r );
387 wpWidgets.appendTitle( widget );
388 $document.trigger( 'widget-updated', [ widget ] );
392 wpWidgets.saveOrder();
397 appendTitle : function(widget) {
398 var title = $('input[id*="-title"]', widget).val() || '';
401 title = ': ' + title.replace(/<[^<>]+>/g, '').replace(/</g, '<').replace(/>/g, '>');
404 $(widget).children('.widget-top').children('.widget-title').children()
405 .children('.in-widget-title').html(title);
409 close : function(widget) {
410 widget.children('.widget-inside').slideUp('fast', function() {
411 widget.attr( 'style', '' );
415 addWidget: function( chooser ) {
416 var widget, widgetId, add, n, viewportTop, viewportBottom, sidebarBounds,
417 sidebarId = chooser.find( '.widgets-chooser-selected' ).data('sidebarId'),
418 sidebar = $( '#' + sidebarId );
420 widget = $('#available-widgets').find('.widget-in-question').clone();
421 widgetId = widget.attr('id');
422 add = widget.find( 'input.add_new' ).val();
423 n = widget.find( 'input.multi_number' ).val();
425 // Remove the cloned chooser from the widget
426 widget.find('.widgets-chooser').remove();
428 if ( 'multi' === add ) {
430 widget.html().replace( /<[^<>]+>/g, function(m) {
431 return m.replace( /__i__|%i%/g, n );
435 widget.attr( 'id', widgetId.replace( '__i__', n ) );
437 $( '#' + widgetId ).find('input.multi_number').val(n);
438 } else if ( 'single' === add ) {
439 widget.attr( 'id', 'new-' + widgetId );
440 $( '#' + widgetId ).hide();
443 // Open the widgets container
444 sidebar.closest( '.widgets-holder-wrap' ).removeClass('closed');
446 sidebar.append( widget );
447 sidebar.sortable('refresh');
449 wpWidgets.save( widget, 0, 0, 1 );
450 // No longer "new" widget
451 widget.find( 'input.add_new' ).val('');
453 $document.trigger( 'widget-added', [ widget ] );
456 * Check if any part of the sidebar is visible in the viewport. If it is, don't scroll.
457 * Otherwise, scroll up to so the sidebar is in view.
459 * We do this by comparing the top and bottom, of the sidebar so see if they are within
460 * the bounds of the viewport.
462 viewportTop = $(window).scrollTop();
463 viewportBottom = viewportTop + $(window).height();
464 sidebarBounds = sidebar.offset();
466 sidebarBounds.bottom = sidebarBounds.top + sidebar.outerHeight();
468 if ( viewportTop > sidebarBounds.bottom || viewportBottom < sidebarBounds.top ) {
469 $( 'html, body' ).animate({
470 scrollTop: sidebarBounds.top - 130
474 window.setTimeout( function() {
475 // Cannot use a callback in the animation above as it fires twice,
476 // have to queue this "by hand".
477 widget.find( '.widget-title' ).trigger('click');
481 closeChooser: function() {
484 $( '.widgets-chooser' ).slideUp( 200, function() {
485 $( '#wpbody-content' ).append( this );
486 self.clearWidgetSelection();
490 clearWidgetSelection: function() {
491 $( '#widgets-left' ).removeClass( 'chooser' );
492 $( '.widget-in-question' ).removeClass( 'widget-in-question' );
496 $document.ready( function(){ wpWidgets.init(); } );