Вход Регистрация
Файл: wordpress/wp-admin/js/widgets.js
Строк: 428
<?php
/*global ajaxurl, isRtl */
var wpWidgets;
(function($) {

wpWidgets = {

    
init : function() {
        var 
remthe_id,
            
self this,
            
chooser = $('.widgets-chooser'),
            
selectSidebar chooser.find('.widgets-chooser-sidebars'),
            
sidebars = $('div.widgets-sortables'),
            
isRTL = !! ( 'undefined' !== typeof isRtl && isRtl );

        $(
'#widgets-right .sidebar-name').click( function() {
            var 
$this = $(this),
                
$wrap $this.closest('.widgets-holder-wrap');

            if ( 
$wrap.hasClass('closed') ) {
                
$wrap.removeClass('closed');
                
$this.parent().sortable('refresh');
            } else {
                
$wrap.addClass('closed');
            }
        });

        $(
'#widgets-left .sidebar-name').click( function() {
            $(
this).closest('.widgets-holder-wrap').toggleClass('closed');
        });

        $(
document.body).bind('click.widgets-toggle', function(e) {
            var 
target = $(e.target),
                
css = { 'z-index'100 },
                
widgetinsidetargetWidthwidgetWidthmargin;

            if ( 
target.parents('.widget-top').length && ! target.parents('#available-widgets').length ) {
                
widget target.closest('div.widget');
                
inside widget.children('.widget-inside');
                
targetWidth parseIntwidget.find('input.widget-width').val(), 10 ),
                
widgetWidth widget.parent().width();

                if ( 
inside.is(':hidden') ) {
                    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';
                        }

                        
cssmargin ] = widgetWidth - ( targetWidth 30 ) + 'px';
                        
widget.csscss );
                    }
                    
widget.addClass'open' );
                    
inside.slideDown('fast');
                } else {
                    
inside.slideUp('fast', function() {
                        
widget.attr'style''' );
                        
widget.removeClass'open' );
                    });
                }
                
e.preventDefault();
            } else if ( 
target.hasClass('widget-control-save') ) {
                
wpWidgets.savetarget.closest('div.widget'), 01);
                
e.preventDefault();
            } else if ( 
target.hasClass('widget-control-remove') ) {
                
wpWidgets.savetarget.closest('div.widget'), 11);
                
e.preventDefault();
            } else if ( 
target.hasClass('widget-control-close') ) {
                
widget target.closest('div.widget');
                
widget.removeClass'open' );
                
wpWidgets.closewidget );
                
e.preventDefault();
            }
        });

        
sidebars.children('.widget').each( function() {
            var 
$this = $(this);

            
wpWidgets.appendTitlethis );

            if ( 
$this.find'p.widget-error' ).length ) {
                
$this.find'a.widget-action' ).trigger('click');
            }
        });

        $(
'#widget-list').children('.widget').draggable({
            
connectToSortable'div.widgets-sortables',
            
handle'> .widget-top > .widget-title',
            
distance2,
            
helper'clone',
            
zIndex100,
            
containment'document',
            
start: function( eventui ) {
                var 
chooser = $(this).find('.widgets-chooser');

                
ui.helper.find('div.widget-description').hide();
                
the_id this.id;

                if ( 
chooser.length ) {
                    
// Hide the chooser and move it out of the widget
                    
$( '#wpbody-content' ).appendchooser.hide() );
                    
// Delete the cloned chooser from the drag helper
                    
ui.helper.find('.widgets-chooser').remove();
                    
self.clearWidgetSelection();
                }
            },
            
stop: function() {
                if ( 
rem ) {
                    $(
rem).hide();
                }

                
rem '';
            }
        });

        
sidebars.sortable({
            
placeholder'widget-placeholder',
            
items'> .widget',
            
handle'> .widget-top > .widget-title',
            
cursor'move',
            
distance2,
            
containment'document',
            
start: function( eventui ) {
                var 
height$this = $(this),
                    
$wrap $this.parent(),
                    
inside ui.item.children('.widget-inside');

                if ( 
inside.css('display') === 'block' ) {
                    
inside.hide();
                    $(
this).sortable('refreshPositions');
                }

                if ( ! 
$wrap.hasClass('closed') ) {
                    
// 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.
                    
height ui.item.hasClass('ui-draggable') ? $this.height() : $this.height();
                    
$this.css'min-height'height 'px' );
                }
            },

            
stop: function( eventui ) {
                var 
addNewwidgetNumber$sidebar$childrenchilditem,
                    
$widget ui.item,
                    
id the_id;

                if ( 
$widget.hasClass('deleting') ) {
                    
wpWidgets.save$widget10); // delete widget
                    
$widget.remove();
                    return;
                }

                
addNew $widget.find('input.add_new').val();
                
widgetNumber $widget.find('input.multi_number').val();

                
$widget.attr'style''' ).removeClass('ui-draggable');
                
the_id '';

                if ( 
addNew ) {
                    if ( 
'multi' === addNew ) {
                        
$widget.html(
                            
$widget.html().replace( /<[^<>]+>/g, function( tag ) {
                                return 
tag.replace( /__i__|%i%/gwidgetNumber );
                            })
                        );

                        
$widget.attr'id'id.replace'__i__'widgetNumber ) );
                        
widgetNumber++;

                        $( 
'div#' id ).find'input.multi_number' ).valwidgetNumber );
                    } else if ( 
'single' === addNew ) {
                        
$widget.attr'id''new-' id );
                        
rem 'div#' id;
                    }

                    
wpWidgets.save$widget00);
                    
$widget.find('input.add_new').val('');
                    $( 
document ).trigger'widget-added', [ $widget ] );
                }

                
$sidebar $widget.parent();

                if ( 
$sidebar.parent().hasClass('closed') ) {
                    
$sidebar.parent().removeClass('closed');
                    
$children $sidebar.children('.widget');

                    
// Make sure the dropped widget is at the top
                    
if ( $children.length ) {
                        
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') );
                }
            },

            
activate: function() {
                $(
this).parent().addClass'widget-hover' );
            },

            
deactivate: function() {
                
// Remove all min-height added on "start"
                
$(this).css'min-height''' ).parent().removeClass'widget-hover' );
            },

            
receive: function( eventui ) {
                var 
$sender = $( ui.sender );

                
// Don't add more widgets to orphaned sidebars
                
if ( this.id.indexOf('orphaned_widgets') > -) {
                    
$sender.sortable('cancel');
                    return;
                }

                
// If the last widget was moved out of an orphaned sidebar, close and remove it.
                
if ( $sender.attr('id').indexOf('orphaned_widgets') > -&& ! $sender.children('.widget').length ) {
                    
$sender.parents('.orphan-sidebar').slideUp400, function(){ $(this).remove(); } );
                }
            }
        }).
sortable'option''connectWith''div.widgets-sortables' );

        $(
'#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').empty();
            },
            
over: function(e,ui) {
                
ui.draggable.addClass('deleting');
                $(
'div.widget-placeholder').hide();

                if ( 
ui.draggable.hasClass('ui-sortable-helper') ) {
                    $(
'#removing-widget').show().children('span')
                    .
htmlui.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').empty();
            }
        });

        
// Area Chooser
        
$( '#widgets-right .widgets-holder-wrap' ).each( function( indexelement ) {
            var 
$element = $( element ),
                
name $element.find'.sidebar-name h3' ).text(),
                
id $element.find'.widgets-sortables' ).attr'id' ),
                
li = $('<li tabindex="0">').text( $.trimname ) );

            if ( 
index === ) {
                
li.addClass'widgets-chooser-selected' );
            }

            
selectSidebar.appendli );
            
li.data'sidebarId'id );
        });

        $( 
'#available-widgets .widget .widget-title' ).on'click.widgets-chooser', function() {
            var 
$widget = $(this).closest'.widget' );

            if ( 
$widget.hasClass'widget-in-question' ) || $( '#widgets-left' ).hasClass'chooser' ) ) {
                
self.closeChooser();
            } else {
                
// Open the chooser
                
self.clearWidgetSelection();
                $( 
'#widgets-left' ).addClass'chooser' );
                
$widget.addClass'widget-in-question' ).children'.widget-description' ).afterchooser );

                
chooser.slideDown300, function() {
                    
selectSidebar.find('.widgets-chooser-selected').focus();
                });

                
selectSidebar.find'li' ).on'focusin.widgets-chooser', function() {
                    
selectSidebar.find('.widgets-chooser-selected').removeClass'widgets-chooser-selected' );
                    $(
this).addClass'widgets-chooser-selected' );
                } );
            }
        });

        
// Add event handlers
        
chooser.on'click.widgets-chooser', function( event ) {
            var 
$target = $( event.target );

            if ( 
$target.hasClass('button-primary') ) {
                
self.addWidgetchooser );
                
self.closeChooser();
            } else if ( 
$target.hasClass('button-secondary') ) {
                
self.closeChooser();
            }
        }).
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.addWidgetchooser );
                    
self.closeChooser();
                }
            } else if ( 
event.which === $.ui.keyCode.ESCAPE ) {
                
self.closeChooser();
            }
        });
    },

    
saveOrder : function( sidebarId ) {
        var 
data = {
            
action'widgets-order',
            
savewidgets: $('#_wpnonce_widgets').val(),
            
sidebars: []
        };

        if ( 
sidebarId ) {
            $( 
'#' sidebarId ).find'.spinner:first' ).addClass'is-active' );
        }

        $(
'div.widgets-sortables').each( function() {
            if ( $(
this).sortable ) {
                
data['sidebars[' + $(this).attr('id') + ']'] = $(this).sortable('toArray').join(',');
            }
        });

        $.
postajaxurldata, function() {
            $( 
'.spinner' ).removeClass'is-active' );
        });
    },

    
save : function( widgetdelanimateorder ) {
        var 
sidebarId widget.closest('div.widgets-sortables').attr('id'),
            
data widget.find('form').serialize(), a;

        
widget = $(widget);
        $( 
'.spinner'widget ).addClass'is-active' );

        
= {
            
action'save-widget',
            
savewidgets: $('#_wpnonce_widgets').val(),
            
sidebarsidebarId
        
};

        if ( 
del ) {
            
a.delete_widget 1;
        }

        
data += '&' + $.param(a);

        $.
postajaxurldata, function(r) {
            var 
id;

            if ( 
del ) {
                if ( ! $(
'input.widget_number'widget).val() ) {
                    
id = $('input.widget-id'widget).val();
                    $(
'#available-widgets').find('input.widget-id').each(function(){
                        if ( $(
this).val() === id ) {
                            $(
this).closest('div.widget').show();
                        }
                    });
                }

                if ( 
animate ) {
                    
order 0;
                    
widget.slideUp('fast', function(){
                        $(
this).remove();
                        
wpWidgets.saveOrder();
                    });
                } else {
                    
widget.remove();
                }
            } else {
                $( 
'.spinner' ).removeClass'is-active' );
                if ( 
&& r.length ) {
                    $( 
'div.widget-content'widget ).html);
                    
wpWidgets.appendTitlewidget );
                    $( 
document ).trigger'widget-updated', [ widget ] );
                }
            }
            if ( 
order ) {
                
wpWidgets.saveOrder();
            }
        });
    },

    
appendTitle : function(widget) {
        var 
title = $('input[id*="-title"]'widget).val() || '';

        if ( 
title ) {
            
title ': ' title.replace(/<[^<>]+>/g'').replace(/</g'&lt;').replace(/>/g'&gt;');
        }

        $(
widget).children('.widget-top').children('.widget-title').children()
                .
children('.in-widget-title').html(title);

    },

    
close : function(widget) {
        
widget.children('.widget-inside').slideUp('fast', function() {
            
widget.attr'style''' );
        });
    },

    
addWidget: function( chooser ) {
        var 
widgetwidgetIdaddnviewportTopviewportBottomsidebarBounds,
            
sidebarId chooser.find'.widgets-chooser-selected' ).data('sidebarId'),
            
sidebar = $( '#' sidebarId );

        
widget = $('#available-widgets').find('.widget-in-question').clone();
        
widgetId widget.attr('id');
        
add widget.find'input.add_new' ).val();
        
widget.find'input.multi_number' ).val();

        
// Remove the cloned chooser from the widget
        
widget.find('.widgets-chooser').remove();

        if ( 
'multi' === add ) {
            
widget.html(
                
widget.html().replace( /<[^<>]+>/g, function(m) {
                    return 
m.replace( /__i__|%i%/g);
                })
            );

            
widget.attr'id'widgetId.replace'__i__') );
            
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');

        
sidebar.appendwidget );
        
sidebar.sortable('refresh');

        
wpWidgets.savewidget00);
        
// No longer "new" widget
        
widget.find'input.add_new' ).val('');

        $( 
document ).trigger'widget-added', [ widget ] );

        
/*
         * 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.
         */
        
viewportTop = $(window).scrollTop();
        
viewportBottom viewportTop + $(window).height();
        
sidebarBounds sidebar.offset();

        
sidebarBounds.bottom sidebarBounds.top sidebar.outerHeight();

        if ( 
viewportTop sidebarBounds.bottom || viewportBottom sidebarBounds.top ) {
            $( 
'html, body' ).animate({
                
scrollTopsidebarBounds.top 130
            
}, 200 );
        }

        
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;

        $( 
'.widgets-chooser' ).slideUp200, function() {
            $( 
'#wpbody-content' ).appendthis );
            
self.clearWidgetSelection();
        });
    },

    
clearWidgetSelection: function() {
        $( 
'#widgets-left' ).removeClass'chooser' );
        $( 
'.widget-in-question' ).removeClass'widget-in-question' );
    }
};

$(
document).ready( function(){ wpWidgets.init(); } );

})(
jQuery);
?>
Онлайн: 2
Реклама