@ -2,6 +2,7 @@ var Hammer = require('../../module/hammer');
var util = require ( '../../util' ) ;
var DataSet = require ( '../../DataSet' ) ;
var DataView = require ( '../../DataView' ) ;
var TimeStep = require ( '../TimeStep' ) ;
var Component = require ( './Component' ) ;
var Group = require ( './Group' ) ;
var BackgroundGroup = require ( './BackgroundGroup' ) ;
@ -41,6 +42,8 @@ function ItemSet(body, options) {
remove : false
} ,
snap : TimeStep . snap ,
onAdd : function ( item , callback ) {
callback ( item ) ;
} ,
@ -271,7 +274,7 @@ ItemSet.prototype._create = function(){
ItemSet . prototype . setOptions = function ( options ) {
if ( options ) {
// 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 ) ;
if ( 'margin' in options ) {
@ -1171,8 +1174,10 @@ ItemSet.prototype._onDrag = function (event) {
if ( this . touchParams . itemProps ) {
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 scale = this . body . util . getScale ( ) ;
var step = this . body . util . getStep ( ) ;
// move
this . touchParams . itemProps . forEach ( function ( props ) {
@ -1183,12 +1188,12 @@ ItemSet.prototype._onDrag = function (event) {
if ( 'start' in props ) {
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 ) {
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 ) {
newProps . end = new Date ( newProps . start . valueOf ( ) + props . duration ) ;
@ -1356,7 +1361,7 @@ ItemSet.prototype._onAddItem = function (event) {
if ( ! this . options . editable . add ) return ;
var me = this ,
snap = this . body . util . snap || null ,
snap = this . options . snap || null ,
item = ItemSet . itemFromTarget ( event ) ;
if ( item ) {
@ -1375,15 +1380,18 @@ ItemSet.prototype._onAddItem = function (event) {
var xAbs = util . getAbsoluteLeft ( this . dom . frame ) ;
var x = event . gesture . center . pageX - xAbs ;
var start = this . body . util . toTime ( x ) ;
var scale = this . body . util . getScale ( ) ;
var step = this . body . util . getStep ( ) ;
var newItem = {
start : snap ? snap ( start ) : start ,
start : snap ? snap ( start , scale , step ) : start ,
content : 'new item'
} ;
// when default type is a range, add a default end date to the new item
if ( this . options . type === 'range' ) {
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 ( ) ;