@ -240,6 +240,7 @@ ItemSet.prototype._create = function(){
this . groupHammer = new Hammer ( this . body . dom . leftContainer ) ;
this . groupHammer = new Hammer ( this . body . dom . leftContainer ) ;
}
}
this . groupHammer . on ( 'tap' , this . _onGroupClick . bind ( this ) ) ;
this . groupHammer . on ( 'panstart' , this . _onGroupDragStart . bind ( this ) ) ;
this . groupHammer . on ( 'panstart' , this . _onGroupDragStart . bind ( this ) ) ;
this . groupHammer . on ( 'panmove' , this . _onGroupDrag . bind ( this ) ) ;
this . groupHammer . on ( 'panmove' , this . _onGroupDrag . bind ( this ) ) ;
this . groupHammer . on ( 'panend' , this . _onGroupDragEnd . bind ( this ) ) ;
this . groupHammer . on ( 'panend' , this . _onGroupDragEnd . bind ( this ) ) ;
@ -812,6 +813,26 @@ ItemSet.prototype.setGroups = function(groups) {
}
}
if ( this . groupsData ) {
if ( this . groupsData ) {
// go over all groups nesting
var groupsData = this . groupsData ;
if ( this . groupsData instanceof DataView ) {
groupsData = this . groupsData . getDataSet ( )
}
groupsData . get ( ) . forEach ( function ( group ) {
if ( group . nestedGroups ) {
group . nestedGroups . forEach ( function ( nestedGroupId ) {
var updatedNestedGroup = groupsData . get ( nestedGroupId ) ;
updatedNestedGroup . nestedInGroup = group . id ;
if ( group . showNested == false ) {
updatedNestedGroup . visible = false ;
}
groupsData . update ( updatedNestedGroup ) ;
} )
}
} )
// subscribe to new dataset
// subscribe to new dataset
var id = this . id ;
var id = this . id ;
util . forEach ( this . groupListeners , function ( callback , event ) {
util . forEach ( this . groupListeners , function ( callback , event ) {
@ -904,7 +925,7 @@ ItemSet.prototype._onUpdate = function(ids) {
var selected ;
var selected ;
if ( item ) {
if ( item ) {
// update item
// update item
if ( ! constructor || ! ( item instanceof constructor ) ) {
if ( ! constructor || ! ( item instanceof constructor ) ) {
// item type has changed, delete the item and recreate it
// item type has changed, delete the item and recreate it
selected = item . selected ; // preserve selection of this item
selected = item . selected ; // preserve selection of this item
@ -921,6 +942,7 @@ ItemSet.prototype._onUpdate = function(ids) {
if ( constructor ) {
if ( constructor ) {
item = new constructor ( itemData , me . conversion , me . options ) ;
item = new constructor ( itemData , me . conversion , me . options ) ;
item . id = id ; // TODO: not so nice setting id afterwards
item . id = id ; // TODO: not so nice setting id afterwards
me . _addItem ( item ) ;
me . _addItem ( item ) ;
if ( selected ) {
if ( selected ) {
this . selection . push ( id ) ;
this . selection . push ( id ) ;
@ -1076,6 +1098,8 @@ ItemSet.prototype._orderGroups = function () {
order : this . options . groupOrder
order : this . options . groupOrder
} ) ;
} ) ;
groupIds = this . _orderNestedGroups ( groupIds ) ;
var changed = ! util . equalArray ( groupIds , this . groupIds ) ;
var changed = ! util . equalArray ( groupIds , this . groupIds ) ;
if ( changed ) {
if ( changed ) {
// hide all groups, removes them from the DOM
// hide all groups, removes them from the DOM
@ -1099,6 +1123,33 @@ ItemSet.prototype._orderGroups = function () {
}
}
} ;
} ;
/ * *
* Reorder the nested groups
* @ return { boolean } changed
* @ private
* /
ItemSet . prototype . _orderNestedGroups = function ( groupIds ) {
var newGroupIdsOrder = [ ] ;
groupIds . forEach ( function ( groupId ) {
var groupData = this . groupsData . get ( groupId ) ;
if ( ! groupData . nestedInGroup ) {
newGroupIdsOrder . push ( groupId )
}
if ( groupData . nestedGroups ) {
var nestedGroups = this . groupsData . get ( {
filter : function ( nestedGroup ) {
return nestedGroup . nestedInGroup == groupId ;
}
} ) ;
var nestedGroupIds = nestedGroups . map ( function ( nestedGroup ) { return nestedGroup . id } )
newGroupIdsOrder = newGroupIdsOrder . concat ( nestedGroupIds ) ;
}
} , this )
return newGroupIdsOrder ;
}
/ * *
/ * *
* Add a new item
* Add a new item
* @ param { Item } item
* @ param { Item } item
@ -1110,6 +1161,13 @@ ItemSet.prototype._addItem = function(item) {
// add to group
// add to group
var groupId = this . _getGroupId ( item . data ) ;
var groupId = this . _getGroupId ( item . data ) ;
var group = this . groups [ groupId ] ;
var group = this . groups [ groupId ] ;
if ( ! group ) {
item . groupShowing = false ;
} else if ( group && group . data && group . data . showNested ) {
item . groupShowing = true ;
}
if ( group ) group . add ( item ) ;
if ( group ) group . add ( item ) ;
} ;
} ;
@ -1126,13 +1184,17 @@ ItemSet.prototype._updateItem = function(item, itemData) {
// update the items data (will redraw the item when displayed)
// update the items data (will redraw the item when displayed)
item . setData ( itemData ) ;
item . setData ( itemData ) ;
var groupId = this . _getGroupId ( item . data ) ;
var group = this . groups [ groupId ] ;
if ( ! group ) {
item . groupShowing = false ;
} else if ( group && group . data && group . data . showNested ) {
item . groupShowing = true ;
}
// update group
// update group
if ( oldGroupId != item . data . group || oldSubGroupId != item . data . subgroup ) {
if ( oldGroupId != item . data . group || oldSubGroupId != item . data . subgroup ) {
var oldGroup = this . groups [ oldGroupId ] ;
var oldGroup = this . groups [ oldGroupId ] ;
if ( oldGroup ) oldGroup . remove ( item ) ;
if ( oldGroup ) oldGroup . remove ( item ) ;
var groupId = this . _getGroupId ( item . data ) ;
var group = this . groups [ groupId ] ;
if ( group ) group . add ( item ) ;
if ( group ) group . add ( item ) ;
}
}
} ;
} ;
@ -1561,6 +1623,35 @@ ItemSet.prototype._onDragEnd = function (event) {
}
}
} ;
} ;
ItemSet . prototype . _onGroupClick = function ( event ) {
var group = this . groupFromTarget ( event ) ;
if ( ! group . nestedGroups ) return ;
var groupsData = this . groupsData ;
if ( this . groupsData instanceof DataView ) {
groupsData = this . groupsData . getDataSet ( )
}
group . showNested = ! group . showNested ;
var nestedGroups = groupsData . get ( group . nestedGroups ) . map ( function ( nestedGroup ) {
if ( nestedGroup . visible == undefined ) { nestedGroup . visible = true ; }
nestedGroup . visible = ! ! group . showNested ;
return nestedGroup ;
} ) ;
groupsData . update ( nestedGroups ) ;
if ( group . showNested ) {
util . removeClassName ( group . dom . label , 'collapsed' ) ;
util . addClassName ( group . dom . label , 'expanded' ) ;
} else {
util . removeClassName ( group . dom . label , 'expanded' ) ;
var collapsedDirClassName = this . options . rtl ? 'collapsed-rtl' : 'collapsed'
util . addClassName ( group . dom . label , collapsedDirClassName ) ;
}
}
ItemSet . prototype . _onGroupDragStart = function ( event ) {
ItemSet . prototype . _onGroupDragStart = function ( event ) {
if ( this . options . groupEditable . order ) {
if ( this . options . groupEditable . order ) {
this . groupTouchParams . group = this . groupFromTarget ( event ) ;
this . groupTouchParams . group = this . groupFromTarget ( event ) ;
@ -1612,15 +1703,16 @@ ItemSet.prototype._onGroupDrag = function (event) {
// switch groups
// switch groups
if ( draggedGroup && targetGroup ) {
if ( draggedGroup && targetGroup ) {
this . options . groupOrderSwap ( draggedGroup , targetGroup , this . groupsData ) ;
groupsData . update ( draggedGroup ) ;
groupsData . update ( targetGroup ) ;
this . options . groupOrderSwap ( draggedGroup , targetGroup , groupsData ) ;
groupsData . update ( draggedGroup ) ;
groupsData . update ( targetGroup ) ;
}
}
// fetch current order of groups
// fetch current order of groups
var newOrder = groupsData . getIds ( {
var newOrder = groupsData . getIds ( {
order : this . options . groupOrder
} ) ;
order : this . options . groupOrder
} ) ;
// in case of changes since _onGroupDragStart
// in case of changes since _onGroupDragStart
if ( ! util . equalArray ( newOrder , this . groupTouchParams . originalOrder ) ) {
if ( ! util . equalArray ( newOrder , this . groupTouchParams . originalOrder ) ) {
@ -1715,14 +1807,14 @@ ItemSet.prototype._onGroupDragEnd = function (event) {
break ;
break ;
}
}
// found a group that has the wrong position -> switch with the
// group at the position where other one should be, fix index arrays and continue
var slippedPosition = newOrder . indexOf ( origOrder [ curPos ] )
var switchGroup = dataset . get ( newOrder [ curPos ] ) ;
var shouldBeGroup = dataset . get ( origOrder [ curPos ] ) ;
me . options . groupOrderSwap ( switchGroup , shouldBeGroup , dataset ) ;
groupsData . update ( switchGroup ) ;
groupsData . update ( shouldBeGroup ) ;
// found a group that has the wrong position -> switch with the
// group at the position where other one should be, fix index arrays and continue
var slippedPosition = newOrder . indexOf ( origOrder [ curPos ] )
var switchGroup = dataset . get ( newOrder [ curPos ] ) ;
var shouldBeGroup = dataset . get ( origOrder [ curPos ] ) ;
me . options . groupOrderSwap ( switchGroup , shouldBeGroup , dataset ) ;
groupsData . update ( switchGroup ) ;
groupsData . update ( shouldBeGroup ) ;
var switchGroupId = newOrder [ curPos ] ;
var switchGroupId = newOrder [ curPos ] ;
newOrder [ curPos ] = origOrder [ curPos ] ;
newOrder [ curPos ] = origOrder [ curPos ] ;