File tree Expand file tree Collapse file tree 2 files changed +26
-2
lines changed
Expand file tree Collapse file tree 2 files changed +26
-2
lines changed Original file line number Diff line number Diff line change 1515 < h1 > Float grid demo</ h1 >
1616 < div >
1717 < a class ="btn btn-primary " onClick ="addNewWidget() " href ="# "> Add Widget</ a >
18+ < a class ="btn btn-primary " onClick ="makeNewWidget() " href ="# "> Make Widget</ a >
1819 < a class ="btn btn-primary " onclick ="toggleFloat() " id ="float " href ="# "> float: true</ a >
1920 </ div >
2021 < br > < br >
@@ -38,16 +39,30 @@ <h1>Float grid demo</h1>
3839 ] ;
3940 let count = 0 ;
4041
41- addNewWidget = function ( ) {
42+ getNode = function ( ) {
4243 let n = items [ count ] || {
4344 x : Math . round ( 12 * Math . random ( ) ) ,
4445 y : Math . round ( 5 * Math . random ( ) ) ,
4546 w : Math . round ( 1 + 3 * Math . random ( ) ) ,
4647 h : Math . round ( 1 + 3 * Math . random ( ) )
4748 } ;
4849 n . content = n . content || String ( count ) ;
49- grid . addWidget ( n ) ;
5050 count ++ ;
51+ return n ;
52+ } ;
53+
54+ addNewWidget = function ( ) {
55+ let w = grid . addWidget ( getNode ( ) ) ;
56+ } ;
57+
58+ makeNewWidget = function ( ) {
59+ let n = getNode ( ) ;
60+ let doc = document . implementation . createHTMLDocument ( ) ;
61+ doc . body . innerHTML = `<div class="item" gs-x="${ n . x } " gs-y="${ n . y } " gs-w="${ n . w || 1 } " gs-h="${ n . h || 1 } "><div class="grid-stack-item-content">${ n . content } </div></div>` ;
62+ let el = doc . body . children [ 0 ] ;
63+ grid . el . appendChild ( el ) ;
64+ // example showing when DOM is created elsewhere (eg Angular/Vue/React) and GS is used to convert to a widget
65+ let w = grid . makeWidget ( el ) ;
5166 } ;
5267
5368 toggleFloat = function ( ) {
Original file line number Diff line number Diff line change @@ -975,6 +975,15 @@ describe('gridstack', function() {
975975 let widget = grid . makeWidget ( el ) ;
976976 expect ( parseInt ( widget . getAttribute ( 'gs-x' ) , 10 ) ) . toBe ( 0 ) ;
977977 } ) ;
978+ it ( 'passing element float=true' , function ( ) {
979+ let grid = GridStack . init ( { float : true } ) ;
980+ let doc = document . implementation . createHTMLDocument ( ) ;
981+ doc . body . innerHTML = '<div><div class="grid-stack-item-content"></div></div>' ;
982+ let el = doc . body . children [ 0 ] as HTMLElement ;
983+ grid . el . appendChild ( el ) ;
984+ let widget = grid . makeWidget ( el ) ;
985+ expect ( parseInt ( widget . getAttribute ( 'gs-x' ) , 10 ) ) . toBe ( 0 ) ;
986+ } ) ;
978987 it ( 'passing class' , function ( ) {
979988 let grid = GridStack . init ( ) ;
980989 let doc = document . implementation . createHTMLDocument ( ) ;
You can’t perform that action at this time.
0 commit comments