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.
|
|
|
|
*
|
|
|
|
* @var element|null
|
|
|
|
*/
|
|
|
|
hoveredSidebar: null,
|
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
|
|
|
|
2013-11-19 23:45:10 -05:00
|
|
|
$('#widgets-right .sidebar-name').click( function() {
|
2013-11-26 20:30:10 -05:00
|
|
|
var $this = $(this),
|
|
|
|
$wrap = $this.closest('.widgets-holder-wrap');
|
2013-11-19 23:45:10 -05:00
|
|
|
|
2013-11-26 20:30:10 -05:00
|
|
|
if ( $wrap.hasClass('closed') ) {
|
|
|
|
$wrap.removeClass('closed');
|
|
|
|
$this.parent().sortable('refresh');
|
2012-08-22 20:04:18 -04:00
|
|
|
} else {
|
2013-11-26 20:30:10 -05:00
|
|
|
$wrap.addClass('closed');
|
2012-08-22 20:04:18 -04:00
|
|
|
}
|
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
|
|
|
$('#widgets-left .sidebar-name').click( function() {
|
|
|
|
$(this).closest('.widgets-holder-wrap').toggleClass('closed');
|
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 },
|
|
|
|
widget, inside, targetWidth, widgetWidth, margin;
|
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');
|
2013-11-19 20:35:10 -05:00
|
|
|
targetWidth = parseInt( widget.find('input.widget-width').val(), 10 ),
|
2013-11-26 20:30:10 -05:00
|
|
|
widgetWidth = widget.parent().width();
|
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
|
|
|
}
|
2014-05-01 10:09:16 -04:00
|
|
|
widget.addClass( 'open' );
|
2012-08-22 20:04:18 -04:00
|
|
|
inside.slideDown('fast');
|
|
|
|
} else {
|
|
|
|
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' );
|
|
|
|
wpWidgets.close( widget );
|
2012-08-22 20:04:18 -04:00
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
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 ) {
|
|
|
|
$this.find( 'a.widget-action' ).trigger('click');
|
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.
|
|
|
|
*
|
|
|
|
* @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-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.
|
|
|
|
*
|
|
|
|
* @param event
|
|
|
|
*/
|
|
|
|
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');
|
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') ) {
|
2013-11-29 12:58:10 -05:00
|
|
|
$sidebar.parent().removeClass('closed');
|
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 ) {
|
|
|
|
$widget.find( 'a.widget-action' ).trigger('click');
|
|
|
|
} 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')
|
|
|
|
.html( ui.draggable.find('div.widget-title').children('h4').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 ),
|
|
|
|
name = $element.find( '.sidebar-name h3' ).text(),
|
|
|
|
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();
|
|
|
|
} else if ( $target.hasClass('button-secondary') ) {
|
|
|
|
self.closeChooser();
|
2013-11-19 13:03:09 -05:00
|
|
|
}
|
|
|
|
}).on( 'keyup.widgets-chooser', function( event ) {
|
|
|
|
if ( event.which === $.ui.keyCode.ENTER ) {
|
|
|
|
if ( $( event.target ).hasClass('button-secondary') ) {
|
|
|
|
// 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-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 ) {
|
|
|
|
var sidebarId = widget.closest('div.widgets-sortables').attr('id'),
|
|
|
|
data = widget.find('form').serialize(), a;
|
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) {
|
2012-08-22 20:04:18 -04:00
|
|
|
var id;
|
|
|
|
|
|
|
|
if ( del ) {
|
2013-11-20 06:12:11 -05:00
|
|
|
if ( ! $('input.widget_number', widget).val() ) {
|
2012-08-22 20:04:18 -04:00
|
|
|
id = $('input.widget-id', widget).val();
|
|
|
|
$('#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;
|
|
|
|
widget.slideUp('fast', function(){
|
|
|
|
$(this).remove();
|
|
|
|
wpWidgets.saveOrder();
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
widget.remove();
|
|
|
|
}
|
|
|
|
} 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 );
|
2015-05-09 20:32:31 -04:00
|
|
|
$document.trigger( 'widget-updated', [ widget ] );
|
2012-08-22 20:04:18 -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
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
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() {
|
|
|
|
widget.attr( 'style', '' );
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open the widgets container
|
|
|
|
sidebar.closest( '.widgets-holder-wrap' ).removeClass('closed');
|
|
|
|
|
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.
|
|
|
|
*
|
|
|
|
* @param sidebar
|
|
|
|
*/
|
|
|
|
closeSidebar: function( sidebar ) {
|
|
|
|
this.hoveredSidebar.addClass( 'closed' );
|
|
|
|
$( sidebar.target ).css( 'min-height', '' );
|
|
|
|
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);
|