Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2012
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * jQuery PHP Plugin
  3.  * version: 0.8.3 (16/03/2009)
  4.  * author:  Anton Shevchuk (http://anton.shevchuk.name)
  5.  * @requires jQuery v1.2.1 or later
  6.  *
  7.  * Examples and documentation at: http://jquery.hohli.com/
  8.  * Dual licensed under the MIT and GPL licenses:
  9.  *   http://www.opensource.org/licenses/mit-license.php
  10.  *   http://www.gnu.org/licenses/gpl.html
  11.  *
  12.  * Revision: $Id$
  13.  */
  14. (function($) {
  15.  
  16. $.extend({
  17.     php: function (url, params) {
  18.         // do an ajax post request
  19.         return $.ajax({
  20.            // AJAX-specified URL
  21.            url: url,
  22.            // JSON
  23.            type: "POST",
  24.            data: params,
  25.            dataType : "json",
  26.            /* Handlers */
  27.  
  28.            // Handle the beforeSend event
  29.            beforeSend: function(){
  30.                return php.beforeSend();
  31.            },
  32.            // Handle the success event
  33.            success: function(data, textStatus){
  34.                return php.success(data, textStatus);
  35.            },
  36.            // Handle the error event
  37.            error: function (xmlEr, typeEr, except) {
  38.                return php.error(xmlEr, typeEr, except);
  39.            },
  40.            // Handle the complete event
  41.            complete: function (XMLHttpRequest, textStatus) {
  42.                return php.complete(XMLHttpRequest, textStatus);
  43.            }
  44.         });
  45.     }
  46. });
  47.  
  48.  
  49. php = {
  50.     /**
  51.      * beforeSend
  52.      */
  53.     beforeSend:function() {
  54.         return true;
  55.     },
  56.     /**
  57.      * success
  58.      * parse AJAX response
  59.      * @param object response
  60.      * @param string textStatus
  61.      */
  62.      success:function (response, textStatus) {
  63.         // call jQuery methods
  64.                 for (var i=0;i<response['q'].length; i++) {
  65.                    
  66.                         var selector  = $(response['q'][i]['s']);
  67.                         var methods   = response['q'][i]['m'];
  68.                         var arguments = response['q'][i]['a'];
  69.                        
  70.                         for (var j=0;j<methods.length; j++) {
  71.                                 try {
  72.                                         var method   = methods[j];
  73.                                         var argument = arguments[j];
  74.                                        
  75.                                         if (method && method!= '' && method!= 'undefined') {
  76.                                             switch (true) {
  77.                                                 // exception for 'ready', 'map', 'queue'
  78.                                                 case (method == 'ready' || method == 'map' || method == 'queue'):
  79.                                                    selector = selector[method](window[argument[0]]);
  80.                                                    break;
  81.                                                 // exception for 'bind' and 'one'
  82.                                                 case ((method == 'bind' || method == 'one') && argument.length == 3):
  83.                                                    selector = selector[method](argument[0],argument[1],window[argument[2]]);
  84.                                                    break;
  85.                                                 // exception for 'toggle' and 'hover'
  86.                                                 case ((method == 'toggle' || method == 'hover') && argument.length == 2):
  87.                                                    selector = selector[method](window[argument[0]],window[argument[1]]);
  88.                                                    break;
  89.                                                 // exception for 'filter'
  90.                                                 case (method == 'filter' && argument.length == 1):
  91.                                                    // try run method
  92.                                                    if (window[argument[0]] && window[argument[0]] != '' && window[argument[0]] != 'undefined') {
  93.                                                        selector = selector[method](window[argument[0]]);
  94.                                                    } else {
  95.                                                        // try filter by specified expression
  96.                                                        selector = selector[method](argument[0]);
  97.                                                    }
  98.                                                    break;
  99.                                                 // exception for effects with callback
  100.                                                 case ((   method == 'show'      || method == 'hide'
  101.                                                        || method == 'slideDown' || method == 'slideUp' || method == 'slideToggle'
  102.                                                        || method == 'fadeIn'    || method == 'fadeOut'
  103.                                                        
  104.                                                      ) && argument.length == 2):
  105.                                                    selector = selector[method](argument[0],window[argument[1]]);
  106.                                                    break;
  107.                                                 // exception for events with callback
  108.                                                 case ((   method == 'blur'      || method == 'change'
  109.                                                        || method == 'click'     || method == 'dblclick'
  110.                                                        || method == 'error'     || method == 'focus'
  111.                                                        || method == 'keydown'   || method == 'keypress'  || method == 'keyup'
  112.                                                        || method == 'load'      || method == 'unload'
  113.                                                        || method == 'mousedown' || method == 'mousemove' || method == 'mouseout'
  114.                                                        || method == 'mouseover' || method == 'mouseup'
  115.                                                        || method == 'resize'    || method == 'scroll'
  116.                                                        || method == 'select'    || method == 'submit'
  117.                                                      ) && argument.length == 1):
  118.                                                    selector = selector[method](window[argument[0]]);
  119.                                                    break;
  120.                                                 // exception for 'fadeTo' with callback
  121.                                                 case (method == 'fadeTo' && argument.length == 3):
  122.                                                    selector = selector[method](argument[0],argument[1],window[argument[2]]);
  123.                                                    break;
  124.                                                 // exception for 'animate' with callback
  125.                                                 case (method == 'animate' && argument.length == 4):
  126.                                                    selector = selector[method](argument[0],argument[1],argument[2],window[argument[3]]);
  127.                                                    break;
  128.                                                    
  129.                                                 // universal
  130.                                                 case (argument.length == 0):
  131.                                                    selector = selector[method]();
  132.                                                    break;
  133.                                                 case (argument.length == 1):
  134.                                                    selector = selector[method](argument[0]);
  135.                                                    break;
  136.                                                 case (argument.length == 2):
  137.                                                    selector = selector[method](argument[0],argument[1]);
  138.                                                    break;
  139.                                                 case (argument.length == 3):
  140.                                                    selector = selector[method](argument[0],argument[1],argument[2]);
  141.                                                    break;
  142.                                                 case (argument.length == 4):
  143.                                                    selector = selector[method](argument[0],argument[1],argument[2],argument[3]);
  144.                                                    break;
  145.                                                 default:
  146.                                                    selector = selector[method](argument);
  147.                                                    break;
  148.                                             }
  149.                                         }
  150.                                 } catch (error) {
  151.                                         // if is error
  152.                                         alert('onAction: $("'+ response['q'][i]['s'] +'").'+ method +'("'+ argument +'")\n'
  153.                                                                         +' in file: ' + error.fileName + '\n'
  154.                                                                         +' on line: ' + error.lineNumber +'\n'
  155.                                                                         +' error:   ' + error.message);
  156.                                 }
  157.                     }
  158.             }
  159.  
  160.         // predefined actions named as
  161.         // Methods of ObjResponse in PHP side
  162.         $.each(response['a'], function (func, params) {
  163.             for (var i=0;i<params.length; i++) {
  164.                 try {
  165.                     php[func](params[i]);
  166.                 } catch (error) {
  167.                     // if is error
  168.                     alert('onAction: ' + func + '('+ params[i] +')\n'
  169.                                        +' in file: ' + error.fileName + '\n'
  170.                                        +' on line: ' + error.lineNumber +'\n'
  171.                                        +' error:   ' + error.message);
  172.                 }
  173.             }
  174.         });
  175.              
  176.     },
  177.  
  178.     /**
  179.      * error
  180.      *
  181.      * @param object xmlEr
  182.      * @param object typeEr
  183.      * @param object except
  184.      */
  185.      error:function (xmlEr, typeEr, except) {
  186.         var exObj = except ? except : false;
  187.        
  188.         $('#php-error').remove();
  189.        
  190.         var printCss  =
  191.             "<style type='text/css'>" +
  192.                 "#php-error{ width:640px; position:absolute; top:4px; right:4px; border:1px solid #f00; }"+
  193.                 "#php-error .php-title{ width:636px; height:26px; position:relative; line-height:26px; background-color:#f66; color:#fff; font-weight:bold; font-size:12px;padding-left:4px; }"+
  194.                 "#php-error .php-more { width:20px;  height:20px; position:absolute; top:2px; right:24px; line-height:20px; text-align:center; cursor:pointer; border:1px solid #f00; background-color:#fee; color:#333; }"+
  195.                 "#php-error .php-close{ width:20px;  height:20px; position:absolute; top:2px; right:2px;  line-height:20px; text-align:center; cursor:pointer; border:1px solid #f00; background-color:#fee; color:#333; }"+
  196.                 "#php-error .php-desc { width:636px; position:relative; background-color:#fee;padding-left:4px;}"+
  197.                 "#php-error .php-content{ display:none;}"+
  198.                 "#php-error textarea{ width:634px;height:400px;overflow:auto;padding:2px;}"+
  199.             "</style>";
  200.        
  201.         // error report for popup window coocking
  202.         var printStr  =
  203.             "<div id='php-error'>"+
  204.                 "<div class='php-title'>Error in AJAX request"+
  205.                     "<div class='php-more'>&raquo;</div>"+
  206.                     "<div class='php-close'>X</div>"+
  207.                 "</div>"+
  208.                 "<div class='php-desc'>";
  209.                
  210.             printStr += "<b>XMLHttpRequest exchange</b>: ";
  211.        
  212.         // XMLHttpRequest.readyState status
  213.         switch (xmlEr.readyState) {
  214.             case 0:
  215.                 readyStDesc = "not initialize";
  216.                 break;
  217.             case 1:
  218.                 readyStDesc = "open";
  219.                 break;
  220.             case 2:
  221.                 readyStDesc = "data transfer";
  222.                 break;
  223.             case 3:
  224.                 readyStDesc = "loading";
  225.                 break;
  226.             case 4:
  227.                 readyStDesc = "finish";
  228.                 break;
  229.             default:
  230.                 return "uncknown state";  
  231.         }
  232.        
  233.         printStr += readyStDesc+" ("+xmlEr.readyState+")";
  234.         printStr += "<br/>\n";
  235.        
  236.         if (exObj!=false) {
  237.             printStr += "exception was catch: "+except.toString();
  238.             printStr += "<br/>\n";
  239.         }
  240.        
  241.         // add http status description
  242.         printStr += "<b>HTTP status</b>: "+xmlEr.status +" - "+xmlEr.statusText;
  243.         printStr += "<br/>\n";
  244.         // add response text
  245.         printStr += "<b>Response text</b> (<small><a href='#' class='php-more2'>show more information &raquo;</a></small>):";
  246.         printStr += "</div>\n";
  247.         printStr += "<div class='php-content'><textarea>"+ xmlEr.responseText+"</textarea></div>";
  248.         printStr += "</div>" ;
  249.        
  250.         $(document.body).append(printCss);
  251.         $(document.body).append(printStr);
  252.        
  253.        
  254.         $('#php-error .php-more').hover(
  255.             function(){
  256.                 $(this).css('background-color','#fff')
  257.             },
  258.             function(){
  259.                 $(this).css('background-color','#fee')
  260.             });
  261.         $('#php-error .php-more').click(function(){
  262.             $('#php-error .php-content').slideToggle();
  263.         });
  264.         $('#php-error .php-more2').click(function(){
  265.             $('#php-error .php-content').slideToggle();
  266.             return false;
  267.         });
  268.        
  269.         $('#php-error .php-close').click(function(){
  270.             $('#php-error').fadeOut('fast',function(){$('#php-error').remove()})
  271.         });
  272.        
  273.         $('#php-error .php-close').hover(
  274.             function(){
  275.                 $(this).css('background-color','#fff')
  276.             },
  277.             function(){
  278.                 $(this).css('background-color','#fee')
  279.             });
  280.     },
  281.    
  282.     /**
  283.      * complete
  284.      *
  285.      * @param object XMLHttpRequest
  286.      * @param String textStatus
  287.      */
  288.     complete:function(XMLHttpRequest, textStatus) {
  289.         return true;
  290.     },
  291.    
  292.     /* Static actions */
  293.    
  294.     /**
  295.      * addMessage
  296.      * system messages callback handler
  297.      * @param object data
  298.      */
  299.     addMessage:function(data) {
  300.         // call registered or default func
  301.         var message        = data.msg      || "";
  302.         var callBackFunc   = data.callback || "defaultCallBack";
  303.         var callBackParams = data.params   || {};
  304.         php.messages[callBackFunc](message, callBackParams);
  305.     },
  306.        
  307.     /**
  308.      * addError
  309.      * system errors callback handler
  310.      * @param object data
  311.      */
  312.     addError:function(data) {
  313.         // call registered or default func
  314.         var message        = data.msg      || "";
  315.         var callBackFunc   = data.callback || "defaultCallBack";
  316.         var callBackParams = data.params   || {};
  317.         php.errors[callBackFunc](message, callBackParams);
  318.     },
  319.  
  320.     /**
  321.      * addData
  322.      *
  323.      * @param object data
  324.      */
  325.     addData:function(data) {
  326.         // call registered or default func
  327.         var callBackFunc   = data.callback || "defaultCallBack";
  328.         php.data[callBackFunc](data.k, data.v);
  329.     },
  330.  
  331.     /**
  332.      * evalScript
  333.      * @param object data
  334.      */
  335.     evalScript:function(data) {
  336.         // why foo?
  337.         var func = data.foo || '';
  338.         eval(func);
  339.     },
  340.    
  341.     /* Default realization of callback functions */
  342.     data : {
  343.         defaultCallBack : function (key, value){
  344.             alert("Server response: " + key + " = " + value);
  345.         }
  346.     },
  347.     messages : {
  348.         defaultCallBack : function (msg, params){
  349.             alert("Server response message: " + msg);
  350.         }
  351.     },
  352.     errors : {
  353.         defaultCallBack : function (msg, params){
  354.             alert("Server response error: " + msg);
  355.         }
  356.     }
  357. };
  358. // end of php actions
  359. })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement