2013-11-09 00:05:10 +01:00
|
|
|
/*global ajaxurl, isRtl */
|
2012-08-23 02:04:18 +02:00
|
|
|
var wpWidgets;
|
|
|
|
(function($) {
|
2015-05-10 02:32:31 +02:00
|
|
|
var $document = $( document );
|
2012-08-23 02:04:18 +02:00
|
|
|
|
|
|
|
wpWidgets = {
|
2015-07-01 02:58:24 +02:00
|
|
|
/**
|
|
|
|
* A closed Sidebar that gets a Widget dragged over it.
|
|
|
|
*
|
|
|
|
* @var element|null
|
|
|
|
*/
|
|
|
|
hoveredSidebar: null,
|
2012-08-23 02:04:18 +02:00
|
|
|
|
|
|
|
init : function() {
|
2013-11-09 00:05:10 +01:00
|
|
|
var rem, the_id,
|
|
|
|
self = this,
|
2013-11-24 23:07:10 +01:00
|
|
|
chooser = $('.widgets-chooser'),
|
2013-11-09 00:05:10 +01:00
|
|
|
selectSidebar = chooser.find('.widgets-chooser-sidebars'),
|
|
|
|
sidebars = $('div.widgets-sortables'),
|
2013-11-20 02:35:10 +01:00
|
|
|
isRTL = !! ( 'undefined' !== typeof isRtl && isRtl );
|
2013-11-09 00:05:10 +01:00
|
|
|
|
2013-11-20 05:45:10 +01:00
|
|
|
$('#widgets-right .sidebar-name').click( function() {
|
2013-11-27 02:30:10 +01:00
|
|
|
var $this = $(this),
|
|
|
|
$wrap = $this.closest('.widgets-holder-wrap');
|
2013-11-20 05:45:10 +01:00
|
|
|
|
2013-11-27 02:30:10 +01:00
|
|
|
if ( $wrap.hasClass('closed') ) {
|
|
|
|
$wrap.removeClass('closed');
|
|
|
|
$this.parent().sortable('refresh');
|
2012-08-23 02:04:18 +02:00
|
|
|
} else {
|
2013-11-27 02:30:10 +01:00
|
|
|
$wrap.addClass('closed');
|
2012-08-23 02:04:18 +02:00
|
|
|
}
|
2015-05-10 02:32:31 +02:00
|
|
|
|
|
|
|
$document.triggerHandler( 'wp-pin-menu' );
|
2012-08-23 02:04:18 +02:00
|
|
|
});
|
|
|
|
|
2013-11-27 02:30:10 +01:00
|
|
|
$('#widgets-left .sidebar-name').click( function() {
|
|
|
|
$(this).closest('.widgets-holder-wrap').toggleClass('closed');
|
2015-05-10 02:32:31 +02:00
|
|
|
$document.triggerHandler( 'wp-pin-menu' );
|
2012-08-23 02:04:18 +02:00
|
|
|
});
|
|
|
|
|
2013-11-27 02:30:10 +01:00
|
|
|
$(document.body).bind('click.widgets-toggle', function(e) {
|
2013-11-20 12:12:11 +01:00
|
|
|
var target = $(e.target),
|
|
|
|
css = { 'z-index': 100 },
|
2017-04-19 23:14:44 +02:00
|
|
|
widget, inside, targetWidth, widgetWidth, margin,
|
|
|
|
toggleBtn = target.closest( '.widget' ).find( '.widget-top button.widget-action' );
|
2012-08-23 02:04:18 +02:00
|
|
|
|
|
|
|
if ( target.parents('.widget-top').length && ! target.parents('#available-widgets').length ) {
|
|
|
|
widget = target.closest('div.widget');
|
|
|
|
inside = widget.children('.widget-inside');
|
2013-11-20 02:35:10 +01:00
|
|
|
targetWidth = parseInt( widget.find('input.widget-width').val(), 10 ),
|
2013-11-27 02:30:10 +01:00
|
|
|
widgetWidth = widget.parent().width();
|
2012-08-23 02:04:18 +02:00
|
|
|
|
|
|
|
if ( inside.is(':hidden') ) {
|
2013-11-20 02:35:10 +01: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-23 02:04:18 +02:00
|
|
|
}
|
2017-04-19 23:14:44 +02: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-23 02:04:18 +02:00
|
|
|
} else {
|
2017-04-19 23:14:44 +02: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-20 02:35:10 +01:00
|
|
|
widget.attr( 'style', '' );
|
2014-05-01 16:09:16 +02:00
|
|
|
widget.removeClass( 'open' );
|
2012-08-23 02:04:18 +02: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 19:36:15 +02:00
|
|
|
widget = target.closest('div.widget');
|
|
|
|
widget.removeClass( 'open' );
|
2017-04-19 23:14:44 +02:00
|
|
|
toggleBtn.attr( 'aria-expanded', 'false' );
|
2014-09-10 19:36:15 +02:00
|
|
|
wpWidgets.close( widget );
|
2012-08-23 02:04:18 +02:00
|
|
|
e.preventDefault();
|
2015-10-21 07:23:26 +02:00
|
|
|
} else if ( target.attr( 'id' ) === 'inactive-widgets-control-remove' ) {
|
|
|
|
wpWidgets.removeInactiveWidgets();
|
|
|
|
e.preventDefault();
|
2012-08-23 02:04:18 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-11-27 02:30:10 +01:00
|
|
|
sidebars.children('.widget').each( function() {
|
|
|
|
var $this = $(this);
|
|
|
|
|
|
|
|
wpWidgets.appendTitle( this );
|
|
|
|
|
|
|
|
if ( $this.find( 'p.widget-error' ).length ) {
|
2017-04-19 23:14:44 +02:00
|
|
|
$this.find( '.widget-action' ).trigger( 'click' ).attr( 'aria-expanded', 'true' );
|
2013-11-20 12:12:11 +01:00
|
|
|
}
|
2012-08-23 02:04:18 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
$('#widget-list').children('.widget').draggable({
|
|
|
|
connectToSortable: 'div.widgets-sortables',
|
|
|
|
handle: '> .widget-top > .widget-title',
|
|
|
|
distance: 2,
|
|
|
|
helper: 'clone',
|
|
|
|
zIndex: 100,
|
2015-05-10 02:32:31 +02:00
|
|
|
containment: '#wpwrap',
|
2015-07-01 02:58:24 +02:00
|
|
|
refreshPositions: true,
|
2013-11-24 23:07:10 +01:00
|
|
|
start: function( event, ui ) {
|
|
|
|
var chooser = $(this).find('.widgets-chooser');
|
|
|
|
|
2012-08-23 02:04:18 +02:00
|
|
|
ui.helper.find('div.widget-description').hide();
|
|
|
|
the_id = this.id;
|
2013-11-24 23:07:10 +01: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-23 02:04:18 +02:00
|
|
|
},
|
2013-11-09 00:05:10 +01:00
|
|
|
stop: function() {
|
2013-11-20 12:12:11 +01:00
|
|
|
if ( rem ) {
|
2012-08-23 02:04:18 +02:00
|
|
|
$(rem).hide();
|
2013-11-20 12:12:11 +01:00
|
|
|
}
|
2012-08-23 02:04:18 +02:00
|
|
|
|
|
|
|
rem = '';
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-07-01 02:58:24 +02:00
|
|
|
/**
|
|
|
|
* Opens and closes previously closed Sidebars when Widgets are dragged over/out of them.
|
|
|
|
*/
|
|
|
|
sidebars.droppable( {
|
2015-07-05 20:20:24 +02:00
|
|
|
tolerance: 'intersect',
|
2015-07-01 02:58:24 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Open Sidebar when a Widget gets dragged over it.
|
|
|
|
*
|
|
|
|
* @param event
|
|
|
|
*/
|
|
|
|
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;
|
|
|
|
$wrap.removeClass( 'closed' );
|
|
|
|
}
|
2015-07-22 03:25:26 +02:00
|
|
|
|
|
|
|
$( this ).sortable( 'refresh' );
|
2015-07-01 02:58:24 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Close Sidebar when the Widget gets dragged out of it.
|
|
|
|
*
|
|
|
|
* @param event
|
|
|
|
*/
|
|
|
|
out: function( event ) {
|
|
|
|
if ( wpWidgets.hoveredSidebar ) {
|
|
|
|
wpWidgets.closeSidebar( event );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
2012-08-23 02:04:18 +02:00
|
|
|
sidebars.sortable({
|
|
|
|
placeholder: 'widget-placeholder',
|
|
|
|
items: '> .widget',
|
|
|
|
handle: '> .widget-top > .widget-title',
|
|
|
|
cursor: 'move',
|
|
|
|
distance: 2,
|
2015-05-10 02:32:31 +02:00
|
|
|
containment: '#wpwrap',
|
2015-06-04 00:59:25 +02:00
|
|
|
tolerance: 'pointer',
|
2015-07-05 20:20:24 +02:00
|
|
|
refreshPositions: true,
|
2013-11-27 02:30:10 +01:00
|
|
|
start: function( event, ui ) {
|
2013-11-29 18:58:10 +01:00
|
|
|
var height, $this = $(this),
|
2013-11-27 02:30:10 +01:00
|
|
|
$wrap = $this.parent(),
|
|
|
|
inside = ui.item.children('.widget-inside');
|
|
|
|
|
2013-11-24 23:07:10 +01:00
|
|
|
if ( inside.css('display') === 'block' ) {
|
2015-09-15 05:10:24 +02:00
|
|
|
ui.item.removeClass('open');
|
2017-04-19 23:14:44 +02:00
|
|
|
ui.item.find( '.widget-top button.widget-action' ).attr( 'aria-expanded', 'false' );
|
2013-11-24 23:07:10 +01:00
|
|
|
inside.hide();
|
|
|
|
$(this).sortable('refreshPositions');
|
|
|
|
}
|
2013-11-27 02:30:10 +01:00
|
|
|
|
2013-11-29 18:58:10 +01:00
|
|
|
if ( ! $wrap.hasClass('closed') ) {
|
2013-11-27 02:30:10 +01: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 18:58:10 +01:00
|
|
|
height = ui.item.hasClass('ui-draggable') ? $this.height() : 1 + $this.height();
|
|
|
|
$this.css( 'min-height', height + 'px' );
|
2013-11-20 12:12:11 +01:00
|
|
|
}
|
2013-11-27 02:30:10 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
stop: function( event, ui ) {
|
|
|
|
var addNew, widgetNumber, $sidebar, $children, child, item,
|
|
|
|
$widget = ui.item,
|
|
|
|
id = the_id;
|
2012-08-23 02:04:18 +02:00
|
|
|
|
2015-07-01 02:58:24 +02:00
|
|
|
// Reset the var to hold a previously closed sidebar.
|
|
|
|
wpWidgets.hoveredSidebar = null;
|
|
|
|
|
2013-11-27 02:30:10 +01:00
|
|
|
if ( $widget.hasClass('deleting') ) {
|
|
|
|
wpWidgets.save( $widget, 1, 0, 1 ); // delete widget
|
|
|
|
$widget.remove();
|
2012-08-23 02:04:18 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-27 02:30:10 +01:00
|
|
|
addNew = $widget.find('input.add_new').val();
|
|
|
|
widgetNumber = $widget.find('input.multi_number').val();
|
2012-08-23 02:04:18 +02:00
|
|
|
|
2013-11-29 18:58:10 +01:00
|
|
|
$widget.attr( 'style', '' ).removeClass('ui-draggable');
|
2012-08-23 02:04:18 +02:00
|
|
|
the_id = '';
|
|
|
|
|
2013-11-27 02:30:10 +01:00
|
|
|
if ( addNew ) {
|
|
|
|
if ( 'multi' === addNew ) {
|
|
|
|
$widget.html(
|
|
|
|
$widget.html().replace( /<[^<>]+>/g, function( tag ) {
|
2013-11-27 02:42:09 +01:00
|
|
|
return tag.replace( /__i__|%i%/g, widgetNumber );
|
2013-11-27 02:30:10 +01: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-23 02:04:18 +02:00
|
|
|
rem = 'div#' + id;
|
|
|
|
}
|
2013-11-27 02:30:10 +01:00
|
|
|
|
|
|
|
wpWidgets.save( $widget, 0, 0, 1 );
|
|
|
|
$widget.find('input.add_new').val('');
|
2015-05-10 02:32:31 +02:00
|
|
|
$document.trigger( 'widget-added', [ $widget ] );
|
2012-08-23 02:04:18 +02:00
|
|
|
}
|
2013-11-27 02:30:10 +01:00
|
|
|
|
|
|
|
$sidebar = $widget.parent();
|
|
|
|
|
|
|
|
if ( $sidebar.parent().hasClass('closed') ) {
|
2013-11-29 18:58:10 +01:00
|
|
|
$sidebar.parent().removeClass('closed');
|
2013-11-27 02:30:10 +01: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 23:14:44 +02:00
|
|
|
$widget.find( '.widget-action' ).trigger( 'click' );
|
2013-11-27 02:30:10 +01:00
|
|
|
} else {
|
|
|
|
wpWidgets.saveOrder( $sidebar.attr('id') );
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-11-29 18:58:10 +01:00
|
|
|
activate: function() {
|
|
|
|
$(this).parent().addClass( 'widget-hover' );
|
2012-08-23 02:04:18 +02:00
|
|
|
},
|
|
|
|
|
2013-11-27 02:42:09 +01:00
|
|
|
deactivate: function() {
|
2013-11-27 02:30:10 +01:00
|
|
|
// Remove all min-height added on "start"
|
2013-11-29 18:58:10 +01:00
|
|
|
$(this).css( 'min-height', '' ).parent().removeClass( 'widget-hover' );
|
2013-11-27 02:30:10 +01: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 12:12:11 +01:00
|
|
|
}
|
2012-08-23 02:04:18 +02:00
|
|
|
|
2013-11-27 02:30:10 +01: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-23 02:04:18 +02:00
|
|
|
}
|
|
|
|
}
|
2013-11-27 02:30:10 +01:00
|
|
|
}).sortable( 'option', 'connectWith', 'div.widgets-sortables' );
|
2012-08-23 02:04:18 +02:00
|
|
|
|
|
|
|
$('#available-widgets').droppable({
|
|
|
|
tolerance: 'pointer',
|
|
|
|
accept: function(o){
|
2013-11-09 00:05:10 +01:00
|
|
|
return $(o).parent().attr('id') !== 'widget-list';
|
2012-08-23 02:04:18 +02:00
|
|
|
},
|
|
|
|
drop: function(e,ui) {
|
|
|
|
ui.draggable.addClass('deleting');
|
2015-03-09 20:44:26 +01:00
|
|
|
$('#removing-widget').hide().children('span').empty();
|
2012-08-23 02:04:18 +02:00
|
|
|
},
|
|
|
|
over: function(e,ui) {
|
|
|
|
ui.draggable.addClass('deleting');
|
|
|
|
$('div.widget-placeholder').hide();
|
|
|
|
|
2013-11-20 12:12:11 +01:00
|
|
|
if ( ui.draggable.hasClass('ui-sortable-helper') ) {
|
2012-08-23 02:04:18 +02:00
|
|
|
$('#removing-widget').show().children('span')
|
2015-10-10 18:34:25 +02:00
|
|
|
.html( ui.draggable.find( 'div.widget-title' ).children( 'h3' ).html() );
|
2013-11-20 12:12:11 +01:00
|
|
|
}
|
2012-08-23 02:04:18 +02:00
|
|
|
},
|
|
|
|
out: function(e,ui) {
|
|
|
|
ui.draggable.removeClass('deleting');
|
|
|
|
$('div.widget-placeholder').show();
|
2015-03-09 20:44:26 +01:00
|
|
|
$('#removing-widget').hide().children('span').empty();
|
2012-08-23 02:04:18 +02:00
|
|
|
}
|
|
|
|
});
|
2013-11-09 00:05:10 +01:00
|
|
|
|
|
|
|
// Area Chooser
|
|
|
|
$( '#widgets-right .widgets-holder-wrap' ).each( function( index, element ) {
|
|
|
|
var $element = $( element ),
|
2015-10-10 18:34:25 +02:00
|
|
|
name = $element.find( '.sidebar-name h2' ).text(),
|
2013-11-09 00:05:10 +01: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-27 02:30:10 +01:00
|
|
|
var $widget = $(this).closest( '.widget' );
|
2013-11-09 00:05:10 +01:00
|
|
|
|
2013-11-27 02:30:10 +01:00
|
|
|
if ( $widget.hasClass( 'widget-in-question' ) || $( '#widgets-left' ).hasClass( 'chooser' ) ) {
|
2013-11-09 00:05:10 +01:00
|
|
|
self.closeChooser();
|
|
|
|
} else {
|
|
|
|
// Open the chooser
|
|
|
|
self.clearWidgetSelection();
|
|
|
|
$( '#widgets-left' ).addClass( 'chooser' );
|
2013-11-27 02:30:10 +01:00
|
|
|
$widget.addClass( 'widget-in-question' ).children( '.widget-description' ).after( chooser );
|
2013-11-09 00:05:10 +01:00
|
|
|
|
2013-11-19 19:03:09 +01:00
|
|
|
chooser.slideDown( 300, function() {
|
2013-11-09 00:05:10 +01:00
|
|
|
selectSidebar.find('.widgets-chooser-selected').focus();
|
|
|
|
});
|
2013-11-19 19:03:09 +01: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 12:12:11 +01:00
|
|
|
} );
|
2013-11-09 00:05:10 +01: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 21:54:28 +02:00
|
|
|
} else if ( $target.hasClass( 'widgets-chooser-cancel' ) ) {
|
2013-11-09 00:05:10 +01:00
|
|
|
self.closeChooser();
|
2013-11-19 19:03:09 +01:00
|
|
|
}
|
|
|
|
}).on( 'keyup.widgets-chooser', function( event ) {
|
|
|
|
if ( event.which === $.ui.keyCode.ENTER ) {
|
2016-09-28 21:54:28 +02:00
|
|
|
if ( $( event.target ).hasClass( 'widgets-chooser-cancel' ) ) {
|
2013-11-19 19:03:09 +01: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-09 00:05:10 +01:00
|
|
|
}
|
|
|
|
});
|
2012-08-23 02:04:18 +02:00
|
|
|
},
|
|
|
|
|
2013-11-27 02:30:10 +01:00
|
|
|
saveOrder : function( sidebarId ) {
|
|
|
|
var data = {
|
2012-08-23 02:04:18 +02:00
|
|
|
action: 'widgets-order',
|
|
|
|
savewidgets: $('#_wpnonce_widgets').val(),
|
|
|
|
sidebars: []
|
|
|
|
};
|
|
|
|
|
2013-11-27 02:30:10 +01:00
|
|
|
if ( sidebarId ) {
|
2015-04-03 06:52:27 +02:00
|
|
|
$( '#' + sidebarId ).find( '.spinner:first' ).addClass( 'is-active' );
|
2013-11-27 02:30:10 +01:00
|
|
|
}
|
|
|
|
|
2012-08-23 02:04:18 +02:00
|
|
|
$('div.widgets-sortables').each( function() {
|
2013-11-20 12:12:11 +01:00
|
|
|
if ( $(this).sortable ) {
|
2013-11-27 02:30:10 +01:00
|
|
|
data['sidebars[' + $(this).attr('id') + ']'] = $(this).sortable('toArray').join(',');
|
2013-11-20 12:12:11 +01:00
|
|
|
}
|
2012-08-23 02:04:18 +02:00
|
|
|
});
|
|
|
|
|
2013-11-27 02:30:10 +01:00
|
|
|
$.post( ajaxurl, data, function() {
|
2015-10-21 07:23:26 +02:00
|
|
|
$( '#inactive-widgets-control-remove' ).prop( 'disabled' , ! $( '#wp_inactive_widgets .widget' ).length );
|
2015-04-03 06:52:27 +02:00
|
|
|
$( '.spinner' ).removeClass( 'is-active' );
|
2012-08-23 02:04:18 +02:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2013-11-27 02:30:10 +01:00
|
|
|
save : function( widget, del, animate, order ) {
|
|
|
|
var sidebarId = widget.closest('div.widgets-sortables').attr('id'),
|
|
|
|
data = widget.find('form').serialize(), a;
|
2013-11-27 02:42:09 +01:00
|
|
|
|
2012-08-23 02:04:18 +02:00
|
|
|
widget = $(widget);
|
2015-04-03 06:52:27 +02:00
|
|
|
$( '.spinner', widget ).addClass( 'is-active' );
|
2012-08-23 02:04:18 +02:00
|
|
|
|
|
|
|
a = {
|
|
|
|
action: 'save-widget',
|
|
|
|
savewidgets: $('#_wpnonce_widgets').val(),
|
2013-11-27 02:30:10 +01:00
|
|
|
sidebar: sidebarId
|
2012-08-23 02:04:18 +02:00
|
|
|
};
|
|
|
|
|
2013-11-20 12:12:11 +01:00
|
|
|
if ( del ) {
|
2013-11-09 00:05:10 +01:00
|
|
|
a.delete_widget = 1;
|
2013-11-20 12:12:11 +01:00
|
|
|
}
|
2012-08-23 02:04:18 +02:00
|
|
|
|
|
|
|
data += '&' + $.param(a);
|
|
|
|
|
2013-11-27 02:30:10 +01:00
|
|
|
$.post( ajaxurl, data, function(r) {
|
2012-08-23 02:04:18 +02:00
|
|
|
var id;
|
|
|
|
|
|
|
|
if ( del ) {
|
2013-11-20 12:12:11 +01:00
|
|
|
if ( ! $('input.widget_number', widget).val() ) {
|
2012-08-23 02:04:18 +02:00
|
|
|
id = $('input.widget-id', widget).val();
|
|
|
|
$('#available-widgets').find('input.widget-id').each(function(){
|
2013-11-20 12:12:11 +01:00
|
|
|
if ( $(this).val() === id ) {
|
2012-08-23 02:04:18 +02:00
|
|
|
$(this).closest('div.widget').show();
|
2013-11-20 12:12:11 +01:00
|
|
|
}
|
2012-08-23 02:04:18 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( animate ) {
|
|
|
|
order = 0;
|
|
|
|
widget.slideUp('fast', function(){
|
|
|
|
$(this).remove();
|
|
|
|
wpWidgets.saveOrder();
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
widget.remove();
|
2015-10-21 07:23:26 +02:00
|
|
|
|
|
|
|
if ( sidebarId === 'wp_inactive_widgets' ) {
|
|
|
|
$( '#inactive-widgets-control-remove' ).prop( 'disabled' , ! $( '#wp_inactive_widgets .widget' ).length );
|
|
|
|
}
|
2012-08-23 02:04:18 +02:00
|
|
|
}
|
|
|
|
} else {
|
2015-04-03 06:52:27 +02:00
|
|
|
$( '.spinner' ).removeClass( 'is-active' );
|
2012-08-23 02:04:18 +02:00
|
|
|
if ( r && r.length > 2 ) {
|
2014-04-06 21:06:14 +02:00
|
|
|
$( 'div.widget-content', widget ).html( r );
|
2013-11-27 02:30:10 +01:00
|
|
|
wpWidgets.appendTitle( widget );
|
2015-05-10 02:32:31 +02:00
|
|
|
$document.trigger( 'widget-updated', [ widget ] );
|
2015-10-21 07:23:26 +02:00
|
|
|
|
|
|
|
if ( sidebarId === 'wp_inactive_widgets' ) {
|
|
|
|
$( '#inactive-widgets-control-remove' ).prop( 'disabled' , ! $( '#wp_inactive_widgets .widget' ).length );
|
|
|
|
}
|
2012-08-23 02:04:18 +02:00
|
|
|
}
|
|
|
|
}
|
2015-10-21 07:23:26 +02:00
|
|
|
|
2013-11-20 12:12:11 +01:00
|
|
|
if ( order ) {
|
2012-08-23 02:04:18 +02:00
|
|
|
wpWidgets.saveOrder();
|
2013-11-20 12:12:11 +01:00
|
|
|
}
|
2012-08-23 02:04:18 +02:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2015-10-21 07:23:26 +02:00
|
|
|
removeInactiveWidgets : function() {
|
|
|
|
var $element = $( '.remove-inactive-widgets' ), a, data;
|
|
|
|
|
|
|
|
$( '.spinner', $element ).addClass( 'is-active' );
|
|
|
|
|
|
|
|
a = {
|
|
|
|
action : 'delete-inactive-widgets',
|
|
|
|
removeinactivewidgets : $( '#_wpnonce_remove_inactive_widgets' ).val()
|
|
|
|
};
|
|
|
|
|
|
|
|
data = $.param( a );
|
|
|
|
|
|
|
|
$.post( ajaxurl, data, function() {
|
|
|
|
$( '#wp_inactive_widgets .widget' ).remove();
|
|
|
|
$( '#inactive-widgets-control-remove' ).prop( 'disabled' , true );
|
|
|
|
$( '.spinner', $element ).removeClass( 'is-active' );
|
|
|
|
} );
|
|
|
|
},
|
|
|
|
|
2012-08-23 02:04:18 +02:00
|
|
|
appendTitle : function(widget) {
|
|
|
|
var title = $('input[id*="-title"]', widget).val() || '';
|
|
|
|
|
2013-11-20 12:12:11 +01:00
|
|
|
if ( title ) {
|
2012-08-23 02:04:18 +02:00
|
|
|
title = ': ' + title.replace(/<[^<>]+>/g, '').replace(/</g, '<').replace(/>/g, '>');
|
2013-11-20 12:12:11 +01:00
|
|
|
}
|
2012-08-23 02:04:18 +02:00
|
|
|
|
|
|
|
$(widget).children('.widget-top').children('.widget-title').children()
|
|
|
|
.children('.in-widget-title').html(title);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
close : function(widget) {
|
2013-11-20 02:35:10 +01:00
|
|
|
widget.children('.widget-inside').slideUp('fast', function() {
|
2017-04-19 23:14:44 +02:00
|
|
|
widget.attr( 'style', '' )
|
|
|
|
.find( '.widget-top button.widget-action' )
|
|
|
|
.attr( 'aria-expanded', 'false' )
|
|
|
|
.focus();
|
2012-08-23 02:04:18 +02:00
|
|
|
});
|
2013-11-09 00:05:10 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
addWidget: function( chooser ) {
|
2013-12-06 00:14:09 +01:00
|
|
|
var widget, widgetId, add, n, viewportTop, viewportBottom, sidebarBounds,
|
2013-11-09 00:05:10 +01:00
|
|
|
sidebarId = chooser.find( '.widgets-chooser-selected' ).data('sidebarId'),
|
|
|
|
sidebar = $( '#' + sidebarId );
|
|
|
|
|
2013-11-19 19:03:09 +01: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-05 01:02:11 +01:00
|
|
|
// Remove the cloned chooser from the widget
|
|
|
|
widget.find('.widgets-chooser').remove();
|
|
|
|
|
2013-11-09 00:05:10 +01: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();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open the widgets container
|
|
|
|
sidebar.closest( '.widgets-holder-wrap' ).removeClass('closed');
|
|
|
|
|
2013-12-05 23:27:11 +01:00
|
|
|
sidebar.append( widget );
|
2013-11-27 02:30:10 +01:00
|
|
|
sidebar.sortable('refresh');
|
2013-11-09 00:05:10 +01:00
|
|
|
|
|
|
|
wpWidgets.save( widget, 0, 0, 1 );
|
|
|
|
// No longer "new" widget
|
|
|
|
widget.find( 'input.add_new' ).val('');
|
|
|
|
|
2015-05-10 02:32:31 +02:00
|
|
|
$document.trigger( 'widget-added', [ widget ] );
|
2014-04-06 21:06:14 +02:00
|
|
|
|
2013-12-11 20:49:11 +01:00
|
|
|
/*
|
2013-12-05 23:22:11 +01: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-06 00:14:09 +01:00
|
|
|
viewportTop = $(window).scrollTop();
|
|
|
|
viewportBottom = viewportTop + $(window).height();
|
|
|
|
sidebarBounds = sidebar.offset();
|
2013-12-11 20:49:11 +01:00
|
|
|
|
2013-12-06 00:14:09 +01:00
|
|
|
sidebarBounds.bottom = sidebarBounds.top + sidebar.outerHeight();
|
2013-12-05 23:22:11 +01:00
|
|
|
|
2013-12-06 00:14:09 +01:00
|
|
|
if ( viewportTop > sidebarBounds.bottom || viewportBottom < sidebarBounds.top ) {
|
2013-12-05 23:22:11 +01:00
|
|
|
$( 'html, body' ).animate({
|
2013-12-06 00:14:09 +01:00
|
|
|
scrollTop: sidebarBounds.top - 130
|
2013-12-05 23:22:11 +01:00
|
|
|
}, 200 );
|
|
|
|
}
|
2013-11-09 00:05:10 +01: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 23:07:10 +01:00
|
|
|
$( '.widgets-chooser' ).slideUp( 200, function() {
|
2013-11-21 20:28:10 +01:00
|
|
|
$( '#wpbody-content' ).append( this );
|
2013-11-09 00:05:10 +01:00
|
|
|
self.clearWidgetSelection();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
clearWidgetSelection: function() {
|
|
|
|
$( '#widgets-left' ).removeClass( 'chooser' );
|
2013-11-19 19:03:09 +01:00
|
|
|
$( '.widget-in-question' ).removeClass( 'widget-in-question' );
|
2015-07-01 02:58:24 +02: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.
|
|
|
|
*
|
|
|
|
* @param sidebar
|
|
|
|
*/
|
|
|
|
closeSidebar: function( sidebar ) {
|
|
|
|
this.hoveredSidebar.addClass( 'closed' );
|
|
|
|
$( sidebar.target ).css( 'min-height', '' );
|
|
|
|
this.hoveredSidebar = null;
|
2012-08-23 02:04:18 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-05-10 02:32:31 +02:00
|
|
|
$document.ready( function(){ wpWidgets.init(); } );
|
2012-08-23 02:04:18 +02:00
|
|
|
|
|
|
|
})(jQuery);
|