2009-04-11 10:37:24 -04:00
|
|
|
var wpWidgets;
|
|
|
|
(function($) {
|
|
|
|
|
|
|
|
wpWidgets = {
|
2009-05-03 01:27:13 -04:00
|
|
|
|
2009-08-18 05:53:37 -04:00
|
|
|
init : function() {
|
2011-06-01 12:29:10 -04:00
|
|
|
var rem, sidebars = $('div.widgets-sortables'), isRTL = !! ( 'undefined' != typeof isRtl && isRtl ),
|
2011-09-23 01:03:31 -04:00
|
|
|
margin = ( isRtl ? 'marginRight' : 'marginLeft' ), the_id;
|
2009-06-01 19:31:34 -04:00
|
|
|
|
2009-08-18 05:53:37 -04:00
|
|
|
$('#widgets-right').children('.widgets-holder-wrap').children('.sidebar-name').click(function(){
|
|
|
|
var c = $(this).siblings('.widgets-sortables'), p = $(this).parent();
|
|
|
|
if ( !p.hasClass('closed') ) {
|
2009-05-10 06:33:30 -04:00
|
|
|
c.sortable('disable');
|
2009-08-18 05:53:37 -04:00
|
|
|
p.addClass('closed');
|
2009-01-02 10:08:58 -05:00
|
|
|
} else {
|
2009-08-18 05:53:37 -04:00
|
|
|
p.removeClass('closed');
|
2009-05-10 06:33:30 -04:00
|
|
|
c.sortable('enable').sortable('refresh');
|
2009-01-02 10:08:58 -05:00
|
|
|
}
|
2009-08-18 05:53:37 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
$('#widgets-left').children('.widgets-holder-wrap').children('.sidebar-name').click(function() {
|
2011-09-03 17:19:36 -04:00
|
|
|
$(this).parent().toggleClass('closed');
|
2009-08-18 05:53:37 -04:00
|
|
|
});
|
|
|
|
|
2011-09-03 17:26:32 -04:00
|
|
|
sidebars.each(function(){
|
|
|
|
if ( $(this).parent().hasClass('inactive') )
|
|
|
|
return true;
|
|
|
|
|
2009-08-18 05:53:37 -04:00
|
|
|
var h = 50, H = $(this).children('.widget').length;
|
|
|
|
h = h + parseInt(H * 48, 10);
|
|
|
|
$(this).css( 'minHeight', h + 'px' );
|
|
|
|
});
|
2009-05-22 12:13:05 -04:00
|
|
|
|
2012-07-18 18:41:43 -04:00
|
|
|
$(document.body).bind('click.widgets-toggle', function(e){
|
|
|
|
var target = $(e.target), css = {}, widget, inside, w;
|
2010-01-15 17:11:12 -05:00
|
|
|
|
2012-07-18 18:41:43 -04:00
|
|
|
if ( target.parents('.widget-top').length && ! target.parents('#available-widgets').length ) {
|
|
|
|
widget = target.closest('div.widget');
|
|
|
|
inside = widget.children('.widget-inside');
|
|
|
|
w = parseInt( widget.find('input.widget-width').val(), 10 );
|
|
|
|
|
|
|
|
if ( inside.is(':hidden') ) {
|
|
|
|
if ( w > 250 && inside.closest('div.widgets-sortables').length ) {
|
|
|
|
css['width'] = w + 30 + 'px';
|
|
|
|
if ( inside.closest('div.widget-liquid-right').length )
|
|
|
|
css[margin] = 235 - w + 'px';
|
|
|
|
widget.css(css);
|
|
|
|
}
|
|
|
|
wpWidgets.fixLabels(widget);
|
|
|
|
inside.slideDown('fast');
|
|
|
|
} else {
|
|
|
|
inside.slideUp('fast', function() {
|
|
|
|
widget.css({'width':'', margin:''});
|
|
|
|
});
|
2009-08-18 05:53:37 -04:00
|
|
|
}
|
2012-07-18 18:41:43 -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') ) {
|
|
|
|
wpWidgets.close( target.closest('div.widget') );
|
|
|
|
e.preventDefault();
|
2009-05-03 01:27:13 -04:00
|
|
|
}
|
2009-08-18 05:53:37 -04:00
|
|
|
});
|
2009-05-24 11:46:09 -04:00
|
|
|
|
2009-08-18 05:53:37 -04:00
|
|
|
sidebars.children('.widget').each(function() {
|
|
|
|
wpWidgets.appendTitle(this);
|
|
|
|
if ( $('p.widget-error', this).length )
|
|
|
|
$('a.widget-action', this).click();
|
2009-05-24 11:46:09 -04:00
|
|
|
});
|
|
|
|
|
2009-08-18 05:53:37 -04:00
|
|
|
$('#widget-list').children('.widget').draggable({
|
|
|
|
connectToSortable: 'div.widgets-sortables',
|
|
|
|
handle: '> .widget-top > .widget-title',
|
2009-04-11 10:37:24 -04:00
|
|
|
distance: 2,
|
|
|
|
helper: 'clone',
|
2012-07-05 15:24:57 -04:00
|
|
|
zIndex: 100,
|
2009-05-09 20:49:48 -04:00
|
|
|
containment: 'document',
|
2009-05-03 01:27:13 -04:00
|
|
|
start: function(e,ui) {
|
2009-08-18 05:53:37 -04:00
|
|
|
ui.helper.find('div.widget-description').hide();
|
2011-09-23 01:03:31 -04:00
|
|
|
the_id = this.id;
|
2009-04-11 10:37:24 -04:00
|
|
|
},
|
|
|
|
stop: function(e,ui) {
|
|
|
|
if ( rem )
|
|
|
|
$(rem).hide();
|
2011-09-23 01:03:31 -04:00
|
|
|
|
2009-04-11 10:37:24 -04:00
|
|
|
rem = '';
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2009-08-18 05:53:37 -04:00
|
|
|
sidebars.sortable({
|
2009-04-11 10:37:24 -04:00
|
|
|
placeholder: 'widget-placeholder',
|
2009-08-18 05:53:37 -04:00
|
|
|
items: '> .widget',
|
|
|
|
handle: '> .widget-top > .widget-title',
|
2009-04-11 10:37:24 -04:00
|
|
|
cursor: 'move',
|
|
|
|
distance: 2,
|
2009-05-09 20:49:48 -04:00
|
|
|
containment: 'document',
|
2009-04-11 10:37:24 -04:00
|
|
|
start: function(e,ui) {
|
2009-08-18 05:53:37 -04:00
|
|
|
ui.item.children('.widget-inside').hide();
|
2011-06-01 12:29:10 -04:00
|
|
|
ui.item.css({margin:'', 'width':''});
|
2009-04-11 10:37:24 -04:00
|
|
|
},
|
|
|
|
stop: function(e,ui) {
|
2010-10-07 00:20:19 -04:00
|
|
|
if ( ui.item.hasClass('ui-draggable') && ui.item.data('draggable') )
|
2009-05-24 11:46:09 -04:00
|
|
|
ui.item.draggable('destroy');
|
|
|
|
|
|
|
|
if ( ui.item.hasClass('deleting') ) {
|
2009-05-31 07:34:08 -04:00
|
|
|
wpWidgets.save( ui.item, 1, 0, 1 ); // delete widget
|
2009-05-24 11:46:09 -04:00
|
|
|
ui.item.remove();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-08-18 05:53:37 -04:00
|
|
|
var add = ui.item.find('input.add_new').val(),
|
|
|
|
n = ui.item.find('input.multi_number').val(),
|
2011-09-25 14:53:34 -04:00
|
|
|
id = the_id,
|
2009-08-18 05:53:37 -04:00
|
|
|
sb = $(this).attr('id');
|
2009-05-24 11:46:09 -04:00
|
|
|
|
2011-06-01 12:29:10 -04:00
|
|
|
ui.item.css({margin:'', 'width':''});
|
2011-09-25 14:53:34 -04:00
|
|
|
the_id = '';
|
2011-09-23 01:03:31 -04:00
|
|
|
|
2009-04-13 12:20:58 -04:00
|
|
|
if ( add ) {
|
2009-04-11 10:37:24 -04:00
|
|
|
if ( 'multi' == add ) {
|
2009-05-07 17:58:45 -04:00
|
|
|
ui.item.html( ui.item.html().replace(/<[^<>]+>/g, function(m){ return m.replace(/__i__|%i%/g, n); }) );
|
2011-09-25 14:53:34 -04:00
|
|
|
ui.item.attr( 'id', id.replace('__i__', n) );
|
2009-04-11 10:37:24 -04:00
|
|
|
n++;
|
2009-05-03 01:27:13 -04:00
|
|
|
$('div#' + id).find('input.multi_number').val(n);
|
2009-04-11 10:37:24 -04:00
|
|
|
} else if ( 'single' == add ) {
|
|
|
|
ui.item.attr( 'id', 'new-' + id );
|
2009-05-03 01:27:13 -04:00
|
|
|
rem = 'div#' + id;
|
2009-04-11 10:37:24 -04:00
|
|
|
}
|
2009-05-31 07:34:08 -04:00
|
|
|
wpWidgets.save( ui.item, 0, 0, 1 );
|
2009-04-11 10:37:24 -04:00
|
|
|
ui.item.find('input.add_new').val('');
|
2009-04-13 12:20:58 -04:00
|
|
|
ui.item.find('a.widget-action').click();
|
2009-05-24 11:46:09 -04:00
|
|
|
return;
|
2009-04-11 10:37:24 -04:00
|
|
|
}
|
|
|
|
wpWidgets.saveOrder(sb);
|
|
|
|
},
|
2011-09-02 23:01:11 -04:00
|
|
|
receive: function(e, ui) {
|
2011-09-03 17:26:32 -04:00
|
|
|
var sender = $(ui.sender);
|
|
|
|
|
2011-09-02 23:01:11 -04:00
|
|
|
if ( !$(this).is(':visible') || this.id.indexOf('orphaned_widgets') != -1 )
|
2011-09-03 17:26:32 -04:00
|
|
|
sender.sortable('cancel');
|
|
|
|
|
2011-09-03 17:45:01 -04:00
|
|
|
if ( sender.attr('id').indexOf('orphaned_widgets') != -1 && !sender.children('.widget').length ) {
|
2011-09-03 17:26:32 -04:00
|
|
|
sender.parents('.orphan-sidebar').slideUp(400, function(){ $(this).remove(); });
|
|
|
|
}
|
2009-01-02 10:08:58 -05:00
|
|
|
}
|
2009-08-24 15:16:53 -04:00
|
|
|
}).sortable('option', 'connectWith', 'div.widgets-sortables').parent().filter('.closed').children('.widgets-sortables').sortable('disable');
|
2009-08-18 05:53:37 -04:00
|
|
|
|
|
|
|
$('#available-widgets').droppable({
|
|
|
|
tolerance: 'pointer',
|
|
|
|
accept: function(o){
|
|
|
|
return $(o).parent().attr('id') != 'widget-list';
|
|
|
|
},
|
|
|
|
drop: function(e,ui) {
|
|
|
|
ui.draggable.addClass('deleting');
|
|
|
|
$('#removing-widget').hide().children('span').html('');
|
|
|
|
},
|
|
|
|
over: function(e,ui) {
|
|
|
|
ui.draggable.addClass('deleting');
|
|
|
|
$('div.widget-placeholder').hide();
|
|
|
|
|
|
|
|
if ( ui.draggable.hasClass('ui-sortable-helper') )
|
|
|
|
$('#removing-widget').show().children('span')
|
|
|
|
.html( ui.draggable.find('div.widget-title').children('h4').html() );
|
|
|
|
},
|
|
|
|
out: function(e,ui) {
|
|
|
|
ui.draggable.removeClass('deleting');
|
|
|
|
$('div.widget-placeholder').show();
|
|
|
|
$('#removing-widget').hide().children('span').html('');
|
|
|
|
}
|
|
|
|
});
|
2009-04-11 10:37:24 -04:00
|
|
|
},
|
2009-01-02 10:08:58 -05:00
|
|
|
|
2009-04-11 10:37:24 -04:00
|
|
|
saveOrder : function(sb) {
|
2009-05-24 11:46:09 -04:00
|
|
|
if ( sb )
|
2009-08-18 05:53:37 -04:00
|
|
|
$('#' + sb).closest('div.widgets-holder-wrap').find('img.ajax-feedback').css('visibility', 'visible');
|
2009-04-13 12:20:58 -04:00
|
|
|
|
|
|
|
var a = {
|
2009-04-11 10:37:24 -04:00
|
|
|
action: 'widgets-order',
|
2009-04-13 12:20:58 -04:00
|
|
|
savewidgets: $('#_wpnonce_widgets').val(),
|
|
|
|
sidebars: []
|
2009-04-11 10:37:24 -04:00
|
|
|
};
|
2009-04-13 12:20:58 -04:00
|
|
|
|
2009-08-18 05:53:37 -04:00
|
|
|
$('div.widgets-sortables').each( function() {
|
2011-09-03 17:26:32 -04:00
|
|
|
if ( $(this).sortable )
|
|
|
|
a['sidebars[' + $(this).attr('id') + ']'] = $(this).sortable('toArray').join(',');
|
2009-04-11 10:37:24 -04:00
|
|
|
});
|
2009-04-13 12:20:58 -04:00
|
|
|
|
|
|
|
$.post( ajaxurl, a, function() {
|
2009-08-18 05:53:37 -04:00
|
|
|
$('img.ajax-feedback').css('visibility', 'hidden');
|
2009-04-11 10:37:24 -04:00
|
|
|
});
|
2009-08-18 05:53:37 -04:00
|
|
|
|
2009-05-24 11:46:09 -04:00
|
|
|
this.resize();
|
2009-04-13 12:20:58 -04:00
|
|
|
},
|
2009-04-12 21:17:59 -04:00
|
|
|
|
2009-05-31 07:34:08 -04:00
|
|
|
save : function(widget, del, animate, order) {
|
2009-08-18 05:53:37 -04:00
|
|
|
var sb = widget.closest('div.widgets-sortables').attr('id'), data = widget.find('form').serialize(), a;
|
2009-05-24 11:46:09 -04:00
|
|
|
widget = $(widget);
|
2009-08-18 05:53:37 -04:00
|
|
|
$('.ajax-feedback', widget).css('visibility', 'visible');
|
2009-04-11 10:37:24 -04:00
|
|
|
|
2009-05-24 11:46:09 -04:00
|
|
|
a = {
|
2009-04-11 10:37:24 -04:00
|
|
|
action: 'save-widget',
|
|
|
|
savewidgets: $('#_wpnonce_widgets').val(),
|
|
|
|
sidebar: sb
|
|
|
|
};
|
|
|
|
|
|
|
|
if ( del )
|
|
|
|
a['delete_widget'] = 1;
|
|
|
|
|
2009-05-03 19:17:47 -04:00
|
|
|
data += '&' + $.param(a);
|
2009-04-11 10:37:24 -04:00
|
|
|
|
2009-05-03 19:17:47 -04:00
|
|
|
$.post( ajaxurl, data, function(r){
|
2009-05-24 11:46:09 -04:00
|
|
|
var id;
|
2009-05-09 20:49:48 -04:00
|
|
|
|
2009-04-11 10:37:24 -04:00
|
|
|
if ( del ) {
|
2009-08-18 05:53:37 -04:00
|
|
|
if ( !$('input.widget_number', widget).val() ) {
|
|
|
|
id = $('input.widget-id', widget).val();
|
|
|
|
$('#available-widgets').find('input.widget-id').each(function(){
|
2009-04-11 10:37:24 -04:00
|
|
|
if ( $(this).val() == id )
|
2009-08-18 05:53:37 -04:00
|
|
|
$(this).closest('div.widget').show();
|
2009-04-11 10:37:24 -04:00
|
|
|
});
|
|
|
|
}
|
2009-05-24 11:46:09 -04:00
|
|
|
|
|
|
|
if ( animate ) {
|
|
|
|
order = 0;
|
|
|
|
widget.slideUp('fast', function(){
|
|
|
|
$(this).remove();
|
|
|
|
wpWidgets.saveOrder();
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
widget.remove();
|
|
|
|
wpWidgets.resize();
|
|
|
|
}
|
2009-05-31 07:34:08 -04:00
|
|
|
} else {
|
|
|
|
$('.ajax-feedback').css('visibility', 'hidden');
|
|
|
|
if ( r && r.length > 2 ) {
|
2009-08-18 05:53:37 -04:00
|
|
|
$('div.widget-content', widget).html(r);
|
2009-05-09 20:49:48 -04:00
|
|
|
wpWidgets.appendTitle(widget);
|
2009-05-31 07:34:08 -04:00
|
|
|
wpWidgets.fixLabels(widget);
|
|
|
|
}
|
2009-05-09 20:49:48 -04:00
|
|
|
}
|
2009-05-24 11:46:09 -04:00
|
|
|
if ( order )
|
|
|
|
wpWidgets.saveOrder();
|
2009-05-09 20:49:48 -04:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
appendTitle : function(widget) {
|
2012-04-27 16:34:35 -04:00
|
|
|
var title = $('input[id*="-title"]', widget).val() || '';
|
|
|
|
|
|
|
|
if ( title )
|
|
|
|
title = ': ' + title.replace(/<[^<>]+>/g, '').replace(/</g, '<').replace(/>/g, '>');
|
|
|
|
|
|
|
|
$(widget).children('.widget-top').children('.widget-title').children()
|
|
|
|
.children('.in-widget-title').html(title);
|
|
|
|
|
2009-04-11 10:37:24 -04:00
|
|
|
},
|
|
|
|
|
2009-05-09 20:49:48 -04:00
|
|
|
resize : function() {
|
2011-09-03 17:26:32 -04:00
|
|
|
$('div.widgets-sortables').each(function(){
|
|
|
|
if ( $(this).parent().hasClass('inactive') )
|
|
|
|
return true;
|
|
|
|
|
2009-08-18 05:53:37 -04:00
|
|
|
var h = 50, H = $(this).children('.widget').length;
|
2009-05-09 20:49:48 -04:00
|
|
|
h = h + parseInt(H * 48, 10);
|
|
|
|
$(this).css( 'minHeight', h + 'px' );
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2011-09-23 01:03:31 -04:00
|
|
|
fixLabels : function(widget) {
|
2009-08-18 05:53:37 -04:00
|
|
|
widget.children('.widget-inside').find('label').each(function(){
|
2009-05-31 07:34:08 -04:00
|
|
|
var f = $(this).attr('for');
|
|
|
|
if ( f && f == $('input', this).attr('id') )
|
|
|
|
$(this).removeAttr('for');
|
|
|
|
});
|
|
|
|
},
|
2009-08-18 05:53:37 -04:00
|
|
|
|
2011-09-23 01:03:31 -04:00
|
|
|
close : function(widget) {
|
2009-08-18 05:53:37 -04:00
|
|
|
widget.children('.widget-inside').slideUp('fast', function(){
|
2011-06-01 12:29:10 -04:00
|
|
|
widget.css({'width':'', margin:''});
|
2009-05-31 07:34:08 -04:00
|
|
|
});
|
2009-01-02 10:08:58 -05:00
|
|
|
}
|
2009-04-11 10:37:24 -04:00
|
|
|
};
|
2009-08-18 05:53:37 -04:00
|
|
|
|
|
|
|
$(document).ready(function($){ wpWidgets.init(); });
|
2009-01-02 10:08:58 -05:00
|
|
|
|
2009-04-11 10:37:24 -04:00
|
|
|
})(jQuery);
|