-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchange.js
183 lines (123 loc) · 3.8 KB
/
change.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
var cache = {};
var main;
var changingPage = false;
function loadPage(url) {
if (cache[url]) {
return new Promise(function(resolve) {
resolve(cache[url]);
});
}
return fetch(url, {
method: 'GET'
}).then(function(response) {
cache[url] = response.text();
return cache[url];
});
}
function changePage() {
var url = window.location.href;
loadPage(url).then(function(responseText) {
var wrapper = document.createElement('div');
wrapper.innerHTML = responseText;
//console.log(responseText);
var oldContent = $('.MainWrap');
var newContent = wrapper.querySelector('.MainWrap');
main.append(newContent);
//newContent.style.opacity = 0;
animate(oldContent, newContent);
document.title = responseText.split("<title>")[1].split("</title>")[0];
//changePageJS(responseText.split('<script id="pageJS" src="')[1].split('"></script>')[0]);
//document.getElementById("pageJS").src = responseText.split('<script id="pageJS" src="')[1].split('"></script>')[0];
//newLoad();
});
}
function animate(oldContent, newContent) {
oldContent.get(0).style.position = 'absolute';
oldContent.get(0).style.width = '100%';
oldContent.get(0).style.top = 0;
//oldContent.get(0).style.zIndex = 99;
oldContent.animate({
top: "100vh"
}, 500);
if(oldContent.get(0).getElementsByClassName("HomeHero")[0]){
document.getElementById("HomeHero").style.position = 'relative';
document.getElementById("HomeHero").style.width = "100%";
document.getElementById("HomeHero").style.top = 0;
$(document.getElementById("HomeHero")).animate({
top:"-200vh"
},500);
}
var fadeIn = function(){
$("html, body").animate({"scrollTop": "0px"}, 500);
if(newContent.getElementsByClassName("HomeHero")[0]){
document.getElementById("HomeHero").style.position = 'relative';
document.getElementById("HomeHero").style.width = "100%";
document.getElementById("HomeHero").style.top = "-200vh";
$(document.getElementById("HomeHero")).animate({
top:"0"
},500);
}
newContent.style.position = "relative";
if(oldContent.get(0).getElementsByClassName("HomeHero")[0]){
newContent.style.top = "calc(90vh - 72px)";
} else{
newContent.style.top = "100vh";
}
$(newContent).animate({
top:0
}, 500, function(){
oldContent.remove();
//changingPage = false;
console.log("Page Transition Complete");
//newLoad();
window.setTimeout(function(){location.reload();},100);
})
}();
}
function changePageJS(el)
{
document.getElementsByTagName("HEAD")[0].removeChild(document.getElementById("pageJS"));
var s = document.createElement("script");
s.src = el;
s.innerHTML = null;
s.id = "pageJS";
//document.getElementById("output").innerHTML = "";
document.getElementsByTagName("HEAD")[0].appendChild(s);
}
//var s;
function setUpPage(){
newLoad();
main = $('body');
window.addEventListener('popstate', changePage);
$("a").on('click', function(e){
if(changingPage){
console.log("Page transition in progress; links disabled");
e.preventDefault();
return;
}
var el = e.target;
while (el && !el.href) {
el = el.parentNode;
}
if (el) {
if(el.host != window.location.host){
console.log("Link to other domain; abandoning animated page transition");
return;
}
e.preventDefault();
changingPage = true;
newUnload();
window.setTimeout(function(){window.location.assign(el.href);},2000);//Fail safe in case of error
var temp = document.getElementsByClassName("SurroundBtn");
for(i = 0; i < temp.length; i++){
temp[i].className = "SurroundBtn";
}
document.getElementsByClassName("MainBtn")[0].className = "MainBtn";
el.className += " CurrPage";
console.log("Animating Page Transition");
history.pushState(null, null, el.href);
changePage();
return;
}
});
}