Browse Source

Merge branch 'master' of https://github.com/nubben/netrunner

pull/2/head
Odilitime 5 years ago
parent
commit
6c71934c5e
  1. 59
      src/BrowsingHistory.cpp
  2. 35
      src/BrowsingHistory.h

59
src/BrowsingHistory.cpp

@ -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);
}

35
src/BrowsingHistory.h

@ -3,20 +3,39 @@ @@ -3,20 +3,39 @@
#include <vector>
#include "URL.h"
#include <functional>
// if it's going to have fwd/back then
// please model after https://developer.mozilla.org/en-US/docs/Web/API/History
/*
NOTE: The first parameter of the constructor is the callback to be called when
calling go(int) (or back or forward or whatever). Should be a
function that navigates to the specified URL.
*/
// modelled after https://developer.mozilla.org/en-US/docs/Web/API/History
class BrowsingHistory {
public:
BrowsingHistory();
void addEntry(URL url);
URL const& goForward();
URL const& goBack();
std::vector<URL> const& getHistory() const;
BrowsingHistory(std::function<void(URL const&)> onGotoPage_);
unsigned int length() const;
//void scrollRestoration(); // TODO: Implement this when it's relevant
//void* state() const; // TODO: Implement when we have Javascript objects.
void back();
void forward();
void go();
void go(int diff);
// stateObj: void* should be a Javascript object once we have those. Just
// give it nullptr for the time being.
// title: "Firefox currently ignores this parameter, although it may
// use it in the future.". So i guess we ignore it too?
void pushState(void* stateObj, std::string const& title, URL const& url);
void replaceState(void* stateObj, std::string const& title, URL const& url);
private:
std::vector<URL> history;
unsigned int currentPosition;
std::vector<URL>::iterator currentPosition;
std::function<void(URL const&)> onGotoPage;
};
#endif

Loading…
Cancel
Save