Skip to content

Commit

Permalink
jumpTo(date) now accepts any date inside the sought domain
Browse files Browse the repository at this point in the history
  • Loading branch information
wa0x6e committed Oct 7, 2013
1 parent 78739ef commit 2128793
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 29 deletions.
46 changes: 30 additions & 16 deletions src/cal-heatmap.js
Expand Up @@ -1799,8 +1799,10 @@ CalHeatMap.prototype = {
*/
getMinuteDomain: function (d, range) {
var start = new Date(d.getFullYear(), d.getMonth(), d.getDate(), d.getHours());
var stop = range;
if (typeof range !== "object") {
var stop = null;
if (typeof range === "object") {
stop = new Date(range.getFullYear(), range.getMonth(), range.getDate(), range.getHours());
} else {
stop = new Date(start.getTime() + 60 * 1000 * range);
}

Expand All @@ -1816,8 +1818,10 @@ CalHeatMap.prototype = {
*/
getHourDomain: function (d, range) {
var start = new Date(d.getFullYear(), d.getMonth(), d.getDate(), d.getHours());
var stop = range;
if (typeof range !== "object") {
var stop = null;
if (typeof range === "object") {
stop = new Date(range.getFullYear(), range.getMonth(), range.getDate(), range.getHours());
} else {
stop = new Date(start.getTime() + 3600 * 1000 * range);
}

Expand All @@ -1833,8 +1837,10 @@ CalHeatMap.prototype = {
*/
getDayDomain: function (d, range) {
var start = new Date(d.getFullYear(), d.getMonth(), d.getDate());
var stop = range;
if (typeof range !== "object") {
var stop = null;
if (typeof range === "object") {
stop = new Date(range.getFullYear(), range.getMonth(), range.getDate());
} else {
stop = new Date(start);
stop = new Date(stop.setDate(stop.getDate() + parseInt(range, 10)));
}
Expand Down Expand Up @@ -1887,8 +1893,10 @@ CalHeatMap.prototype = {
*/
getMonthDomain: function (d, range) {
var start = new Date(d.getFullYear(), d.getMonth());
var stop = range;
if (typeof range !== "object") {
var stop = null;
if (typeof range === "object") {
stop = new Date(range.getFullYear(), range.getMonth());
} else {
stop = new Date(start);
stop = stop.setMonth(stop.getMonth()+range);
}
Expand All @@ -1905,8 +1913,10 @@ CalHeatMap.prototype = {
*/
getYearDomain: function(d, range){
var start = new Date(d.getFullYear(), 0);
var stop = range;
if (typeof range !== "object") {
var stop = null;
if (typeof range === "object") {
stop = new Date(range.getFullYear(), 0);
} else {
stop = new Date(d.getFullYear()+range, 0);
}

Expand Down Expand Up @@ -2177,7 +2187,7 @@ CalHeatMap.prototype = {
/**
* Handle the calendar layout and dimension
*
* Expand and skrink the container depending on its children dimension
* Expand and shrink the container depending on its children dimension
* Also rearrange the children position depending on their dimension,
* and the legend position
*
Expand Down Expand Up @@ -2268,12 +2278,16 @@ CalHeatMap.prototype = {
var firstDomain = domains[0];
var lastDomain = domains[domains.length-1];

if (date > firstDomain && date <= lastDomain && reset) {
return this.loadNextDomain(this.getDomain(firstDomain, date).length);
} else if (date < firstDomain) {
if (date < firstDomain) {
return this.loadPreviousDomain(this.getDomain(firstDomain, date).length);
} else if (date > lastDomain) {
return this.loadNextDomain(this.getDomain(lastDomain, date).length);
} else {
if (reset) {
return this.loadNextDomain(this.getDomain(firstDomain, date).length);
}

if (date > lastDomain) {
return this.loadNextDomain(this.getDomain(lastDomain, date).length);
}
}

return false;
Expand Down
65 changes: 59 additions & 6 deletions test/src/api/jumpto.js
Expand Up @@ -36,10 +36,24 @@ _testJumpTo(
);

_testJumpTo(
new Date(2000, 7),
new Date(2000, 0, 16, 23),
false,
true,
new Date(2000, 4)
new Date(2000, 0)
);

_testJumpTo(
new Date(2000, 6),
false,
true,
new Date(2000, 3)
);

_testJumpTo(
new Date(2000, 6, 12, 8),
false,
true,
new Date(2000, 3)
);

// Without reset, out-of-bound date
Expand Down Expand Up @@ -73,6 +87,14 @@ _testJumpTo(
"already visible, no jump"
);

_testJumpTo(
new Date(2000, 2, 25, 13, 18),
false,
false,
new Date(2000, 2),
"already visible, no jump"
);

_testJumpTo(
new Date(2000, 3),
false,
Expand All @@ -97,6 +119,37 @@ _testJumpTo(
"already visible, no jump"
);

_testJumpTo(
new Date(2000, 5, 30, 23, 59),
false,
false,
new Date(2000, 2),
"already visible, no jump"
);

// With reset

_testJumpTo(
new Date(2000, 0),
true,
true,
new Date(2000, 0)
);

_testJumpTo(
new Date(2000, 0, 6, 16),
true,
true,
new Date(2000, 0)
);

_testJumpTo(
new Date(2000, 6, 18),
true,
true,
new Date(2000, 6)
);

// With reset, out-of-bound date

_testJumpTo(
Expand All @@ -119,28 +172,28 @@ _testJumpTo(
// Set the calendar first domain to the jumped dated

_testJumpTo(
new Date(2000, 2),
new Date(2000, 2, 15),
true,
false,
new Date(2000, 2)
);

_testJumpTo(
new Date(2000, 3),
new Date(2000, 3, 26),
true,
true,
new Date(2000, 3)
);

_testJumpTo(
new Date(2000, 4),
new Date(2000, 4, 5),
true,
true,
new Date(2000, 4)
);

_testJumpTo(
new Date(2000, 5),
new Date(2000, 5, 30),
true,
true,
new Date(2000, 5)
Expand Down
67 changes: 60 additions & 7 deletions test/test.js
@@ -1,4 +1,4 @@
/*! cal-heatmap v3.2.1 (Mon Oct 07 2013 15:50:35)
/*! cal-heatmap v3.3.0 (Mon Oct 07 2013 18:56:45)
* ---------------------------------------------
* Cal-Heatmap is a javascript module to create calendar heatmap to visualize time series data, a la github contribution graph
* https://github.com/kamisama/cal-heatmap
Expand Down Expand Up @@ -89,10 +89,24 @@ _testJumpTo(
);

_testJumpTo(
new Date(2000, 7),
new Date(2000, 0, 16, 23),
false,
true,
new Date(2000, 4)
new Date(2000, 0)
);

_testJumpTo(
new Date(2000, 6),
false,
true,
new Date(2000, 3)
);

_testJumpTo(
new Date(2000, 6, 12, 8),
false,
true,
new Date(2000, 3)
);

// Without reset, out-of-bound date
Expand Down Expand Up @@ -126,6 +140,14 @@ _testJumpTo(
"already visible, no jump"
);

_testJumpTo(
new Date(2000, 2, 25, 13, 18),
false,
false,
new Date(2000, 2),
"already visible, no jump"
);

_testJumpTo(
new Date(2000, 3),
false,
Expand All @@ -150,6 +172,37 @@ _testJumpTo(
"already visible, no jump"
);

_testJumpTo(
new Date(2000, 5, 30, 23, 59),
false,
false,
new Date(2000, 2),
"already visible, no jump"
);

// With reset

_testJumpTo(
new Date(2000, 0),
true,
true,
new Date(2000, 0)
);

_testJumpTo(
new Date(2000, 0, 6, 16),
true,
true,
new Date(2000, 0)
);

_testJumpTo(
new Date(2000, 6, 18),
true,
true,
new Date(2000, 6)
);

// With reset, out-of-bound date

_testJumpTo(
Expand All @@ -172,28 +225,28 @@ _testJumpTo(
// Set the calendar first domain to the jumped dated

_testJumpTo(
new Date(2000, 2),
new Date(2000, 2, 15),
true,
false,
new Date(2000, 2)
);

_testJumpTo(
new Date(2000, 3),
new Date(2000, 3, 26),
true,
true,
new Date(2000, 3)
);

_testJumpTo(
new Date(2000, 4),
new Date(2000, 4, 5),
true,
true,
new Date(2000, 4)
);

_testJumpTo(
new Date(2000, 5),
new Date(2000, 5, 30),
true,
true,
new Date(2000, 5)
Expand Down

0 comments on commit 2128793

Please sign in to comment.