2013-11-08 18:05:10 -05:00
|
|
|
/*global ajaxurl, isRtl */
|
2012-08-22 20:04:18 -04:00
|
|
|
var wpWidgets;
|
|
|
|
(function($) {
|
2015-05-09 20:32:31 -04:00
|
|
|
var $document = $( document );
|
2012-08-22 20:04:18 -04:00
|
|
|
|
|
|
|
wpWidgets = {
|
2015-06-30 20:58:24 -04:00
|
|
|
/**
|
|
|
|
* A closed Sidebar that gets a Widget dragged over it.
|
|
|
|
*
|
2017-09-08 15:11:43 -04:00
|
|
|
* @var {element|null}
|
2015-06-30 20:58:24 -04:00
|
|
|
*/
|
|
|
|
hoveredSidebar: null,
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2017-09-08 15:11:43 -04:00
|
|
|
/**
|
|
|
|
* Translations.
|
|
|
|
*
|
|
|
|
* Exported from PHP in wp_default_scripts().
|
|
|
|
*
|
|
|
|
* @var {object}
|
|
|
|
*/
|
|
|
|
l10n: {
|
|
|
|
save: '{save}',
|
|
|
|
saved: '{saved}',
|
|
|
|
saveAlert: '{saveAlert}'
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Lookup of which widgets have had change events triggered.
|
|
|
|
*
|
|
|
|
* @var {object}
|
|
|
|
*/
|
|
|
|
dirtyWidgets: {},
|
|
|
|
|
2012-08-22 20:04:18 -04:00
|
|
|
init : function() {
|
2013-11-08 18:05:10 -05:00
|
|
|
var rem, the_id,
|
|
|
|
self = this,
|
2013-11-24 17:07:10 -05:00
|
|
|
chooser = $('.widgets-chooser'),
|
2013-11-08 18:05:10 -05:00
|
|
|
selectSidebar = chooser.find('.widgets-chooser-sidebars'),
|
|
|
|
sidebars = $('div.widgets-sortables'),
|
2013-11-19 20:35:10 -05:00
|
|
|
isRTL = !! ( 'undefined' !== typeof isRtl && isRtl );
|
2013-11-08 18:05:10 -05:00
|
|
|
|
2017-09-27 12:29:44 -04:00
|
|
|
// Handle the widgets containers in the right column.
|
|
|
|
$( '#widgets-right .sidebar-name' )
|
|
|
|
/*
|
|
|
|
* Toggle the widgets containers when clicked and update the toggle
|
|
|
|
* button `aria-expanded` attribute value.
|
|
|
|
*/
|
|
|
|
.click( function() {
|
|
|
|
var $this = $( this ),
|
|
|
|
$wrap = $this.closest( '.widgets-holder-wrap '),
|
|
|
|
$toggle = $this.find( '.handlediv' );
|
2013-11-19 23:45:10 -05:00
|
|
|
|
2017-09-27 12:29:44 -04:00
|
|
|
if ( $wrap.hasClass( 'closed' ) ) {
|
|
|
|
$wrap.removeClass( 'closed' );
|
|
|
|
$toggle.attr( 'aria-expanded', 'true' );
|
|
|
|
// Refresh the jQuery UI sortable items.
|
|
|
|
$this.parent().sortable( 'refresh' );
|
|
|
|
} else {
|
|
|
|
$wrap.addClass( 'closed' );
|
|
|
|
$toggle.attr( 'aria-expanded', 'false' );
|
|
|
|
}
|
2015-05-09 20:32:31 -04:00
|
|
|
|
2017-09-27 12:29:44 -04:00
|
|
|
// Update the admin menu "sticky" state.
|
|
|
|
$document.triggerHandler( 'wp-pin-menu' );
|
|
|
|
})
|
|
|
|
/*
|
|
|
|
* Set the initial `aria-expanded` attribute value on the widgets
|
|
|
|
* containers toggle button. The first one is expanded by default.
|
|
|
|
*/
|
|
|
|
.find( '.handlediv' ).each( function( index ) {
|
|
|
|
if ( 0 === index ) {
|
|
|
|
// jQuery equivalent of `continue` within an `each()` loop.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$( this ).attr( 'aria-expanded', 'false' );
|
|
|
|
});
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2017-09-08 15:11:43 -04:00
|
|
|
// Show AYS dialog when there are unsaved widget changes.
|
|
|
|
$( window ).on( 'beforeunload.widgets', function( event ) {
|
|
|
|
var dirtyWidgetIds = [], unsavedWidgetsElements;
|
|
|
|
$.each( self.dirtyWidgets, function( widgetId, dirty ) {
|
|
|
|
if ( dirty ) {
|
|
|
|
dirtyWidgetIds.push( widgetId );
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if ( 0 !== dirtyWidgetIds.length ) {
|
|
|
|
unsavedWidgetsElements = $( '#widgets-right' ).find( '.widget' ).filter( function() {
|
|
|
|
return -1 !== dirtyWidgetIds.indexOf( $( this ).prop( 'id' ).replace( /^widget-\d+_/, '' ) );
|
|
|
|
});
|
|
|
|
unsavedWidgetsElements.each( function() {
|
|
|
|
if ( ! $( this ).hasClass( 'open' ) ) {
|
|
|
|
$( this ).find( '.widget-title-action:first' ).click();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Bring the first unsaved widget into view and focus on the first tabbable field.
|
|
|
|
unsavedWidgetsElements.first().each( function() {
|
|
|
|
if ( this.scrollIntoViewIfNeeded ) {
|
|
|
|
this.scrollIntoViewIfNeeded();
|
|
|
|
} else {
|
|
|
|
this.scrollIntoView();
|
|
|
|
}
|
|
|
|
$( this ).find( '.widget-inside :tabbable:first' ).focus();
|
|
|
|
} );
|
|
|
|
|
|
|
|
event.returnValue = wpWidgets.l10n.saveAlert;
|
|
|
|
return event.returnValue;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-09-27 12:29:44 -04:00
|
|
|
// Handle the widgets containers in the left column.
|
|
|
|
$( '#widgets-left .sidebar-name' ).click( function() {
|
|
|
|
var $wrap = $( this ).closest( '.widgets-holder-wrap' );
|
|
|
|
|
|
|
|
$wrap
|
|
|
|
.toggleClass( 'closed' )
|
|
|
|
.find( '.handlediv' ).attr( 'aria-expanded', ! $wrap.hasClass( 'closed' ) );
|
|
|
|
|
|
|
|
// Update the admin menu "sticky" state.
|
2015-05-09 20:32:31 -04:00
|
|
|
$document.triggerHandler( 'wp-pin-menu' );
|
2012-08-22 20:04:18 -04:00
|
|
|
});
|
|
|
|
|
2013-11-26 20:30:10 -05:00
|
|
|
$(document.body).bind('click.widgets-toggle', function(e) {
|
2013-11-20 06:12:11 -05:00
|
|
|
var target = $(e.target),
|
|
|
|
css = { 'z-index': 100 },
|
2017-09-08 15:11:43 -04:00
|
|
|
widget, inside, targetWidth, widgetWidth, margin, saveButton, widgetId,
|
2017-04-19 17:14:44 -04:00
|
|
|
toggleBtn = target.closest( '.widget' ).find( '.widget-top button.widget-action' );
|
2012-08-22 20:04:18 -04:00
|
|
|
|
|
|
|
if ( target.parents('.widget-top').length && ! target.parents('#available-widgets').length ) {
|
|
|
|
widget = target.closest('div.widget');
|
|
|
|
inside = widget.children('.widget-inside');
|
2017-09-08 15:11:43 -04:00
|
|
|
targetWidth = parseInt( widget.find('input.widget-width').val(), 10 );
|
2013-11-26 20:30:10 -05:00
|
|
|
widgetWidth = widget.parent().width();
|
2017-09-08 15:11:43 -04:00
|
|
|
widgetId = inside.find( '.widget-id' ).val();
|
|
|
|
|
|
|
|
// Save button is initially disabled, but is enabled when a field is changed.
|
|
|
|
if ( ! widget.data( 'dirty-state-initialized' ) ) {
|
|
|
|
saveButton = inside.find( '.widget-control-save' );
|
|
|
|
saveButton.prop( 'disabled', true ).val( wpWidgets.l10n.saved );
|
|
|
|
inside.on( 'input change', function() {
|
|
|
|
self.dirtyWidgets[ widgetId ] = true;
|
|
|
|
widget.addClass( 'widget-dirty' );
|
|
|
|
saveButton.prop( 'disabled', false ).val( wpWidgets.l10n.save );
|
|
|
|
});
|
|
|
|
widget.data( 'dirty-state-initialized', true );
|
|
|
|
}
|
2012-08-22 20:04:18 -04:00
|
|
|
|
|
|
|
if ( inside.is(':hidden') ) {
|
2013-11-19 20:35:10 -05:00
|
|
|
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 );
|
2012-08-22 20:04:18 -04:00
|
|
|
}
|
2017-04-19 17:14:44 -04:00
|
|
|
/*
|
|
|
|
* Don't change the order of attributes changes and animation:
|
|
|
|
* it's important for screen readers, see ticket #31476.
|
|
|
|
*/
|
|
|
|
toggleBtn.attr( 'aria-expanded', 'true' );
|
|
|
|
inside.slideDown( 'fast', function() {
|
|
|
|
widget.addClass( 'open' );
|
|
|
|
});
|
2012-08-22 20:04:18 -04:00
|
|
|
} else {
|
2017-04-19 17:14:44 -04:00
|
|
|
/*
|
|
|
|
* Don't change the order of attributes changes and animation:
|
|
|
|
* it's important for screen readers, see ticket #31476.
|
|
|
|
*/
|
|
|
|
toggleBtn.attr( 'aria-expanded', 'false' );
|
|
|
|
inside.slideUp( 'fast', function() {
|
2013-11-19 20:35:10 -05:00
|
|
|
widget.attr( 'style', '' );
|
2014-05-01 10:09:16 -04:00
|
|
|
widget.removeClass( 'open' );
|
2012-08-22 20:04:18 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
e.preventDefault();
|
|
|
|
} else if ( target.hasClass('widget-control-save') ) {
|
|
|
|
wpWidgets.save( target.closest('div.widget'), 0, 1, 0 );
|
|
|
|
e.preventDefault();
|
|
|
|
} else if ( target.hasClass('widget-control-remove') ) {
|
|
|
|
wpWidgets.save( target.closest('div.widget'), 1, 1, 0 );
|
|
|
|
e.preventDefault();
|
|
|
|
} else if ( target.hasClass('widget-control-close') ) {
|
2014-09-10 13:36:15 -04:00
|
|
|
widget = target.closest('div.widget');
|
|
|
|
widget.removeClass( 'open' );
|
2017-04-19 17:14:44 -04:00
|
|
|
toggleBtn.attr( 'aria-expanded', 'false' );
|
2014-09-10 13:36:15 -04:00
|
|
|
wpWidgets.close( widget );
|
2012-08-22 20:04:18 -04:00
|
|
|
e.preventDefault();
|
2015-10-21 01:23:26 -04:00
|
|
|
} else if ( target.attr( 'id' ) === 'inactive-widgets-control-remove' ) {
|
|
|
|
wpWidgets.removeInactiveWidgets();
|
|
|
|
e.preventDefault();
|
2012-08-22 20:04:18 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-11-26 20:30:10 -05:00
|
|
|
sidebars.children('.widget').each( function() {
|
|
|
|
var $this = $(this);
|
|
|
|
|
|
|
|
wpWidgets.appendTitle( this );
|
|
|
|
|
|
|
|
if ( $this.find( 'p.widget-error' ).length ) {
|
2017-04-19 17:14:44 -04:00
|
|
|
$this.find( '.widget-action' ).trigger( 'click' ).attr( 'aria-expanded', 'true' );
|
2013-11-20 06:12:11 -05:00
|
|
|
}
|
2012-08-22 20:04:18 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
$('#widget-list').children('.widget').draggable({
|
|
|
|
connectToSortable: 'div.widgets-sortables',
|
|
|
|
handle: '> .widget-top > .widget-title',
|
|
|
|
distance: 2,
|
|
|
|
helper: 'clone',
|
|
|
|
zIndex: 100,
|
2015-05-09 20:32:31 -04:00
|
|
|
containment: '#wpwrap',
|
2015-06-30 20:58:24 -04:00
|
|
|
refreshPositions: true,
|
2013-11-24 17:07:10 -05:00
|
|
|
start: function( event, ui ) {
|
|
|
|
var chooser = $(this).find('.widgets-chooser');
|
|
|
|
|
2012-08-22 20:04:18 -04:00
|
|
|
ui.helper.find('div.widget-description').hide();
|
|
|
|
the_id = this.id;
|
2013-11-24 17:07:10 -05:00
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
2012-08-22 20:04:18 -04:00
|
|
|
},
|
2013-11-08 18:05:10 -05:00
|
|
|
stop: function() {
|
2013-11-20 06:12:11 -05:00
|
|
|
if ( rem ) {
|
2012-08-22 20:04:18 -04:00
|
|
|
$(rem).hide();
|
2013-11-20 06:12:11 -05:00
|
|
|
}
|
2012-08-22 20:04:18 -04:00
|
|
|
|
|
|
|
rem = '';
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-06-30 20:58:24 -04:00
|
|
|
/**
|
|
|
|
* Opens and closes previously closed Sidebars when Widgets are dragged over/out of them.
|
|
|
|
*/
|
|
|
|
sidebars.droppable( {
|
2015-07-05 14:20:24 -04:00
|
|
|
tolerance: 'intersect',
|
2015-06-30 20:58:24 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Open Sidebar when a Widget gets dragged over it.
|
|
|
|
*
|
2017-09-27 12:29:44 -04:00
|
|
|
* @param {object} event jQuery event object.
|
2015-06-30 20:58:24 -04:00
|
|
|
*/
|
|
|
|
over: function( event ) {
|
|
|
|
var $wrap = $( event.target ).parent();
|
|
|
|
|
|
|
|
if ( wpWidgets.hoveredSidebar && ! $wrap.is( wpWidgets.hoveredSidebar ) ) {
|
|
|
|
// Close the previous Sidebar as the Widget has been dragged onto another Sidebar.
|
|
|
|
wpWidgets.closeSidebar( event );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $wrap.hasClass( 'closed' ) ) {
|
|
|
|
wpWidgets.hoveredSidebar = $wrap;
|
2017-09-27 12:29:44 -04:00
|
|
|
$wrap
|
|
|
|
.removeClass( 'closed' )
|
|
|
|
.find( '.handlediv' ).attr( 'aria-expanded', 'true' );
|
2015-06-30 20:58:24 -04:00
|
|
|
}
|
2015-07-21 21:25:26 -04:00
|
|
|
|
|
|
|
$( this ).sortable( 'refresh' );
|
2015-06-30 20:58:24 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Close Sidebar when the Widget gets dragged out of it.
|
|
|
|
*
|
2017-09-27 12:29:44 -04:00
|
|
|
* @param {object} event jQuery event object.
|
2015-06-30 20:58:24 -04:00
|
|
|
*/
|
|
|
|
out: function( event ) {
|
|
|
|
if ( wpWidgets.hoveredSidebar ) {
|
|
|
|
wpWidgets.closeSidebar( event );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
2012-08-22 20:04:18 -04:00
|
|
|
sidebars.sortable({
|
|
|
|
placeholder: 'widget-placeholder',
|
|
|
|
items: '> .widget',
|
|
|
|
handle: '> .widget-top > .widget-title',
|
|
|
|
cursor: 'move',
|
|
|
|
distance: 2,
|
2015-05-09 20:32:31 -04:00
|
|
|
containment: '#wpwrap',
|
2015-06-03 18:59:25 -04:00
|
|
|
tolerance: 'pointer',
|
2015-07-05 14:20:24 -04:00
|
|
|
refreshPositions: true,
|
2013-11-26 20:30:10 -05:00
|
|
|
start: function( event, ui ) {
|
2013-11-29 12:58:10 -05:00
|
|
|
var height, $this = $(this),
|
2013-11-26 20:30:10 -05:00
|
|
|
$wrap = $this.parent(),
|
|
|
|
inside = ui.item.children('.widget-inside');
|
|
|
|
|
2013-11-24 17:07:10 -05:00
|
|
|
if ( inside.css('display') === 'block' ) {
|
2015-09-14 23:10:24 -04:00
|
|
|
ui.item.removeClass('open');
|
2017-04-19 17:14:44 -04:00
|
|
|
ui.item.find( '.widget-top button.widget-action' ).attr( 'aria-expanded', 'false' );
|
2013-11-24 17:07:10 -05:00
|
|
|
inside.hide();
|
|
|
|
$(this).sortable('refreshPositions');
|
|
|
|
}
|
2013-11-26 20:30:10 -05:00
|
|
|
|
2013-11-29 12:58:10 -05:00
|
|
|
if ( ! $wrap.hasClass('closed') ) {
|
2013-11-26 20:30:10 -05:00
|
|
|
// 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.
|
2013-11-29 12:58:10 -05:00
|
|
|
height = ui.item.hasClass('ui-draggable') ? $this.height() : 1 + $this.height();
|
|
|
|
$this.css( 'min-height', height + 'px' );
|
2013-11-20 06:12:11 -05:00
|
|
|
}
|
2013-11-26 20:30:10 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
stop: function( event, ui ) {
|
|
|
|
var addNew, widgetNumber, $sidebar, $children, child, item,
|
|
|
|
$widget = ui.item,
|
|
|
|
id = the_id;
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2015-06-30 20:58:24 -04:00
|
|
|
// Reset the var to hold a previously closed sidebar.
|
|
|
|
wpWidgets.hoveredSidebar = null;
|
|
|
|
|
2013-11-26 20:30:10 -05:00
|
|
|
if ( $widget.hasClass('deleting') ) {
|
|
|
|
wpWidgets.save( $widget, 1, 0, 1 ); // delete widget
|
|
|
|
$widget.remove();
|
2012-08-22 20:04:18 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-26 20:30:10 -05:00
|
|
|
addNew = $widget.find('input.add_new').val();
|
|
|
|
widgetNumber = $widget.find('input.multi_number').val();
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2013-11-29 12:58:10 -05:00
|
|
|
$widget.attr( 'style', '' ).removeClass('ui-draggable');
|
2012-08-22 20:04:18 -04:00
|
|
|
the_id = '';
|
|
|
|
|
2013-11-26 20:30:10 -05:00
|
|
|
if ( addNew ) {
|
|
|
|
if ( 'multi' === addNew ) {
|
|
|
|
$widget.html(
|
|
|
|
$widget.html().replace( /<[^<>]+>/g, function( tag ) {
|
2013-11-26 20:42:09 -05:00
|
|
|
return tag.replace( /__i__|%i%/g, widgetNumber );
|
2013-11-26 20:30:10 -05:00
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
$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 );
|
2012-08-22 20:04:18 -04:00
|
|
|
rem = 'div#' + id;
|
|
|
|
}
|
2013-11-26 20:30:10 -05:00
|
|
|
|
|
|
|
wpWidgets.save( $widget, 0, 0, 1 );
|
|
|
|
$widget.find('input.add_new').val('');
|
2015-05-09 20:32:31 -04:00
|
|
|
$document.trigger( 'widget-added', [ $widget ] );
|
2012-08-22 20:04:18 -04:00
|
|
|
}
|
2013-11-26 20:30:10 -05:00
|
|
|
|
|
|
|
$sidebar = $widget.parent();
|
|
|
|
|
|
|
|
if ( $sidebar.parent().hasClass('closed') ) {
|
2017-09-27 12:29:44 -04:00
|
|
|
$sidebar.parent()
|
|
|
|
.removeClass( 'closed' )
|
|
|
|
.find( '.handlediv' ).attr( 'aria-expanded', 'true' );
|
|
|
|
|
2013-11-26 20:30:10 -05:00
|
|
|
$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 ) {
|
2017-04-19 17:14:44 -04:00
|
|
|
$widget.find( '.widget-action' ).trigger( 'click' );
|
2013-11-26 20:30:10 -05:00
|
|
|
} else {
|
|
|
|
wpWidgets.saveOrder( $sidebar.attr('id') );
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-11-29 12:58:10 -05:00
|
|
|
activate: function() {
|
|
|
|
$(this).parent().addClass( 'widget-hover' );
|
2012-08-22 20:04:18 -04:00
|
|
|
},
|
|
|
|
|
2013-11-26 20:42:09 -05:00
|
|
|
deactivate: function() {
|
2013-11-26 20:30:10 -05:00
|
|
|
// Remove all min-height added on "start"
|
2013-11-29 12:58:10 -05:00
|
|
|
$(this).css( 'min-height', '' ).parent().removeClass( 'widget-hover' );
|
2013-11-26 20:30:10 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
receive: function( event, ui ) {
|
|
|
|
var $sender = $( ui.sender );
|
|
|
|
|
|
|
|
// Don't add more widgets to orphaned sidebars
|
|
|
|
if ( this.id.indexOf('orphaned_widgets') > -1 ) {
|
|
|
|
$sender.sortable('cancel');
|
|
|
|
return;
|
2013-11-20 06:12:11 -05:00
|
|
|
}
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2013-11-26 20:30:10 -05:00
|
|
|
// 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(); } );
|
2012-08-22 20:04:18 -04:00
|
|
|
}
|
|
|
|
}
|
2013-11-26 20:30:10 -05:00
|
|
|
}).sortable( 'option', 'connectWith', 'div.widgets-sortables' );
|
2012-08-22 20:04:18 -04:00
|
|
|
|
|
|
|
$('#available-widgets').droppable({
|
|
|
|
tolerance: 'pointer',
|
|
|
|
accept: function(o){
|
2013-11-08 18:05:10 -05:00
|
|
|
return $(o).parent().attr('id') !== 'widget-list';
|
2012-08-22 20:04:18 -04:00
|
|
|
},
|
|
|
|
drop: function(e,ui) {
|
|
|
|
ui.draggable.addClass('deleting');
|
2015-03-09 15:44:26 -04:00
|
|
|
$('#removing-widget').hide().children('span').empty();
|
2012-08-22 20:04:18 -04:00
|
|
|
},
|
|
|
|
over: function(e,ui) {
|
|
|
|
ui.draggable.addClass('deleting');
|
|
|
|
$('div.widget-placeholder').hide();
|
|
|
|
|
2013-11-20 06:12:11 -05:00
|
|
|
if ( ui.draggable.hasClass('ui-sortable-helper') ) {
|
2012-08-22 20:04:18 -04:00
|
|
|
$('#removing-widget').show().children('span')
|
2015-10-10 12:34:25 -04:00
|
|
|
.html( ui.draggable.find( 'div.widget-title' ).children( 'h3' ).html() );
|
2013-11-20 06:12:11 -05:00
|
|
|
}
|
2012-08-22 20:04:18 -04:00
|
|
|
},
|
|
|
|
out: function(e,ui) {
|
|
|
|
ui.draggable.removeClass('deleting');
|
|
|
|
$('div.widget-placeholder').show();
|
2015-03-09 15:44:26 -04:00
|
|
|
$('#removing-widget').hide().children('span').empty();
|
2012-08-22 20:04:18 -04:00
|
|
|
}
|
|
|
|
});
|
2013-11-08 18:05:10 -05:00
|
|
|
|
|
|
|
// Area Chooser
|
|
|
|
$( '#widgets-right .widgets-holder-wrap' ).each( function( index, element ) {
|
|
|
|
var $element = $( element ),
|
2015-10-10 12:34:25 -04:00
|
|
|
name = $element.find( '.sidebar-name h2' ).text(),
|
2013-11-08 18:05:10 -05:00
|
|
|
id = $element.find( '.widgets-sortables' ).attr( 'id' ),
|
|
|
|
li = $('<li tabindex="0">').text( $.trim( name ) );
|
|
|
|
|
|
|
|
if ( index === 0 ) {
|
|
|
|
li.addClass( 'widgets-chooser-selected' );
|
|
|
|
}
|
|
|
|
|
|
|
|
selectSidebar.append( li );
|
|
|
|
li.data( 'sidebarId', id );
|
|
|
|
});
|
|
|
|
|
|
|
|
$( '#available-widgets .widget .widget-title' ).on( 'click.widgets-chooser', function() {
|
2013-11-26 20:30:10 -05:00
|
|
|
var $widget = $(this).closest( '.widget' );
|
2013-11-08 18:05:10 -05:00
|
|
|
|
2013-11-26 20:30:10 -05:00
|
|
|
if ( $widget.hasClass( 'widget-in-question' ) || $( '#widgets-left' ).hasClass( 'chooser' ) ) {
|
2013-11-08 18:05:10 -05:00
|
|
|
self.closeChooser();
|
|
|
|
} else {
|
|
|
|
// Open the chooser
|
|
|
|
self.clearWidgetSelection();
|
|
|
|
$( '#widgets-left' ).addClass( 'chooser' );
|
2013-11-26 20:30:10 -05:00
|
|
|
$widget.addClass( 'widget-in-question' ).children( '.widget-description' ).after( chooser );
|
2013-11-08 18:05:10 -05:00
|
|
|
|
2013-11-19 13:03:09 -05:00
|
|
|
chooser.slideDown( 300, function() {
|
2013-11-08 18:05:10 -05:00
|
|
|
selectSidebar.find('.widgets-chooser-selected').focus();
|
|
|
|
});
|
2013-11-19 13:03:09 -05:00
|
|
|
|
|
|
|
selectSidebar.find( 'li' ).on( 'focusin.widgets-chooser', function() {
|
|
|
|
selectSidebar.find('.widgets-chooser-selected').removeClass( 'widgets-chooser-selected' );
|
|
|
|
$(this).addClass( 'widgets-chooser-selected' );
|
2013-11-20 06:12:11 -05:00
|
|
|
} );
|
2013-11-08 18:05:10 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Add event handlers
|
|
|
|
chooser.on( 'click.widgets-chooser', function( event ) {
|
|
|
|
var $target = $( event.target );
|
|
|
|
|
|
|
|
if ( $target.hasClass('button-primary') ) {
|
|
|
|
self.addWidget( chooser );
|
|
|
|
self.closeChooser();
|
2016-09-28 15:54:28 -04:00
|
|
|
} else if ( $target.hasClass( 'widgets-chooser-cancel' ) ) {
|
2013-11-08 18:05:10 -05:00
|
|
|
self.closeChooser();
|
2013-11-19 13:03:09 -05:00
|
|
|
}
|
|
|
|
}).on( 'keyup.widgets-chooser', function( event ) {
|
|
|
|
if ( event.which === $.ui.keyCode.ENTER ) {
|
2016-09-28 15:54:28 -04:00
|
|
|
if ( $( event.target ).hasClass( 'widgets-chooser-cancel' ) ) {
|
2013-11-19 13:03:09 -05:00
|
|
|
// Close instead of adding when pressing Enter on the Cancel button
|
|
|
|
self.closeChooser();
|
|
|
|
} else {
|
|
|
|
self.addWidget( chooser );
|
|
|
|
self.closeChooser();
|
|
|
|
}
|
|
|
|
} else if ( event.which === $.ui.keyCode.ESCAPE ) {
|
|
|
|
self.closeChooser();
|
2013-11-08 18:05:10 -05:00
|
|
|
}
|
|
|
|
});
|
2012-08-22 20:04:18 -04:00
|
|
|
},
|
|
|
|
|
2013-11-26 20:30:10 -05:00
|
|
|
saveOrder : function( sidebarId ) {
|
|
|
|
var data = {
|
2012-08-22 20:04:18 -04:00
|
|
|
action: 'widgets-order',
|
|
|
|
savewidgets: $('#_wpnonce_widgets').val(),
|
|
|
|
sidebars: []
|
|
|
|
};
|
|
|
|
|
2013-11-26 20:30:10 -05:00
|
|
|
if ( sidebarId ) {
|
2015-04-03 00:52:27 -04:00
|
|
|
$( '#' + sidebarId ).find( '.spinner:first' ).addClass( 'is-active' );
|
2013-11-26 20:30:10 -05:00
|
|
|
}
|
|
|
|
|
2012-08-22 20:04:18 -04:00
|
|
|
$('div.widgets-sortables').each( function() {
|
2013-11-20 06:12:11 -05:00
|
|
|
if ( $(this).sortable ) {
|
2013-11-26 20:30:10 -05:00
|
|
|
data['sidebars[' + $(this).attr('id') + ']'] = $(this).sortable('toArray').join(',');
|
2013-11-20 06:12:11 -05:00
|
|
|
}
|
2012-08-22 20:04:18 -04:00
|
|
|
});
|
|
|
|
|
2013-11-26 20:30:10 -05:00
|
|
|
$.post( ajaxurl, data, function() {
|
2015-10-21 01:23:26 -04:00
|
|
|
$( '#inactive-widgets-control-remove' ).prop( 'disabled' , ! $( '#wp_inactive_widgets .widget' ).length );
|
2015-04-03 00:52:27 -04:00
|
|
|
$( '.spinner' ).removeClass( 'is-active' );
|
2012-08-22 20:04:18 -04:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2013-11-26 20:30:10 -05:00
|
|
|
save : function( widget, del, animate, order ) {
|
2017-09-08 15:11:43 -04:00
|
|
|
var self = this, data, a,
|
|
|
|
sidebarId = widget.closest( 'div.widgets-sortables' ).attr( 'id' ),
|
2018-01-16 20:31:41 -05:00
|
|
|
form = widget.find( 'form' ),
|
|
|
|
isAdd = widget.find( 'input.add_new' ).val();
|
2017-09-08 15:11:43 -04:00
|
|
|
|
2018-01-16 20:31:41 -05:00
|
|
|
if ( ! del && ! isAdd && form.prop( 'checkValidity' ) && ! form[0].checkValidity() ) {
|
2017-09-08 15:11:43 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
data = form.serialize();
|
2013-11-26 20:42:09 -05:00
|
|
|
|
2012-08-22 20:04:18 -04:00
|
|
|
widget = $(widget);
|
2015-04-03 00:52:27 -04:00
|
|
|
$( '.spinner', widget ).addClass( 'is-active' );
|
2012-08-22 20:04:18 -04:00
|
|
|
|
|
|
|
a = {
|
|
|
|
action: 'save-widget',
|
|
|
|
savewidgets: $('#_wpnonce_widgets').val(),
|
2013-11-26 20:30:10 -05:00
|
|
|
sidebar: sidebarId
|
2012-08-22 20:04:18 -04:00
|
|
|
};
|
|
|
|
|
2013-11-20 06:12:11 -05:00
|
|
|
if ( del ) {
|
2013-11-08 18:05:10 -05:00
|
|
|
a.delete_widget = 1;
|
2013-11-20 06:12:11 -05:00
|
|
|
}
|
2012-08-22 20:04:18 -04:00
|
|
|
|
|
|
|
data += '&' + $.param(a);
|
|
|
|
|
2013-11-26 20:30:10 -05:00
|
|
|
$.post( ajaxurl, data, function(r) {
|
2017-09-08 15:11:43 -04:00
|
|
|
var id = $('input.widget-id', widget).val();
|
2012-08-22 20:04:18 -04:00
|
|
|
|
|
|
|
if ( del ) {
|
2013-11-20 06:12:11 -05:00
|
|
|
if ( ! $('input.widget_number', widget).val() ) {
|
2012-08-22 20:04:18 -04:00
|
|
|
$('#available-widgets').find('input.widget-id').each(function(){
|
2013-11-20 06:12:11 -05:00
|
|
|
if ( $(this).val() === id ) {
|
2012-08-22 20:04:18 -04:00
|
|
|
$(this).closest('div.widget').show();
|
2013-11-20 06:12:11 -05:00
|
|
|
}
|
2012-08-22 20:04:18 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( animate ) {
|
|
|
|
order = 0;
|
2017-10-10 16:39:47 -04:00
|
|
|
widget.slideUp( 'fast', function() {
|
|
|
|
$( this ).remove();
|
2012-08-22 20:04:18 -04:00
|
|
|
wpWidgets.saveOrder();
|
2017-10-10 16:39:47 -04:00
|
|
|
delete self.dirtyWidgets[ id ];
|
2012-08-22 20:04:18 -04:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
widget.remove();
|
2017-10-10 16:39:47 -04:00
|
|
|
delete self.dirtyWidgets[ id ];
|
2015-10-21 01:23:26 -04:00
|
|
|
|
|
|
|
if ( sidebarId === 'wp_inactive_widgets' ) {
|
|
|
|
$( '#inactive-widgets-control-remove' ).prop( 'disabled' , ! $( '#wp_inactive_widgets .widget' ).length );
|
|
|
|
}
|
2012-08-22 20:04:18 -04:00
|
|
|
}
|
|
|
|
} else {
|
2015-04-03 00:52:27 -04:00
|
|
|
$( '.spinner' ).removeClass( 'is-active' );
|
2012-08-22 20:04:18 -04:00
|
|
|
if ( r && r.length > 2 ) {
|
2014-04-06 15:06:14 -04:00
|
|
|
$( 'div.widget-content', widget ).html( r );
|
2013-11-26 20:30:10 -05:00
|
|
|
wpWidgets.appendTitle( widget );
|
2017-09-08 15:11:43 -04:00
|
|
|
|
|
|
|
// Re-disable the save button.
|
|
|
|
widget.find( '.widget-control-save' ).prop( 'disabled', true ).val( wpWidgets.l10n.saved );
|
|
|
|
|
|
|
|
widget.removeClass( 'widget-dirty' );
|
|
|
|
|
|
|
|
// Clear the dirty flag from the widget.
|
|
|
|
delete self.dirtyWidgets[ id ];
|
|
|
|
|
2015-05-09 20:32:31 -04:00
|
|
|
$document.trigger( 'widget-updated', [ widget ] );
|
2015-10-21 01:23:26 -04:00
|
|
|
|
|
|
|
if ( sidebarId === 'wp_inactive_widgets' ) {
|
|
|
|
$( '#inactive-widgets-control-remove' ).prop( 'disabled' , ! $( '#wp_inactive_widgets .widget' ).length );
|
|
|
|
}
|
2012-08-22 20:04:18 -04:00
|
|
|
}
|
|
|
|
}
|
2015-10-21 01:23:26 -04:00
|
|
|
|
2013-11-20 06:12:11 -05:00
|
|
|
if ( order ) {
|
2012-08-22 20:04:18 -04:00
|
|
|
wpWidgets.saveOrder();
|
2013-11-20 06:12:11 -05:00
|
|
|
}
|
2012-08-22 20:04:18 -04:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2015-10-21 01:23:26 -04:00
|
|
|
removeInactiveWidgets : function() {
|
2017-10-10 16:39:47 -04:00
|
|
|
var $element = $( '.remove-inactive-widgets' ), self = this, a, data;
|
2015-10-21 01:23:26 -04:00
|
|
|
|
|
|
|
$( '.spinner', $element ).addClass( 'is-active' );
|
|
|
|
|
|
|
|
a = {
|
|
|
|
action : 'delete-inactive-widgets',
|
|
|
|
removeinactivewidgets : $( '#_wpnonce_remove_inactive_widgets' ).val()
|
|
|
|
};
|
|
|
|
|
|
|
|
data = $.param( a );
|
|
|
|
|
|
|
|
$.post( ajaxurl, data, function() {
|
2017-10-10 16:39:47 -04:00
|
|
|
$( '#wp_inactive_widgets .widget' ).each(function() {
|
|
|
|
var $widget = $( this );
|
|
|
|
delete self.dirtyWidgets[ $widget.find( 'input.widget-id' ).val() ];
|
|
|
|
$widget.remove();
|
|
|
|
});
|
|
|
|
$( '#inactive-widgets-control-remove' ).prop( 'disabled', true );
|
2015-10-21 01:23:26 -04:00
|
|
|
$( '.spinner', $element ).removeClass( 'is-active' );
|
|
|
|
} );
|
|
|
|
},
|
|
|
|
|
2012-08-22 20:04:18 -04:00
|
|
|
appendTitle : function(widget) {
|
|
|
|
var title = $('input[id*="-title"]', widget).val() || '';
|
|
|
|
|
2013-11-20 06:12:11 -05:00
|
|
|
if ( title ) {
|
2012-08-22 20:04:18 -04:00
|
|
|
title = ': ' + title.replace(/<[^<>]+>/g, '').replace(/</g, '<').replace(/>/g, '>');
|
2013-11-20 06:12:11 -05:00
|
|
|
}
|
2012-08-22 20:04:18 -04:00
|
|
|
|
|
|
|
$(widget).children('.widget-top').children('.widget-title').children()
|
|
|
|
.children('.in-widget-title').html(title);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
close : function(widget) {
|
2013-11-19 20:35:10 -05:00
|
|
|
widget.children('.widget-inside').slideUp('fast', function() {
|
2017-04-19 17:14:44 -04:00
|
|
|
widget.attr( 'style', '' )
|
|
|
|
.find( '.widget-top button.widget-action' )
|
|
|
|
.attr( 'aria-expanded', 'false' )
|
|
|
|
.focus();
|
2012-08-22 20:04:18 -04:00
|
|
|
});
|
2013-11-08 18:05:10 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
addWidget: function( chooser ) {
|
2013-12-05 18:14:09 -05:00
|
|
|
var widget, widgetId, add, n, viewportTop, viewportBottom, sidebarBounds,
|
2013-11-08 18:05:10 -05:00
|
|
|
sidebarId = chooser.find( '.widgets-chooser-selected' ).data('sidebarId'),
|
|
|
|
sidebar = $( '#' + sidebarId );
|
|
|
|
|
2013-11-19 13:03:09 -05:00
|
|
|
widget = $('#available-widgets').find('.widget-in-question').clone();
|
|
|
|
widgetId = widget.attr('id');
|
|
|
|
add = widget.find( 'input.add_new' ).val();
|
|
|
|
n = widget.find( 'input.multi_number' ).val();
|
|
|
|
|
2013-12-04 19:02:11 -05:00
|
|
|
// Remove the cloned chooser from the widget
|
|
|
|
widget.find('.widgets-chooser').remove();
|
|
|
|
|
2013-11-08 18:05:10 -05:00
|
|
|
if ( 'multi' === add ) {
|
|
|
|
widget.html(
|
|
|
|
widget.html().replace( /<[^<>]+>/g, function(m) {
|
|
|
|
return m.replace( /__i__|%i%/g, n );
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
widget.attr( 'id', widgetId.replace( '__i__', n ) );
|
|
|
|
n++;
|
|
|
|
$( '#' + widgetId ).find('input.multi_number').val(n);
|
|
|
|
} else if ( 'single' === add ) {
|
|
|
|
widget.attr( 'id', 'new-' + widgetId );
|
|
|
|
$( '#' + widgetId ).hide();
|
|
|
|
}
|
|
|
|
|
2017-09-27 12:29:44 -04:00
|
|
|
// Open the widgets container.
|
|
|
|
sidebar.closest( '.widgets-holder-wrap' )
|
|
|
|
.removeClass( 'closed' )
|
|
|
|
.find( '.handlediv' ).attr( 'aria-expanded', 'true' );
|
2013-11-08 18:05:10 -05:00
|
|
|
|
2013-12-05 17:27:11 -05:00
|
|
|
sidebar.append( widget );
|
2013-11-26 20:30:10 -05:00
|
|
|
sidebar.sortable('refresh');
|
2013-11-08 18:05:10 -05:00
|
|
|
|
|
|
|
wpWidgets.save( widget, 0, 0, 1 );
|
|
|
|
// No longer "new" widget
|
|
|
|
widget.find( 'input.add_new' ).val('');
|
|
|
|
|
2015-05-09 20:32:31 -04:00
|
|
|
$document.trigger( 'widget-added', [ widget ] );
|
2014-04-06 15:06:14 -04:00
|
|
|
|
2013-12-11 14:49:11 -05:00
|
|
|
/*
|
2013-12-05 17:22:11 -05:00
|
|
|
* Check if any part of the sidebar is visible in the viewport. If it is, don't scroll.
|
|
|
|
* Otherwise, scroll up to so the sidebar is in view.
|
|
|
|
*
|
|
|
|
* We do this by comparing the top and bottom, of the sidebar so see if they are within
|
|
|
|
* the bounds of the viewport.
|
|
|
|
*/
|
2013-12-05 18:14:09 -05:00
|
|
|
viewportTop = $(window).scrollTop();
|
|
|
|
viewportBottom = viewportTop + $(window).height();
|
|
|
|
sidebarBounds = sidebar.offset();
|
2013-12-11 14:49:11 -05:00
|
|
|
|
2013-12-05 18:14:09 -05:00
|
|
|
sidebarBounds.bottom = sidebarBounds.top + sidebar.outerHeight();
|
2013-12-05 17:22:11 -05:00
|
|
|
|
2013-12-05 18:14:09 -05:00
|
|
|
if ( viewportTop > sidebarBounds.bottom || viewportBottom < sidebarBounds.top ) {
|
2013-12-05 17:22:11 -05:00
|
|
|
$( 'html, body' ).animate({
|
2013-12-05 18:14:09 -05:00
|
|
|
scrollTop: sidebarBounds.top - 130
|
2013-12-05 17:22:11 -05:00
|
|
|
}, 200 );
|
|
|
|
}
|
2013-11-08 18:05:10 -05:00
|
|
|
|
|
|
|
window.setTimeout( function() {
|
|
|
|
// Cannot use a callback in the animation above as it fires twice,
|
|
|
|
// have to queue this "by hand".
|
|
|
|
widget.find( '.widget-title' ).trigger('click');
|
|
|
|
}, 250 );
|
|
|
|
},
|
|
|
|
|
|
|
|
closeChooser: function() {
|
|
|
|
var self = this;
|
|
|
|
|
2013-11-24 17:07:10 -05:00
|
|
|
$( '.widgets-chooser' ).slideUp( 200, function() {
|
2013-11-21 14:28:10 -05:00
|
|
|
$( '#wpbody-content' ).append( this );
|
2013-11-08 18:05:10 -05:00
|
|
|
self.clearWidgetSelection();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
clearWidgetSelection: function() {
|
|
|
|
$( '#widgets-left' ).removeClass( 'chooser' );
|
2013-11-19 13:03:09 -05:00
|
|
|
$( '.widget-in-question' ).removeClass( 'widget-in-question' );
|
2015-06-30 20:58:24 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Closes a Sidebar that was previously closed, but opened by dragging a Widget over it.
|
|
|
|
*
|
|
|
|
* Used when a Widget gets dragged in/out of the Sidebar and never dropped.
|
|
|
|
*
|
2017-09-27 12:29:44 -04:00
|
|
|
* @param {object} event jQuery event object.
|
2015-06-30 20:58:24 -04:00
|
|
|
*/
|
2017-09-27 12:29:44 -04:00
|
|
|
closeSidebar: function( event ) {
|
|
|
|
this.hoveredSidebar
|
|
|
|
.addClass( 'closed' )
|
|
|
|
.find( '.handlediv' ).attr( 'aria-expanded', 'false' );
|
|
|
|
|
|
|
|
$( event.target ).css( 'min-height', '' );
|
2015-06-30 20:58:24 -04:00
|
|
|
this.hoveredSidebar = null;
|
2012-08-22 20:04:18 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-05-09 20:32:31 -04:00
|
|
|
$document.ready( function(){ wpWidgets.init(); } );
|
2012-08-22 20:04:18 -04:00
|
|
|
|
|
|
|
})(jQuery);
|