mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-08-16 01:52:19 +08:00
42 lines
975 B
TypeScript
42 lines
975 B
TypeScript
import { Location as HistoryLocation } from 'history';
|
|
|
|
const RESTRICT_SIZE = 25;
|
|
|
|
export class LocationHistory {
|
|
locationHistory: HistoryLocation[] = [];
|
|
|
|
add(location: HistoryLocation) {
|
|
this.locationHistory.push(location);
|
|
if (this.locationHistory.length > RESTRICT_SIZE) {
|
|
this.locationHistory.splice(0, 10);
|
|
}
|
|
}
|
|
|
|
pop() {
|
|
this.locationHistory.pop();
|
|
}
|
|
|
|
replace(location: HistoryLocation) {
|
|
this.locationHistory.pop();
|
|
this.locationHistory.push(location);
|
|
}
|
|
|
|
findLastLocationByUrl(url: string) {
|
|
for (let i = this.locationHistory.length - 1; i >= 0; i--) {
|
|
const location = this.locationHistory[i];
|
|
if (location.pathname.toLocaleLowerCase() === url.toLocaleLowerCase()) {
|
|
return location;
|
|
}
|
|
}
|
|
return undefined;
|
|
}
|
|
|
|
previous() {
|
|
return this.locationHistory[this.locationHistory.length - 2];
|
|
}
|
|
|
|
current() {
|
|
return this.locationHistory[this.locationHistory.length - 1];
|
|
}
|
|
}
|