@ -2,6 +2,7 @@ var Hammer = require('../../module/hammer');
var util = require ( '../../util' ) ;
var util = require ( '../../util' ) ;
var DataSet = require ( '../../DataSet' ) ;
var DataSet = require ( '../../DataSet' ) ;
var DataView = require ( '../../DataView' ) ;
var DataView = require ( '../../DataView' ) ;
var TimeStep = require ( '../TimeStep' ) ;
var Component = require ( './Component' ) ;
var Component = require ( './Component' ) ;
var Group = require ( './Group' ) ;
var Group = require ( './Group' ) ;
var BackgroundGroup = require ( './BackgroundGroup' ) ;
var BackgroundGroup = require ( './BackgroundGroup' ) ;
@ -41,6 +42,8 @@ function ItemSet(body, options) {
remove : false
remove : false
} ,
} ,
snap : TimeStep . snap ,
onAdd : function ( item , callback ) {
onAdd : function ( item , callback ) {
callback ( item ) ;
callback ( item ) ;
} ,
} ,
@ -271,7 +274,7 @@ ItemSet.prototype._create = function(){
ItemSet . prototype . setOptions = function ( options ) {
ItemSet . prototype . setOptions = function ( options ) {
if ( options ) {
if ( options ) {
// copy all options that we know
// copy all options that we know
var fields = [ 'type' , 'align' , 'orientation' , 'padding' , 'stack' , 'selectable' , 'groupOrder' , 'dataAttributes' , 'template' , 'hide' ] ;
var fields = [ 'type' , 'align' , 'orientation' , 'padding' , 'stack' , 'selectable' , 'groupOrder' , 'dataAttributes' , 'template' , 'hide' , 'snap' ] ;
util . selectiveExtend ( fields , this . options , options ) ;
util . selectiveExtend ( fields , this . options , options ) ;
if ( 'margin' in options ) {
if ( 'margin' in options ) {
@ -1171,8 +1174,10 @@ ItemSet.prototype._onDrag = function (event) {
if ( this . touchParams . itemProps ) {
if ( this . touchParams . itemProps ) {
var me = this ;
var me = this ;
var snap = this . body . util . snap || null ;
var snap = this . options . snap || null ;
var xOffset = this . body . dom . root . offsetLeft + this . body . domProps . left . width ;
var xOffset = this . body . dom . root . offsetLeft + this . body . domProps . left . width ;
var scale = this . body . util . getScale ( ) ;
var step = this . body . util . getStep ( ) ;
// move
// move
this . touchParams . itemProps . forEach ( function ( props ) {
this . touchParams . itemProps . forEach ( function ( props ) {
@ -1183,12 +1188,12 @@ ItemSet.prototype._onDrag = function (event) {
if ( 'start' in props ) {
if ( 'start' in props ) {
var start = new Date ( props . start + offset ) ;
var start = new Date ( props . start + offset ) ;
newProps . start = snap ? snap ( start ) : start ;
newProps . start = snap ? snap ( start , scale , step ) : start ;
}
}
if ( 'end' in props ) {
if ( 'end' in props ) {
var end = new Date ( props . end + offset ) ;
var end = new Date ( props . end + offset ) ;
newProps . end = snap ? snap ( end ) : end ;
newProps . end = snap ? snap ( end , scale , step ) : end ;
}
}
else if ( 'duration' in props ) {
else if ( 'duration' in props ) {
newProps . end = new Date ( newProps . start . valueOf ( ) + props . duration ) ;
newProps . end = new Date ( newProps . start . valueOf ( ) + props . duration ) ;
@ -1356,7 +1361,7 @@ ItemSet.prototype._onAddItem = function (event) {
if ( ! this . options . editable . add ) return ;
if ( ! this . options . editable . add ) return ;
var me = this ,
var me = this ,
snap = this . body . util . snap || null ,
snap = this . options . snap || null ,
item = ItemSet . itemFromTarget ( event ) ;
item = ItemSet . itemFromTarget ( event ) ;
if ( item ) {
if ( item ) {
@ -1375,15 +1380,18 @@ ItemSet.prototype._onAddItem = function (event) {
var xAbs = util . getAbsoluteLeft ( this . dom . frame ) ;
var xAbs = util . getAbsoluteLeft ( this . dom . frame ) ;
var x = event . gesture . center . pageX - xAbs ;
var x = event . gesture . center . pageX - xAbs ;
var start = this . body . util . toTime ( x ) ;
var start = this . body . util . toTime ( x ) ;
var scale = this . body . util . getScale ( ) ;
var step = this . body . util . getStep ( ) ;
var newItem = {
var newItem = {
start : snap ? snap ( start ) : start ,
start : snap ? snap ( start , scale , step ) : start ,
content : 'new item'
content : 'new item'
} ;
} ;
// when default type is a range, add a default end date to the new item
// when default type is a range, add a default end date to the new item
if ( this . options . type === 'range' ) {
if ( this . options . type === 'range' ) {
var end = this . body . util . toTime ( x + this . props . width / 5 ) ;
var end = this . body . util . toTime ( x + this . props . width / 5 ) ;
newItem . end = snap ? snap ( end ) : end ;
newItem . end = snap ? snap ( end , scale , step ) : end ;
}
}
newItem [ this . itemsData . _fieldId ] = util . randomUUID ( ) ;
newItem [ this . itemsData . _fieldId ] = util . randomUUID ( ) ;