home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Blogs / wordpress2.6.exe / wordpress2.6 / wp-includes / js / wp-lists.js < prev   
Encoding:
JavaScript  |  2008-07-30  |  11.4 KB  |  373 lines

  1. (function($) {
  2. var currentFormEl = false;
  3. var fs = {add:'ajaxAdd',del:'ajaxDel',dim:'ajaxDim',process:'process',recolor:'recolor'};
  4.  
  5. var wpList = {
  6.     settings: {
  7.         url: wpListL10n.url, type: 'POST',
  8.         response: 'ajax-response',
  9.  
  10.         what: '',
  11.         alt: 'alternate', altOffset: 0,
  12.         addColor: null, delColor: null, dimAddColor: null, dimDelColor: null,
  13.  
  14.         confirm: null,
  15.         addBefore: null, addAfter: null,
  16.         delBefore: null, delAfter: null,
  17.         dimBefore: null, dimAfter: null
  18.     },
  19.  
  20.     nonce: function(e,s) {
  21.         var url = wpAjax.unserialize(e.attr('href'));
  22.         return s.nonce || url._ajax_nonce || $('#' + s.element + ' input[name=_ajax_nonce]').val() || url._wpnonce || $('#' + s.element + ' input[name=_wpnonce]').val() || 0;
  23.     },
  24.  
  25.     parseClass: function(e,t) {
  26.         var c = [], cl;
  27.         try {
  28.             cl = $(e).attr('class') || '';
  29.             cl = cl.match(new RegExp(t+':[A-Za-z0-9:_=-]+'));
  30.             if ( cl ) { c = cl[0].split(':'); }
  31.         } catch(r) {}
  32.         return c;
  33.     },
  34.  
  35.     pre: function(e,s,a) {
  36.         var bg; var r;
  37.         s = $.extend( {}, this.wpList.settings, {
  38.             element: null,
  39.             nonce: 0,
  40.             target: e.get(0)
  41.         }, s || {} );
  42.         if ( $.isFunction( s.confirm ) ) {
  43.             if ( 'add' != a ) {
  44.                 bg = $('#' + s.element).css('backgroundColor');
  45.                 $('#' + s.element).css('backgroundColor', '#FF9966');
  46.             }
  47.             r = s.confirm.call(this,e,s,a,bg);
  48.             if ( 'add' != a ) { $('#' + s.element).css('backgroundColor', bg ); }
  49.             if ( !r ) { return false; }
  50.         }
  51.         return s;
  52.     },
  53.  
  54.     ajaxAdd: function( e, s ) {
  55.         var list = this; e = $(e); s = s || {};
  56.         var cls = wpList.parseClass(e,'add');
  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 ) { return false; }
  64.  
  65.         if ( !e.is("[class^=add:" + list.id + ":]") ) { return !wpList.add.call( list, e, s ); }
  66.  
  67.         if ( !s.element ) { return true; }
  68.  
  69.         s.action = 'add-' + s.what;
  70.  
  71.         s.nonce = wpList.nonce(e,s);
  72.  
  73.         var es = $('#' + s.element + ' :input').not('[name=_ajax_nonce], [name=_wpnonce], [name=action]');
  74.         var valid = wpAjax.validateForm( '#' + s.element );
  75.         if ( !valid ) { return false; }
  76.  
  77.         s.data = $.param( $.extend( { _ajax_nonce: s.nonce, action: s.action }, wpAjax.unserialize( cls[4] || '' ) ) );
  78.         var formData = $.isFunction(es.fieldSerialize) ? es.fieldSerialize() : es.serialize();
  79.         if ( formData ) { s.data += '&' + formData; }
  80.  
  81.         if ( $.isFunction(s.addBefore) ) {
  82.             s = s.addBefore( s );
  83.             if ( !s ) { return true; }
  84.         }
  85.         if ( !s.data.match(/_ajax_nonce=[a-f0-9]+/) ) { return true; }
  86.  
  87.         s.success = function(r) {
  88.             var res = wpAjax.parseAjaxResponse(r, s.response, s.element);
  89.             if ( !res || res.errors ) { return false; }
  90.  
  91.             if ( true === res ) { return true; }
  92.  
  93.             jQuery.each( res.responses, function() {
  94.                 wpList.add.call( list, this.data, $.extend( {}, s, { // this.firstChild.nodevalue
  95.                     pos: this.position || 0,
  96.                     id: this.id || 0,
  97.                     oldId: this.oldId || null
  98.                 } ) );
  99.             } );
  100.  
  101.             if ( $.isFunction(s.addAfter) ) {
  102.                 var o = this.complete;
  103.                 this.complete = function(x,st) {
  104.                     var _s = $.extend( { xml: x, status: st, parsed: res }, s );
  105.                     s.addAfter( r, _s );
  106.                     if ( $.isFunction(o) ) { o(x,st); }
  107.                 };
  108.             }
  109.             list.wpList.recolor();
  110.             wpList.clear.call(list,'#' + s.element);
  111.         };
  112.  
  113.         $.ajax( s );
  114.         return false;
  115.     },
  116.  
  117.     ajaxDel: function( e, s ) {
  118.         var list = this; e = $(e); s = s || {};
  119.         var cls = wpList.parseClass(e,'delete');
  120.         s = wpList.pre.call( list, e, s, 'delete' );
  121.  
  122.         s.element = cls[2] || s.element || null;
  123.         if ( cls[3] ) { s.delColor = '#' + cls[3]; }
  124.         else { s.delColor = s.delColor || '#FF3333'; }
  125.  
  126.         if ( !s || !s.element ) { return false; }
  127.  
  128.         s.action = 'delete-' + s.what;
  129.  
  130.         s.nonce = wpList.nonce(e,s);
  131.  
  132.         s.data = $.extend(
  133.             { action: s.action, id: s.element.split('-').pop(), _ajax_nonce: s.nonce },
  134.             wpAjax.unserialize( cls[4] || '' )
  135.         );
  136.  
  137.         if ( $.isFunction(s.delBefore) ) {
  138.             s = s.delBefore( s );
  139.             if ( !s ) { return true; }
  140.         }
  141.         if ( !s.data._ajax_nonce ) { return true; }
  142.  
  143.         var element = $('#' + s.element);
  144.  
  145.         if ( 'none' != s.delColor ) {
  146.             var anim = 'slideUp';
  147.             if ( element.css( 'display' ).match(/table/) )
  148.                 anim = 'fadeOut'; // Can't slideup table rows and other table elements.  Known jQuery bug
  149.             element
  150.                 .animate( { backgroundColor: s.delColor }, 'fast'  )[anim]( 'fast' )
  151.                 .queue( function() { list.wpList.recolor(); $(this).dequeue(); } );
  152.         } else {
  153.             list.wpList.recolor();
  154.         }
  155.  
  156.         s.success = function(r) {
  157.             var res = wpAjax.parseAjaxResponse(r, s.response, s.element);
  158.             if ( !res || res.errors ) {
  159.                 element.stop().stop().css( 'backgroundColor', '#FF3333' ).show().queue( function() { list.wpList.recolor(); $(this).dequeue(); } );
  160.                 return false;
  161.             }
  162.             if ( $.isFunction(s.delAfter) ) {
  163.                 var o = this.complete;
  164.                 this.complete = function(x,st) {
  165.                     element.queue( function() {
  166.                         var _s = $.extend( { xml: x, status: st, parsed: res }, s );
  167.                         s.delAfter( r, _s );
  168.                         if ( $.isFunction(o) ) { o(x,st); }
  169.                     } ).dequeue();
  170.                 };
  171.             }
  172.         };
  173.         $.ajax( s );
  174.         return false;
  175.     },
  176.  
  177.     ajaxDim: function( e, s ) {
  178.         var list = this; e = $(e); s = s || {};
  179.         var cls = wpList.parseClass(e,'dim');
  180.         s = wpList.pre.call( list, e, s, 'dim' );
  181.  
  182.         s.element = cls[2] || s.element || null;
  183.         s.dimClass =  cls[3] || s.dimClass || null;
  184.         if ( cls[4] ) { s.dimAddColor = '#' + cls[4]; }
  185.         else { s.dimAddColor = s.dimAddColor || '#FFFF33'; }
  186.         if ( cls[5] ) { s.dimDelColor = '#' + cls[5]; }
  187.         else { s.dimDelColor = s.dimDelColor || '#FF3333'; }
  188.  
  189.         if ( !s || !s.element || !s.dimClass ) { return true; }
  190.  
  191.         s.action = 'dim-' + s.what;
  192.  
  193.         s.nonce = wpList.nonce(e,s);
  194.  
  195.         s.data = $.extend(
  196.             { action: s.action, id: s.element.split('-').pop(), dimClass: s.dimClass, _ajax_nonce : s.nonce },
  197.             wpAjax.unserialize( cls[6] || '' )
  198.         );
  199.  
  200.         if ( $.isFunction(s.dimBefore) ) {
  201.             s = s.dimBefore( s );
  202.             if ( !s ) { return true; }
  203.         }
  204.  
  205.         var element = $('#' + s.element);
  206.         var isClass = element.toggleClass(s.dimClass).is('.' + s.dimClass);
  207.         var color = wpList.getColor( element );
  208.         element.toggleClass( s.dimClass )
  209.         var dimColor = isClass ? s.dimAddColor : s.dimDelColor;
  210.         if ( 'none' != dimColor ) {
  211.             element
  212.                 .animate( { backgroundColor: dimColor }, 'fast' )
  213.                 .queue( function() { element.toggleClass(s.dimClass); $(this).dequeue(); } )
  214.                 .animate( { backgroundColor: color }, { complete: function() { $(this).css( 'backgroundColor', '' ); } } );
  215.         }
  216.  
  217.         if ( !s.data._ajax_nonce ) { return true; }
  218.  
  219.         s.success = function(r) {
  220.             var res = wpAjax.parseAjaxResponse(r, s.response, s.element);
  221.             if ( !res || res.errors ) {
  222.                 element.stop().stop().css( 'backgroundColor', '#FF3333' )[isClass?'removeClass':'addClass'](s.dimClass).show().queue( function() { list.wpList.recolor(); $(this).dequeue(); } );
  223.                 return false;
  224.             }
  225.             if ( $.isFunction(s.dimAfter) ) {
  226.                 var o = this.complete;
  227.                 this.complete = function(x,st) {
  228.                     element.queue( function() {
  229.                         var _s = $.extend( { xml: x, status: st, parsed: res }, s );
  230.                         s.dimAfter( r, _s );
  231.                         if ( $.isFunction(o) ) { o(x,st); }
  232.                     } ).dequeue();
  233.                 };
  234.             }
  235.         };
  236.  
  237.         $.ajax( s );
  238.         return false;
  239.     },
  240.  
  241.     // From jquery.color.js: jQuery Color Animation by John Resig
  242.     getColor: function( el ) {
  243.         if ( el.constructor == Object )
  244.             el = el.get(0);
  245.         var elem = el, color, rgbaTrans = new RegExp( "rgba\\(\\s*0,\\s*0,\\s*0,\\s*0\\s*\\)", "i" );
  246.         do {
  247.             color = jQuery.curCSS(elem, 'backgroundColor');
  248.             if ( color != '' && color != 'transparent' && !color.match(rgbaTrans) || jQuery.nodeName(elem, "body") )
  249.                 break;
  250.         } while ( elem = elem.parentNode );
  251.         return color || '#ffffff';
  252.     },
  253.  
  254.     add: function( e, s ) {
  255.         var list = $(this);
  256.         e = $(e);
  257.  
  258.         var old = false;
  259.         var _s = { pos: 0, id: 0, oldId: null };
  260.         if ( 'string' == typeof s ) { s = { what: s }; }
  261.         s = $.extend(_s, this.wpList.settings, s);
  262.         if ( !e.size() || !s.what ) { return false; }
  263.         if ( s.oldId ) { old = $('#' + s.what + '-' + s.oldId); }
  264.         if ( s.id && ( s.id != s.oldId || !old || !old.size() ) ) { $('#' + s.what + '-' + s.id).remove(); }
  265.  
  266.         if ( old && old.size() ) {
  267.             old.replaceWith(e);
  268.         } else if ( isNaN(s.pos) ) {
  269.             var ba = 'after';
  270.             if ( '-' == s.pos.substr(0,1) ) {
  271.                 s.pos = s.pos.substr(1);
  272.                 ba = 'before';
  273.             }
  274.             var ref = list.find( '#' + s.pos );
  275.             if ( 1 === ref.size() ) { ref[ba](e); }
  276.             else { list.append(e); }
  277.         } else if ( s.pos < 0 ) {
  278.             list.prepend(e);
  279.         } else {
  280.             list.append(e);
  281.         }
  282.  
  283.         if ( s.alt ) {
  284.             if ( ( list.children(':visible').index( e[0] ) + s.altOffset ) % 2 ) { e.removeClass( s.alt ); }
  285.             else { e.addClass( s.alt ); }
  286.         }
  287.  
  288.         if ( 'none' != s.addColor ) {
  289.             var color = wpList.getColor( e );
  290.             e.css( 'backgroundColor', s.addColor ).animate( { backgroundColor: color }, { complete: function() { $(this).css( 'backgroundColor', '' ); } } );
  291.         }
  292.         list.each( function() { this.wpList.process( e ); } );
  293.         return e;
  294.     },
  295.  
  296.     clear: function(e) {
  297.         var list = this;
  298.         e = $(e);
  299.         if ( list.wpList && e.parents( '#' + list.id ).size() ) { return; }
  300.         e.find(':input').each( function() {
  301.             if ( $(this).parents('.form-no-clear').size() )
  302.                 return;
  303.             var t = this.type.toLowerCase(); var tag = this.tagName.toLowerCase();
  304.             if ( 'text' == t || 'password' == t || 'textarea' == tag ) { this.value = ''; }
  305.             else if ( 'checkbox' == t || 'radio' == t ) { this.checked = false; }
  306.             else if ( 'select' == tag ) { this.selectedIndex = null; }
  307.         });
  308.     },
  309.  
  310.     process: function(el) {
  311.         var list = this;
  312.         $("[class^=add:" + list.id + ":]", el || null)
  313.             .filter('form').submit( function() { return list.wpList.add(this); } ).end()
  314.             .not('form').click( function() { return list.wpList.add(this); } ).each( function() {
  315.                 var addEl = this;
  316.                 var c = wpList.parseClass(this,'add')[2] || addEl.id;
  317.                 if ( !c ) { return; }
  318.                 var forms = []; var ins = []; // this is all really inefficient
  319.                 $('#' + c + ' :input').focus( function() { currentFormEl = this; } ).blur( function() { currentFormEl = false; } ).each( function() {
  320.                     ins.push(this);
  321.                     var f = $(this).parents('form:first').get(0);
  322.                     if ( $.inArray(f,forms) < 0 ) { forms.push(f); }
  323.                 } );
  324.                 $(forms).submit( function() {
  325.                     if ( 0 <= $.inArray(currentFormEl,ins) ) {
  326.                         $(addEl).trigger( 'click' );
  327.                         $(currentFormEl).focus();
  328.                         return false;
  329.                     }
  330.                 } );
  331.             } );
  332.         $("[class^=delete:" + list.id + ":]", el || null).click( function() { return list.wpList.del(this); } );
  333.         $("[class^=dim:" + list.id + ":]", el || null).click( function() { return list.wpList.dim(this); } );
  334.     },
  335.  
  336.     recolor: function() {
  337.         var list = this;
  338.         if ( !list.wpList.settings.alt ) { return; }
  339.         var items = $('.list-item:visible', list);
  340.         if ( !items.size() ) { items = $(list).children(':visible'); }
  341.         var eo = [':even',':odd'];
  342.         if ( list.wpList.settings.altOffset % 2 ) { eo.reverse(); }
  343.         items.filter(eo[0]).addClass(list.wpList.settings.alt).end().filter(eo[1]).removeClass(list.wpList.settings.alt);
  344.     },
  345.  
  346.     init: function() {
  347.         var lists = this;
  348.         lists.wpList.process = function(a) {
  349.             lists.each( function() {
  350.                 this.wpList.process(a);
  351.             } );
  352.         };
  353.         lists.wpList.recolor = function() {
  354.             lists.each( function() {
  355.                 this.wpList.recolor();
  356.             } );
  357.         };
  358.     }
  359. };
  360.  
  361. $.fn.wpList = function( settings ) {
  362.     this.each( function() {
  363.         var _this = this;
  364.         this.wpList = { settings: $.extend( {}, wpList.settings, { what: wpList.parseClass(this,'list')[1] || '' }, settings ) };
  365.         $.each( fs, function(i,f) { _this.wpList[i] = function( e, s ) { return wpList[f].call( _this, e, s ); }; } );
  366.     } );
  367.     wpList.init.call(this);
  368.     this.wpList.process();
  369.     return this;
  370. };
  371.  
  372. })(jQuery);
  373.