1
1
import { GridStack } from '../src/gridstack' ;
2
- import { GridStackDD } from '../src/gridstack-dd' ;
2
+ import { GridStackDD } from '../src/gridstack-dd' ; // html5 vs Jquery set when including all file above
3
3
import { Utils } from '../src/utils' ;
4
4
5
5
describe ( 'gridstack' , function ( ) {
@@ -8,10 +8,10 @@ describe('gridstack', function() {
8
8
// grid has 4x2 and 4x4 top-left aligned - used on most test cases
9
9
let gridHTML =
10
10
'<div class="grid-stack">' +
11
- ' <div class="grid-stack-item" data-gs-x="0" data-gs-y="0" data-gs-width="4" data-gs-height="2" data-gs-id="item1 " id="item1">' +
11
+ ' <div class="grid-stack-item" data-gs-x="0" data-gs-y="0" data-gs-width="4" data-gs-height="2" data-gs-id="gsItem1 " id="item1">' +
12
12
' <div class="grid-stack-item-content">item 1 text</div>' +
13
13
' </div>' +
14
- ' <div class="grid-stack-item" data-gs-x="4" data-gs-y="0" data-gs-width="4" data-gs-height="4" data-gs-id="item2 " id="item2">' +
14
+ ' <div class="grid-stack-item" data-gs-x="4" data-gs-y="0" data-gs-width="4" data-gs-height="4" data-gs-id="gsItem2 " id="item2">' +
15
15
' <div class="grid-stack-item-content">item 2 text</div>' +
16
16
' </div>' +
17
17
'</div>' ;
@@ -1103,56 +1103,104 @@ describe('gridstack', function() {
1103
1103
} ) ;
1104
1104
} ) ;
1105
1105
1106
- describe ( 'grid.moveNode ' , function ( ) {
1106
+ describe ( 'grid.update ' , function ( ) {
1107
1107
beforeEach ( function ( ) {
1108
1108
document . body . insertAdjacentHTML ( 'afterbegin' , gridstackHTML ) ;
1109
1109
} ) ;
1110
1110
afterEach ( function ( ) {
1111
1111
document . body . removeChild ( document . getElementById ( 'gs-cont' ) ) ;
1112
1112
} ) ;
1113
- it ( 'should do nothing and return NULL to mean nothing happened' , function ( ) {
1114
- let grid :any = GridStack . init ( ) ;
1115
- let items = Utils . getElements ( '.grid-stack-item' ) ;
1116
- grid . _updateElement ( items [ 0 ] , function ( el , node ) {
1117
- let hasMoved = grid . engine . moveNode ( node ) ;
1118
- expect ( hasMoved ) . toBe ( null ) ;
1119
- } ) ;
1113
+ it ( 'should move and resize widget' , function ( ) {
1114
+ let grid = GridStack . init ( { float : true } ) ;
1115
+ let el = Utils . getElements ( '.grid-stack-item' ) [ 1 ] ;
1116
+ expect ( parseInt ( el . getAttribute ( 'data-gs-width' ) , 10 ) ) . toBe ( 4 ) ;
1117
+
1118
+ grid . update ( el , { x : 5 , y : 4 , height : 2 } ) ;
1119
+ expect ( parseInt ( el . getAttribute ( 'data-gs-x' ) , 10 ) ) . toBe ( 5 ) ;
1120
+ expect ( parseInt ( el . getAttribute ( 'data-gs-y' ) , 10 ) ) . toBe ( 4 ) ;
1121
+ expect ( parseInt ( el . getAttribute ( 'data-gs-width' ) , 10 ) ) . toBe ( 4 ) ;
1122
+ expect ( parseInt ( el . getAttribute ( 'data-gs-height' ) , 10 ) ) . toBe ( 2 ) ;
1120
1123
} ) ;
1121
- it ( 'should do nothing and return node ' , function ( ) {
1122
- let grid : any = GridStack . init ( ) ;
1124
+ it ( 'should change noMove ' , function ( ) {
1125
+ let grid = GridStack . init ( { float : true } ) ;
1123
1126
let items = Utils . getElements ( '.grid-stack-item' ) ;
1124
- grid . minWidth ( items [ 0 ] , 1 ) ;
1125
- grid . maxWidth ( items [ 0 ] , 2 ) ;
1126
- grid . minHeight ( items [ 0 ] , 1 ) ;
1127
- grid . maxHeight ( items [ 0 ] , 2 ) ;
1128
- grid . _updateElement ( items [ 0 ] , function ( el , node ) {
1129
- let newNode = grid . engine . moveNode ( node ) ;
1130
- expect ( newNode ) . toBe ( node ) ;
1131
- } ) ;
1132
- } ) ;
1133
- } ) ;
1127
+ let el = items [ 1 ] ;
1128
+ let dd = GridStackDD . get ( ) ;
1129
+
1130
+ grid . update ( el , { noMove : true , noResize : false } ) ;
1131
+ expect ( el . getAttribute ( 'data-gs-no-move' ) ) . toBe ( 'true' ) ;
1132
+ expect ( el . getAttribute ( 'data-gs-no-resize' ) ) . toBe ( null ) ; // false is no-op
1133
+ expect ( dd . isResizable ( el ) ) . toBe ( true ) ;
1134
+ expect ( dd . isDraggable ( el ) ) . toBe ( false ) ;
1135
+ expect ( dd . isResizable ( items [ 0 ] ) ) . toBe ( true ) ;
1136
+ expect ( dd . isDraggable ( items [ 0 ] ) ) . toBe ( true ) ;
1137
+
1138
+ expect ( parseInt ( el . getAttribute ( 'data-gs-x' ) , 10 ) ) . toBe ( 4 ) ;
1139
+ expect ( parseInt ( el . getAttribute ( 'data-gs-y' ) , 10 ) ) . toBe ( 0 ) ;
1140
+ expect ( parseInt ( el . getAttribute ( 'data-gs-width' ) , 10 ) ) . toBe ( 4 ) ;
1141
+ expect ( parseInt ( el . getAttribute ( 'data-gs-height' ) , 10 ) ) . toBe ( 4 ) ;
1142
+ } ) ;
1143
+ it ( 'should change content and id, and move' , function ( ) {
1144
+ let grid = GridStack . init ( { float : true } ) ;
1145
+ let items = Utils . getElements ( '.grid-stack-item' ) ;
1146
+ let el = items [ 1 ] ;
1147
+ let sub = el . querySelector ( '.grid-stack-item-content' ) ;
1148
+
1149
+ grid . update ( el , { id : 'newID' , y : 1 , content : 'new content' } ) ;
1150
+ expect ( el . getAttribute ( 'data-gs-id' ) ) . toBe ( 'newID' ) ;
1151
+ expect ( sub . innerHTML ) . toBe ( 'new content' ) ;
1152
+ expect ( parseInt ( el . getAttribute ( 'data-gs-x' ) , 10 ) ) . toBe ( 4 ) ;
1153
+ expect ( parseInt ( el . getAttribute ( 'data-gs-y' ) , 10 ) ) . toBe ( 1 ) ;
1154
+ expect ( parseInt ( el . getAttribute ( 'data-gs-width' ) , 10 ) ) . toBe ( 4 ) ;
1155
+ expect ( parseInt ( el . getAttribute ( 'data-gs-height' ) , 10 ) ) . toBe ( 4 ) ;
1156
+ } ) ;
1157
+ it ( 'should change max and constrain a wanted resize' , function ( ) {
1158
+ let grid = GridStack . init ( { float : true } ) ;
1159
+ let items = Utils . getElements ( '.grid-stack-item' ) ;
1160
+ let el = items [ 1 ] ;
1161
+ expect ( el . getAttribute ( 'data-gs-max-width' ) ) . toBe ( null ) ;
1134
1162
1135
- describe ( 'grid.update' , function ( ) {
1136
- beforeEach ( function ( ) {
1137
- document . body . insertAdjacentHTML ( 'afterbegin' , gridstackHTML ) ;
1163
+ grid . update ( el , { maxWidth : 2 , width : 5 } ) ;
1164
+ expect ( parseInt ( el . getAttribute ( 'data-gs-x' ) , 10 ) ) . toBe ( 4 ) ;
1165
+ expect ( parseInt ( el . getAttribute ( 'data-gs-y' ) , 10 ) ) . toBe ( 0 ) ;
1166
+ expect ( parseInt ( el . getAttribute ( 'data-gs-width' ) , 10 ) ) . toBe ( 2 ) ;
1167
+ expect ( parseInt ( el . getAttribute ( 'data-gs-height' ) , 10 ) ) . toBe ( 4 ) ;
1168
+ expect ( parseInt ( el . getAttribute ( 'data-gs-max-width' ) , 10 ) ) . toBe ( 2 ) ;
1138
1169
} ) ;
1139
- afterEach ( function ( ) {
1140
- document . body . removeChild ( document . getElementById ( 'gs-cont' ) ) ;
1170
+ it ( 'should change max and constrain existing' , function ( ) {
1171
+ let grid = GridStack . init ( { float : true } ) ;
1172
+ let items = Utils . getElements ( '.grid-stack-item' ) ;
1173
+ let el = items [ 1 ] ;
1174
+ expect ( el . getAttribute ( 'data-gs-max-width' ) ) . toBe ( null ) ;
1175
+
1176
+ grid . update ( el , { maxWidth : 2 } ) ;
1177
+ expect ( parseInt ( el . getAttribute ( 'data-gs-x' ) , 10 ) ) . toBe ( 4 ) ;
1178
+ expect ( parseInt ( el . getAttribute ( 'data-gs-y' ) , 10 ) ) . toBe ( 0 ) ;
1179
+ expect ( parseInt ( el . getAttribute ( 'data-gs-height' ) , 10 ) ) . toBe ( 4 ) ;
1180
+ expect ( parseInt ( el . getAttribute ( 'data-gs-max-width' ) , 10 ) ) . toBe ( 2 ) ;
1181
+ expect ( parseInt ( el . getAttribute ( 'data-gs-width' ) , 10 ) ) . toBe ( 2 ) ;
1141
1182
} ) ;
1142
- it ( 'should move and resize widget' , function ( ) {
1143
- let options = {
1144
- cellHeight : 80 ,
1145
- margin : 5 ,
1146
- float : true
1147
- } ;
1148
- let grid = GridStack . init ( options ) ;
1183
+ it ( 'should change all max and move' , function ( ) {
1184
+ let grid = GridStack . init ( { float : true } ) ;
1149
1185
let items = Utils . getElements ( '.grid-stack-item' ) ;
1150
- grid . update ( items [ 0 ] , 5 , 5 , 5 , 5 ) ;
1151
- expect ( parseInt ( items [ 0 ] . getAttribute ( 'data-gs-width' ) , 10 ) ) . toBe ( 5 ) ;
1152
- expect ( parseInt ( items [ 0 ] . getAttribute ( 'data-gs-height' ) , 10 ) ) . toBe ( 5 ) ;
1153
- expect ( parseInt ( items [ 0 ] . getAttribute ( 'data-gs-x' ) , 10 ) ) . toBe ( 5 ) ;
1154
- expect ( parseInt ( items [ 0 ] . getAttribute ( 'data-gs-y' ) , 10 ) ) . toBe ( 5 ) ;
1186
+
1187
+ items . forEach ( item => {
1188
+ expect ( item . getAttribute ( 'data-gs-max-width' ) ) . toBe ( null ) ;
1189
+ expect ( item . getAttribute ( 'data-gs-max-height' ) ) . toBe ( null ) ;
1190
+ } ) ;
1191
+
1192
+ grid . update ( '.grid-stack-item' , { maxWidth : 2 , maxHeight : 2 } ) ;
1193
+ expect ( parseInt ( items [ 0 ] . getAttribute ( 'data-gs-x' ) , 10 ) ) . toBe ( 0 ) ;
1194
+ expect ( parseInt ( items [ 1 ] . getAttribute ( 'data-gs-x' ) , 10 ) ) . toBe ( 4 ) ;
1195
+ items . forEach ( item => {
1196
+ expect ( parseInt ( item . getAttribute ( 'data-gs-y' ) , 10 ) ) . toBe ( 0 ) ;
1197
+ expect ( parseInt ( item . getAttribute ( 'data-gs-height' ) , 10 ) ) . toBe ( 2 ) ;
1198
+ expect ( parseInt ( item . getAttribute ( 'data-gs-width' ) , 10 ) ) . toBe ( 2 ) ;
1199
+ expect ( parseInt ( item . getAttribute ( 'data-gs-max-width' ) , 10 ) ) . toBe ( 2 ) ;
1200
+ expect ( parseInt ( item . getAttribute ( 'data-gs-max-height' ) , 10 ) ) . toBe ( 2 ) ;
1201
+ } ) ;
1155
1202
} ) ;
1203
+
1156
1204
} ) ;
1157
1205
1158
1206
describe ( 'grid.margin' , function ( ) {
@@ -1679,35 +1727,35 @@ describe('gridstack', function() {
1679
1727
it ( 'save layout' , function ( ) {
1680
1728
let grid = GridStack . init ( ) ;
1681
1729
let layout = grid . save ( false ) ;
1682
- expect ( layout ) . toEqual ( [ { x :0 , y :0 , width :4 , height :2 , id :'item1 ' } , { x :4 , y :0 , width :4 , height :4 , id :'item2 ' } ] ) ;
1730
+ expect ( layout ) . toEqual ( [ { x :0 , y :0 , width :4 , height :2 , id :'gsItem1 ' } , { x :4 , y :0 , width :4 , height :4 , id :'gsItem2 ' } ] ) ;
1683
1731
layout = grid . save ( ) ;
1684
- expect ( layout ) . toEqual ( [ { x :0 , y :0 , width :4 , height :2 , id :'item1 ' , content :'item 1 text' } , { x :4 , y :0 , width :4 , height :4 , id :'item2 ' , content :'item 2 text' } ] ) ;
1732
+ expect ( layout ) . toEqual ( [ { x :0 , y :0 , width :4 , height :2 , id :'gsItem1 ' , content :'item 1 text' } , { x :4 , y :0 , width :4 , height :4 , id :'gsItem2 ' , content :'item 2 text' } ] ) ;
1685
1733
layout = grid . save ( true ) ;
1686
- expect ( layout ) . toEqual ( [ { x :0 , y :0 , width :4 , height :2 , id :'item1 ' , content :'item 1 text' } , { x :4 , y :0 , width :4 , height :4 , id :'item2 ' , content :'item 2 text' } ] ) ;
1734
+ expect ( layout ) . toEqual ( [ { x :0 , y :0 , width :4 , height :2 , id :'gsItem1 ' , content :'item 1 text' } , { x :4 , y :0 , width :4 , height :4 , id :'gsItem2 ' , content :'item 2 text' } ] ) ;
1687
1735
} ) ;
1688
1736
it ( 'load move 1 item, delete others' , function ( ) {
1689
1737
let grid = GridStack . init ( ) ;
1690
- grid . load ( [ { x :2 , height :1 , id :'item2 ' } ] ) ;
1738
+ grid . load ( [ { x :2 , height :1 , id :'gsItem2 ' } ] ) ;
1691
1739
let layout = grid . save ( false ) ;
1692
- expect ( layout ) . toEqual ( [ { x :2 , y :0 , width :4 , height :1 , id :'item2 ' } ] ) ;
1740
+ expect ( layout ) . toEqual ( [ { x :2 , y :0 , width :4 , height :1 , id :'gsItem2 ' } ] ) ;
1693
1741
} ) ;
1694
1742
it ( 'load add new, delete others' , function ( ) {
1695
1743
let grid = GridStack . init ( ) ;
1696
- grid . load ( [ { width :2 , height :1 , id :'item3 ' } ] , true ) ;
1744
+ grid . load ( [ { width :2 , height :1 , id :'gsItem3 ' } ] , true ) ;
1697
1745
let layout = grid . save ( false ) ;
1698
- expect ( layout ) . toEqual ( [ { x :0 , y :0 , width :2 , height :1 , id :'item3 ' } ] ) ;
1746
+ expect ( layout ) . toEqual ( [ { x :0 , y :0 , width :2 , height :1 , id :'gsItem3 ' } ] ) ;
1699
1747
} ) ;
1700
1748
it ( 'load size 1 item only' , function ( ) {
1701
1749
let grid = GridStack . init ( ) ;
1702
- grid . load ( [ { height :3 , id :'item1 ' } ] , false ) ;
1750
+ grid . load ( [ { height :3 , id :'gsItem1 ' } ] , false ) ;
1703
1751
let layout = grid . save ( false ) ;
1704
- expect ( layout ) . toEqual ( [ { x :0 , y :0 , width :4 , height :3 , id :'item1 ' } , { x :4 , y :0 , width :4 , height :4 , id :'item2 ' } ] ) ;
1752
+ expect ( layout ) . toEqual ( [ { x :0 , y :0 , width :4 , height :3 , id :'gsItem1 ' } , { x :4 , y :0 , width :4 , height :4 , id :'gsItem2 ' } ] ) ;
1705
1753
} ) ;
1706
1754
it ( 'load size 1 item only with callback' , function ( ) {
1707
1755
let grid = GridStack . init ( ) ;
1708
- grid . load ( [ { height :3 , id :'item1 ' } ] , ( ) => { } ) ;
1756
+ grid . load ( [ { height :3 , id :'gsItem1 ' } ] , ( ) => { } ) ;
1709
1757
let layout = grid . save ( false ) ;
1710
- expect ( layout ) . toEqual ( [ { x :0 , y :0 , width :4 , height :3 , id :'item1 ' } , { x :4 , y :0 , width :4 , height :4 , id :'item2 ' } ] ) ;
1758
+ expect ( layout ) . toEqual ( [ { x :0 , y :0 , width :4 , height :3 , id :'gsItem1 ' } , { x :4 , y :0 , width :4 , height :4 , id :'gsItem2 ' } ] ) ;
1711
1759
} ) ;
1712
1760
} ) ;
1713
1761
0 commit comments