@ -14,8 +14,9 @@ var DateUtil = require('./DateUtil');
* /
* /
function Range ( body , options ) {
function Range ( body , options ) {
var now = moment ( ) . hours ( 0 ) . minutes ( 0 ) . seconds ( 0 ) . milliseconds ( 0 ) ;
var now = moment ( ) . hours ( 0 ) . minutes ( 0 ) . seconds ( 0 ) . milliseconds ( 0 ) ;
this . start = now . clone ( ) . add ( - 3 , 'days' ) . valueOf ( ) ; // Number
this . end = now . clone ( ) . add ( 4 , 'days' ) . valueOf ( ) ; // Number
this . start = options . start || now . clone ( ) . add ( - 3 , 'days' ) . valueOf ( ) ;
this . end = options . end || now . clone ( ) . add ( 4 , 'days' ) . valueOf ( ) ;
this . rolling = false ;
this . body = body ;
this . body = body ;
this . deltaDifference = 0 ;
this . deltaDifference = 0 ;
@ -55,6 +56,9 @@ function Range(body, options) {
this . body . emitter . on ( 'touch' , this . _onTouch . bind ( this ) ) ;
this . body . emitter . on ( 'touch' , this . _onTouch . bind ( this ) ) ;
this . body . emitter . on ( 'pinch' , this . _onPinch . bind ( this ) ) ;
this . body . emitter . on ( 'pinch' , this . _onPinch . bind ( this ) ) ;
// on click of rolling mode button
this . body . dom . rollingModeBtn . addEventListener ( 'click' , this . startRolling . bind ( this ) ) ;
this . setOptions ( options ) ;
this . setOptions ( options ) ;
}
}
@ -80,11 +84,14 @@ Range.prototype.setOptions = function (options) {
if ( options ) {
if ( options ) {
// copy the options that we know
// copy the options that we know
var fields = [
var fields = [
'direction' , 'min' , 'max' , 'zoomMin' , 'zoomMax' , 'moveable' , 'zoomable' ,
'moment' , 'activate' , 'hiddenDates' , 'zoomKey' , 'rtl' , 'horizontalScroll'
'animation' , ' direction' , 'min' , 'max' , 'zoomMin' , 'zoomMax' , 'moveable' , 'zoomable' ,
'moment' , 'activate' , 'hiddenDates' , 'zoomKey' , 'rtl' , 'showCurrentTime' , 'rollMode' , ' horizontalScroll'
] ;
] ;
util . selectiveExtend ( fields , this . options , options ) ;
util . selectiveExtend ( fields , this . options , options ) ;
if ( options . rollingMode ) {
this . startRolling ( ) ;
}
if ( 'start' in options || 'end' in options ) {
if ( 'start' in options || 'end' in options ) {
// apply a new range. both start and end are optional
// apply a new range. both start and end are optional
this . setRange ( options . start , options . end ) ;
this . setRange ( options . start , options . end ) ;
@ -103,6 +110,52 @@ function validateDirection (direction) {
}
}
}
}
/ * *
* Start auto refreshing the current time bar
* /
Range . prototype . startRolling = function ( ) {
var me = this ;
function update ( ) {
me . stopRolling ( ) ;
me . rolling = true ;
var interval = me . end - me . start ;
var t = util . convert ( new Date ( ) , 'Date' ) . valueOf ( ) ;
var start = t - interval / 2 ;
var end = t + interval / 2 ;
var animation = ( me . options && me . options . animation !== undefined ) ? me . options . animation : true ;
me . setRange ( start , end , false ) ;
// determine interval to refresh
var scale = me . conversion ( me . body . domProps . center . width ) . scale ;
var interval = 1 / scale / 10 ;
if ( interval < 30 ) interval = 30 ;
if ( interval > 1000 ) interval = 1000 ;
me . body . dom . rollingModeBtn . style . visibility = "hidden" ;
// start a renderTimer to adjust for the new time
me . currentTimeTimer = setTimeout ( update , interval ) ;
}
update ( ) ;
} ;
/ * *
* Stop auto refreshing the current time bar
* /
Range . prototype . stopRolling = function ( ) {
if ( this . currentTimeTimer !== undefined ) {
clearTimeout ( this . currentTimeTimer ) ;
this . rolling = false ;
this . body . dom . rollingModeBtn . style . visibility = "visible" ;
}
} ;
/ * *
/ * *
* Set a new start and end range
* Set a new start and end range
* @ param { Date | Number | String } [ start ]
* @ param { Date | Number | String } [ start ]
@ -388,6 +441,8 @@ Range.prototype._onDragStart = function(event) {
// when releasing the fingers in opposite order from the touch screen
// when releasing the fingers in opposite order from the touch screen
if ( ! this . props . touch . allowDragging ) return ;
if ( ! this . props . touch . allowDragging ) return ;
this . stopRolling ( ) ;
this . props . touch . start = this . start ;
this . props . touch . start = this . start ;
this . props . touch . end = this . end ;
this . props . touch . end = this . end ;
this . props . touch . dragging = true ;
this . props . touch . dragging = true ;
@ -520,7 +575,7 @@ Range.prototype._onMouseWheel = function(event) {
// Prevent default actions caused by mouse wheel
// Prevent default actions caused by mouse wheel
// (else the page and timeline both scroll)
// (else the page and timeline both scroll)
event . preventDefault ( ) ;
event . preventDefault ( ) ;
// calculate a single scroll jump relative to the range scale
// calculate a single scroll jump relative to the range scale
var diff = delta * ( this . end - this . start ) / 20 ;
var diff = delta * ( this . end - this . start ) / 20 ;
// calculate new start and end
// calculate new start and end
@ -555,9 +610,13 @@ Range.prototype._onMouseWheel = function(event) {
}
}
// calculate center, the date to zoom around
// calculate center, the date to zoom around
var pointer = this . getPointer ( { x : event . clientX , y : event . clientY } , this . body . dom . center ) ;
var pointerDate = this . _pointerToDate ( pointer ) ;
var pointerDate
if ( this . rolling ) {
pointerDate = ( this . start + this . end ) / 2 ;
} else {
var pointer = this . getPointer ( { x : event . clientX , y : event . clientY } , this . body . dom . center ) ;
pointerDate = this . _pointerToDate ( pointer ) ;
}
this . zoom ( scale , pointerDate , delta , event ) ;
this . zoom ( scale , pointerDate , delta , event ) ;
// Prevent default actions caused by mouse wheel
// Prevent default actions caused by mouse wheel
@ -594,6 +653,8 @@ Range.prototype._onPinch = function (event) {
this . props . touch . center = this . getPointer ( event . center , this . body . dom . center ) ;
this . props . touch . center = this . getPointer ( event . center , this . body . dom . center ) ;
}
}
this . stopRolling ( ) ;
var scale = 1 / ( event . scale + this . scaleOffset ) ;
var scale = 1 / ( event . scale + this . scaleOffset ) ;
var centerDate = this . _pointerToDate ( this . props . touch . center ) ;
var centerDate = this . _pointerToDate ( this . props . touch . center ) ;