Skip to content
This repository was archived by the owner on Jul 29, 2019. It is now read-only.

Commit 6c22663

Browse files
bradhyotamberk
authored andcommitted
Minor cleanups in Timeline Range. (#2633)
Fixes default end, and reduces duplicated code. Adds tests.
1 parent 1393eee commit 6c22663

File tree

5 files changed

+76
-8
lines changed

5 files changed

+76
-8
lines changed

lib/timeline/Range.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ var DateUtil = require('./DateUtil');
1515
function Range(body, options) {
1616
var now = moment().hours(0).minutes(0).seconds(0).milliseconds(0);
1717
var start = now.clone().add(-3, 'days').valueOf();
18-
var end = now.clone().add(-3, 'days').valueOf();
18+
var end = now.clone().add(3, 'days').valueOf();
1919

2020
if(options === undefined) {
2121
this.start = start;
@@ -256,6 +256,13 @@ Range.prototype.setRange = function(start, end, animation, byUser, event) {
256256
}
257257
};
258258

259+
/**
260+
* Get the number of milliseconds per pixel.
261+
*/
262+
Range.prototype.getMillisecondsPerPixel = function() {
263+
return (this.end - this.start) / this.body.dom.center.clientWidth;
264+
}
265+
259266
/**
260267
* Stop an animation
261268
* @private
@@ -291,7 +298,7 @@ Range.prototype._applyRange = function(start, end) {
291298
throw new Error('Invalid end "' + end + '"');
292299
}
293300

294-
// prevent start < end
301+
// prevent end < start
295302
if (newEnd < newStart) {
296303
newEnd = newStart;
297304
}

lib/timeline/component/item/BoxItem.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,14 @@ BoxItem.prototype = new Item (null, null, null);
3737

3838
/**
3939
* Check whether this item is visible inside given range
40-
* @returns {{start: Number, end: Number}} range with a timestamp for start and end
40+
* @param {{start: Number, end: Number}} range with a timestamp for start and end
4141
* @returns {boolean} True if visible
4242
*/
4343
BoxItem.prototype.isVisible = function(range) {
4444
// determine visibility
4545
var isVisible;
4646
var align = this.options.align;
47-
var msPerPixel = (range.end - range.start) / range.body.dom.center.clientWidth;
48-
var widthInMs = this.width * msPerPixel;
47+
var widthInMs = this.width * range.getMillisecondsPerPixel();
4948

5049
if (align == 'right') {
5150
isVisible = (this.data.start.getTime() > range.start ) && (this.data.start.getTime() - widthInMs < range.end);

lib/timeline/component/item/PointItem.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,12 @@ PointItem.prototype = new Item (null, null, null);
3838

3939
/**
4040
* Check whether this item is visible inside given range
41-
* @returns {{start: Number, end: Number}} range with a timestamp for start and end
41+
* @param {{start: Number, end: Number}} range with a timestamp for start and end
4242
* @returns {boolean} True if visible
4343
*/
4444
PointItem.prototype.isVisible = function(range) {
4545
// determine visibility
46-
var msPerPixel = (range.end - range.start) / range.body.dom.center.clientWidth;
47-
var widthInMs = this.width * msPerPixel;
46+
var widthInMs = this.width * range.getMillisecondsPerPixel();
4847

4948
return (this.data.start.getTime() + widthInMs > range.start ) && (this.data.start < range.end);
5049
};

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@
5252
"gulp-concat": "^2.6.0",
5353
"gulp-rename": "^1.2.2",
5454
"gulp-util": "^3.0.7",
55+
"jsdom": "9.9.1",
5556
"mocha": "^3.1.2",
57+
"mocha-jsdom": "^1.1.0",
5658
"rimraf": "^2.5.2",
5759
"uglify-js": "^2.6.2",
5860
"uuid": "^3.0.1",

test/TimelineRange.test.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
var assert = require('assert');
2+
var vis = require('../dist/vis');
3+
var jsdom = require('mocha-jsdom')
4+
var moment = vis.moment;
5+
var timeline = vis.timeline;
6+
var Range = timeline.Range;
7+
8+
function buildSimpleBody() {
9+
var body = {
10+
dom: {
11+
center: {
12+
clientWidth: 1000
13+
}
14+
},
15+
domProps: this.props,
16+
emitter: {
17+
on: function() {},
18+
off: function() {},
19+
emit: function() {}
20+
},
21+
hiddenDates: [],
22+
util: {}
23+
};
24+
body.dom.rollingModeBtn = document.createElement('div');
25+
return body;
26+
}
27+
28+
describe('Timeline Range', function () {
29+
30+
jsdom();
31+
32+
it('should have start default before now', function () {
33+
var now = moment().hours(0).minutes(0).seconds(0).milliseconds(0).valueOf();
34+
var range = new Range(buildSimpleBody());
35+
assert(range.start < now, "Default start is before now");
36+
});
37+
38+
it('should have end default after now', function () {
39+
var now = moment().hours(0).minutes(0).seconds(0).milliseconds(0).valueOf();
40+
var range = new Range(buildSimpleBody());
41+
assert(range.end > now, "Default end is after now");
42+
});
43+
44+
it('should support custom start and end dates', function () {
45+
var range = new Range(buildSimpleBody());
46+
range.setRange(new Date(2017, 0, 26, 13, 26, 3, 320), new Date(2017, 3, 11, 0, 23, 35, 0), false, false, null);
47+
assert.equal(range.start, new Date(2017, 0, 26, 13, 26, 3, 320).valueOf(), "start is as expected");
48+
assert.equal(range.end, new Date(2017, 3, 11, 0, 23, 35, 0).valueOf(), "end is as expected");
49+
});
50+
51+
it('should calculate milliseconds per pixel', function () {
52+
var range = new Range(buildSimpleBody());
53+
assert(range.getMillisecondsPerPixel() > 0, "positive value for milliseconds per pixel");
54+
});
55+
56+
it('should calculate 1 millisecond per pixel for simple range', function () {
57+
var range = new Range(buildSimpleBody());
58+
range.setRange(new Date(2017, 0, 26, 13, 26, 3, 320), new Date(2017, 0, 26, 13, 26, 4, 320), false, false, null);
59+
assert.equal(range.getMillisecondsPerPixel(), 1, "one second over 1000 pixels");
60+
});
61+
});

0 commit comments

Comments
 (0)