Вход Регистрация
Файл: gamele.ru/func/lib/JsHttpRequest/debug/JsHttpRequest-form.js
Строк: 702
<?php
/**
 * JsHttpRequest: JavaScript "AJAX" data loader (form support only!)
 *
 * @license LGPL
 * @author Dmitry Koterov, http://en.dklab.ru/lib/JsHttpRequest/
 * @version 5.x $Id$
 */
// {{{
function JsHttpRequest() {
    
// Standard properties.
    
var this;
    
t.onreadystatechange null;
    
t.readyState         0;
    
t.responseText       null;
    
t.responseXML        null;
    
t.status             200;
    
t.statusText         "OK";
    
// JavaScript response array/hash
    
t.responseJS         null;

    
// Additional properties.
    
t.caching            false;        // need to use caching?
    
t.loader             null;         // loader to use ('form', 'script', 'xml'; null - autodetect)
    
t.session_name       "PHPSESSID";  // set to SID cookie or GET parameter name

    // Internals.
    
t._ldObj              null;  // used loader object
    
t._reqHeaders        = [];    // collected request headers
    
t._openArgs          null;  // parameters from open()
    
t._errors = {
        
inv_form_el:        'Invalid FORM element detected: name=%, tag=%',
        
must_be_single_el:  'If used, <form> must be a single HTML element in the list.',
        
js_invalid:         'JavaScript code generated by backend is invalid!n%',
        
url_too_long:       'Cannot use so long query with GET request (URL is larger than % bytes)',
        
unk_loader:         'Unknown loader: %',
        
no_loaders:         'No loaders registered at all, please check JsHttpRequest.LOADERS array',
        
no_loader_matched:  'Cannot find a loader which may process the request. Notices are:n%'
    
}
    
    
/**
     * Aborts the request. Behaviour of this function for onreadystatechange() 
     * is identical to IE (most universal and common case). E.g., readyState -> 4
     * on abort() after send().
     */
    
t.abort = function() { with (this) {
        if (
_ldObj && _ldObj.abort_ldObj.abort();
        
_cleanup();
        if (
readyState == 0) {
            
// start->abort: no change of readyState (IE behaviour)
            
return;
        }
        if (
readyState == && !_ldObj) {
            
// open->abort: no onreadystatechange call, but change readyState to 0 (IE).
            // send->abort: change state to 4 (_ldObj is not null when send() is called)
            
readyState 0;
            return;
        }
        
_changeReadyState(4true); // 4 in IE & FF on abort() call; Opera does not change to 4.
    
}}
    
    
/**
     * Prepares the object for data loading.
     * You may also pass URLs like "GET url" or "script.GET url".
     */
    
t.open = function(methodurlasyncFlagusernamepassword) { with (this) {
        
// Extract methor and loader from the URL (if present).
        
if (url.match(/^((w+).)?(GET|POST)s+(.*)/i)) {
            
this.loader RegExp.$2RegExp.$null;
            
method RegExp.$3;
            
url RegExp.$4
        }
        
// Append SID to original URL. Use try...catch for security problems.
        
try {
            if (
                
document.location.search.match(new RegExp('[&?]' session_name '=([^&?]*)'))
                || 
document.cookie.match(new RegExp('(?:;|^)\s*' session_name '=([^;]*)'))
            ) {
                
url += (url.indexOf('?') >= 0'&' '?') + session_name "=" this.escape(RegExp.$1);
            }
        } catch (
e) {}
        
// Store open arguments to hash.
        
_openArgs = {
            
method:     (method || '').toUpperCase(),
            
url:        url,
            
asyncFlag:  asyncFlag,
            
username:   username != nullusername '',
            
password:   password != nullpassword ''
        
}
        
_ldObj null;
        
_changeReadyState(1true); // compatibility with XMLHttpRequest
        
return true;
    }}
    
    
/**
     * Sends a request to a server.
     */
    
t.send = function(content) {
        if (!
this.readyState) {
            
// send without open or after abort: no action (IE behaviour).
            
return;
        }
        
this._changeReadyState(1true); // compatibility with XMLHttpRequest
        
this._ldObj null;
        
        
// Prepare to build QUERY_STRING from query hash.
        
var queryText = [];
        var 
queryElem = [];
        if (!
this._hash2query(contentnullqueryTextqueryElem)) return;
    
        
// Solve the query hashcode & return on cache hit.
        
var hash null;
        if (
this.caching && !queryElem.length) {
            
hash this._openArgs.username ':' this._openArgs.password '@' this._openArgs.url '|' queryText "#" this._openArgs.method;
            var 
cache JsHttpRequest.CACHE[hash];
            if (
cache) {
                
this._dataReady(cache[0], cache[1]);
                return 
false;
            }
        }
    
        
// Try all the loaders.
        
var loader = (this.loader || '').toLowerCase();
        if (
loader && !JsHttpRequest.LOADERS[loader]) return this._error('unk_loader'loader);
        var 
errors = [];
        var 
lds JsHttpRequest.LOADERS;
        for (var 
tryLoader in lds) {
            var 
ldr lds[tryLoader].loader;
            if (!
ldr) continue; // exclude possibly derived prototype properties from "for .. in".
            
if (loader && tryLoader != loader) continue;
            
// Create sending context.
            
var ldObj = new ldr(this);
            
JsHttpRequest.extend(ldObjthis._openArgs);
            
JsHttpRequest.extend(ldObj, {
                
queryText:  queryText.join('&'),
                
queryElem:  queryElem,
                
id:         (new Date().getTime()) + "" JsHttpRequest.COUNT++,
                
hash:       hash,
                
span:       null
            
});
            var 
error ldObj.load();
            if (!
error) {
                
// Save loading script.
                
this._ldObj ldObj;
                
JsHttpRequest.PENDING[ldObj.id] = this;
                return 
true;
            }
            if (!
loader) {
                
errors[errors.length] = '- ' tryLoader.toUpperCase() + ': ' this._l(error);
            } else {
                return 
this._error(error);
            }
        }
    
        
// If no loader matched, generate error message.
        
return tryLoaderthis._error('no_loader_matched'errors.join('n')) : this._error('no_loaders');
    }
    
    
/**
     * Returns all response headers (if supported).
     */
    
t.getAllResponseHeaders = function() { with (this) {
        return 
_ldObj && _ldObj.getAllResponseHeaders_ldObj.getAllResponseHeaders() : [];
    }}

    
/**
     * Returns one response header (if supported).
     */
    
t.getResponseHeader = function(label) { with (this) {
        return 
_ldObj && _ldObj.getResponseHeader_ldObj.getResponseHeader(label) : null;
    }}

    
/**
     * Adds a request header to a future query.
     */
    
t.setRequestHeader = function(labelvalue) { with (this) {
        
_reqHeaders[_reqHeaders.length] = [labelvalue];
    }}
    
    
//
    // Internal functions.
    //
    
    /**
     * Do all the work when a data is ready.
     */
    
t._dataReady = function(textjs) { with (this) {
        if (
caching && _ldObjJsHttpRequest.CACHE[_ldObj.hash] = [textjs];
        
responseText responseXML text;
        
responseJS js;
        if (
js !== null) {
            
status 200;
            
statusText "OK";
        } else {
            
status 500;
            
statusText "Internal Server Error";
        }
        
_changeReadyState(2);
        
_changeReadyState(3);
        
_changeReadyState(4);
        
_cleanup();
    }}
    
    
/**
     * Analog of sprintf(), but translates the first parameter by _errors.
     */
    
t._l = function(args) {
        var 
00msg this._errors[args[0]];
        
// Cannot use replace() with a callback, because it is incompatible with IE5.
        
while ((msg.indexOf('%'p)) >= 0) {
            var 
args[++i] + "";
            
msg msg.substring(0p) + msg.substring(1msg.length);
            
+= a.length;
        }
        return 
msg;
    }

    
/** 
     * Called on error.
     */
    
t._error = function(msg) {
        
msg this._l(typeof(msg) == 'string'arguments msg)
        
msg "JsHttpRequest: " msg;
        if (!
window.Error) {
            
// Very old browser...
            
throw msg;
        } else if ((new 
Error(1'test')).description == "test") {
            
// We MUST (!!!) pass 2 parameters to the Error() constructor for IE5.
            
throw new Error(1msg);
        } else {
            
// Mozilla does not support two-parameter call style.
            
throw new Error(msg);
        }
    }
    
    
/**
     * Convert hash to QUERY_STRING.
     * If next value is scalar or hash, push it to queryText.
     * If next value is form element, push [name, element] to queryElem.
     */
    
t._hash2query = function(contentprefixqueryTextqueryElem) {
        if (
prefix == nullprefix "";
        if((
''+typeof(content)).toLowerCase() == 'object') {
            var 
formAdded false;
            if (
content && content.parentNode && content.parentNode.appendChild && content.tagName && content.tagName.toUpperCase() == 'FORM') {
                
content = { formcontent };
            }
            for (var 
k in content) {
                var 
content[k];
                if (
instanceof Function) continue;
                var 
curPrefix prefixprefix '[' this.escape(k) + ']' this.escape(k);
                var 
isFormElement && v.parentNode && v.parentNode.appendChild && v.tagName;
                if (
isFormElement) {
                    var 
tn v.tagName.toUpperCase();
                    if (
tn == 'FORM') {
                        
// FORM itself is passed.
                        
formAdded true;
                    } else if (
tn == 'INPUT' || tn == 'TEXTAREA' || tn == 'SELECT') {
                        
// This is a single form elemenent.
                    
} else {
                        return 
this._error('inv_form_el', (v.name||''), v.tagName);
                    }
                    
queryElem[queryElem.length] = { namecurPrefixe};
                } else if (
instanceof Object) {
                    
this._hash2query(vcurPrefixqueryTextqueryElem);
                } else {
                    
// We MUST skip NULL values, because there is no method
                    // to pass NULL's via GET or POST request in PHP.
                    
if (=== null) continue;
                    
// Convert JS boolean true and false to corresponding PHP values.
                    
if (=== true1
                    if (
=== false'';
                    
queryText[queryText.length] = curPrefix "=" this.escape('' v);
                }
                if (
formAdded && queryElem.length 1) {
                    return 
this._error('must_be_single_el');
                }
            }
        } else {
            
queryText[queryText.length] = content;
        }
        return 
true;
    }
    
    
/**
     * Remove last used script element (clean memory).
     */
    
t._cleanup = function() {
        var 
ldObj this._ldObj;
        if (!
ldObj) return;
        
// Mark this loading as aborted.
        
JsHttpRequest.PENDING[ldObj.id] = false;
        var 
span ldObj.span;
        if (!
span) return;
        
// Do NOT use iframe.contentWindow.back() - it is incompatible with Opera 9!
        
ldObj.span null;
        var 
closure = function() {
            
span.parentNode.removeChild(span);
        }
        
// IE5 crashes on setTimeout(function() {...}, ...) construction! Use tmp variable.
        
JsHttpRequest.setTimeout(closure50);
    }
    
    
/**
     * Change current readyState and call trigger method.
     */
    
t._changeReadyState = function(sreset) { with (this) {
        if (
reset) {
            
status statusText responseJS null;
            
responseText '';
        }
        
readyState s;
        if (
onreadystatechangeonreadystatechange();
    }}
    
    
/**
     * JS escape() does not quote '+'.
     */
    
t.escape = function(s) {
        return 
escape(s).replace(new RegExp('\+','g'), '%2B');
    }
}


// Global library variables.
JsHttpRequest.COUNT 0;              // unique ID; used while loading IDs generation
JsHttpRequest.MAX_URL_LEN 2000;     // maximum URL length
JsHttpRequest.CACHE = {};             // cached data
JsHttpRequest.PENDING = {};           // pending loadings
JsHttpRequest.LOADERS = {};           // list of supported data loaders (filled at the bottom of the file)
JsHttpRequest._dummy = function() {}; // avoid memory leaks


/**
 * These functions are dirty hacks for IE 5.0 which does not increment a
 * reference counter for an object passed via setTimeout(). So, if this 
 * object (closure function) is out of scope at the moment of timeout 
 * applying, IE 5.0 crashes. 
 */

/**
 * Timeout wrappers storage. Used to avoid zeroing of referece counts in IE 5.0.
 * Please note that you MUST write "window.setTimeout", not "setTimeout", else
 * IE 5.0 crashes again. Strange, very strange...
 */
JsHttpRequest.TIMEOUTS = { swindow.setTimeoutcwindow.clearTimeout };

/**
 * Wrapper for IE5 buggy setTimeout.
 * Use this function instead of a usual setTimeout().
 */
JsHttpRequest.setTimeout = function(funcdt) {
    
// Always save inside the window object before a call (for FF)!
    
window.JsHttpRequest_tmp JsHttpRequest.TIMEOUTS.s
    if (
typeof(func) == "string") {
        
id window.JsHttpRequest_tmp(funcdt);
    } else {
        var 
id null;
        var 
mediator = function() {
            
func();
            
delete JsHttpRequest.TIMEOUTS[id]; // remove circular reference
        
}
        
id window.JsHttpRequest_tmp(mediatordt);
        
// Store a reference to the mediator function to the global array
        // (reference count >= 1); use timeout ID as an array key;
        
JsHttpRequest.TIMEOUTS[id] = mediator;
    }
    
window.JsHttpRequest_tmp null// no delete() in IE5 for window
    
return id;
}

/**
 * Complimental wrapper for clearTimeout. 
 * Use this function instead of usual clearTimeout().
 */
JsHttpRequest.clearTimeout = function(id) {
    
window.JsHttpRequest_tmp JsHttpRequest.TIMEOUTS.c;
    
delete JsHttpRequest.TIMEOUTS[id]; // remove circular reference
    
var window.JsHttpRequest_tmp(id);
    
window.JsHttpRequest_tmp null// no delete() in IE5 for window
    
return r;
}


/**
 * Global static function.
 * Simple interface for most popular use-cases.
 * You may also pass URLs like "GET url" or "script.GET url".
 */
JsHttpRequest.query = function(urlcontentonreadynocache) {
    var 
req = new this();
    
req.caching = !nocache;
    
req.onreadystatechange = function() {
        if (
req.readyState == 4) {
            
onready(req.responseJSreq.responseText);
        }
    }
    
req.open(nullurltrue);
    
req.send(content);
}


/**
 * Global static function.
 * Called by server backend script on data load.
 */
JsHttpRequest.dataReady = function(d) {
    var 
th this.PENDING[d.id];
    
delete this.PENDING[d.id];
    if (
th) {
        
th._dataReady(d.textd.js);
    } else if (
th !== false) {
        throw 
"dataReady(): unknown pending id: " d.id;
    }
}


// Adds all the properties of src to dest.
JsHttpRequest.extend = function(destsrc) {
    for (var 
k in srcdest[k] = src[k];
}

/**
 * Each loader has the following properties which must be initialized:
 * - method
 * - url
 * - asyncFlag (ignored)
 * - username
 * - password
 * - queryText (string)
 * - queryElem (array)
 * - id
 * - hash
 * - span
 */ 
 
// }}}

// {{{ form
// Loader: FORM & IFRAME.
// [+] Supports file uploading.
// [+] GET and POST methods are supported.
// [+] Supports loading from different domains.
// [-] Uses a lot of system resources.
// [-] Backend data cannot be browser-cached.
// [-] Pollutes browser history on some old browsers.
//
JsHttpRequest.LOADERS.form = { loader: function(req) {
    
JsHttpRequest.extend(req._errors, {
        
form_el_not_belong:  'Element "%" does not belong to any form!',
        
form_el_belong_diff'Element "%" belongs to a different form. All elements must belong to the same form!',
        
form_el_inv_enctype'Attribute "enctype" of the form must be "%" (for IE), "%" given.'
    
})
    
    
this.load = function() {
        var 
th this;
     
        if (!
th.methodth.method 'POST';
        
th.url += (th.url.indexOf('?') >= 0'&' '?') + 'JsHttpRequest=' th.id '-' 'form';
        
        
// If GET, build full URL. Then copy QUERY_STRING to queryText.
        
if (th.method == 'GET') {
            if (
th.queryTextth.url += (th.url.indexOf('?') >= 0'&' '?') + th.queryText;
            if (
th.url.length JsHttpRequest.MAX_URL_LEN) return ['url_too_long'JsHttpRequest.MAX_URL_LEN];
            var 
th.url.split('?'2);
            
th.url p[0];
            
th.queryText p[1] || '';
        }

        
// Check if all form elements belong to same form.
        
var form null;
        var 
wholeFormSending false;
        if (
th.queryElem.length) {
            if (
th.queryElem[0].e.tagName.toUpperCase() == 'FORM') {
                
// Whole FORM sending.
                
form th.queryElem[0].e;
                
wholeFormSending true;
                
th.queryElem = [];
            } else {
                
// If we have at least one form element, we use its FORM as a POST container.
                
form th.queryElem[0].e.form;
                
// Validate all the elements.
                
for (var 0th.queryElem.lengthi++) {
                    var 
th.queryElem[i].e;
                    if (!
e.form) {
                        return [
'form_el_not_belong'e.name];
                    }
                    if (
e.form != form) {
                        return [
'form_el_belong_diff'e.name];
                    }
                }
            }
            
            
// Check enctype of the form.
            
if (th.method == 'POST') {
                var 
need "multipart/form-data";
                var 
given = (form.attributes.encType && form.attributes.encType.nodeValue) || (form.attributes.enctype && form.attributes.enctype.value) || form.enctype;
                if (
given != need) {
                    return [
'form_el_inv_enctype'needgiven];
                }
            }
        }

        
// Create invisible IFRAME with temporary form (form is used on empty queryElem).
        // We ALWAYS create th IFRAME in the document of the form - for Opera 7.20.
        
var form && (form.ownerDocument || form.document) || document;
        var 
ifname 'jshr_i_' th.id;
        var 
th.span d.createElement('DIV');
        
s.style.position 'absolute';
        
s.style.display 'none';
        
s.style.visibility 'hidden';
        
s.innerHTML 
            (
form'' '<form' + (th.method == 'POST'' enctype="multipart/form-data" method="post"' '') + '></form>') + // stupid IE, MUST use innerHTML assignment :-(
            
'<iframe name="' ifname '" id="' ifname '" style="width:0px; height:0px; overflow:hidden; border:none"></iframe>'
        
if (!form) {
            
form th.span.firstChild;
        }

        
// Insert generated form inside the document.
        // Be careful: don't forget to close FORM container in document body!
        
d.body.insertBefore(sd.body.lastChild);

        
// Function to safely set the form attributes. Parameter attr is NOT a hash 
        // but an array, because "for ... in" may badly iterate over derived attributes.
        
var setAttributes = function(eattr) {
            var 
sv = [];
            var 
form e;
            
// This strange algorythm is needed, because form may  contain element 
            // with name like 'action'. In IE for such attribute will be returned
            // form element node, not form action. Workaround: copy all attributes
            // to new empty form and work with it, then copy them back. This is
            // THE ONLY working algorythm since a lot of bugs in IE5.0 (e.g. 
            // with e.attributes property: causes IE crash).
            
if (e.mergeAttributes) {
                var 
form d.createElement('form');
                
form.mergeAttributes(efalse);
            }
            for (var 
0attr.lengthi++) {
                var 
attr[i][0], attr[i][1];
                
// TODO: http://forum.dklab.ru/viewtopic.php?p=129059#129059
                
sv[sv.length] = [kform.getAttribute(k)];
                
form.setAttribute(kv);
            }
            if (
e.mergeAttributes) {
                
e.mergeAttributes(formfalse);
            }
            return 
sv;
        }

        
// Run submit with delay - for old Opera: it needs some time to create IFRAME.
        
var closure = function() {
            
// Save JsHttpRequest object to new IFRAME.
            
top.JsHttpRequestGlobal JsHttpRequest;
            
            
// Disable ALL the form elements.
            
var savedNames = [];
            if (!
wholeFormSending) {
                for (var 
0form.elements.lengthni++) {
                    
savedNames[i] = form.elements[i].name;
                    
form.elements[i].name '';
                }
            }

            
// Insert hidden fields to the form.
            
var qt th.queryText.split('&');
            for (var 
qt.length 1>= 0i--) {
                var 
pair qt[i].split('='2);
                var 
d.createElement('INPUT');
                
e.type 'hidden';
                
e.name unescape(pair[0]);
                
e.value pair[1] != nullunescape(pair[1]) : '';
                
form.appendChild(e);
            }


            
// Change names of along user-passed form elements.
            
for (var 0th.queryElem.lengthi++) {
                
th.queryElem[i].e.name th.queryElem[i].name;
            }

            
// Temporary modify form attributes, submit form, restore attributes back.
            
var sv setAttributes(
                
form
                [
                    [
'action',   th.url],
                    [
'method',   th.method],
                    [
'onsubmit'null],
                    [
'target',   ifname]
                ]
            );
            
form.submit();
            
setAttributes(formsv);

            
// Remove generated temporary hidden elements from the top of the form.
            
for (var 0qt.lengthi++) {
                
// Use "form.firstChild.parentNode", not "form", or IE5 crashes!
                
form.lastChild.parentNode.removeChild(form.lastChild);
            }
            
// Enable all disabled elements back.
            
if (!wholeFormSending) {
                for (var 
0form.elements.lengthni++) {
                    
form.elements[i].name savedNames[i];
                }
            }
        }
        
JsHttpRequest.setTimeout(closure100);

        
// Success.
        
return null;
    }    
}}
// }}}
?>
Онлайн: 3
Реклама