[ Index ]

WordPress Cross Reference

title

Body

[close]

/wp-admin/js/ -> widgets.js (source)

   1  /*global ajaxurl, isRtl */
   2  var wpWidgets;
   3  (function($) {
   4  
   5  wpWidgets = {
   6  
   7      init : function() {
   8          var rem, the_id,
   9              self = this,
  10              chooser = $('.widgets-chooser'),
  11              selectSidebar = chooser.find('.widgets-chooser-sidebars'),
  12              sidebars = $('div.widgets-sortables'),
  13              isRTL = !! ( 'undefined' !== typeof isRtl && isRtl );
  14  
  15          $('#widgets-right .sidebar-name').click( function() {
  16              var $this = $(this),
  17                  $wrap = $this.closest('.widgets-holder-wrap');
  18  
  19              if ( $wrap.hasClass('closed') ) {
  20                  $wrap.removeClass('closed');
  21                  $this.parent().sortable('refresh');
  22              } else {
  23                  $wrap.addClass('closed');
  24              }
  25          });
  26  
  27          $('#widgets-left .sidebar-name').click( function() {
  28              $(this).closest('.widgets-holder-wrap').toggleClass('closed');
  29          });
  30  
  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;
  35  
  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();
  41  
  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';
  46                          } else {
  47                              margin = isRTL ? 'margin-left' : 'margin-right';
  48                          }
  49  
  50                          css[ margin ] = widgetWidth - ( targetWidth + 30 ) + 'px';
  51                          widget.css( css );
  52                      }
  53                      inside.slideDown('fast');
  54                  } else {
  55                      inside.slideUp('fast', function() {
  56                          widget.attr( 'style', '' );
  57                      });
  58                  }
  59                  e.preventDefault();
  60              } else if ( target.hasClass('widget-control-save') ) {
  61                  wpWidgets.save( target.closest('div.widget'), 0, 1, 0 );
  62                  e.preventDefault();
  63              } else if ( target.hasClass('widget-control-remove') ) {
  64                  wpWidgets.save( target.closest('div.widget'), 1, 1, 0 );
  65                  e.preventDefault();
  66              } else if ( target.hasClass('widget-control-close') ) {
  67                  wpWidgets.close( target.closest('div.widget') );
  68                  e.preventDefault();
  69              }
  70          });
  71  
  72          sidebars.children('.widget').each( function() {
  73              var $this = $(this);
  74  
  75              wpWidgets.appendTitle( this );
  76  
  77              if ( $this.find( 'p.widget-error' ).length ) {
  78                  $this.find( 'a.widget-action' ).trigger('click');
  79              }
  80          });
  81  
  82          $('#widget-list').children('.widget').draggable({
  83              connectToSortable: 'div.widgets-sortables',
  84              handle: '> .widget-top > .widget-title',
  85              distance: 2,
  86              helper: 'clone',
  87              zIndex: 100,
  88              containment: 'document',
  89              start: function( event, ui ) {
  90                  var chooser = $(this).find('.widgets-chooser');
  91  
  92                  ui.helper.find('div.widget-description').hide();
  93                  the_id = this.id;
  94  
  95                  if ( chooser.length ) {
  96                      // Hide the chooser and move it out of the widget
  97                      $( '#wpbody-content' ).append( chooser.hide() );
  98                      // Delete the cloned chooser from the drag helper
  99                      ui.helper.find('.widgets-chooser').remove();
 100                      self.clearWidgetSelection();
 101                  }
 102              },
 103              stop: function() {
 104                  if ( rem ) {
 105                      $(rem).hide();
 106                  }
 107  
 108                  rem = '';
 109              }
 110          });
 111  
 112          sidebars.sortable({
 113              placeholder: 'widget-placeholder',
 114              items: '> .widget',
 115              handle: '> .widget-top > .widget-title',
 116              cursor: 'move',
 117              distance: 2,
 118              containment: 'document',
 119              start: function( event, ui ) {
 120                  var height, $this = $(this),
 121                      $wrap = $this.parent(),
 122                      inside = ui.item.children('.widget-inside');
 123  
 124                  if ( inside.css('display') === 'block' ) {
 125                      inside.hide();
 126                      $(this).sortable('refreshPositions');
 127                  }
 128  
 129                  if ( ! $wrap.hasClass('closed') ) {
 130                      // Lock all open sidebars min-height when starting to drag.
 131                      // Prevents jumping when dragging a widget from an open sidebar to a closed sidebar below.
 132                      height = ui.item.hasClass('ui-draggable') ? $this.height() : 1 + $this.height();
 133                      $this.css( 'min-height', height + 'px' );
 134                  }
 135              },
 136  
 137              stop: function( event, ui ) {
 138                  var addNew, widgetNumber, $sidebar, $children, child, item,
 139                      $widget = ui.item,
 140                      id = the_id;
 141  
 142                  if ( $widget.hasClass('deleting') ) {
 143                      wpWidgets.save( $widget, 1, 0, 1 ); // delete widget
 144                      $widget.remove();
 145                      return;
 146                  }
 147  
 148                  addNew = $widget.find('input.add_new').val();
 149                  widgetNumber = $widget.find('input.multi_number').val();
 150  
 151                  $widget.attr( 'style', '' ).removeClass('ui-draggable');
 152                  the_id = '';
 153  
 154                  if ( addNew ) {
 155                      if ( 'multi' === addNew ) {
 156                          $widget.html(
 157                              $widget.html().replace( /<[^<>]+>/g, function( tag ) {
 158                                  return tag.replace( /__i__|%i%/g, widgetNumber );
 159                              })
 160                          );
 161  
 162                          $widget.attr( 'id', id.replace( '__i__', widgetNumber ) );
 163                          widgetNumber++;
 164  
 165                          $( 'div#' + id ).find( 'input.multi_number' ).val( widgetNumber );
 166                      } else if ( 'single' === addNew ) {
 167                          $widget.attr( 'id', 'new-' + id );
 168                          rem = 'div#' + id;
 169                      }
 170  
 171                      wpWidgets.save( $widget, 0, 0, 1 );
 172                      $widget.find('input.add_new').val('');
 173                  }
 174  
 175                  $sidebar = $widget.parent();
 176  
 177                  if ( $sidebar.parent().hasClass('closed') ) {
 178                      $sidebar.parent().removeClass('closed');
 179                      $children = $sidebar.children('.widget');
 180  
 181                      // Make sure the dropped widget is at the top
 182                      if ( $children.length > 1 ) {
 183                          child = $children.get(0);
 184                          item = $widget.get(0);
 185  
 186                          if ( child.id && item.id && child.id !== item.id ) {
 187                              $( child ).before( $widget );
 188                          }
 189                      }
 190                  }
 191  
 192                  if ( addNew ) {
 193                      $widget.find( 'a.widget-action' ).trigger('click');
 194                  } else {
 195                      wpWidgets.saveOrder( $sidebar.attr('id') );
 196                  }
 197              },
 198  
 199              activate: function() {
 200                  $(this).parent().addClass( 'widget-hover' );
 201              },
 202  
 203              deactivate: function() {
 204                  // Remove all min-height added on "start"
 205                  $(this).css( 'min-height', '' ).parent().removeClass( 'widget-hover' );
 206              },
 207  
 208              receive: function( event, ui ) {
 209                  var $sender = $( ui.sender );
 210  
 211                  // Don't add more widgets to orphaned sidebars
 212                  if ( this.id.indexOf('orphaned_widgets') > -1 ) {
 213                      $sender.sortable('cancel');
 214                      return;
 215                  }
 216  
 217                  // If the last widget was moved out of an orphaned sidebar, close and remove it.
 218                  if ( $sender.attr('id').indexOf('orphaned_widgets') > -1 && ! $sender.children('.widget').length ) {
 219                      $sender.parents('.orphan-sidebar').slideUp( 400, function(){ $(this).remove(); } );
 220                  }
 221              }
 222          }).sortable( 'option', 'connectWith', 'div.widgets-sortables' );
 223  
 224          $('#available-widgets').droppable({
 225              tolerance: 'pointer',
 226              accept: function(o){
 227                  return $(o).parent().attr('id') !== 'widget-list';
 228              },
 229              drop: function(e,ui) {
 230                  ui.draggable.addClass('deleting');
 231                  $('#removing-widget').hide().children('span').html('');
 232              },
 233              over: function(e,ui) {
 234                  ui.draggable.addClass('deleting');
 235                  $('div.widget-placeholder').hide();
 236  
 237                  if ( ui.draggable.hasClass('ui-sortable-helper') ) {
 238                      $('#removing-widget').show().children('span')
 239                      .html( ui.draggable.find('div.widget-title').children('h4').html() );
 240                  }
 241              },
 242              out: function(e,ui) {
 243                  ui.draggable.removeClass('deleting');
 244                  $('div.widget-placeholder').show();
 245                  $('#removing-widget').hide().children('span').html('');
 246              }
 247          });
 248  
 249          // Area Chooser
 250          $( '#widgets-right .widgets-holder-wrap' ).each( function( index, element ) {
 251              var $element = $( element ),
 252                  name = $element.find( '.sidebar-name h3' ).text(),
 253                  id = $element.find( '.widgets-sortables' ).attr( 'id' ),
 254                  li = $('<li tabindex="0">').text( $.trim( name ) );
 255  
 256              if ( index === 0 ) {
 257                  li.addClass( 'widgets-chooser-selected' );
 258              }
 259  
 260              selectSidebar.append( li );
 261              li.data( 'sidebarId', id );
 262          });
 263  
 264          $( '#available-widgets .widget .widget-title' ).on( 'click.widgets-chooser', function() {
 265              var $widget = $(this).closest( '.widget' );
 266  
 267              if ( $widget.hasClass( 'widget-in-question' ) || $( '#widgets-left' ).hasClass( 'chooser' ) ) {
 268                  self.closeChooser();
 269              } else {
 270                  // Open the chooser
 271                  self.clearWidgetSelection();
 272                  $( '#widgets-left' ).addClass( 'chooser' );
 273                  $widget.addClass( 'widget-in-question' ).children( '.widget-description' ).after( chooser );
 274  
 275                  chooser.slideDown( 300, function() {
 276                      selectSidebar.find('.widgets-chooser-selected').focus();
 277                  });
 278  
 279                  selectSidebar.find( 'li' ).on( 'focusin.widgets-chooser', function() {
 280                      selectSidebar.find('.widgets-chooser-selected').removeClass( 'widgets-chooser-selected' );
 281                      $(this).addClass( 'widgets-chooser-selected' );
 282                  } );
 283              }
 284          });
 285  
 286          // Add event handlers
 287          chooser.on( 'click.widgets-chooser', function( event ) {
 288              var $target = $( event.target );
 289  
 290              if ( $target.hasClass('button-primary') ) {
 291                  self.addWidget( chooser );
 292                  self.closeChooser();
 293              } else if ( $target.hasClass('button-secondary') ) {
 294                  self.closeChooser();
 295              }
 296          }).on( 'keyup.widgets-chooser', function( event ) {
 297              if ( event.which === $.ui.keyCode.ENTER ) {
 298                  if ( $( event.target ).hasClass('button-secondary') ) {
 299                      // Close instead of adding when pressing Enter on the Cancel button
 300                      self.closeChooser();
 301                  } else {
 302                      self.addWidget( chooser );
 303                      self.closeChooser();
 304                  }
 305              } else if ( event.which === $.ui.keyCode.ESCAPE ) {
 306                  self.closeChooser();
 307              }
 308          });
 309      },
 310  
 311      saveOrder : function( sidebarId ) {
 312          var data = {
 313              action: 'widgets-order',
 314              savewidgets: $('#_wpnonce_widgets').val(),
 315              sidebars: []
 316          };
 317  
 318          if ( sidebarId ) {
 319              $( '#' + sidebarId ).find('.spinner:first').css('display', 'inline-block');
 320          }
 321  
 322          $('div.widgets-sortables').each( function() {
 323              if ( $(this).sortable ) {
 324                  data['sidebars[' + $(this).attr('id') + ']'] = $(this).sortable('toArray').join(',');
 325              }
 326          });
 327  
 328          $.post( ajaxurl, data, function() {
 329              $('.spinner').hide();
 330          });
 331      },
 332  
 333      save : function( widget, del, animate, order ) {
 334          var sidebarId = widget.closest('div.widgets-sortables').attr('id'),
 335              data = widget.find('form').serialize(), a;
 336  
 337          widget = $(widget);
 338          $('.spinner', widget).show();
 339  
 340          a = {
 341              action: 'save-widget',
 342              savewidgets: $('#_wpnonce_widgets').val(),
 343              sidebar: sidebarId
 344          };
 345  
 346          if ( del ) {
 347              a.delete_widget = 1;
 348          }
 349  
 350          data += '&' + $.param(a);
 351  
 352          $.post( ajaxurl, data, function(r) {
 353              var id;
 354  
 355              if ( del ) {
 356                  if ( ! $('input.widget_number', widget).val() ) {
 357                      id = $('input.widget-id', widget).val();
 358                      $('#available-widgets').find('input.widget-id').each(function(){
 359                          if ( $(this).val() === id ) {
 360                              $(this).closest('div.widget').show();
 361                          }
 362                      });
 363                  }
 364  
 365                  if ( animate ) {
 366                      order = 0;
 367                      widget.slideUp('fast', function(){
 368                          $(this).remove();
 369                          wpWidgets.saveOrder();
 370                      });
 371                  } else {
 372                      widget.remove();
 373                  }
 374              } else {
 375                  $('.spinner').hide();
 376                  if ( r && r.length > 2 ) {
 377                      $( 'div.widget-content', widget ).html(r);
 378                      wpWidgets.appendTitle( widget );
 379                  }
 380              }
 381              if ( order ) {
 382                  wpWidgets.saveOrder();
 383              }
 384          });
 385      },
 386  
 387      appendTitle : function(widget) {
 388          var title = $('input[id*="-title"]', widget).val() || '';
 389  
 390          if ( title ) {
 391              title = ': ' + title.replace(/<[^<>]+>/g, '').replace(/</g, '&lt;').replace(/>/g, '&gt;');
 392          }
 393  
 394          $(widget).children('.widget-top').children('.widget-title').children()
 395                  .children('.in-widget-title').html(title);
 396  
 397      },
 398  
 399      close : function(widget) {
 400          widget.children('.widget-inside').slideUp('fast', function() {
 401              widget.attr( 'style', '' );
 402          });
 403      },
 404  
 405      addWidget: function( chooser ) {
 406          var widget, widgetId, add, n, viewportTop, viewportBottom, sidebarBounds,
 407              sidebarId = chooser.find( '.widgets-chooser-selected' ).data('sidebarId'),
 408              sidebar = $( '#' + sidebarId );
 409  
 410          widget = $('#available-widgets').find('.widget-in-question').clone();
 411          widgetId = widget.attr('id');
 412          add = widget.find( 'input.add_new' ).val();
 413          n = widget.find( 'input.multi_number' ).val();
 414  
 415          // Remove the cloned chooser from the widget
 416          widget.find('.widgets-chooser').remove();
 417  
 418          if ( 'multi' === add ) {
 419              widget.html(
 420                  widget.html().replace( /<[^<>]+>/g, function(m) {
 421                      return m.replace( /__i__|%i%/g, n );
 422                  })
 423              );
 424  
 425              widget.attr( 'id', widgetId.replace( '__i__', n ) );
 426              n++;
 427              $( '#' + widgetId ).find('input.multi_number').val(n);
 428          } else if ( 'single' === add ) {
 429              widget.attr( 'id', 'new-' + widgetId );
 430              $( '#' + widgetId ).hide();
 431          }
 432  
 433          // Open the widgets container
 434          sidebar.closest( '.widgets-holder-wrap' ).removeClass('closed');
 435  
 436          sidebar.append( widget );
 437          sidebar.sortable('refresh');
 438  
 439          wpWidgets.save( widget, 0, 0, 1 );
 440          // No longer "new" widget
 441          widget.find( 'input.add_new' ).val('');
 442  
 443          /*
 444           * Check if any part of the sidebar is visible in the viewport. If it is, don't scroll.
 445           * Otherwise, scroll up to so the sidebar is in view.
 446           *
 447           * We do this by comparing the top and bottom, of the sidebar so see if they are within
 448           * the bounds of the viewport.
 449           */
 450          viewportTop = $(window).scrollTop();
 451          viewportBottom = viewportTop + $(window).height();
 452          sidebarBounds = sidebar.offset();
 453  
 454          sidebarBounds.bottom = sidebarBounds.top + sidebar.outerHeight();
 455  
 456          if ( viewportTop > sidebarBounds.bottom || viewportBottom < sidebarBounds.top ) {
 457              $( 'html, body' ).animate({
 458                  scrollTop: sidebarBounds.top - 130
 459              }, 200 );
 460          }
 461  
 462          window.setTimeout( function() {
 463              // Cannot use a callback in the animation above as it fires twice,
 464              // have to queue this "by hand".
 465              widget.find( '.widget-title' ).trigger('click');
 466          }, 250 );
 467      },
 468  
 469      closeChooser: function() {
 470          var self = this;
 471  
 472          $( '.widgets-chooser' ).slideUp( 200, function() {
 473              $( '#wpbody-content' ).append( this );
 474              self.clearWidgetSelection();
 475          });
 476      },
 477  
 478      clearWidgetSelection: function() {
 479          $( '#widgets-left' ).removeClass( 'chooser' );
 480          $( '.widget-in-question' ).removeClass( 'widget-in-question' );
 481      }
 482  };
 483  
 484  $(document).ready( function(){ wpWidgets.init(); } );
 485  
 486  })(jQuery);


Generated: Tue Mar 25 01:41:18 2014 WordPress honlapkészítés: online1.hu