X-Git-Url: https://scripts.mit.edu/gitweb/autoinstalls/wordpress.git/blobdiff_plain/6c8f14c09105d0afa4c1574215c59b5021040e76..refs/tags/wordpress-3.8.2-scripts:/wp-admin/js/widgets.js?ds=sidebyside diff --git a/wp-admin/js/widgets.js b/wp-admin/js/widgets.js index 87be4ddf..046907b4 100644 --- a/wp-admin/js/widgets.js +++ b/wp-admin/js/widgets.js @@ -1,56 +1,59 @@ +/*global ajaxurl, isRtl */ var wpWidgets; (function($) { wpWidgets = { init : function() { - var rem, sidebars = $('div.widgets-sortables'), isRTL = !! ( 'undefined' != typeof isRtl && isRtl ), - margin = ( isRtl ? 'marginRight' : 'marginLeft' ), the_id; - - $('#widgets-right').children('.widgets-holder-wrap').children('.sidebar-name').click(function(){ - var c = $(this).siblings('.widgets-sortables'), p = $(this).parent(); - if ( !p.hasClass('closed') ) { - c.sortable('disable'); - p.addClass('closed'); + var rem, the_id, + self = this, + chooser = $('.widgets-chooser'), + selectSidebar = chooser.find('.widgets-chooser-sidebars'), + sidebars = $('div.widgets-sortables'), + isRTL = !! ( 'undefined' !== typeof isRtl && isRtl ); + + $('#widgets-right .sidebar-name').click( function() { + var $this = $(this), + $wrap = $this.closest('.widgets-holder-wrap'); + + if ( $wrap.hasClass('closed') ) { + $wrap.removeClass('closed'); + $this.parent().sortable('refresh'); } else { - p.removeClass('closed'); - c.sortable('enable').sortable('refresh'); + $wrap.addClass('closed'); } }); - $('#widgets-left').children('.widgets-holder-wrap').children('.sidebar-name').click(function() { - $(this).parent().toggleClass('closed'); + $('#widgets-left .sidebar-name').click( function() { + $(this).closest('.widgets-holder-wrap').toggleClass('closed'); }); - sidebars.each(function(){ - if ( $(this).parent().hasClass('inactive') ) - return true; - - var h = 50, H = $(this).children('.widget').length; - h = h + parseInt(H * 48, 10); - $(this).css( 'minHeight', h + 'px' ); - }); - - $(document.body).bind('click.widgets-toggle', function(e){ - var target = $(e.target), css = {}, widget, inside, w; + $(document.body).bind('click.widgets-toggle', function(e) { + var target = $(e.target), + css = { 'z-index': 100 }, + widget, inside, targetWidth, widgetWidth, margin; if ( target.parents('.widget-top').length && ! target.parents('#available-widgets').length ) { widget = target.closest('div.widget'); inside = widget.children('.widget-inside'); - w = parseInt( widget.find('input.widget-width').val(), 10 ); + targetWidth = parseInt( widget.find('input.widget-width').val(), 10 ), + widgetWidth = widget.parent().width(); if ( inside.is(':hidden') ) { - if ( w > 250 && inside.closest('div.widgets-sortables').length ) { - css['width'] = w + 30 + 'px'; - if ( inside.closest('div.widget-liquid-right').length ) - css[margin] = 235 - w + 'px'; - widget.css(css); + if ( targetWidth > 250 && ( targetWidth + 30 > widgetWidth ) && widget.closest('div.widgets-sortables').length ) { + if ( widget.closest('div.widget-liquid-right').length ) { + margin = isRTL ? 'margin-right' : 'margin-left'; + } else { + margin = isRTL ? 'margin-left' : 'margin-right'; + } + + css[ margin ] = widgetWidth - ( targetWidth + 30 ) + 'px'; + widget.css( css ); } - wpWidgets.fixLabels(widget); inside.slideDown('fast'); } else { inside.slideUp('fast', function() { - widget.css({'width':'', margin:''}); + widget.attr( 'style', '' ); }); } e.preventDefault(); @@ -66,10 +69,14 @@ wpWidgets = { } }); - sidebars.children('.widget').each(function() { - wpWidgets.appendTitle(this); - if ( $('p.widget-error', this).length ) - $('a.widget-action', this).click(); + sidebars.children('.widget').each( function() { + var $this = $(this); + + wpWidgets.appendTitle( this ); + + if ( $this.find( 'p.widget-error' ).length ) { + $this.find( 'a.widget-action' ).trigger('click'); + } }); $('#widget-list').children('.widget').draggable({ @@ -79,13 +86,24 @@ wpWidgets = { helper: 'clone', zIndex: 100, containment: 'document', - start: function(e,ui) { + start: function( event, ui ) { + var chooser = $(this).find('.widgets-chooser'); + ui.helper.find('div.widget-description').hide(); the_id = this.id; + + if ( chooser.length ) { + // Hide the chooser and move it out of the widget + $( '#wpbody-content' ).append( chooser.hide() ); + // Delete the cloned chooser from the drag helper + ui.helper.find('.widgets-chooser').remove(); + self.clearWidgetSelection(); + } }, - stop: function(e,ui) { - if ( rem ) + stop: function() { + if ( rem ) { $(rem).hide(); + } rem = ''; } @@ -98,61 +116,115 @@ wpWidgets = { cursor: 'move', distance: 2, containment: 'document', - start: function(e,ui) { - ui.item.children('.widget-inside').hide(); - ui.item.css({margin:'', 'width':''}); + start: function( event, ui ) { + var height, $this = $(this), + $wrap = $this.parent(), + inside = ui.item.children('.widget-inside'); + + if ( inside.css('display') === 'block' ) { + inside.hide(); + $(this).sortable('refreshPositions'); + } + + if ( ! $wrap.hasClass('closed') ) { + // Lock all open sidebars min-height when starting to drag. + // Prevents jumping when dragging a widget from an open sidebar to a closed sidebar below. + height = ui.item.hasClass('ui-draggable') ? $this.height() : 1 + $this.height(); + $this.css( 'min-height', height + 'px' ); + } }, - stop: function(e,ui) { - if ( ui.item.hasClass('ui-draggable') && ui.item.data('draggable') ) - ui.item.draggable('destroy'); - if ( ui.item.hasClass('deleting') ) { - wpWidgets.save( ui.item, 1, 0, 1 ); // delete widget - ui.item.remove(); + stop: function( event, ui ) { + var addNew, widgetNumber, $sidebar, $children, child, item, + $widget = ui.item, + id = the_id; + + if ( $widget.hasClass('deleting') ) { + wpWidgets.save( $widget, 1, 0, 1 ); // delete widget + $widget.remove(); return; } - var add = ui.item.find('input.add_new').val(), - n = ui.item.find('input.multi_number').val(), - id = the_id, - sb = $(this).attr('id'); + addNew = $widget.find('input.add_new').val(); + widgetNumber = $widget.find('input.multi_number').val(); - ui.item.css({margin:'', 'width':''}); + $widget.attr( 'style', '' ).removeClass('ui-draggable'); the_id = ''; - if ( add ) { - if ( 'multi' == add ) { - ui.item.html( ui.item.html().replace(/<[^<>]+>/g, function(m){ return m.replace(/__i__|%i%/g, n); }) ); - ui.item.attr( 'id', id.replace('__i__', n) ); - n++; - $('div#' + id).find('input.multi_number').val(n); - } else if ( 'single' == add ) { - ui.item.attr( 'id', 'new-' + id ); + if ( addNew ) { + if ( 'multi' === addNew ) { + $widget.html( + $widget.html().replace( /<[^<>]+>/g, function( tag ) { + return tag.replace( /__i__|%i%/g, widgetNumber ); + }) + ); + + $widget.attr( 'id', id.replace( '__i__', widgetNumber ) ); + widgetNumber++; + + $( 'div#' + id ).find( 'input.multi_number' ).val( widgetNumber ); + } else if ( 'single' === addNew ) { + $widget.attr( 'id', 'new-' + id ); rem = 'div#' + id; } - wpWidgets.save( ui.item, 0, 0, 1 ); - ui.item.find('input.add_new').val(''); - ui.item.find('a.widget-action').click(); - return; + + wpWidgets.save( $widget, 0, 0, 1 ); + $widget.find('input.add_new').val(''); + } + + $sidebar = $widget.parent(); + + if ( $sidebar.parent().hasClass('closed') ) { + $sidebar.parent().removeClass('closed'); + $children = $sidebar.children('.widget'); + + // Make sure the dropped widget is at the top + if ( $children.length > 1 ) { + child = $children.get(0); + item = $widget.get(0); + + if ( child.id && item.id && child.id !== item.id ) { + $( child ).before( $widget ); + } + } + } + + if ( addNew ) { + $widget.find( 'a.widget-action' ).trigger('click'); + } else { + wpWidgets.saveOrder( $sidebar.attr('id') ); } - wpWidgets.saveOrder(sb); }, - receive: function(e, ui) { - var sender = $(ui.sender); - if ( !$(this).is(':visible') || this.id.indexOf('orphaned_widgets') != -1 ) - sender.sortable('cancel'); + activate: function() { + $(this).parent().addClass( 'widget-hover' ); + }, + + deactivate: function() { + // Remove all min-height added on "start" + $(this).css( 'min-height', '' ).parent().removeClass( 'widget-hover' ); + }, + + receive: function( event, ui ) { + var $sender = $( ui.sender ); - if ( sender.attr('id').indexOf('orphaned_widgets') != -1 && !sender.children('.widget').length ) { - sender.parents('.orphan-sidebar').slideUp(400, function(){ $(this).remove(); }); + // Don't add more widgets to orphaned sidebars + if ( this.id.indexOf('orphaned_widgets') > -1 ) { + $sender.sortable('cancel'); + return; + } + + // If the last widget was moved out of an orphaned sidebar, close and remove it. + if ( $sender.attr('id').indexOf('orphaned_widgets') > -1 && ! $sender.children('.widget').length ) { + $sender.parents('.orphan-sidebar').slideUp( 400, function(){ $(this).remove(); } ); } } - }).sortable('option', 'connectWith', 'div.widgets-sortables').parent().filter('.closed').children('.widgets-sortables').sortable('disable'); + }).sortable( 'option', 'connectWith', 'div.widgets-sortables' ); $('#available-widgets').droppable({ tolerance: 'pointer', accept: function(o){ - return $(o).parent().attr('id') != 'widget-list'; + return $(o).parent().attr('id') !== 'widget-list'; }, drop: function(e,ui) { ui.draggable.addClass('deleting'); @@ -162,9 +234,10 @@ wpWidgets = { ui.draggable.addClass('deleting'); $('div.widget-placeholder').hide(); - if ( ui.draggable.hasClass('ui-sortable-helper') ) + if ( ui.draggable.hasClass('ui-sortable-helper') ) { $('#removing-widget').show().children('span') .html( ui.draggable.find('div.widget-title').children('h4').html() ); + } }, out: function(e,ui) { ui.draggable.removeClass('deleting'); @@ -172,55 +245,120 @@ wpWidgets = { $('#removing-widget').hide().children('span').html(''); } }); - }, - saveOrder : function(sb) { - if ( sb ) - $('#' + sb).closest('div.widgets-holder-wrap').find('.spinner').css('display', 'inline-block'); + // Area Chooser + $( '#widgets-right .widgets-holder-wrap' ).each( function( index, element ) { + var $element = $( element ), + name = $element.find( '.sidebar-name h3' ).text(), + id = $element.find( '.widgets-sortables' ).attr( 'id' ), + li = $('