Вход Регистрация
Файл: js/jquery.ajaxRequest.js
Строк: 687
<?php
(function ($) {
    function 
fnAjaxRequest(selectorcontaineroptions) {
        
options optionsFor(containeroptions);
        return 
this.on("click.ajaxRequest"selector, function (event) {
            var 
opts options;
            if (!
opts.container) {
                
opts = $.extend({}, options);
                
opts.container "body";
            }
            
handleClick(eventopts);
        });
    }

    function 
handleClick(eventcontaineroptions) {
        
options optionsFor(containeroptions);
        var 
link event.currentTarget;
        var 
$link = $(link);
        if (
link.tagName.toUpperCase() !== "A") {
            throw 
"$.fn.ajaxRequest or $.ajaxRequest.click requires an anchor element";
        }
        if (
event.which || event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) {
            return;
        }
        if (/.(?:
jp(?:e?g|e|2)|gif|png|tiff?|bmp|ico|mp3)$/i.test(link.href) === true) {
            return;
        }
        if (
$link.attr('target') !== undefined || $link.attr('download') !== undefined) {
            return;
        }
        if (
location.protocol !== link.protocol || location.hostname !== link.hostname) {
            return;
        }
        if (
link.href.indexOf("#") > -&& stripHash(link) === stripHash(location)) {
            return;
        }
        if (
event.isDefaultPrevented()) {
            return;
        }
        var 
defaults = {urllink.hrefcontainer"a"targetlink};
        var 
opts = $.extend({}, defaultsoptions);
        var 
clickEvent = $.Event("ajaxRequest:click");
        
$link.trigger(clickEvent, [opts]);
        if (!
clickEvent.isDefaultPrevented()) {
            
ajaxRequest(opts);
            
event.preventDefault();
            
$link.trigger("ajaxRequest:clicked", [opts]);
        }
    }

    var 
pressingButton = [];

    function 
handlePressingButton(event) {
        
pressingButton = $(event.currentTarget);
        
pressingButton.attr("value""u041fu043eu0434u043eu0436u0434u0438u0442u0435...");
    }

    function 
handleSubmit(eventcontaineroptions) {
        
options optionsFor(containeroptions);
        var 
form event.currentTarget;
        var 
$form = $(form);
        if (
form.tagName.toUpperCase() !== "FORM") {
            throw 
"$.ajaxRequest.submit requires a form element";
        }
        var 
defaults = {
            
type: ($form.attr("method") || "GET").toUpperCase(),
            
url$form.attr("action"),
            
container"form",
            
targetform
        
};

        if (
defaults.type !== "GET" && window.FormData !== undefined) {

            
defaults.data = new FormData(form);
            
defaults.processData false;
            
defaults.contentType false;
        } else {
            if (
$form.find(":file").length) {
                return;
            }
            
defaults.data $form.serializeArray();
        }
        if (
pressingButton.length && pressingButton.attr("name") !== undefined) {
            if (
defaults.data instanceof FormData)
                
defaults.data.append(pressingButton.attr("name"), pressingButton.attr("value"));
            else
                
defaults.data.push({namepressingButton.attr("name"), valuepressingButton.attr("value")});
        }
        
ajaxRequest($.extend({}, defaultsoptions));
        
event.preventDefault();
    }

    function 
ajaxRequest(options) {
        
options = $.extend(true, {}, $.ajaxSettingsajaxRequest.defaultsoptions);
        if ($.
isFunction(options.url)) {
            
options.url options.url();
        }
        var 
hash options.url.hash;
        var 
containerType = $.type(options.container);
        if (
containerType !== "string") {
            throw 
"expected string value for 'container' option; got " containerType;
        }
        var 
context options.context = $(options.container);
        if (!
context.length) {
            throw 
"the container selector '" options.container "' did not match anything";
        }
        if (!
options.data) {
            
options.data = {};
        }
        function 
fire(typeargsprops) {
            if (!
props) {
                
props = {};
            }
            
props.relatedTarget options.target;
            var 
event = $.Event(typeprops);
            
context.trigger(eventargs);
            return !
event.isDefaultPrevented();
        }

        var 
timeoutTimer;
        
options.beforeSend = function (xhrsettings) {
            if (
settings.type !== "GET") {
                
settings.timeout 0;
            }

            if (!
fire("ajaxRequest:beforeSend", [xhrsettings])) {
                return 
false;
            }
            if (
settings.timeout 0) {
                
timeoutTimer setTimeout(function () {
                    if (
fire("ajaxRequest:timeout", [xhroptions])) {
                        
xhr.abort("timeout");
                    }
                }, 
settings.timeout);
                
settings.timeout 0;
            }
            var 
url settings.url;
            if (
hash) {
                
url.hash hash;
            }
            
options.requestUrl url;
        };
        
options.complete = function (xhrtextStatus) {
            if (
timeoutTimer) {
                
clearTimeout(timeoutTimer);
            }
            
fire("ajaxRequest:complete", [xhrtextStatusoptions]);
            
fire("ajaxRequest:end", [xhroptions]);
        };
        
options.error = function (xhrtextStatuserrorThrown) {
            var 
container extractContainer(""xhroptions);
            var 
allowed fire("ajaxRequest:error", [xhrtextStatuserrorThrownoptions]);
            if (
options.type === "GET" && textStatus !== "abort" && allowed) {
                
locationReplace(container.url);
            }
        };
        
options.success = function (datastatusxhr) {

            var 
previousState ajaxRequest.state;
            var 
container extractContainer(dataxhroptions);
            var 
url container.url;
            if (
hash) {
                
url.hash hash;
                
container.url url.href;
            }
            if (!
container.contents) {
                
locationReplace(container.url);
                return;
            }
            
ajaxRequest.state = {
                
idoptions.id || uniqueId(),
                
urlcontainer.url,
                
titlecontainer.title,
                
containeroptions.container,
                
timeoutoptions.timeout
            
};
            if (
options.push || options.replace) {
                
window.history.replaceState(ajaxRequest.statecontainer.titlecontainer.url);
            }
            var 
blurFocus = $.contains(contextdocument.activeElement);
            if (
blurFocus) {
                try {
                    
document.activeElement.blur();
                } catch (
e) {
                }
            }
            if (
container.title) {
                
document.title container.title;
            }
            
fire("ajaxRequest:beforeReplace", [container.contentsoptions], {
                
stateajaxRequest.state,
                
previousStatepreviousState
            
});
            
context.html(container.contents);
            var 
autofocusEl context.find("input[autofocus], textarea[autofocus]").last()[0];
            if (
autofocusEl && document.activeElement !== autofocusEl) {
                
autofocusEl.focus();
            }
            
executeScriptTags(container.scripts);
            var 
scrollTo options.scrollTo;
            if (
hash) {
                var 
name decodeURIComponent(hash.slice(1));
                var 
target document.getElementById(name) || document.getElementsByName(name)[0];
                if (
target) {
                    
scrollTo = $(target).offset().top;
                }
            }
            if (
typeof scrollTo === "number") {
                $(
window).scrollTop(scrollTo);
            }
            
fire("ajaxRequest:success", [datastatusxhroptions]);
        };

        if (!
ajaxRequest.state) {
            
ajaxRequest.state = {
                
iduniqueId(),
                
urlwindow.location.href,
                
titledocument.title,
                
containeroptions.container,
                
timeoutoptions.timeout
            
};
            
window.history.replaceState(ajaxRequest.statedocument.title);
        }
        
abortXHR(ajaxRequest.xhr);
        
ajaxRequest.options options;
        var 
xhr ajaxRequest.xhr = $.ajax(options);
        if (
xhr.readyState 0) {
            if (
options.push && !options.replace) {
                
cachePush(ajaxRequest.state.id, [options.containercloneContents(context)]);
                
window.history.pushState(null""options.requestUrl);
            }
            
fire("ajaxRequest:start", [xhroptions]);
            
fire("ajaxRequest:send", [xhroptions]);
        }
        return 
ajaxRequest.xhr;
    }

    function 
ajaxRequestReload(containeroptions) {
        var 
defaults = {urlwindow.location.hrefpushfalsereplacetruescrollTofalse};
        return 
ajaxRequest($.extend(defaultsoptionsFor(containeroptions)));
    }

    function 
locationReplace(url) {
        
window.history.replaceState(null""ajaxRequest.state.url);
        
window.location.replace(url);
    }

    var 
initialPop true;
    var 
initialURL window.location.href;
    var 
initialState window.history.state;
    if (
initialState && initialState.container) {
        
ajaxRequest.state initialState;
    }
    if (
"state" in window.history) {
        
initialPop false;
    }
    function 
onAjaxRequestPopstate(event) {
        if (!
initialPop) {
            
abortXHR(ajaxRequest.xhr);
        }
        var 
previousState ajaxRequest.state;
        var 
state event.state;
        var 
direction;
        if (
state && state.container) {
            if (
initialPop && initialURL === state.url) {
                return;
            }
            if (
previousState) {
                if (
previousState.id === state.id) {
                    return;
                }
                
direction previousState.id state.id "forward" "back";
            }
            var 
cache cacheMapping[state.id] || [];
            var 
containerSelector cache[0] || state.container;
            var 
container = $(containerSelector), contents cache[1];
            if (
container.length) {
                if (
previousState) {
                    
cachePop(directionpreviousState.id, [containerSelectorcloneContents(container)]);
                }
                var 
popstateEvent = $.Event("ajaxRequest:popstate", {statestatedirectiondirection});
                
container.trigger(popstateEvent);
                var 
options = {
                    
idstate.id,
                    
urlstate.url,
                    
containercontainerSelector,
                    
pushfalse,
                    
timeoutstate.timeout,
                    
scrollTofalse
                
};
                if (
contents) {
                    
container.trigger("ajaxRequest:start", [nulloptions]);
                    
ajaxRequest.state state;
                    if (
state.title) {
                        
document.title state.title;
                    }
                    var 
beforeReplaceEvent = $.Event("ajaxRequest:beforeReplace", {
                        
statestate,
                        
previousStatepreviousState
                    
});
                    
container.trigger(beforeReplaceEvent, [contentsoptions]);
                    
container.html(contents);
                    
container.trigger("ajaxRequest:end", [nulloptions]);
                } else {
                    
ajaxRequest(options);
                }
                
container[0].offsetHeight;
            } else {
                
locationReplace(location.href);
            }
        }
        
initialPop false;
    }

    function 
abortXHR(xhr) {
        if (
xhr && xhr.readyState 4) {
            
xhr.onreadystatechange = $.noop;
            
xhr.abort();
        }
    }

    function 
uniqueId() {
        return (new 
Date).getTime();
    }

    function 
cloneContents(container) {
        var 
cloned container.clone();
        
cloned.find("script").each(function () {
            if (!
this.src) {
                $.
_data(this"globalEval"false);
            }
        });
        return 
cloned.contents();
    }

    function 
stripHash(location) {
        return 
location.href.replace(/#.*/, "");
    
}

    function 
optionsFor(containeroptions) {
        if (
container && options) {
            
options = $.extend({}, options);
            
options.container container;
            return 
options;
        } else {
            if ($.
isPlainObject(container)) {
                return 
container;
            } else {
                return {
containercontainer};
            }
        }
    }

    function 
findAll(elemsselector) {
        return 
elems.filter(selector).add(elems.find(selector));
    }

    function 
parseHTML(html) {
        return $.
parseHTML(htmldocumenttrue);
    }

    function 
extractContainer(dataxhroptions) {
        var 
obj = {}, fullDocument = /<html/i.test(data);
        var 
serverUrl xhr.getResponseHeader("X-AJAX-REQUEST-URL");
        
obj.url serverUrl serverUrl options.requestUrl;
        var 
$head$body;
        if (
fullDocument) {
            
$body = $(parseHTML(data.match(/<body[^>]*>([sS.]*)</body>/i)[0]));
            var 
head data.match(/<head[^>]*>([sS.]*)</head>/i);
            
$head head !== null ? $(parseHTML(head[0])) : $body;
        } else {
            
$head $body = $(parseHTML(data));
        }
        if (
$body.length === 0) {
            return 
obj;
        }
        
obj.title findAll($head"title").last().text();
        if (
options.container) {
            if (
options.container !== "body") {
                
$body findAll($bodyoptions.container).first();
            }
            if (
$body.length) {
                
obj.contents options.container === "body" $body $body.contents();
                if (!
obj.title) {
                    
obj.title $body.attr("title") || $body.data("title");
                }
            }
        } else {
            if (!
fullDocument) {
                
obj.contents $body;
            }
        }
        if (
obj.contents) {
            
obj.contents obj.contents.not(function () {
                return $(
this).is("title");
            });
            
obj.contents.find("title").remove();
            
obj.scripts findAll(obj.contents"script[src]:not([data-noajax])").remove();
            
obj.contents obj.contents.not(obj.scripts);
        }
        if (
obj.title) {
            
obj.title = $.trim(obj.title);
        }
        return 
obj;
    }

    function 
executeScriptTags(scripts) {
        if (!
scripts) {
            return;
        }
        var 
existingScripts = $("script[src]:not([data-noajax])");
        
scripts.each(function () {
            var 
src this.src;
            var 
matchedScripts existingScripts.filter(function () {
                return 
this.src === src;
            });
            if (
matchedScripts.length) {
                return;
            }
            var 
script document.createElement("script");
            var 
type = $(this).attr("type");
            if (
type) {
                
script.type type;
            }
            
script.src = $(this).attr("src");
            
document.head.appendChild(script);
        });
    }

    var 
cacheMapping = {};
    var 
cacheForwardStack = [];
    var 
cacheBackStack = [];

    function 
cachePush(idvalue) {
        
cacheMapping[id] = value;
        
cacheBackStack.push(id);
        
trimCacheStack(cacheForwardStack0);
        
trimCacheStack(cacheBackStackajaxRequest.defaults.maxCacheLength);
    }

    function 
cachePop(directionidvalue) {
        var 
pushStackpopStack;
        
cacheMapping[id] = value;
        if (
direction === "forward") {
            
pushStack cacheBackStack;
            
popStack cacheForwardStack;
        } else {
            
pushStack cacheForwardStack;
            
popStack cacheBackStack;
        }
        
pushStack.push(id);
        
id popStack.pop();
        if (
id) {
            
delete cacheMapping[id];
        }
        
trimCacheStack(pushStackajaxRequest.defaults.maxCacheLength);
    }

    function 
trimCacheStack(stacklength) {
        while (
stack.length length) {
            
delete cacheMapping[stack.shift()];
        }
    }

    function 
enable() {
        $.fn.
ajaxRequest fnAjaxRequest;
        $.
ajaxRequest ajaxRequest;
        $.
ajaxRequest.enable = $.noop;
        $.
ajaxRequest.disable disable;
        $.
ajaxRequest.click handleClick;
        $.
ajaxRequest.submit handleSubmit;
        $.
ajaxRequest.pressingButton handlePressingButton;
        $.
ajaxRequest.reload ajaxRequestReload;
        $.
ajaxRequest.defaults = {
            
timeout650,
            
pushtrue,
            
replacefalse,
            
type"GET",
            
dataType"html",
            
scrollTo0,
            
maxCacheLength20
        
};
        $(
window).on("popstate.ajaxRequest"onAjaxRequestPopstate);
    }

    function 
disable() {
        $.fn.
ajaxRequest = function () {
            return 
this;
        };
    }

    if ($.
event.props && $.inArray("state", $.event.props) < 0) {
        $.
event.props.push("state");
    } else {
        if (!(
"state" in $.Event.prototype)) {
            $.
event.addProp("state");
        }
    }
    $.
support.ajaxRequest window.history && window.history.pushState && window.history.replaceState && !navigator.userAgent.match(/((iPod|iPhone|iPad).+bOSs+[1-4]D|WebApps/.+CFNetwork)/);
    if ($.
support.ajaxRequest) {
        
enable();
    } else {
        
disable();
    }
})(
jQuery);
?>
Онлайн: 0
Реклама