12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019 |
- (function($){
- function fnPjax(selector, container, options) {
- options = optionsFor(container, options)
- var handler = function(event) {
- var opts = options
- if (!opts.container) {
- opts = $.extend({history: true}, options)
- opts.container = $(this).attr('data-pjax')
- }
- handleClick(event, opts)
- }
- $(selector).removeClass('data-pjax');
- return this
- .off('click.pjax', selector, handler)
- .on('click.pjax', selector, handler);
- }
- function handleClick(event, container, options) {
- options = optionsFor(container, options)
- var link = event.currentTarget
- var $link = $(link)
-
- if (parseInt($link.data('pjax')) === 0) {
- return
- }
- if (link.tagName.toUpperCase() !== 'A')
- throw "$.fn.pjax or $.pjax.click requires an anchor element"
-
-
- if ( event.which > 1 || event.metaKey || event.ctrlKey || event.shiftKey || event.altKey )
- return
-
- if ( location.protocol !== link.protocol || location.hostname !== link.hostname )
- return
-
- if ( link.href.indexOf('#') > -1 && stripHash(link) == stripHash(location) )
- return
-
- if (event.isDefaultPrevented())
- return
- var defaults = {
- url: link.href,
- container: $link.attr('data-pjax'),
- target: link
- }
- var opts = $.extend({}, defaults, options)
- var clickEvent = $.Event('pjax:click')
- $link.trigger(clickEvent, [opts])
- if (!clickEvent.isDefaultPrevented()) {
- pjax(opts)
- event.preventDefault()
- $link.trigger('pjax:clicked', [opts])
- }
- }
- function handleSubmit(event, container, options) {
-
- if (event.result === false)
- return false;
- options = optionsFor(container, options)
- var form = event.currentTarget
- var $form = $(form)
- if (form.tagName.toUpperCase() !== 'FORM')
- throw "$.pjax.submit requires a form element"
- var defaults = {
- type: ($form.attr('method') || 'GET').toUpperCase(),
- url: $form.attr('action'),
- container: $form.attr('data-pjax'),
- target: form
- }
- 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()
- }
- pjax($.extend({}, defaults, options))
- event.preventDefault()
- }
- function pjax(options) {
- options = $.extend(true, {}, $.ajaxSettings, pjax.defaults, options)
- if ($.isFunction(options.url)) {
- options.url = options.url()
- }
- var hash = parseURL(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 = {}
- if ($.isArray(options.data)) {
- options.data = $.grep(options.data, function(obj) { return '_pjax' !== obj.name })
- options.data.push({name: '_pjax', value: options.container})
- } else {
- options.data._pjax = options.container
- }
- function fire(type, args, props) {
- if (!props) props = {}
- props.relatedTarget = options.target
- var event = $.Event(type, props)
- context.trigger(event, args)
- return !event.isDefaultPrevented()
- }
- var timeoutTimer
- options.beforeSend = function(xhr, settings) {
-
-
- if (settings.type !== 'GET') {
- settings.timeout = 0
- }
- xhr.setRequestHeader('X-PJAX', 'true')
- xhr.setRequestHeader('X-PJAX-Container', options.container)
- if (settings.ieRedirectCompatibility) {
- var ua = window.navigator.userAgent
- if (ua.indexOf('MSIE ') > 0 || ua.indexOf('Trident/') > 0 || ua.indexOf('Edge/') > 0) {
- xhr.setRequestHeader('X-Ie-Redirect-Compatibility', 'true')
- }
- }
- if (!fire('pjax:beforeSend', [xhr, settings]))
- return false
- if (settings.timeout > 0) {
- timeoutTimer = setTimeout(function() {
- if (fire('pjax:timeout', [xhr, options]))
- xhr.abort('timeout')
- }, settings.timeout)
-
- settings.timeout = 0
- }
- var url = parseURL(settings.url)
- if (hash) url.hash = hash
- options.requestUrl = stripInternalParams(url)
- }
- options.complete = function(xhr, textStatus) {
- if (timeoutTimer)
- clearTimeout(timeoutTimer)
- fire('pjax:complete', [xhr, textStatus, options])
- fire('pjax:end', [xhr, options])
- }
- options.error = function(xhr, textStatus, errorThrown) {
- var container = extractContainer("", xhr, options)
-
- var redirect = xhr.status >= 301 && xhr.status <= 303
-
- var allowed = redirect || fire('pjax:error', [xhr, textStatus, errorThrown, options])
- if (redirect || options.type == 'GET' && textStatus !== 'abort' && allowed) {
- if (options.replaceRedirect) {
- locationReplace(container.url)
- } else if (options.pushRedirect) {
- window.history.pushState(null, "", container.url)
- window.location.replace(container.url)
- }
- }
- }
- options.success = function(data, status, xhr) {
- var previousState = pjax.state
-
-
- var currentVersion = typeof $.pjax.defaults.version === 'function' ?
- $.pjax.defaults.version() :
- $.pjax.defaults.version
- var latestVersion = xhr.getResponseHeader('X-PJAX-Version')
- var container = extractContainer(data, xhr, options)
- var url = parseURL(container.url)
- if (hash) {
- url.hash = hash
- container.url = url.href
- }
-
- if (currentVersion && latestVersion && currentVersion !== latestVersion) {
- locationReplace(container.url)
- return
- }
-
- if (!container.contents) {
- locationReplace(container.url)
- return
- }
- pjax.state = {
- id: options.id || uniqueId(),
- url: container.url,
- title: container.title,
- container: options.container,
- fragment: options.fragment,
- timeout: options.timeout,
- cache: options.cache
- }
- if (options.history && (options.push || options.replace)) {
- window.history.replaceState(pjax.state, container.title, container.url)
- }
-
- var blurFocus = $.contains(context, document.activeElement)
-
- if (blurFocus) {
- try {
- document.activeElement.blur()
- } catch (e) { }
- }
- if (container.title) document.title = container.title
- fire('pjax:beforeReplace', [container.contents, options], {
- state: pjax.state,
- previousState: previousState
- })
- context.html(container.contents)
-
-
-
-
-
- var autofocusEl = context.find('input[autofocus], textarea[autofocus]').last()[0]
- if (autofocusEl && document.activeElement !== autofocusEl) {
- autofocusEl.focus()
- }
- executeScriptTags(container.scripts, context)
- loadLinkTags(container.links)
-
- if (typeof options.scrollTo === 'function') {
- var scrollTo = options.scrollTo(context, hash)
- } else {
- var scrollTo = options.scrollTo
-
- if (hash || true === scrollTo) {
- var name = decodeURIComponent(hash.slice(1))
- var target = true === scrollTo ? context : (document.getElementById(name) || document.getElementsByName(name)[0])
- if (target) scrollTo = $(target).offset().top
- }
- }
- if (typeof options.scrollOffset === 'function')
- var scrollOffset = options.scrollOffset(scrollTo)
- else
- var scrollOffset = options.scrollOffset
-
- if (typeof scrollTo === 'number') {
- scrollTo = scrollTo + scrollOffset;
- if (scrollTo < 0) scrollTo = 0
- $(window).scrollTop(scrollTo)
- }
- fire('pjax:success', [data, status, xhr, options])
- }
-
-
-
-
- if (!pjax.state) {
- pjax.state = {
- id: uniqueId(),
- url: window.location.href,
- title: document.title,
- container: options.container,
- fragment: options.fragment,
- timeout: options.timeout,
- cache: options.cache
- }
- if (options.history)
- window.history.replaceState(pjax.state, document.title)
- }
-
- if (pjax.xhr && pjax.xhr.readyState < 4 && pjax.options.skipOuterContainers) {
- return
- }
-
- abortXHR(pjax.xhr)
- pjax.options = options
- var xhr = pjax.xhr = $.ajax(options)
- if (xhr.readyState > 0) {
- if (options.history && (options.push && !options.replace)) {
-
- cachePush(pjax.state.id, [options.container, cloneContents(context)])
- window.history.pushState(null, "", options.requestUrl)
- }
- fire('pjax:start', [xhr, options])
- fire('pjax:send', [xhr, options])
- }
- return pjax.xhr
- }
- function pjaxReload(container, options) {
- var defaults = {
- url: window.location.href,
- push: false,
- replace: true,
- scrollTo: false
- }
- return pjax($.extend(defaults, optionsFor(container, options)))
- }
- function locationReplace(url) {
- if (!pjax.options.history) return
- window.history.replaceState(null, "", pjax.state.url)
- window.location.replace(url)
- }
- var initialPop = true
- var initialURL = window.location.href
- var initialState = window.history.state
- if (initialState && initialState.container) {
- pjax.state = initialState
- }
- if ('state' in window.history) {
- initialPop = false
- }
- function onPjaxPopstate(event) {
-
- if (!initialPop) {
- abortXHR(pjax.xhr)
- }
- var previousState = pjax.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) {
- var options = {
- id: state.id,
- url: state.url,
- container: containerSelector,
- push: false,
- fragment: state.fragment,
- timeout: state.timeout,
- cache: state.cache,
- scrollTo: false
- }
- if (previousState && options.cache) {
-
-
- cachePop(direction, previousState.id, [containerSelector, cloneContents(container)])
- }
- var popstateEvent = $.Event('pjax:popstate', {
- state: state,
- direction: direction
- })
- container.trigger(popstateEvent)
- if (contents) {
- container.trigger('pjax:start', [null, options])
- pjax.state = state
- if (state.title) document.title = state.title
- var beforeReplaceEvent = $.Event('pjax:beforeReplace', {
- state: state,
- previousState: previousState
- })
- container.trigger(beforeReplaceEvent, [contents, options])
- container.html(contents)
- container.trigger('pjax:end', [null, options])
- } else {
- pjax(options)
- }
-
-
- container[0].offsetHeight
- } else {
- locationReplace(location.href)
- }
- }
- initialPop = false
- }
- function fallbackPjax(options) {
- var url = $.isFunction(options.url) ? options.url() : options.url,
- method = options.type ? options.type.toUpperCase() : 'GET'
- var form = $('<form>', {
- method: method === 'GET' ? 'GET' : 'POST',
- action: url,
- style: 'display:none'
- })
- if (method !== 'GET' && method !== 'POST') {
- form.append($('<input>', {
- type: 'hidden',
- name: '_method',
- value: method.toLowerCase()
- }))
- }
- var data = options.data
- if (typeof data === 'string') {
- $.each(data.split('&'), function(index, value) {
- var pair = value.split('=')
- form.append($('<input>', {type: 'hidden', name: pair[0], value: pair[1]}))
- })
- } else if ($.isArray(data)) {
- $.each(data, function(index, value) {
- form.append($('<input>', {type: 'hidden', name: value.name, value: value.value}))
- })
- } else if (typeof data === 'object') {
- var key
- for (key in data)
- form.append($('<input>', {type: 'hidden', name: key, value: data[key]}))
- }
- $(document.body).append(form)
- form.submit()
- }
- 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 stripInternalParams(url) {
- url.search = url.search.replace(/([?&])(_pjax|_)=[^&]*/g, '').replace(/^&/, '')
- return url.href.replace(/\?($|#)/, '$1')
- }
- function parseURL(url) {
- var a = document.createElement('a')
- a.href = url
- return a
- }
- function stripHash(location) {
- return location.href.replace(/#.*/, '')
- }
- function optionsFor(container, options) {
- if (container && options) {
- options = $.extend({}, options)
- options.container = container
- return options
- } else if ($.isPlainObject(container)) {
- return container
- } else {
- return {container: container}
- }
- }
- function findAll(elems, selector) {
- return elems.filter(selector).add(elems.find(selector))
- }
- function parseHTML(html) {
- return $.parseHTML(html, document, true)
- }
- function extractContainer(data, xhr, options) {
- var obj = {}, fullDocument = /<html/i.test(data)
-
-
- var serverUrl = xhr.getResponseHeader('X-PJAX-URL')
- obj.url = serverUrl ? stripInternalParams(parseURL(serverUrl)) : options.requestUrl
- var $head, $body
-
- if (fullDocument) {
- $body = $(parseHTML(data.match(/<body[^>]*>([\s\S.]*)<\/body>/i)[0]))
- var head = data.match(/<head[^>]*>([\s\S.]*)<\/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.fragment) {
- var $fragment = $body
-
-
- if (options.fragment !== 'body') {
- $fragment = findAll($fragment, options.fragment).first()
- }
- if ($fragment.length) {
- obj.contents = options.fragment === 'body' ? $fragment : $fragment.contents()
-
-
- if (!obj.title)
- obj.title = $fragment.attr('title') || $fragment.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').remove()
- obj.contents = obj.contents.not(obj.scripts)
-
- obj.links = findAll(obj.contents, 'link[href]').remove()
- obj.contents = obj.contents.not(obj.links)
- }
-
- if (obj.title) obj.title = $.trim(obj.title)
- return obj
- }
- function executeScriptTags(scripts, context) {
- if (!scripts) return
- var existingScripts = $('script[src]')
- var cb = function (next) {
- var src = this.src
- var matchedScripts = existingScripts.filter(function () {
- return this.src === src
- })
- if (matchedScripts.length) {
- next()
- return
- }
- if (src) {
- $.getScript(src).done(next).fail(next)
- document.head.appendChild(this)
- } else {
- context.append(this)
- next()
- }
- }
- var i = 0
- var next = function () {
- if (i >= scripts.length) {
- return
- }
- var script = scripts[i]
- i++
- cb.call(script, next)
- }
- next()
- }
- function loadLinkTags(links) {
- if (!links) return
- var existingLinks = $('link[href]')
- links.each(function() {
- var href = this.href,
- alreadyLoadedLinks = existingLinks.filter(function() {
- return this.href === href
- })
- if (alreadyLoadedLinks.length) return
- document.head.appendChild(this)
- })
- }
- var cacheMapping = {}
- var cacheForwardStack = []
- var cacheBackStack = []
- function cachePush(id, value) {
- if (!pjax.options.cache) {
- return
- }
- cacheMapping[id] = value
- cacheBackStack.push(id)
-
- trimCacheStack(cacheForwardStack, 0)
-
- trimCacheStack(cacheBackStack, pjax.defaults.maxCacheLength)
- }
- function cachePop(direction, id, value) {
- var pushStack, popStack
- 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(pushStack, pjax.defaults.maxCacheLength)
- }
- function trimCacheStack(stack, length) {
- while (stack.length > length)
- delete cacheMapping[stack.shift()]
- }
- function findVersion() {
- return $('meta').filter(function() {
- var name = $(this).attr('http-equiv')
- return name && name.toUpperCase() === 'X-PJAX-VERSION'
- }).attr('content')
- }
- function enable() {
- $.fn.pjax = fnPjax
- $.pjax = pjax
- $.pjax.enable = $.noop
- $.pjax.disable = disable
- $.pjax.click = handleClick
- $.pjax.submit = handleSubmit
- $.pjax.reload = pjaxReload
- $.pjax.defaults = {
- history: true,
- cache: true,
- timeout: 650,
- push: true,
- replace: false,
- type: 'GET',
- dataType: 'html',
- scrollTo: 0,
- scrollOffset: 0,
- maxCacheLength: 20,
- version: findVersion,
- pushRedirect: false,
- replaceRedirect: true,
- skipOuterContainers: false,
- ieRedirectCompatibility: true
- }
- $(window).on('popstate.pjax', onPjaxPopstate)
- }
- function disable() {
- $.fn.pjax = function() { return this }
- $.pjax = fallbackPjax
- $.pjax.enable = enable
- $.pjax.disable = $.noop
- $.pjax.click = $.noop
- $.pjax.submit = $.noop
- $.pjax.reload = function() { window.location.reload() }
- $(window).off('popstate.pjax', onPjaxPopstate)
- }
- if ($.event.props && $.inArray('state', $.event.props) < 0) {
- $.event.props.push('state')
- } else if (!('state' in $.Event.prototype)) {
- $.event.addProp('state')
- }
- $.support.pjax =
- window.history && window.history.pushState && window.history.replaceState &&
-
- !navigator.userAgent.match(/((iPod|iPhone|iPad).+\bOS\s+[1-4]\D|WebApps\/.+CFNetwork)/)
- if ($.support.pjax) {
- enable()
- } else {
- disable()
- }
- })(jQuery);
|