From bcd942f73607f3deb82825c60e6c7b87329e8ca2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Radek=20=C5=A0er=C3=BD?= Date: Wed, 6 Mar 2019 11:32:14 +0100 Subject: [PATCH] - if `title` element has `data-ajax-update`, then use this title when pushing new state and set `document.title` to this value; see https://github.com/vojtech-dobes/nette.ajax.js/issues/151 --- client-side/history.ajax.js | 202 +++++++++++++++++++----------------- 1 file changed, 105 insertions(+), 97 deletions(-) diff --git a/client-side/history.ajax.js b/client-side/history.ajax.js index 8fdc8e7..12a8e7e 100644 --- a/client-side/history.ajax.js +++ b/client-side/history.ajax.js @@ -1,113 +1,121 @@ (function($, undefined) { -// Is History API reliably supported? (based on Modernizr & PJAX) -if (!(window.history && history.pushState && window.history.replaceState)) { - return; -} - -$.nette.ext('redirect', false); - -var findSnippets = function () { - var result = []; - $('[id^="snippet-"]').each(function () { - var $el = $(this); - if (!$el.is('[data-history-nocache]')) { - result.push({ - id: $el.attr('id'), - html: $el.html() - }); - } - }); - return result; -}; -var handleState = function (context, name, args) { - var handler = context['handle' + name.substring(0, 1).toUpperCase() + name.substring(1)]; - if (handler) { - handler.apply(context, args); + // Is History API reliably supported? (based on Modernizr & PJAX) + if (!(window.history && history.pushState && window.history.replaceState)) { + return; } -}; -$.nette.ext('history', { - init: function () { - var snippetsExt; - if (this.cache && (snippetsExt = $.nette.ext('snippets'))) { - this.handleUI = function (domCache) { - var snippets = {}; - $.each(domCache, function () { - snippets[this.id] = this.html; + $.nette.ext('redirect', false); + + var findSnippets = function () { + var result = []; + $('[id^="snippet-"]').each(function () { + var $el = $(this); + if (!$el.is('[data-history-nocache]')) { + result.push({ + id: $el.attr('id'), + html: $el.html() }); - snippetsExt.updateSnippets(snippets, true); - $.nette.load(); - }; + } + }); + return result; + }; + var handleState = function (context, name, args) { + var handler = context['handle' + name.substring(0, 1).toUpperCase() + name.substring(1)]; + if (handler) { + handler.apply(context, args); } + }; - this.popped = !!('state' in window.history) && !!window.history.state; - var initialUrl = window.location.href; - - $(window).on('popstate.nette', $.proxy(function (e) { - var state = e.originalEvent.state || this.initialState; - var initialPop = (!this.popped && initialUrl === state.href); - this.popped = true; - if (initialPop || !e.originalEvent.state) { - return; + $.nette.ext('history', { + init: function () { + var snippetsExt; + if (this.cache && (snippetsExt = $.nette.ext('snippets'))) { + this.handleUI = function (domCache) { + var snippets = {}; + $.each(domCache, function () { + snippets[this.id] = this.html; + }); + snippetsExt.updateSnippets(snippets, true); + $.nette.load(); + }; } - if (this.cache && state.ui) { - handleState(this, 'UI', [state.ui]); - handleState(this, 'title', [state.title]); + + this.popped = !!('state' in window.history) && !!window.history.state; + var initialUrl = window.location.href; + + $(window).on('popstate.nette', $.proxy(function (e) { + var state = e.originalEvent.state || this.initialState; + var initialPop = (!this.popped && initialUrl === state.href); + this.popped = true; + if (initialPop || !e.originalEvent.state) { + return; + } + if (this.cache && state.ui) { + handleState(this, 'UI', [state.ui]); + handleState(this, 'title', [state.title]); + } else { + $.nette.ajax({ + url: state.href, + off: ['history'] + }); + } + }, this)); + + history.replaceState(this.initialState = { + nette: true, + href: window.location.href, + title: document.title, + ui: this.cache ? findSnippets() : null + }, document.title, window.location.href); + }, + before: function (xhr, settings) { + if (!settings.nette) { + this.href = null; + } else if (!settings.nette.form) { + this.href = settings.nette.ui.href; + } else if (settings.nette.form.get(0).method === 'get') { + this.href = settings.nette.form.get(0).action || window.location.href; } else { - $.nette.ajax({ - url: state.href, - off: ['history'] - }); + this.href = null; } - }, this)); + }, + success: function (payload) { + var redirect = payload.redirect || payload.url; // backwards compatibility for 'url' + if (redirect) { + var regexp = new RegExp('//' + window.location.host + '($|/)'); + if ((redirect.substring(0,4) === 'http') ? regexp.test(redirect) : true) { + this.href = redirect; + } else { + window.location.href = redirect; + } + } + if (this.href && this.href !== window.location.href) { + // Title could also be present in svg! + var titleEl = document.querySelector('head title'); + var title = titleEl.getAttribute('data-ajax-update') || document.title; - history.replaceState(this.initialState = { - nette: true, - href: window.location.href, - title: document.title, - ui: this.cache ? findSnippets() : null - }, document.title, window.location.href); - }, - before: function (xhr, settings) { - if (!settings.nette) { - this.href = null; - } else if (!settings.nette.form) { - this.href = settings.nette.ui.href; - } else if (settings.nette.form.get(0).method === 'get') { - this.href = settings.nette.form.get(0).action || window.location.href; - } else { - this.href = null; - } - }, - success: function (payload) { - var redirect = payload.redirect || payload.url; // backwards compatibility for 'url' - if (redirect) { - var regexp = new RegExp('//' + window.location.host + '($|/)'); - if ((redirect.substring(0,4) === 'http') ? regexp.test(redirect) : true) { - this.href = redirect; - } else { - window.location.href = redirect; + titleEl.removeAttribute('data-ajax-update'); + + history.pushState({ + nette: true, + href: this.href, + title: title, + ui: this.cache ? findSnippets() : null + }, title, this.href); + + document.title = title; } + this.href = null; + this.popped = true; } - if (this.href && this.href != window.location.href) { - history.pushState({ - nette: true, - href: this.href, - title: document.title, - ui: this.cache ? findSnippets() : null - }, document.title, this.href); + }, { + href: null, + cache: true, + popped: false, + handleTitle: function (title) { + document.title = title; } - this.href = null; - this.popped = true; - } -}, { - href: null, - cache: true, - popped: false, - handleTitle: function (title) { - document.title = title; - } -}); + }); })(jQuery);