Skip to content

Commit

Permalink
- if title element has data-ajax-update, then use this title when…
Browse files Browse the repository at this point in the history
… pushing new state and set `document.title` to this value; see vojtech-dobes/nette.ajax.js#151
  • Loading branch information
zipper committed Mar 6, 2019
1 parent e2022aa commit bcd942f
Showing 1 changed file with 105 additions and 97 deletions.
202 changes: 105 additions & 97 deletions client-side/history.ajax.js
Original file line number Diff line number Diff line change
@@ -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);

0 comments on commit bcd942f

Please sign in to comment.