2 changed files with 66 additions and 28 deletions
@ -1,33 +1,52 @@
@@ -1,33 +1,52 @@
|
||||
#include "BrowsingHistory.h" |
||||
|
||||
BrowsingHistory::BrowsingHistory() : |
||||
BrowsingHistory::BrowsingHistory(std::function<void(URL const&)> onGotoPage_) : |
||||
history(), |
||||
currentPosition(0) {} |
||||
currentPosition(history.end()), |
||||
onGotoPage(onGotoPage_) {} |
||||
|
||||
void BrowsingHistory::addEntry(URL url) { |
||||
if (!history.empty()) { |
||||
++currentPosition; |
||||
} |
||||
if (currentPosition < history.size()) { |
||||
history.erase(history.begin() + currentPosition, history.end()); |
||||
} |
||||
history.push_back(url); |
||||
unsigned int BrowsingHistory::length() const { |
||||
return history.size(); |
||||
} |
||||
|
||||
void BrowsingHistory::back() { |
||||
go(-1); |
||||
} |
||||
|
||||
void BrowsingHistory::forward() { |
||||
go(1); |
||||
} |
||||
|
||||
void BrowsingHistory::go() { |
||||
go(0); |
||||
} |
||||
|
||||
URL const& BrowsingHistory::goForward() { |
||||
if (currentPosition < history.size()) { |
||||
++currentPosition; |
||||
void BrowsingHistory::go(int diff) { |
||||
if (history.size() == 0) { |
||||
return; |
||||
} |
||||
std::vector<URL>::iterator newPos = currentPosition + diff; |
||||
if (newPos >= history.begin() && newPos < history.end()) { |
||||
currentPosition = newPos; |
||||
onGotoPage(*currentPosition); |
||||
} |
||||
return history[currentPosition]; |
||||
} |
||||
|
||||
URL const& BrowsingHistory::goBack() { |
||||
if (currentPosition > 0) { |
||||
--currentPosition; |
||||
void BrowsingHistory::pushState(void* stateObj, std::string const& title, URL const& url) { |
||||
if (history.size() == 0) { |
||||
history.push_back(url); |
||||
currentPosition = history.begin(); |
||||
return; |
||||
} |
||||
return history[currentPosition]; |
||||
URL currentURL = *currentPosition; |
||||
history.erase(++currentPosition, history.end()); |
||||
history.push_back(currentURL.merge(url)); |
||||
currentPosition = history.end() - 1; |
||||
} |
||||
|
||||
std::vector<URL> const& BrowsingHistory::getHistory() const { |
||||
return history; |
||||
void BrowsingHistory::replaceState(void* stateObj, std::string const& title, URL const& url) { |
||||
if (history.size() == 0) { |
||||
return; |
||||
} |
||||
*currentPosition = (*currentPosition).merge(url); |
||||
} |
||||
|
Loading…
Reference in new issue