reverse traverse implemented
This commit is contained in:
@@ -48,7 +48,8 @@ __webpack_require__.r(__webpack_exports__);
|
||||
|
||||
class HammerGestures {
|
||||
constructor() {
|
||||
this.history = JSON.parse(sessionStorage.getItem('historyArray')) || [];
|
||||
this.pageTransition = new _PageTransitions__WEBPACK_IMPORTED_MODULE_0__["default"]();
|
||||
this.history = this.pageTransition.historyArray;
|
||||
this.init();
|
||||
}
|
||||
init() {
|
||||
@@ -60,56 +61,63 @@ class HammerGestures {
|
||||
hammer.get('swipe').set({
|
||||
direction: Hammer.DIRECTION_ALL
|
||||
});
|
||||
var pageTransition = new _PageTransitions__WEBPACK_IMPORTED_MODULE_0__["default"]();
|
||||
|
||||
// Add a swipe event listener for swipe left
|
||||
hammer.on('swipeleft', ev => {
|
||||
this.currentUrl = window.location.href;
|
||||
this.goForward(this.currentUrl);
|
||||
// this.goForward(this.currentUrl)
|
||||
console.log(ev.type);
|
||||
|
||||
// pageTransition.animatePageTransition(() => {
|
||||
// pageTransition.loadContent(pageTransition.navigateArray('forward'), 'content-container')
|
||||
// this.pageTransition.animatePageTransition(() => {
|
||||
// this.pageTransition.loadContent(this.pageTransition.navigateArray('forward'), 'content-container')
|
||||
// })
|
||||
});
|
||||
|
||||
// Add a swipe event listener for swipe right
|
||||
hammer.on('swiperight', ev => {
|
||||
this.currentUrl = window.location.href;
|
||||
this.goBack(this.currentUrl);
|
||||
// this.goBack(this.currentUrl)
|
||||
console.log(ev.type);
|
||||
pageTransition.animatePageTransition(() => {
|
||||
pageTransition.loadContent(pageTransition.navigateArray('backward'), 'content-container');
|
||||
});
|
||||
console.log(JSON.parse(sessionStorage.getItem('historyArray')));
|
||||
if (JSON.parse(sessionStorage.getItem('historyArray')).length > 1) {
|
||||
this.pageTransition.animatePageTransition(() => {
|
||||
this.pageTransition.loadContent(this.pageTransition.navigateArray('backward'), 'content-container');
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
goForward(currentUrl) {
|
||||
if (this.history.length > 0) {
|
||||
const nextUrl = this.history[this.history.length - 1];
|
||||
if (HammerGestures.isSameDomain(currentUrl, nextUrl)) {
|
||||
console.log(`Going forward to ${nextUrl}`);
|
||||
this.history.pop();
|
||||
} else {
|
||||
console.log('Cannot go forward. Different domain.');
|
||||
}
|
||||
} else {
|
||||
console.log('Cannot go forward. History is empty.');
|
||||
}
|
||||
}
|
||||
goBack(currentUrl) {
|
||||
if (this.history.length > 0) {
|
||||
const previousUrl = this.history.pop();
|
||||
if (HammerGestures.isSameDomain(currentUrl, previousUrl)) {
|
||||
console.log(`Going back to ${previousUrl}`);
|
||||
} else {
|
||||
console.log('Cannot go back. Different domain.');
|
||||
this.history.push(previousUrl); // Re-add the popped URL
|
||||
}
|
||||
} else {
|
||||
console.log('Cannot go back. History is empty.');
|
||||
}
|
||||
}
|
||||
|
||||
// goForward(currentUrl) {
|
||||
// if (this.history.length > 0) {
|
||||
// const nextUrl = this.history[this.history.length - 1]
|
||||
// if (HammerGestures.isSameDomain(currentUrl, nextUrl)) {
|
||||
// console.log(`Going forward to ${nextUrl}`)
|
||||
// this.history.pop()
|
||||
// } else {
|
||||
// console.log('Cannot go forward. Different domain.')
|
||||
// }
|
||||
// } else {
|
||||
// console.log('Cannot go forward. History is empty.')
|
||||
// }
|
||||
// }
|
||||
|
||||
// goBack(currentUrl) {
|
||||
// if (this.history.length > 0) {
|
||||
// const previousUrl = this.history.pop()
|
||||
// if (HammerGestures.isSameDomain(currentUrl, previousUrl)) {
|
||||
// console.log(`Going back to ${previousUrl}`)
|
||||
// } else {
|
||||
// console.log('Cannot go back. Different domain.')
|
||||
// this.history.push(previousUrl) // Re-add the popped URL
|
||||
// return
|
||||
// }
|
||||
// } else {
|
||||
// console.log('Cannot go back. History is empty.')
|
||||
// return
|
||||
// }
|
||||
// }
|
||||
|
||||
static isSameDomain(url1, url2) {
|
||||
const domain1 = new URL(url1).hostname;
|
||||
const domain2 = new URL(url2).hostname;
|
||||
@@ -177,6 +185,9 @@ __webpack_require__.r(__webpack_exports__);
|
||||
|
||||
class PageTransitions {
|
||||
constructor() {
|
||||
// Check if the history array already exists in the sessionStorage
|
||||
this.historyArray = JSON.parse(sessionStorage.getItem('historyArray')) || [];
|
||||
this.navControl = new _NavControl__WEBPACK_IMPORTED_MODULE_1__["default"]();
|
||||
this.init();
|
||||
}
|
||||
init() {
|
||||
@@ -194,7 +205,6 @@ class PageTransitions {
|
||||
if (link.href === window.location.href) {
|
||||
return;
|
||||
}
|
||||
this.navControl = new _NavControl__WEBPACK_IMPORTED_MODULE_1__["default"]();
|
||||
if (this.navControl.toggleMenuBtn.classList.contains('active')) {
|
||||
this.navControl.toggleNavMenu();
|
||||
}
|
||||
@@ -216,6 +226,7 @@ class PageTransitions {
|
||||
this.forms = document.querySelectorAll(selector);
|
||||
this.forms.forEach(form => {
|
||||
form.addEventListener('submit', e => {
|
||||
console.log(e);
|
||||
e.preventDefault();
|
||||
if (this.navControl.toggleMenuBtn.classList.contains('active')) {
|
||||
this.navControl.toggleNavMenu();
|
||||
@@ -223,6 +234,7 @@ class PageTransitions {
|
||||
this.animatePageTransition(() => {
|
||||
this.submitFormAsync(form);
|
||||
});
|
||||
this.formResetter(form);
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -242,7 +254,6 @@ class PageTransitions {
|
||||
element.classList.remove('page-transition');
|
||||
}
|
||||
loadContent(targetUrl, elementId) {
|
||||
const contentContainer = document.getElementById('content-container');
|
||||
fetch(targetUrl).then(response => response.text()).then(data => {
|
||||
this.updateContent(data, elementId);
|
||||
history.pushState(null, null, targetUrl);
|
||||
@@ -255,19 +266,9 @@ class PageTransitions {
|
||||
body: formData
|
||||
}).then(response => response.text()).then(data => {
|
||||
// Assuming the response contains the new content to be loaded
|
||||
const targetUrl = form.action; // You might need to adjust this based on your server's response
|
||||
const targetUrl = form.action + '?s=' + formData.get('s'); // You might need to adjust this based on your server's response
|
||||
const elementId = 'content-container';
|
||||
|
||||
// Clear the search form
|
||||
var inputs = form.getElementsByTagName('input');
|
||||
Array.from(inputs).forEach(function (input) {
|
||||
// Reset the input value
|
||||
input.value = '';
|
||||
|
||||
// Unfocus the input
|
||||
input.blur();
|
||||
});
|
||||
|
||||
// Update content, push state, and remove transition class
|
||||
this.updateContent(data, elementId);
|
||||
history.pushState(null, null, targetUrl);
|
||||
@@ -290,6 +291,17 @@ class PageTransitions {
|
||||
this.addLinkClickListener('#content-container a');
|
||||
}).catch(error => console.error('Error submitting form:', error));
|
||||
}
|
||||
formResetter(form) {
|
||||
// Clear the search form
|
||||
var inputs = form.getElementsByTagName('input');
|
||||
Array.from(inputs).forEach(function (input) {
|
||||
// Reset the input value
|
||||
input.value = '';
|
||||
|
||||
// Unfocus the input
|
||||
input.blur();
|
||||
});
|
||||
}
|
||||
updateContent(data, elementId) {
|
||||
const contentContainer = document.getElementById('content-container');
|
||||
const parsedData = new DOMParser().parseFromString(data, 'text/html');
|
||||
@@ -331,9 +343,6 @@ class PageTransitions {
|
||||
return adminUrlRegex.test(link.href);
|
||||
}
|
||||
createHistoryTracker() {
|
||||
// Check if the history array already exists in the sessionStorage
|
||||
let historyArray = JSON.parse(sessionStorage.getItem('historyArray')) || [];
|
||||
|
||||
// Get the initial page's href
|
||||
let initialHref = window.location.href;
|
||||
|
||||
@@ -344,19 +353,28 @@ class PageTransitions {
|
||||
|
||||
// Check if the currentHref is the same as the last item in the history array
|
||||
// and if it's different from the initialHref
|
||||
if (currentHref !== initialHref && (historyArray.length === 0 || currentHref !== historyArray[historyArray.length])) {
|
||||
// Remove all occurrences of the currentHref from historyArray
|
||||
historyArray = historyArray.filter(url => url !== currentHref);
|
||||
if (currentHref !== initialHref && (this.historyArray.length === 0 || currentHref !== this.historyArray[this.historyArray.length])) {
|
||||
// Remove all occurrences of the currentHref from this.historyArray
|
||||
this.historyArray = this.historyArray.filter(url => url !== currentHref);
|
||||
|
||||
// Add the currentHref to the history array
|
||||
historyArray.unshift(initialHref);
|
||||
initialHref = currentHref;
|
||||
this.historyArray.unshift(currentHref);
|
||||
// initialHref = currentHref
|
||||
// this.historyArray.pop()
|
||||
|
||||
// Save the updated history array to the sessionStorage
|
||||
sessionStorage.setItem('historyArray', JSON.stringify(historyArray));
|
||||
const itemToRemove = 'https://' + window.location.hostname + '/?s=';
|
||||
console.log(itemToRemove);
|
||||
console.log('Yes');
|
||||
|
||||
// Use the filter method to create a new array without the item to remove
|
||||
this.historyArray = this.historyArray.filter(item => item !== itemToRemove);
|
||||
sessionStorage.setItem('historyArray', JSON.stringify(this.historyArray));
|
||||
this.historyArray = JSON.parse(sessionStorage.getItem('historyArray')) || [];
|
||||
// sessionStorage.setItem('historyArray', JSON.stringify(this.historyArray))
|
||||
|
||||
// Log the history array (you can replace this with your own logic)
|
||||
console.log('History Array:', historyArray);
|
||||
// console.log('History Array:', this.historyArray)
|
||||
}
|
||||
};
|
||||
|
||||
@@ -376,15 +394,12 @@ class PageTransitions {
|
||||
observer.observe(document.body, config);
|
||||
}
|
||||
navigateArray(direction) {
|
||||
// Retrieve the historyArray from storage (assuming it's stored as a JSON string)
|
||||
const storedHistory = sessionStorage.getItem('historyArray');
|
||||
|
||||
// Parse the JSON string into an array or provide a default empty array if null
|
||||
const historyArray = storedHistory ? JSON.parse(storedHistory) : [];
|
||||
if (direction === 'forward') {
|
||||
console.log('Forward Navigation is Null');
|
||||
} else if (direction === 'backward') {
|
||||
console.log('Backward Naviation is Allowed And Will Be Implemented Soon');
|
||||
this.historyArray.shift();
|
||||
sessionStorage.setItem('historyArray', JSON.stringify(this.historyArray));
|
||||
return this.historyArray[0];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user