Index: chrome/browser/resources/history/history.js |
diff --git a/chrome/browser/resources/history/history.js b/chrome/browser/resources/history/history.js |
index 57532a0f1bbcf197de2ebf1130dec15ef7c9a1ad..a38a2f54c9c79de1f693b1d070b4c9c2c534c73f 100644 |
--- a/chrome/browser/resources/history/history.js |
+++ b/chrome/browser/resources/history/history.js |
@@ -358,12 +358,14 @@ HistoryModel.prototype.reload = function() { |
var search = this.searchText_; |
var page = this.requestedPage_; |
var range = this.rangeInDays_; |
+ var offset = this.offset_; |
var groupByDomain = this.groupByDomain_; |
this.clearModel_(); |
this.searchText_ = search; |
this.requestedPage_ = page; |
this.rangeInDays_ = range; |
+ this.offset_ = offset; |
this.groupByDomain_ = groupByDomain; |
this.queryHistory_(); |
}; |
@@ -479,6 +481,22 @@ Object.defineProperty(HistoryModel.prototype, 'rangeInDays', { |
} |
}); |
+/** |
+ * Getter and setter for HistoryModel.offset_. The offset moves the current |
+ * query 'window' |range| days behind. As such for range set to WEEK an offset |
+ * of 0 refers to the last 7 days, an offset of 1 refers to the 7 day period |
+ * that ended 7 days ago, etc. For MONTH an offset of 0 refers to the current |
+ * calendar month, 1 to the previous one, etc. |
+ */ |
+Object.defineProperty(HistoryModel.prototype, 'offset', { |
James Hawkins
2013/01/30 16:30:09
What is the purpose of using defineProperty?
Sergiu
2013/01/30 17:07:13
Well, I didn't want to access private members from
|
+ get: function() { |
+ return this.offset_; |
+ }, |
+ set: function(offset) { |
+ this.offset_ = offset; |
+ } |
+}); |
+ |
// HistoryModel, Private: ----------------------------------------------------- |
/** |
@@ -500,6 +518,9 @@ HistoryModel.prototype.clearModel_ = function() { |
// to fetch it and call back when we're done. |
this.requestedPage_ = 0; |
+ // Skip |offset_| * weeks/months from the begining. |
+ this.offset_ = 0; |
+ |
// The range of history to view or search over, WEEK by default. |
this.rangeInDays_ = HistoryModel.Range.RECENT; |
@@ -547,13 +568,15 @@ HistoryModel.prototype.updateSearch_ = function() { |
* @private |
*/ |
HistoryModel.prototype.queryHistory_ = function() { |
- // Set the range to the values from the interface. |
+ // Set the range and offset to the values from the interface. |
+ var offset = this.offset_; |
var range = this.rangeInDays_; |
$('loading-spinner').hidden = false; |
this.inFlight_ = true; |
chrome.send('queryHistory', |
- [this.searchText_, range, this.queryCursor_, this.getMaxResults()]); |
+ [this.searchText_, offset, range, this.queryCursor_, |
+ this.getMaxResults()]); |
}; |
/** |
@@ -582,6 +605,7 @@ HistoryModel.prototype.canFillPage_ = function(page) { |
*/ |
HistoryModel.prototype.setGroupByDomain = function(groupByDomain) { |
this.groupByDomain_ = groupByDomain; |
+ this.offset_ = 0; |
}; |
/** |
@@ -639,6 +663,25 @@ function HistoryView(model) { |
$('display-filter-sites').addEventListener('click', function(e) { |
self.setGroupByDomain($('display-filter-sites').checked); |
}); |
+ |
+ $('range-previous').addEventListener('click', function(e) { |
+ if (self.getRangeInDays() == HistoryModel.Range.RECENT) |
+ self.setPage(self.pageIndex_ + 1); |
+ else |
+ self.setOffset(self.getOffset() + 1); |
+ }); |
+ $('range-next').addEventListener('click', function(e) { |
+ if (self.getRangeInDays() == HistoryModel.Range.RECENT) |
+ self.setPage(self.pageIndex_ - 1); |
+ else |
+ self.setOffset(self.getOffset() - 1); |
+ }); |
+ $('range-today').addEventListener('click', function(e) { |
+ if (self.getRangeInDays() == HistoryModel.Range.RECENT) |
+ self.setPage(0); |
+ else |
+ self.setOffset(0); |
+ }); |
} |
// HistoryView, public: ------------------------------------------------------- |
@@ -653,7 +696,7 @@ HistoryView.prototype.setSearch = function(term, opt_page) { |
window.scrollTo(0, 0); |
this.model_.setSearchText(term, this.pageIndex_); |
pageState.setUIState(term, this.pageIndex_, this.model_.getGroupByDomain(), |
- this.getRangeInDays()); |
+ this.getRangeInDays(), this.getOffset()); |
}; |
/** |
@@ -669,7 +712,8 @@ HistoryView.prototype.setGroupByDomain = function(groupedByDomain) { |
pageState.setUIState(this.model_.getSearchText(), |
this.pageIndex_, |
this.model_.getGroupByDomain(), |
- this.getRangeInDays()); |
+ this.getRangeInDays(), |
+ this.getOffset()); |
}; |
/** |
@@ -692,7 +736,8 @@ HistoryView.prototype.setPage = function(page) { |
pageState.setUIState(this.model_.getSearchText(), |
this.pageIndex_, |
this.model_.getGroupByDomain(), |
- this.getRangeInDays()); |
+ this.getRangeInDays(), |
+ this.getOffset()); |
}; |
/** |
@@ -707,12 +752,13 @@ HistoryView.prototype.getPage = function() { |
* @param {string} range The number of days to which the range should be set. |
*/ |
HistoryView.prototype.setRangeInDays = function(range) { |
- // Set the range and reset the page |
+ // Set the range offset and reset the page |
this.model_.rangeInDays = range; |
+ this.model_.offset = 0; |
this.pageIndex_ = 0; |
this.model_.reload(); |
pageState.setUIState(this.model_.getSearchText(), this.pageIndex_, |
- this.model_.getGroupByDomain(), range); |
+ this.model_.getGroupByDomain(), range, this.getOffset()); |
}; |
/** |
@@ -724,6 +770,29 @@ HistoryView.prototype.getRangeInDays = function() { |
}; |
/** |
+ * Set the current offset for grouped results. |
+ * @param {number} offset Offset to set. |
+ */ |
+HistoryView.prototype.setOffset = function(offset) { |
+ window.scrollTo(0, 0); |
+ this.model_.offset = offset; |
+ this.model_.reload(); |
+ pageState.setUIState(this.model_.getSearchText(), |
+ this.pageIndex_, |
+ this.model_.getGroupByDomain(), |
+ this.getRangeInDays(), |
+ this.getOffset()); |
+}; |
+ |
+/** |
+ * Get the current offset. |
+ * @return {number} Current offset from the model. |
+ */ |
+HistoryView.prototype.getOffset = function() { |
+ return this.model_.offset; |
+}; |
+ |
+/** |
* Callback for the history model to let it know that it has data ready for us |
* to view. |
*/ |
@@ -814,6 +883,40 @@ HistoryView.prototype.getGroupedVisitsDOM_ = function( |
}; |
/** |
+ * Enables or disables the time range buttons. |
+ * @private |
+ */ |
+HistoryView.prototype.setRangeButtonsState_ = function() { |
+ // The enabled state for the previous, today and next buttons. |
+ var previousState = false; |
+ var todayState = false; |
+ var nextState = false; |
+ var usePage = (this.getRangeInDays() == HistoryModel.Range.RECENT); |
+ var offset = this.getOffset(); |
+ var page = this.getPage(); |
+ |
+ // Use pagination for most recent visits, offset otherwise. |
+ // TODO(sergiu): Maybe send just one variable in the future. |
+ if (usePage) { |
+ if (page != 0) { |
+ nextState = true; |
+ todayState = true; |
+ } |
+ previousState = this.model_.hasMoreResults(); |
+ } else { |
+ if (offset != 0) { |
+ nextState = true; |
+ todayState = true; |
+ } |
+ previousState = !this.model_.isQueryFinished_; |
+ } |
+ |
+ $('range-previous').disabled = !previousState; |
+ $('range-today').disabled = !todayState; |
+ $('range-next').disabled = !nextState; |
+}; |
+ |
+/** |
* Groups visits by domain, sorting them by the number of visits. |
* @param {Array} visits Visits received from the query results. |
* @param {Element} results Object where the results are added to. |
@@ -954,7 +1057,8 @@ HistoryView.prototype.displayResults_ = function() { |
if (results.length == 0) { |
var noResults = document.createElement('div'); |
noResults.textContent = loadTimeData.getString('noresults'); |
- this.resultDiv_.appendChild(noResults); |
+ resultsFragment.appendChild(noResults); |
+ this.resultDiv_.appendChild(resultsFragment); |
this.updateNavBar_(); |
return; |
} |
@@ -991,6 +1095,7 @@ HistoryView.prototype.displayResults_ = function() { |
* @private |
*/ |
HistoryView.prototype.updateNavBar_ = function() { |
+ this.setRangeButtonsState_(); |
$('newest-button').hidden = this.pageIndex_ == 0; |
$('newer-button').hidden = this.pageIndex_ == 0; |
$('older-button').hidden = |
@@ -1032,6 +1137,8 @@ function PageState(model, view) { |
state_obj.view.setGroupByDomain(isGroupedByDomain); |
} else if (parseInt(hashData.r, 10) != state_obj.model.rangeInDays) { |
state_obj.view.setRangeInDays(parseInt(hashData.r, 10)); |
+ } else if (parseInt(hashData.o, 10) != state_obj.model.offset) { |
+ state_obj.view.setOffset(parseInt(hashData.o, 10)); |
} |
}), 50, this); |
} |
@@ -1050,7 +1157,8 @@ PageState.prototype.getHashData = function() { |
q: '', |
p: 0, |
g: false, |
- r: 0 |
+ r: 0, |
+ o: 0 |
}; |
if (!window.location.hash) |
@@ -1076,8 +1184,9 @@ PageState.prototype.getHashData = function() { |
* @param {boolean} grouped Whether the results are grouped or not. |
* @param {number} range The range in days to view or search over. If 0, use |
* the entire history. |
+ * @param {number} offset Set the begining of the query to range * offset days. |
*/ |
-PageState.prototype.setUIState = function(term, page, grouped, range) { |
+PageState.prototype.setUIState = function(term, page, grouped, range, offset) { |
// Make sure the form looks pretty. |
$('search-field').value = term; |
if (grouped) |
@@ -1086,9 +1195,9 @@ PageState.prototype.setUIState = function(term, page, grouped, range) { |
$('display-filter-sites').checked = false; |
var hash = this.getHashData(); |
if (hash.q != term || hash.p != page || hash.g != grouped || |
- hash.r != range) { |
+ hash.r != range || hash.o != offset) { |
window.location.hash = PageState.getHashString( |
- term, page, grouped, range); |
+ term, page, grouped, range, offset); |
} |
}; |
@@ -1098,9 +1207,10 @@ PageState.prototype.setUIState = function(term, page, grouped, range) { |
* @param {number} page The page currently being viewed. |
* @param {boolean} grouped Whether the results are grouped or not. |
* @param {number} range The range in days to view or search over. |
+ * @param {number} offset Set the begining of the query to range * offset days. |
* @return {string} The string to be used in a hash. |
*/ |
-PageState.getHashString = function(term, page, grouped, range) { |
+PageState.getHashString = function(term, page, grouped, range, offset) { |
// Omit elements that are empty. |
var newHash = []; |
@@ -1116,6 +1226,9 @@ PageState.getHashString = function(term, page, grouped, range) { |
if (range) |
newHash.push('r=' + range); |
+ if (offset) |
+ newHash.push('o=' + offset); |
+ |
return newHash.join('&'); |
}; |