1
/*global ajaxurl, isRtl */
10
chooser = $('.widgets-chooser'),
11
selectSidebar = chooser.find('.widgets-chooser-sidebars'),
12
sidebars = $('div.widgets-sortables'),
13
isRTL = !! ( 'undefined' !== typeof isRtl && isRtl );
15
$('#widgets-right .sidebar-name').click( function() {
17
$wrap = $this.closest('.widgets-holder-wrap');
19
if ( $wrap.hasClass('closed') ) {
20
$wrap.removeClass('closed');
21
$this.parent().sortable('refresh');
23
$wrap.addClass('closed');
27
$('#widgets-left .sidebar-name').click( function() {
28
$(this).closest('.widgets-holder-wrap').toggleClass('closed');
31
$(document.body).bind('click.widgets-toggle', function(e) {
32
var target = $(e.target),
33
css = { 'z-index': 100 },
34
widget, inside, targetWidth, widgetWidth, margin;
36
if ( target.parents('.widget-top').length && ! target.parents('#available-widgets').length ) {
37
widget = target.closest('div.widget');
38
inside = widget.children('.widget-inside');
39
targetWidth = parseInt( widget.find('input.widget-width').val(), 10 ),
40
widgetWidth = widget.parent().width();
42
if ( inside.is(':hidden') ) {
43
if ( targetWidth > 250 && ( targetWidth + 30 > widgetWidth ) && widget.closest('div.widgets-sortables').length ) {
44
if ( widget.closest('div.widget-liquid-right').length ) {
45
margin = isRTL ? 'margin-right' : 'margin-left';
47
margin = isRTL ? 'margin-left' : 'margin-right';
50
css[ margin ] = widgetWidth - ( targetWidth + 30 ) + 'px';
53
widget.addClass( 'open' );
54
inside.slideDown('fast');
56
inside.slideUp('fast', function() {
57
widget.attr( 'style', '' );
58
widget.removeClass( 'open' );
62
} else if ( target.hasClass('widget-control-save') ) {
63
wpWidgets.save( target.closest('div.widget'), 0, 1, 0 );
65
} else if ( target.hasClass('widget-control-remove') ) {
66
wpWidgets.save( target.closest('div.widget'), 1, 1, 0 );
68
} else if ( target.hasClass('widget-control-close') ) {
69
wpWidgets.close( target.closest('div.widget') );
74
sidebars.children('.widget').each( function() {
77
wpWidgets.appendTitle( this );
79
if ( $this.find( 'p.widget-error' ).length ) {
80
$this.find( 'a.widget-action' ).trigger('click');
84
$('#widget-list').children('.widget').draggable({
85
connectToSortable: 'div.widgets-sortables',
86
handle: '> .widget-top > .widget-title',
90
containment: 'document',
91
start: function( event, ui ) {
92
var chooser = $(this).find('.widgets-chooser');
94
ui.helper.find('div.widget-description').hide();
97
if ( chooser.length ) {
98
// Hide the chooser and move it out of the widget
99
$( '#wpbody-content' ).append( chooser.hide() );
100
// Delete the cloned chooser from the drag helper
101
ui.helper.find('.widgets-chooser').remove();
102
self.clearWidgetSelection();
115
placeholder: 'widget-placeholder',
117
handle: '> .widget-top > .widget-title',
120
containment: 'document',
121
start: function( event, ui ) {
122
var height, $this = $(this),
123
$wrap = $this.parent(),
124
inside = ui.item.children('.widget-inside');
126
if ( inside.css('display') === 'block' ) {
128
$(this).sortable('refreshPositions');
131
if ( ! $wrap.hasClass('closed') ) {
132
// Lock all open sidebars min-height when starting to drag.
133
// Prevents jumping when dragging a widget from an open sidebar to a closed sidebar below.
134
height = ui.item.hasClass('ui-draggable') ? $this.height() : 1 + $this.height();
135
$this.css( 'min-height', height + 'px' );
139
stop: function( event, ui ) {
140
var addNew, widgetNumber, $sidebar, $children, child, item,
144
if ( $widget.hasClass('deleting') ) {
145
wpWidgets.save( $widget, 1, 0, 1 ); // delete widget
150
addNew = $widget.find('input.add_new').val();
151
widgetNumber = $widget.find('input.multi_number').val();
153
$widget.attr( 'style', '' ).removeClass('ui-draggable');
157
if ( 'multi' === addNew ) {
159
$widget.html().replace( /<[^<>]+>/g, function( tag ) {
160
return tag.replace( /__i__|%i%/g, widgetNumber );
164
$widget.attr( 'id', id.replace( '__i__', widgetNumber ) );
167
$( 'div#' + id ).find( 'input.multi_number' ).val( widgetNumber );
168
} else if ( 'single' === addNew ) {
169
$widget.attr( 'id', 'new-' + id );
173
wpWidgets.save( $widget, 0, 0, 1 );
174
$widget.find('input.add_new').val('');
175
$( document ).trigger( 'widget-added', [ $widget ] );
178
$sidebar = $widget.parent();
180
if ( $sidebar.parent().hasClass('closed') ) {
181
$sidebar.parent().removeClass('closed');
182
$children = $sidebar.children('.widget');
184
// Make sure the dropped widget is at the top
185
if ( $children.length > 1 ) {
186
child = $children.get(0);
187
item = $widget.get(0);
189
if ( child.id && item.id && child.id !== item.id ) {
190
$( child ).before( $widget );
196
$widget.find( 'a.widget-action' ).trigger('click');
198
wpWidgets.saveOrder( $sidebar.attr('id') );
202
activate: function() {
203
$(this).parent().addClass( 'widget-hover' );
206
deactivate: function() {
207
// Remove all min-height added on "start"
208
$(this).css( 'min-height', '' ).parent().removeClass( 'widget-hover' );
211
receive: function( event, ui ) {
212
var $sender = $( ui.sender );
214
// Don't add more widgets to orphaned sidebars
215
if ( this.id.indexOf('orphaned_widgets') > -1 ) {
216
$sender.sortable('cancel');
220
// If the last widget was moved out of an orphaned sidebar, close and remove it.
221
if ( $sender.attr('id').indexOf('orphaned_widgets') > -1 && ! $sender.children('.widget').length ) {
222
$sender.parents('.orphan-sidebar').slideUp( 400, function(){ $(this).remove(); } );
225
}).sortable( 'option', 'connectWith', 'div.widgets-sortables' );
227
$('#available-widgets').droppable({
228
tolerance: 'pointer',
230
return $(o).parent().attr('id') !== 'widget-list';
232
drop: function(e,ui) {
233
ui.draggable.addClass('deleting');
234
$('#removing-widget').hide().children('span').html('');
236
over: function(e,ui) {
237
ui.draggable.addClass('deleting');
238
$('div.widget-placeholder').hide();
240
if ( ui.draggable.hasClass('ui-sortable-helper') ) {
241
$('#removing-widget').show().children('span')
242
.html( ui.draggable.find('div.widget-title').children('h4').html() );
245
out: function(e,ui) {
246
ui.draggable.removeClass('deleting');
247
$('div.widget-placeholder').show();
248
$('#removing-widget').hide().children('span').html('');
253
$( '#widgets-right .widgets-holder-wrap' ).each( function( index, element ) {
254
var $element = $( element ),
255
name = $element.find( '.sidebar-name h3' ).text(),
256
id = $element.find( '.widgets-sortables' ).attr( 'id' ),
257
li = $('<li tabindex="0">').text( $.trim( name ) );
260
li.addClass( 'widgets-chooser-selected' );
263
selectSidebar.append( li );
264
li.data( 'sidebarId', id );
267
$( '#available-widgets .widget .widget-title' ).on( 'click.widgets-chooser', function() {
268
var $widget = $(this).closest( '.widget' );
270
if ( $widget.hasClass( 'widget-in-question' ) || $( '#widgets-left' ).hasClass( 'chooser' ) ) {
274
self.clearWidgetSelection();
275
$( '#widgets-left' ).addClass( 'chooser' );
276
$widget.addClass( 'widget-in-question' ).children( '.widget-description' ).after( chooser );
278
chooser.slideDown( 300, function() {
279
selectSidebar.find('.widgets-chooser-selected').focus();
282
selectSidebar.find( 'li' ).on( 'focusin.widgets-chooser', function() {
283
selectSidebar.find('.widgets-chooser-selected').removeClass( 'widgets-chooser-selected' );
284
$(this).addClass( 'widgets-chooser-selected' );
289
// Add event handlers
290
chooser.on( 'click.widgets-chooser', function( event ) {
291
var $target = $( event.target );
293
if ( $target.hasClass('button-primary') ) {
294
self.addWidget( chooser );
296
} else if ( $target.hasClass('button-secondary') ) {
299
}).on( 'keyup.widgets-chooser', function( event ) {
300
if ( event.which === $.ui.keyCode.ENTER ) {
301
if ( $( event.target ).hasClass('button-secondary') ) {
302
// Close instead of adding when pressing Enter on the Cancel button
305
self.addWidget( chooser );
308
} else if ( event.which === $.ui.keyCode.ESCAPE ) {
314
saveOrder : function( sidebarId ) {
316
action: 'widgets-order',
317
savewidgets: $('#_wpnonce_widgets').val(),
322
$( '#' + sidebarId ).find('.spinner:first').css('display', 'inline-block');
325
$('div.widgets-sortables').each( function() {
326
if ( $(this).sortable ) {
327
data['sidebars[' + $(this).attr('id') + ']'] = $(this).sortable('toArray').join(',');
331
$.post( ajaxurl, data, function() {
332
$('.spinner').hide();
336
save : function( widget, del, animate, order ) {
337
var sidebarId = widget.closest('div.widgets-sortables').attr('id'),
338
data = widget.find('form').serialize(), a;
341
$('.spinner', widget).show();
344
action: 'save-widget',
345
savewidgets: $('#_wpnonce_widgets').val(),
353
data += '&' + $.param(a);
355
$.post( ajaxurl, data, function(r) {
359
if ( ! $('input.widget_number', widget).val() ) {
360
id = $('input.widget-id', widget).val();
361
$('#available-widgets').find('input.widget-id').each(function(){
362
if ( $(this).val() === id ) {
363
$(this).closest('div.widget').show();
370
widget.slideUp('fast', function(){
372
wpWidgets.saveOrder();
378
$('.spinner').hide();
379
if ( r && r.length > 2 ) {
380
$( 'div.widget-content', widget ).html( r );
381
wpWidgets.appendTitle( widget );
382
$( document ).trigger( 'widget-updated', [ widget ] );
386
wpWidgets.saveOrder();
391
appendTitle : function(widget) {
392
var title = $('input[id*="-title"]', widget).val() || '';
395
title = ': ' + title.replace(/<[^<>]+>/g, '').replace(/</g, '<').replace(/>/g, '>');
398
$(widget).children('.widget-top').children('.widget-title').children()
399
.children('.in-widget-title').html(title);
403
close : function(widget) {
404
widget.children('.widget-inside').slideUp('fast', function() {
405
widget.attr( 'style', '' );
409
addWidget: function( chooser ) {
410
var widget, widgetId, add, n, viewportTop, viewportBottom, sidebarBounds,
411
sidebarId = chooser.find( '.widgets-chooser-selected' ).data('sidebarId'),
412
sidebar = $( '#' + sidebarId );
414
widget = $('#available-widgets').find('.widget-in-question').clone();
415
widgetId = widget.attr('id');
416
add = widget.find( 'input.add_new' ).val();
417
n = widget.find( 'input.multi_number' ).val();
419
// Remove the cloned chooser from the widget
420
widget.find('.widgets-chooser').remove();
422
if ( 'multi' === add ) {
424
widget.html().replace( /<[^<>]+>/g, function(m) {
425
return m.replace( /__i__|%i%/g, n );
429
widget.attr( 'id', widgetId.replace( '__i__', n ) );
431
$( '#' + widgetId ).find('input.multi_number').val(n);
432
} else if ( 'single' === add ) {
433
widget.attr( 'id', 'new-' + widgetId );
434
$( '#' + widgetId ).hide();
437
// Open the widgets container
438
sidebar.closest( '.widgets-holder-wrap' ).removeClass('closed');
440
sidebar.append( widget );
441
sidebar.sortable('refresh');
443
wpWidgets.save( widget, 0, 0, 1 );
444
// No longer "new" widget
445
widget.find( 'input.add_new' ).val('');
447
$( document ).trigger( 'widget-added', [ widget ] );
450
* Check if any part of the sidebar is visible in the viewport. If it is, don't scroll.
451
* Otherwise, scroll up to so the sidebar is in view.
453
* We do this by comparing the top and bottom, of the sidebar so see if they are within
454
* the bounds of the viewport.
456
viewportTop = $(window).scrollTop();
457
viewportBottom = viewportTop + $(window).height();
458
sidebarBounds = sidebar.offset();
460
sidebarBounds.bottom = sidebarBounds.top + sidebar.outerHeight();
462
if ( viewportTop > sidebarBounds.bottom || viewportBottom < sidebarBounds.top ) {
463
$( 'html, body' ).animate({
464
scrollTop: sidebarBounds.top - 130
468
window.setTimeout( function() {
469
// Cannot use a callback in the animation above as it fires twice,
470
// have to queue this "by hand".
471
widget.find( '.widget-title' ).trigger('click');
475
closeChooser: function() {
478
$( '.widgets-chooser' ).slideUp( 200, function() {
479
$( '#wpbody-content' ).append( this );
480
self.clearWidgetSelection();
484
clearWidgetSelection: function() {
485
$( '#widgets-left' ).removeClass( 'chooser' );
486
$( '.widget-in-question' ).removeClass( 'widget-in-question' );
490
$(document).ready( function(){ wpWidgets.init(); } );