|
1 | 1 | (function($, undefined) { |
2 | 2 |
|
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; |
27 | 6 | } |
28 | | -}; |
29 | 7 |
|
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() |
38 | 18 | }); |
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); |
42 | 27 | } |
| 28 | + }; |
43 | 29 |
|
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 | + }; |
53 | 42 | } |
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; |
57 | 79 | } else { |
58 | | - $.nette.ajax({ |
59 | | - url: state.href, |
60 | | - off: ['history'] |
61 | | - }); |
| 80 | + this.href = null; |
62 | 81 | } |
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; |
64 | 97 |
|
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; |
91 | 108 | } |
| 109 | + this.href = null; |
| 110 | + this.popped = true; |
92 | 111 | } |
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; |
100 | 118 | } |
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 | + }); |
112 | 120 |
|
113 | 121 | })(jQuery); |
0 commit comments