@ -209,11 +209,11 @@ Timeline.prototype.setItems = function(items) {
if ( initialLoad ) {
if ( initialLoad ) {
if ( this . options . start != undefined || this . options . end != undefined ) {
if ( this . options . start != undefined || this . options . end != undefined ) {
if ( this . options . start == undefined || this . options . end == undefined ) {
if ( this . options . start == undefined || this . options . end == undefined ) {
var dataR ange = this . _getData Range( ) ;
var r ange = this . getItem Range( ) ;
}
}
var start = this . options . start != undefined ? this . options . start : dataRange . start ;
var end = this . options . end != undefined ? this . options . end : dataRange . end ;
var start = this . options . start != undefined ? this . options . start : range . min ;
var end = this . options . end != undefined ? this . options . end : range . max ;
this . setWindow ( start , end , { animation : false } ) ;
this . setWindow ( start , end , { animation : false } ) ;
}
}
@ -344,44 +344,114 @@ Timeline.prototype.focus = function(id, options) {
} ;
} ;
/ * *
/ * *
* Get the data range of the item set .
* @ returns { { min : Date , max : Date } } range A range with a start and end Date .
* When no minimum is found , min == null
* When no maximum is found , max == null
* Set Timeline window such that it fits all items
* @ param { Object } [ options ] Available options :
* ` animation: boolean | {duration: number, easingFunction: string} `
* If true ( default ) , the range is animated
* smoothly to the new window . An object can be
* provided to specify duration and easing function .
* Default duration is 500 ms , and default easing
* function is 'easeInOutQuad' .
* /
* /
Timeline . prototype . getItemRange = function ( ) {
// calculate min from start filed
var dataset = this . itemsData && this . itemsData . getDataSet ( ) ;
Timeline . prototype . fit = function ( options ) {
var animation = ( options && options . animation !== undefined ) ? options . animation : true ;
var range = this . getItemRange ( ) ;
this . range . setRange ( range . min , range . max , animation ) ;
} ;
/ * *
* Determine the range of the items , taking into account their actual width
* and a margin of 10 pixels on both sides .
* @ return { { min : Date | null , max : Date | null } }
* /
Timeline . prototype . getItemRange = function ( ) {
// get a rough approximation for the range based on the items start and end dates
var range = this . getDataRange ( ) ;
var min = range . min ;
var max = range . max ;
var minItem = null ;
var maxItem = null ;
if ( min != null && max != null ) {
var interval = ( max - min ) ; // ms
if ( interval <= 0 ) {
interval = 10 ;
}
var factor = interval / this . props . center . width ;
function getStart ( item ) {
return util . convert ( item . data . start , 'Date' ) . valueOf ( )
}
function getEnd ( item ) {
var end = item . data . end != undefined ? item . data . end : item . data . start ;
return util . convert ( end , 'Date' ) . valueOf ( ) ;
}
// calculate the date of the left side and right side of the items given
util . forEach ( this . itemSet . items , function ( item ) {
item . show ( ) ;
var start = getStart ( item ) ;
var end = getEnd ( item ) ;
var left = new Date ( start - ( item . getWidthLeft ( ) + 10 ) * factor ) ;
var right = new Date ( end + ( item . getWidthRight ( ) + 10 ) * factor ) ;
if ( left < min ) {
min = left ;
minItem = item ;
}
if ( right > max ) {
max = right ;
maxItem = item ;
}
} . bind ( this ) ) ;
if ( minItem && maxItem ) {
var lhs = minItem . getWidthLeft ( ) + 10 ;
var rhs = maxItem . getWidthRight ( ) + 10 ;
var delta = this . props . center . width - lhs - rhs ; // px
if ( delta > 0 ) {
min = getStart ( minItem ) - lhs * interval / delta ; // ms
max = getEnd ( maxItem ) + rhs * interval / delta ; // ms
}
}
}
return {
min : min != null ? new Date ( min ) : null ,
max : max != null ? new Date ( max ) : null
}
} ;
/ * *
* Calculate the data range of the items start and end dates
* @ returns { { min : Date | null , max : Date | null } }
* /
Timeline . prototype . getDataRange = function ( ) {
var min = null ;
var min = null ;
var max = null ;
var max = null ;
var dataset = this . itemsData && this . itemsData . getDataSet ( ) ;
if ( dataset ) {
if ( dataset ) {
// calculate the minimum value of the field 'start'
var minItem = dataset . min ( 'start' ) ;
min = minItem ? util . convert ( minItem . start , 'Date' ) . valueOf ( ) : null ;
// Note: we convert first to Date and then to number because else
// a conversion from ISODate to Number will fail
// calculate maximum value of fields 'start' and 'end'
var maxStartItem = dataset . max ( 'start' ) ;
if ( maxStartItem ) {
max = util . convert ( maxStartItem . start , 'Date' ) . valueOf ( ) ;
}
var maxEndItem = dataset . max ( 'end' ) ;
if ( maxEndItem ) {
if ( max == null ) {
max = util . convert ( maxEndItem . end , 'Date' ) . valueOf ( ) ;
dataset . forEach ( function ( item ) {
var start = util . convert ( item . start , 'Date' ) . valueOf ( ) ;
var end = util . convert ( item . end != undefined ? item . end : item . start , 'Date' ) . valueOf ( ) ;
if ( min === null || start < min ) {
min = start ;
}
}
else {
max = Math . max ( max , util . convert ( maxEndItem . end , 'Date' ) . valueOf ( ) ) ;
if ( max === null || end > max ) {
max = start ;
}
}
}
} ) ;
}
}
return {
return {
min : ( min != null ) ? new Date ( min ) : null ,
max : ( max != null ) ? new Date ( max ) : null
} ;
min : min != null ? new Date ( min ) : null ,
max : max != null ? new Date ( max ) : null
}
} ;
} ;
/ * *
/ * *