[ Index ]

MailPress 7.2

[ Index ]     [ Classes ]     [ Functions ]     [ Variables ]     [ Constants ]     [ Statistics ]    

title

Body

[close]

/mp-includes/js/ -> mp_lists.js (source)

   1  (function($) {
   2  var fs = {add:'ajaxAdd',del:'ajaxDel',dim:'ajaxDim',process:'process',recolor:'recolor'}, wpList;
   3  
   4  wpList = {
   5      settings: {
   6          url: ajaxurl, type: 'POST',
   7          response: 'ajax-response',
   8  
   9          what: '',
  10          alt: 'alternate', altOffset: 0,
  11          addColor: null, delColor: null, dimAddColor: null, dimDelColor: null,
  12  
  13          confirm: null,
  14          addBefore: null, addAfter: null,
  15          delBefore: null, delAfter: null,
  16          dimBefore: null, dimAfter: null
  17      },
  18  
  19      nonce: function(e,s) {
  20          var url = wpAjax.unserialize(e.attr('href'));
  21          return s.nonce || url._ajax_nonce || $('#' + s.element + ' input[name="_ajax_nonce"]').val() || url._wpnonce || $('#' + s.element + ' input[name="_wpnonce"]').val() || 0;
  22      },
  23  
  24      parseClass: function(e,t) {
  25          var c = [], cl;
  26          try {
  27              cl = $(e).attr('class') || '';
  28              cl = cl.match(new RegExp(t+':[\\S]+'));
  29              if ( cl ) c = cl[0].split(':');
  30          } catch(r) {}
  31          return c;
  32      },
  33  
  34      pre: function(e,s,a) {
  35          var bg, r;
  36          s = $.extend( {}, this.wpList.settings, {
  37              element: null,
  38              nonce: 0,
  39              target: e.get(0)
  40          }, s || {} );
  41          if ( $.isFunction( s.confirm ) ) {
  42              if ( 'add' != a ) {
  43                  bg = $('#' + s.element).css('backgroundColor');
  44                  $('#' + s.element).css('backgroundColor', '#FF9966');
  45              }
  46              r = s.confirm.call(this, e, s, a, bg);
  47              if ( 'add' != a ) $('#' + s.element).css('backgroundColor', bg );
  48              if ( !r ) { return false; }
  49          }
  50          return s;
  51      },
  52  
  53      ajaxAdd: function( e, s ) {
  54          e = $(e);
  55          s = s || {};
  56          var list = this, cls = wpList.parseClass(e,'add'), es, valid, formData, res, rres;
  57          s = wpList.pre.call( list, e, s, 'add' );
  58  
  59          s.element = cls[2] || e.attr( 'id' ) || s.element || null;
  60          if ( cls[3] ) s.addColor = '#' + cls[3];
  61          else s.addColor = s.addColor || '#FFFF33';
  62  
  63          if ( !s )
  64              return false;
  65  
  66          if ( !e.is('[class^="add:' + list.id + ':"]') )
  67              return !wpList.add.call( list, e, s );
  68  
  69          if ( !s.element )
  70              return true;
  71  
  72          s.action = 'mp_ajax';
  73          s.mp_action = 'add_' + s.what;
  74  
  75          s.nonce = wpList.nonce(e,s);
  76  
  77          es = $('#' + s.element + ' :input').not('[name="_ajax_nonce"], [name="_wpnonce"], [name="action"]');
  78          valid = wpAjax.validateForm( '#' + s.element );
  79  
  80          if ( !valid )
  81              return false;
  82  
  83          s.data = $.param( $.extend( { _ajax_nonce: s.nonce, action: s.action, mp_action: s.mp_action }, wpAjax.unserialize( cls[4] || '' ) ) );
  84          formData = $.isFunction(es.fieldSerialize) ? es.fieldSerialize() : es.serialize();
  85  
  86          if ( formData )
  87              s.data += '&' + formData;
  88  
  89          if ( $.isFunction(s.addBefore) ) {
  90              s = s.addBefore( s );
  91              if ( !s )
  92                  return true;
  93          }
  94  
  95          if ( !s.data.match(/_ajax_nonce=[a-f0-9]+/) )
  96              return true;
  97  
  98          s.success = function(r) {
  99              res = wpAjax.parseAjaxResponse(r, s.response, s.element);
 100  
 101              rres = r;
 102  
 103              if ( !res || res.errors )
 104                  return false;
 105  
 106              if ( true === res )
 107                  return true;
 108  
 109              jQuery.each( res.responses, function() {
 110                  wpList.add.call( list, this.data, $.extend( {}, s, { // this.firstChild.nodevalue
 111                      pos: this.position || 0,
 112                      id: this.id || 0,
 113                      oldId: this.oldId || null
 114                  } ) );
 115              } );
 116  
 117              list.wpList.recolor();
 118              $(list).trigger( 'wpListAddEnd', [ s, list.wpList ] );
 119              wpList.clear.call(list,'#' + s.element);
 120          };
 121  
 122          s.complete = function(x, st) {
 123              if ( $.isFunction(s.addAfter) ) {
 124                  var _s = $.extend( { xml: x, status: st, parsed: res }, s );
 125                  s.addAfter( rres, _s );
 126              }
 127          };
 128  
 129          $.ajax( s );
 130          return false;
 131      },
 132  
 133      ajaxDel: function( e, s ) {
 134          e = $(e);
 135          s = s || {};
 136          var list = this, cls = wpList.parseClass(e,'delete'), element, res, rres;
 137  
 138          s = wpList.pre.call( list, e, s, 'delete' );
 139  
 140          s.element = cls[2] || s.element || null;
 141  
 142          if ( cls[3] )
 143              s.delColor = '#' + cls[3];
 144          else
 145              s.delColor = s.delColor || '#faa';
 146  
 147          if ( !s || !s.element )
 148              return false;
 149  
 150          s.action = 'mp_ajax';
 151          s.mp_action = 'delete_' + s.what;
 152  
 153          s.nonce = wpList.nonce(e,s);
 154  
 155          s.data = $.extend(
 156              { action: s.action, mp_action: s.mp_action, id: s.element.split('-').pop(), _ajax_nonce: s.nonce },
 157              wpAjax.unserialize( cls[4] || '' )
 158          );
 159  
 160          if ( $.isFunction(s.delBefore) ) {
 161              s = s.delBefore( s, list );
 162              if ( !s )
 163                  return true;
 164          }
 165  
 166          if ( !s.data._ajax_nonce )
 167              return true;
 168  
 169          element = $('#' + s.element);
 170  
 171          if ( 'none' != s.delColor ) {
 172              element.css( 'backgroundColor', s.delColor ).fadeOut( 350, function(){
 173                  list.wpList.recolor();
 174                  $(list).trigger( 'wpListDelEnd', [ s, list.wpList ] );
 175              });
 176          } else {
 177              list.wpList.recolor();
 178              $(list).trigger( 'wpListDelEnd', [ s, list.wpList ] );
 179          }
 180  
 181          s.success = function(r) {
 182              res = wpAjax.parseAjaxResponse(r, s.response, s.element);
 183              rres = r;
 184  
 185              if ( !res || res.errors ) {
 186                  element.stop().stop().css( 'backgroundColor', '#faa' ).show().queue( function() { list.wpList.recolor(); $(this).dequeue(); } );
 187                  return false;
 188              }
 189          };
 190  
 191          s.complete = function(x, st) {
 192              if ( $.isFunction(s.delAfter) ) {
 193                  element.queue( function() {
 194                      var _s = $.extend( { xml: x, status: st, parsed: res }, s );
 195                      s.delAfter( rres, _s );
 196                  }).dequeue();
 197              }
 198          }
 199  
 200          $.ajax( s );
 201          return false;
 202      },
 203  
 204      ajaxDim: function( e, s ) {
 205          if ( $(e).parent().css('display') == 'none' ) // Prevent hidden links from being clicked by hotkeys
 206              return false;
 207  
 208          e = $(e);
 209          s = s || {};
 210  
 211          var list = this, cls = wpList.parseClass(e,'dim'), element, isClass, color, dimColor, res, rres;
 212  
 213          s = wpList.pre.call( list, e, s, 'dim' );
 214  
 215          s.element = cls[2] || s.element || null;
 216          s.dimClass =  cls[3] || s.dimClass || null;
 217  
 218          if ( cls[4] )
 219              s.dimAddColor = '#' + cls[4];
 220          else
 221              s.dimAddColor = s.dimAddColor || '#FFFF33';
 222  
 223          if ( cls[5] )
 224              s.dimDelColor = '#' + cls[5];
 225          else
 226              s.dimDelColor = s.dimDelColor || '#FF3333';
 227  
 228          if ( !s || !s.element || !s.dimClass )
 229              return true;
 230  
 231          s.action = 'mp_ajax';
 232          s.mp_action = 'dim_' + s.what;
 233  
 234          s.nonce = wpList.nonce(e,s);
 235  
 236          s.data = $.extend(
 237              { action: s.action, mp_action: s.mp_action, id: s.element.split('-').pop(), dimClass: s.dimClass, _ajax_nonce : s.nonce },
 238              wpAjax.unserialize( cls[6] || '' )
 239          );
 240  
 241          if ( $.isFunction(s.dimBefore) ) {
 242              s = s.dimBefore( s );
 243              if ( !s )
 244                  return true;
 245          }
 246  
 247          element = $('#' + s.element);
 248          isClass = element.toggleClass(s.dimClass).is('.' + s.dimClass);
 249          color = wpList.getColor( element );
 250          element.toggleClass( s.dimClass );
 251          dimColor = isClass ? s.dimAddColor : s.dimDelColor;
 252  
 253          if ( 'none' != dimColor ) {
 254              element
 255                  .animate( { backgroundColor: dimColor }, 'fast' )
 256                  .queue( function() { element.toggleClass(s.dimClass); $(this).dequeue(); } )
 257                  .animate( { backgroundColor: color }, { complete: function() {
 258                          $(this).css( 'backgroundColor', '' );
 259                          $(list).trigger( 'wpListDimEnd', [ s, list.wpList ] );
 260                      }
 261                  });
 262          } else {
 263              $(list).trigger( 'wpListDimEnd', [ s, list.wpList ] );
 264          }
 265  
 266          if ( !s.data._ajax_nonce )
 267              return true;
 268  
 269          s.success = function(r) {
 270              res = wpAjax.parseAjaxResponse(r, s.response, s.element);
 271              rres = r;
 272  
 273              if ( !res || res.errors ) {
 274                  element.stop().stop().css( 'backgroundColor', '#FF3333' )[isClass?'removeClass':'addClass'](s.dimClass).show().queue( function() { list.wpList.recolor(); $(this).dequeue(); } );
 275                  return false;
 276              }
 277          };
 278  
 279          s.complete = function(x, st) {
 280              if ( $.isFunction(s.dimAfter) ) {
 281                  element.queue( function() {
 282                      var _s = $.extend( { xml: x, status: st, parsed: res }, s );
 283                      s.dimAfter( rres, _s );
 284                  }).dequeue();
 285              }
 286          };
 287  
 288          $.ajax( s );
 289          return false;
 290      },
 291  
 292      getColor: function( el ) {
 293          var color = jQuery(el).css('backgroundColor');
 294  
 295          return color || '#ffffff';
 296      },
 297  
 298      add: function( e, s ) {
 299          e = $(e);
 300  
 301          var list = $(this), old = false, _s = { pos: 0, id: 0, oldId: null }, ba, ref, color;
 302  
 303          if ( 'string' == typeof s )
 304              s = { what: s };
 305  
 306          s = $.extend(_s, this.wpList.settings, s);
 307  
 308          if ( !e.size() || !s.what )
 309              return false;
 310  
 311          if ( s.oldId )
 312              old = $('#' + s.what + '-' + s.oldId);
 313  
 314          if ( s.id && ( s.id != s.oldId || !old || !old.size() ) )
 315              $('#' + s.what + '-' + s.id).remove();
 316  
 317          if ( old && old.size() ) {
 318              old.before(e);
 319              old.remove();
 320          } else if ( isNaN(s.pos) ) {
 321              ba = 'after';
 322  
 323              if ( '-' == s.pos.substr(0,1) ) {
 324                  s.pos = s.pos.substr(1);
 325                  ba = 'before';
 326              }
 327  
 328              ref = list.find( '#' + s.pos );
 329  
 330              if ( 1 === ref.size() )
 331                  ref[ba](e);
 332              else
 333                  list.append(e);
 334  
 335          } else if ( 'comment' != s.what || 0 === $('#' + s.element).length ) {
 336              if ( s.pos < 0 ) {
 337                  list.prepend(e);
 338              } else {
 339                  list.append(e);
 340              }
 341          }
 342  
 343          if ( s.alt ) {
 344              if ( ( list.children(':visible').index( e[0] ) + s.altOffset ) % 2 ) { e.removeClass( s.alt ); }
 345              else { e.addClass( s.alt ); }
 346          }
 347  
 348          if ( 'none' != s.addColor ) {
 349              color = wpList.getColor( e );
 350              e.css( 'backgroundColor', s.addColor ).animate( { backgroundColor: color }, { complete: function() { $(this).css( 'backgroundColor', '' ); } } );
 351          }
 352          list.each( function() { this.wpList.process( e ); } );
 353          return e;
 354      },
 355  
 356      clear: function(e) {
 357          var list = this, t, tag;
 358  
 359          e = $(e);
 360  
 361          if ( list.wpList && e.parents( '#' + list.id ).size() )
 362              return;
 363  
 364          e.find(':input').each( function() {
 365              if ( $(this).parents('.form-no-clear').size() )
 366                  return;
 367  
 368              t = this.type.toLowerCase();
 369              tag = this.tagName.toLowerCase();
 370  
 371              if ( 'text' == t || 'password' == t || 'textarea' == tag )
 372                  this.value = '';
 373              else if ( 'checkbox' == t || 'radio' == t )
 374                  this.checked = false;
 375              else if ( 'select' == tag )
 376                  this.selectedIndex = null;
 377          });
 378      },
 379  
 380      process: function(el) {
 381          var list = this,
 382              $el = $(el || document);
 383  
 384          $el.delegate( 'form[class^="add:' + list.id + ':"]', 'submit', function(){
 385              return list.wpList.add(this);
 386          });
 387  
 388          $el.delegate( '[class^="add:' + list.id + ':"]:not(form)', 'click', function(){
 389              return list.wpList.add(this);
 390          });
 391  
 392          $el.delegate( '[class^="delete:' + list.id + ':"]', 'click', function(){
 393              return list.wpList.del(this);
 394          });
 395  
 396          $el.delegate( '[class^="dim:' + list.id + ':"]', 'click', function(){
 397              return list.wpList.dim(this);
 398          });
 399      },
 400  
 401      recolor: function() {
 402          var list = this, items, eo;
 403  
 404          if ( !list.wpList.settings.alt )
 405              return;
 406  
 407          items = $('.list-item:visible', list);
 408  
 409          if ( !items.size() )
 410              items = $(list).children(':visible');
 411  
 412          eo = [':even',':odd'];
 413  
 414          if ( list.wpList.settings.altOffset % 2 )
 415              eo.reverse();
 416  
 417          items.filter(eo[0]).addClass(list.wpList.settings.alt).end().filter(eo[1]).removeClass(list.wpList.settings.alt);
 418      },
 419  
 420      init: function() {
 421          var lists = this;
 422  
 423          lists.wpList.process = function(a) {
 424              lists.each( function() {
 425                  this.wpList.process(a);
 426              } );
 427          };
 428  
 429          lists.wpList.recolor = function() {
 430              lists.each( function() {
 431                  this.wpList.recolor();
 432              } );
 433          };
 434      }
 435  };
 436  
 437  $.fn.wpList = function( settings ) {
 438      this.each( function() {
 439          var _this = this;
 440  
 441          this.wpList = { settings: $.extend( {}, wpList.settings, { what: wpList.parseClass(this,'list')[1] || '' }, settings ) };
 442          $.each( fs, function(i,f) { _this.wpList[i] = function( e, s ) { return wpList[f].call( _this, e, s ); }; } );
 443      } );
 444  
 445      wpList.init.call(this);
 446  
 447      this.wpList.process();
 448  
 449      return this;
 450  };
 451  
 452  })(jQuery);


Generated: Tue May 19 15:55:14 2020 Cross-referenced by PHPXref 0.7.1