Skip to content

Commit bcd942f

Browse files
committed
- if title element has data-ajax-update, then use this title when pushing new state and set document.title to this value; see vojtech-dobes/nette.ajax.js#151
1 parent e2022aa commit bcd942f

File tree

1 file changed

+105
-97
lines changed

1 file changed

+105
-97
lines changed

client-side/history.ajax.js

Lines changed: 105 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,113 +1,121 @@
11
(function($, undefined) {
22

3-
// Is History API reliably supported? (based on Modernizr & PJAX)
4-
if (!(window.history && history.pushState && window.history.replaceState)) {
5-
return;
6-
}
7-
8-
$.nette.ext('redirect', false);
9-
10-
var findSnippets = function () {
11-
var result = [];
12-
$('[id^="snippet-"]').each(function () {
13-
var $el = $(this);
14-
if (!$el.is('[data-history-nocache]')) {
15-
result.push({
16-
id: $el.attr('id'),
17-
html: $el.html()
18-
});
19-
}
20-
});
21-
return result;
22-
};
23-
var handleState = function (context, name, args) {
24-
var handler = context['handle' + name.substring(0, 1).toUpperCase() + name.substring(1)];
25-
if (handler) {
26-
handler.apply(context, args);
3+
// Is History API reliably supported? (based on Modernizr & PJAX)
4+
if (!(window.history && history.pushState && window.history.replaceState)) {
5+
return;
276
}
28-
};
297

30-
$.nette.ext('history', {
31-
init: function () {
32-
var snippetsExt;
33-
if (this.cache && (snippetsExt = $.nette.ext('snippets'))) {
34-
this.handleUI = function (domCache) {
35-
var snippets = {};
36-
$.each(domCache, function () {
37-
snippets[this.id] = this.html;
8+
$.nette.ext('redirect', false);
9+
10+
var findSnippets = function () {
11+
var result = [];
12+
$('[id^="snippet-"]').each(function () {
13+
var $el = $(this);
14+
if (!$el.is('[data-history-nocache]')) {
15+
result.push({
16+
id: $el.attr('id'),
17+
html: $el.html()
3818
});
39-
snippetsExt.updateSnippets(snippets, true);
40-
$.nette.load();
41-
};
19+
}
20+
});
21+
return result;
22+
};
23+
var handleState = function (context, name, args) {
24+
var handler = context['handle' + name.substring(0, 1).toUpperCase() + name.substring(1)];
25+
if (handler) {
26+
handler.apply(context, args);
4227
}
28+
};
4329

44-
this.popped = !!('state' in window.history) && !!window.history.state;
45-
var initialUrl = window.location.href;
46-
47-
$(window).on('popstate.nette', $.proxy(function (e) {
48-
var state = e.originalEvent.state || this.initialState;
49-
var initialPop = (!this.popped && initialUrl === state.href);
50-
this.popped = true;
51-
if (initialPop || !e.originalEvent.state) {
52-
return;
30+
$.nette.ext('history', {
31+
init: function () {
32+
var snippetsExt;
33+
if (this.cache && (snippetsExt = $.nette.ext('snippets'))) {
34+
this.handleUI = function (domCache) {
35+
var snippets = {};
36+
$.each(domCache, function () {
37+
snippets[this.id] = this.html;
38+
});
39+
snippetsExt.updateSnippets(snippets, true);
40+
$.nette.load();
41+
};
5342
}
54-
if (this.cache && state.ui) {
55-
handleState(this, 'UI', [state.ui]);
56-
handleState(this, 'title', [state.title]);
43+
44+
this.popped = !!('state' in window.history) && !!window.history.state;
45+
var initialUrl = window.location.href;
46+
47+
$(window).on('popstate.nette', $.proxy(function (e) {
48+
var state = e.originalEvent.state || this.initialState;
49+
var initialPop = (!this.popped && initialUrl === state.href);
50+
this.popped = true;
51+
if (initialPop || !e.originalEvent.state) {
52+
return;
53+
}
54+
if (this.cache && state.ui) {
55+
handleState(this, 'UI', [state.ui]);
56+
handleState(this, 'title', [state.title]);
57+
} else {
58+
$.nette.ajax({
59+
url: state.href,
60+
off: ['history']
61+
});
62+
}
63+
}, this));
64+
65+
history.replaceState(this.initialState = {
66+
nette: true,
67+
href: window.location.href,
68+
title: document.title,
69+
ui: this.cache ? findSnippets() : null
70+
}, document.title, window.location.href);
71+
},
72+
before: function (xhr, settings) {
73+
if (!settings.nette) {
74+
this.href = null;
75+
} else if (!settings.nette.form) {
76+
this.href = settings.nette.ui.href;
77+
} else if (settings.nette.form.get(0).method === 'get') {
78+
this.href = settings.nette.form.get(0).action || window.location.href;
5779
} else {
58-
$.nette.ajax({
59-
url: state.href,
60-
off: ['history']
61-
});
80+
this.href = null;
6281
}
63-
}, this));
82+
},
83+
success: function (payload) {
84+
var redirect = payload.redirect || payload.url; // backwards compatibility for 'url'
85+
if (redirect) {
86+
var regexp = new RegExp('//' + window.location.host + '($|/)');
87+
if ((redirect.substring(0,4) === 'http') ? regexp.test(redirect) : true) {
88+
this.href = redirect;
89+
} else {
90+
window.location.href = redirect;
91+
}
92+
}
93+
if (this.href && this.href !== window.location.href) {
94+
// Title could also be present in svg!
95+
var titleEl = document.querySelector('head title');
96+
var title = titleEl.getAttribute('data-ajax-update') || document.title;
6497

65-
history.replaceState(this.initialState = {
66-
nette: true,
67-
href: window.location.href,
68-
title: document.title,
69-
ui: this.cache ? findSnippets() : null
70-
}, document.title, window.location.href);
71-
},
72-
before: function (xhr, settings) {
73-
if (!settings.nette) {
74-
this.href = null;
75-
} else if (!settings.nette.form) {
76-
this.href = settings.nette.ui.href;
77-
} else if (settings.nette.form.get(0).method === 'get') {
78-
this.href = settings.nette.form.get(0).action || window.location.href;
79-
} else {
80-
this.href = null;
81-
}
82-
},
83-
success: function (payload) {
84-
var redirect = payload.redirect || payload.url; // backwards compatibility for 'url'
85-
if (redirect) {
86-
var regexp = new RegExp('//' + window.location.host + '($|/)');
87-
if ((redirect.substring(0,4) === 'http') ? regexp.test(redirect) : true) {
88-
this.href = redirect;
89-
} else {
90-
window.location.href = redirect;
98+
titleEl.removeAttribute('data-ajax-update');
99+
100+
history.pushState({
101+
nette: true,
102+
href: this.href,
103+
title: title,
104+
ui: this.cache ? findSnippets() : null
105+
}, title, this.href);
106+
107+
document.title = title;
91108
}
109+
this.href = null;
110+
this.popped = true;
92111
}
93-
if (this.href && this.href != window.location.href) {
94-
history.pushState({
95-
nette: true,
96-
href: this.href,
97-
title: document.title,
98-
ui: this.cache ? findSnippets() : null
99-
}, document.title, this.href);
112+
}, {
113+
href: null,
114+
cache: true,
115+
popped: false,
116+
handleTitle: function (title) {
117+
document.title = title;
100118
}
101-
this.href = null;
102-
this.popped = true;
103-
}
104-
}, {
105-
href: null,
106-
cache: true,
107-
popped: false,
108-
handleTitle: function (title) {
109-
document.title = title;
110-
}
111-
});
119+
});
112120

113121
})(jQuery);

0 commit comments

Comments
 (0)