@@ -235,9 +235,16 @@ export class DashboardLayout extends Layout {
235235 widget : DashboardWidget ,
236236 pos : Widgetstore . WidgetPosition
237237 ) : boolean {
238- this . startBatch ( ) ;
238+ const wasInBatch = this . inBatch ;
239+ if ( ! wasInBatch ) {
240+ this . startBatch ( ) ;
241+ }
242+
239243 const success = this . _updateWidget ( widget , pos ) ;
240- this . endBatch ( ) ;
244+
245+ if ( ! wasInBatch ) {
246+ this . endBatch ( ) ;
247+ }
241248 return success ;
242249 }
243250
@@ -287,10 +294,10 @@ export class DashboardLayout extends Layout {
287294
288295 // Snap to grid if in grid mode.
289296 if ( this . _mode === 'grid' ) {
290- left = mround ( left , this . _gridWidth ) ;
291- top = mround ( top , this . _gridHeight ) ;
292- width = Math . max ( mround ( width , this . _gridWidth ) , this . _gridWidth ) ;
293- height = Math . max ( mround ( height , this . _gridHeight ) , this . _gridHeight ) ;
297+ left = mround ( left , this . _gridSize ) ;
298+ top = mround ( top , this . _gridSize ) ;
299+ width = Math . max ( mround ( width , this . _gridSize ) , this . _gridSize ) ;
300+ height = Math . max ( mround ( height , this . _gridSize ) , this . _gridSize ) ;
294301 // Change width/height now to force grid changes if they're small.
295302 item . update ( 0 , 0 , 0 , 0 ) ;
296303 }
@@ -531,7 +538,7 @@ export class DashboardLayout extends Layout {
531538 each ( overlaps , ( overlap ) => void this . _handleOverlap ( pos , overlap ) ) ;
532539 }
533540
534- fixOverlaps ( widget : DashboardWidget ) {
541+ fixOverlaps ( widget : DashboardWidget ) : void {
535542 const overlaps = filter (
536543 this . _widgetsInSelection ( widget . pos ) ,
537544 ( overlap ) => overlap . widget !== widget
@@ -678,16 +685,60 @@ export class DashboardLayout extends Layout {
678685 let { left, top, width, height } = pos ;
679686
680687 if ( this . mode === 'grid' ) {
681- width = Math . max ( mround ( width , this . _gridWidth ) , this . _gridWidth ) ;
682- height = Math . max ( mround ( height , this . _gridHeight ) , this . _gridWidth ) ;
683- left = mround ( left , this . _gridWidth ) ;
684- top = mround ( top , this . _gridHeight ) ;
688+ width = Math . max ( mround ( width , this . _gridSize ) , this . _gridSize ) ;
689+ height = Math . max ( mround ( height , this . _gridSize ) , this . _gridSize ) ;
690+ left = mround ( left , this . _gridSize ) ;
691+ top = mround ( top , this . _gridSize ) ;
685692 }
686693
687694 context . strokeRect ( left , top , width , height ) ;
688695 context . fillRect ( left , top , width , height ) ;
689696 }
690697
698+ set gridSize ( s : number ) {
699+ this . _gridSize = s ;
700+ const backgroundPosition = `0 0, 0 ${ s } px, ${ s } px -${ s } px, -${ s } px 0px` ;
701+
702+ this . canvas . style . backgroundPosition = backgroundPosition ;
703+ this . canvas . style . backgroundSize = `${ 2 * s } px ${ 2 * s } px` ;
704+ this . parent . update ( ) ;
705+
706+ this . startBatch ( ) ;
707+ each ( this , ( _widget ) => {
708+ const widget = _widget as DashboardWidget ;
709+ this . updateWidget ( widget , widget . pos ) ;
710+ } ) ;
711+ this . endBatch ( ) ;
712+ }
713+
714+ get gridSize ( ) : number {
715+ return this . _gridSize ;
716+ }
717+
718+ trimCanvas ( ) : void {
719+ const model = ( this . parent as Dashboard ) . model ;
720+ let maxWidth = 0 ;
721+ let maxHeight = 0 ;
722+
723+ each ( this , ( _widget ) => {
724+ const widget = _widget as DashboardWidget ;
725+ const { left, top, width, height } = widget . pos ;
726+ if ( left + width > maxWidth ) {
727+ maxWidth = left + width ;
728+ }
729+ if ( top + height > maxHeight ) {
730+ maxHeight = top + height ;
731+ }
732+ } ) ;
733+
734+ if ( maxWidth ) {
735+ model . width = maxWidth ;
736+ }
737+ if ( maxHeight ) {
738+ model . height = maxHeight ;
739+ }
740+ }
741+
691742 // Map from widget ids to LayoutItems
692743 private _items : Map < string , LayoutItem > ;
693744 // Datastore widgets are rendered from / saved to.
@@ -705,8 +756,7 @@ export class DashboardLayout extends Layout {
705756 // Parent dashboard.
706757 private _dashboard : Dashboard ;
707758
708- private _gridWidth = 100 ;
709- private _gridHeight = 100 ;
759+ private _gridSize = 50 ;
710760
711761 // Changed signal
712762 private _changed = new Signal < this, IDashboardChange [ ] > ( this ) ;
0 commit comments