@ -2,6 +2,7 @@ var util = require('../util');
var hammerUtil = require ( '../hammerUtil' ) ;
var hammerUtil = require ( '../hammerUtil' ) ;
var moment = require ( '../module/moment' ) ;
var moment = require ( '../module/moment' ) ;
var Component = require ( './component/Component' ) ;
var Component = require ( './component/Component' ) ;
var DateUtil = require ( './DateUtil' ) ;
/ * *
/ * *
* @ constructor Range
* @ constructor Range
@ -78,7 +79,7 @@ Range.prototype = new Component();
Range . prototype . setOptions = function ( options ) {
Range . prototype . setOptions = function ( options ) {
if ( options ) {
if ( options ) {
// copy the options that we know
// copy the options that we know
var fields = [ 'direction' , 'min' , 'max' , 'zoomMin' , 'zoomMax' , 'moveable' , 'zoomable' , 'activate' , 'hide' ] ;
var fields = [ 'direction' , 'min' , 'max' , 'zoomMin' , 'zoomMax' , 'moveable' , 'zoomable' , 'activate' , 'hide' ] ;
util . selectiveExtend ( fields , this . options , options ) ;
util . selectiveExtend ( fields , this . options , options ) ;
if ( 'start' in options || 'end' in options ) {
if ( 'start' in options || 'end' in options ) {
@ -375,51 +376,30 @@ Range.prototype._onDrag = function (event) {
delta -= this . deltaDifference ;
delta -= this . deltaDifference ;
var interval = ( this . props . touch . end - this . props . touch . start ) ;
var interval = ( this . props . touch . end - this . props . touch . start ) ;
// normalize dragging speed if cutout is in between.
// normalize dragging speed if cutout is in between.
var startDate = new Date ( this . options . hide . start ) . getTime ( ) ;
var endDate = new Date ( this . options . hide . end ) . getTime ( ) ;
var duration = endDate - startDate ;
if ( startDate >= this . start && endDate < this . end ) {
interval -= duration ;
}
var duration = DateUtil . getHiddenDuration ( this . body . hiddenDates , this ) ;
interval -= duration ;
var width = ( direction == 'horizontal' ) ? this . body . domProps . center . width : this . body . domProps . center . height ;
var width = ( direction == 'horizontal' ) ? this . body . domProps . center . width : this . body . domProps . center . height ;
var diffRange = - delta / width * interval ;
var diffRange = - delta / width * interval ;
var newStart = this . props . touch . start + diffRange ;
var newEnd = this . props . touch . end + diffRange ;
// snapping times away from hidden zones
// snapping times away from hidden zones
var start = this . props . touch . start + diffRange ;
var end = this . props . touch . end + diffRange ;
if ( start >= startDate && start < endDate && this . previousDelta - delta > 0 ) { // if the start is entering the zone from the left
this . deltaDifference += delta ;
this . props . touch . start = endDate + 1 ;
this . props . touch . end = end + duration ; // to cancel the time subtraction events;
this . _onDrag ( event ) ;
return ;
}
else if ( start >= startDate && start < endDate && this . previousDelta - delta < 0 ) { // if the start is entering the zone from the right
this . deltaDifference += delta ;
this . props . touch . start = startDate - 1 ;
this . props . touch . end = end - duration ; // to cancel the time subtraction events;
this . _onDrag ( event ) ;
return ;
}
else if ( end >= startDate && end < endDate && this . previousDelta - delta > 0 ) { // if the start is entering the zone from the right
this . deltaDifference += delta ;
this . props . touch . end = endDate + 1 ;
this . props . touch . start = start ; // to cancel the time subtraction events;
this . _onDrag ( event ) ;
return ;
}
else if ( end >= startDate && end < endDate && this . previousDelta - delta < 0 ) { // if the start is entering the zone from the right
this . deltaDifference += delta ;
this . props . touch . end = startDate - 1 ;
this . props . touch . start = start ; // to cancel the time subtraction events;
var safeDates = DateUtil . snapAwayFromHidden ( this . body . hiddenDates , this , newStart , newEnd , delta ) ;
if ( safeDates !== false ) {
this . props . touch . start = safeDates . newStart ;
this . props . touch . end = safeDates . newEnd ;
this . _onDrag ( event ) ;
this . _onDrag ( event ) ;
return ;
return ;
}
}
this . previousDelta = delta ;
this . previousDelta = delta ;
this . _applyRange ( s tart, end ) ;
this . _applyRange ( newStart , newEnd ) ;
// fire a rangechange event
// fire a rangechange event
this . body . emitter . emit ( 'rangechange' , {
this . body . emitter . emit ( 'rangechange' , {
@ -494,7 +474,7 @@ Range.prototype._onMouseWheel = function(event) {
pointer = getPointer ( gesture . center , this . body . dom . center ) ,
pointer = getPointer ( gesture . center , this . body . dom . center ) ,
pointerDate = this . _pointerToDate ( pointer ) ;
pointerDate = this . _pointerToDate ( pointer ) ;
this . zoom ( scale , pointerDate ) ;
this . zoom ( scale , pointerDate , delta ) ;
}
}
// Prevent default actions caused by mouse wheel
// Prevent default actions caused by mouse wheel
@ -537,12 +517,24 @@ Range.prototype._onPinch = function (event) {
this . props . touch . center = getPointer ( event . gesture . center , this . body . dom . center ) ;
this . props . touch . center = getPointer ( event . gesture . center , this . body . dom . center ) ;
}
}
var scale = 1 / event . gesture . scale ,
initDate = this . _pointerToDate ( this . props . touch . center ) ;
var scale = 1 / event . gesture . scale ;
var center = this . _pointerToDate ( this . props . touch . center ) ;
var hiddenDuration = DateUtil . getHiddenDuration ( this . body . hiddenDates , this ) ;
// calculate new start and end
// calculate new start and end
var newStart = parseInt ( initDate + ( this . props . touch . start - initDate ) * scale ) ;
var newEnd = parseInt ( initDate + ( this . props . touch . end - initDate ) * scale ) ;
var newStart = center + ( this . props . touch . start - center ) * scale ;
var newEnd = ( center + hiddenDuration ) + ( this . props . touch . end - ( center + hiddenDuration ) ) * scale ;
this . previousDelta = 1 ;
var safeDates = DateUtil . snapAwayFromHidden ( this . body . hiddenDates , this , newStart , newEnd , event . gesture . scale , true ) ;
if ( safeDates !== false ) {
this . props . touch . start = safeDates . newStart ;
this . props . touch . end = safeDates . newEnd ;
newStart = safeDates . newStart ;
newEnd = safeDates . newEnd ;
}
// apply new range
// apply new range
this . setRange ( newStart , newEnd ) ;
this . setRange ( newStart , newEnd ) ;
@ -563,7 +555,10 @@ Range.prototype._pointerToDate = function (pointer) {
if ( direction == 'horizontal' ) {
if ( direction == 'horizontal' ) {
var width = this . body . domProps . center . width ;
var width = this . body . domProps . center . width ;
conversion = this . conversion ( width ) ;
var duration = DateUtil . getHiddenDuration ( this . body . hiddenDates , this ) ;
//return DateUtil.toTime(this.body, this, pointer.x, width);
conversion = this . conversion ( width , duration ) ;
//console.log(new Date(pointer.x / conversion.scale + conversion.offset + duration));
return pointer . x / conversion . scale + conversion . offset ;
return pointer . x / conversion . scale + conversion . offset ;
}
}
else {
else {
@ -597,19 +592,33 @@ function getPointer (touch, element) {
* @ param { Number } [ center ] Value representing a date around which will
* @ param { Number } [ center ] Value representing a date around which will
* be zoomed .
* be zoomed .
* /
* /
Range . prototype . zoom = function ( scale , center ) {
Range . prototype . zoom = function ( scale , center , delta ) {
// if centerDate is not provided, take it half between start Date and end Date
// if centerDate is not provided, take it half between start Date and end Date
if ( center == null ) {
if ( center == null ) {
center = ( this . start + this . end ) / 2 ;
center = ( this . start + this . end ) / 2 ;
}
}
var hiddenDuration = DateUtil . getHiddenDuration ( this . body . hiddenDates , this ) ;
// calculate new start and end
// calculate new start and end
var newStart = center + ( this . start - center ) * scale ;
var newStart = center + ( this . start - center ) * scale ;
var newEnd = center + ( this . end - center ) * scale ;
var newEnd = ( center + hiddenDuration ) + ( this . end - ( center + hiddenDuration ) ) * scale ;
this . previousDelta = 0 ;
// snapping times away from hidden zones
var safeDates = DateUtil . snapAwayFromHidden ( this . body . hiddenDates , this , newStart , newEnd , delta , true ) ;
//console.log(new Date(this.start), new Date(this.end), new Date(newStart), new Date(newEnd),new Date(safeDates.newStart), new Date(safeDates.newEnd));
if ( safeDates !== false ) {
newStart = safeDates . newStart ;
newEnd = safeDates . newEnd ;
}
this . setRange ( newStart , newEnd ) ;
this . setRange ( newStart , newEnd ) ;
} ;
} ;
/ * *
/ * *
* Move the range with a given delta to the left or right . Start and end
* Move the range with a given delta to the left or right . Start and end
* value will be adjusted . For example , try delta = 0.1 or - 0.1
* value will be adjusted . For example , try delta = 0.1 or - 0.1