Skip to content

Commit 8bcc658

Browse files
author
Alain Dumesny
committed
add float(val?) set/get method
* to change the grid option, which will relayout widgets when gravity is back on * renamed internal _updateCounter (number) to _batchMode (boolean) was never a counter to begin with.
1 parent a9bed40 commit 8bcc658

File tree

7 files changed

+143
-47
lines changed

7 files changed

+143
-47
lines changed

demo/two.html

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,17 @@ <h1>Two grids demo</h1>
7676

7777
<div class="row">
7878
<div class="col-md-6">
79+
<a class="btn btn-primary" id="float1" href="#">float: false</a>
7980
<div class="grid-stack grid-stack-6" id="grid1">
8081
</div>
8182
</div>
8283
<div class="col-md-6">
84+
<a class="btn btn-primary" id="float2" href="#">float: true</a>
8385
<div class="grid-stack grid-stack-6" id="grid2">
84-
</div>
85-
</div>
86+
</div>
8687
</div>
8788
</div>
89+
</div>
8890

8991

9092
<script type="text/javascript">
@@ -124,6 +126,14 @@ <h1>Two grids demo</h1>
124126
scroll: false,
125127
appendTo: 'body',
126128
});
129+
130+
$('#float1').click(function() { toggleFloat('#float1', '#grid1'); });
131+
$('#float2').click(function() { toggleFloat('#float2', '#grid2'); });
132+
function toggleFloat(button, grid) {
133+
var grid = $(grid).data('gridstack');
134+
grid.float(! grid.float());
135+
$(button).html('float: ' + grid.float());
136+
}
127137
});
128138
</script>
129139
</body>

doc/CHANGES.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Change log
2727

2828
## v0.5.5-dev (upcoming changes)
2929

30-
TBD
30+
- add `float(val)` to set/get the grid float mode
3131

3232
## v0.5.5 (2019-11-27)
3333

doc/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ gridstack.js API
3232
- [enable()](#enable)
3333
- [enableMove(doEnable, includeNewWidgets)](#enablemovedoenable-includenewwidgets)
3434
- [enableResize(doEnable, includeNewWidgets)](#enableresizedoenable-includenewwidgets)
35+
- [float(val?)](#floatval)
3536
- [getCellFromPixel(position[, useOffset])](#getcellfrompixelposition-useoffset)
3637
- [isAreaEmpty(x, y, width, height)](#isareaemptyx-y-width-height)
3738
- [locked(el, val)](#lockedel-val)
@@ -314,6 +315,12 @@ Enables/disables widget resizing. `includeNewWidgets` will force new widgets to
314315
grid.resizable(this.container.children('.' + this.opts.itemClass), doEnable);
315316
```
316317

318+
### float(val?)
319+
320+
set/get floating widgets (default: `false`)
321+
322+
- `val` - boolean to set true/false, else get the current value
323+
317324
### getCellFromPixel(position[, useOffset])
318325

319326
Get the position of the cell under a pixel on screen.

spec/gridstack-engine-spec.js

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ describe('gridstack engine', function() {
8383
});
8484

8585
beforeEach(function() {
86-
engine._updateCounter = 0;
86+
engine._batchMode = false;
8787
});
8888

8989
it('should return all dirty nodes', function() {
@@ -94,8 +94,8 @@ describe('gridstack engine', function() {
9494
expect(nodes[1].idx).toEqual(2);
9595
});
9696

97-
it('should\'n clean nodes if _updateCounter > 0', function() {
98-
engine._updateCounter = 1;
97+
it('should\'n clean nodes if _batchMode true', function() {
98+
engine._batchMode = true;
9999
engine.cleanNodes();
100100

101101
expect(engine.getDirtyNodes().length).toBeGreaterThan(0);
@@ -118,12 +118,22 @@ describe('gridstack engine', function() {
118118
it('should work on not float grids', function() {
119119
expect(engine.float).toEqual(false);
120120
engine.batchUpdate();
121-
expect(engine._updateCounter).toBeGreaterThan(0);
121+
expect(engine._batchMode).toBeTrue();
122122
expect(engine.float).toEqual(true);
123123
engine.commit();
124-
expect(engine._updateCounter).toEqual(0);
124+
expect(engine._batchMode).toBeFalse();
125125
expect(engine.float).toEqual(false);
126126
});
127+
128+
it('should work on float grids', function() {
129+
engine.float = true;
130+
engine.batchUpdate();
131+
expect(engine._batchMode).toBeTrue();
132+
expect(engine.float).toEqual(true);
133+
engine.commit();
134+
expect(engine._batchMode).toBeFalse();
135+
expect(engine.float).toEqual(true);
136+
});
127137
});
128138

129139
describe('test batchUpdate/commit', function() {
@@ -136,10 +146,10 @@ describe('gridstack engine', function() {
136146
it('should work on float grids', function() {
137147
expect(engine.float).toEqual(true);
138148
engine.batchUpdate();
139-
expect(engine._updateCounter).toBeGreaterThan(0);
149+
expect(engine._batchMode).toBeTrue();
140150
expect(engine.float).toEqual(true);
141151
engine.commit();
142-
expect(engine._updateCounter).toEqual(0);
152+
expect(engine._batchMode).toBeFalse();
143153
expect(engine.float).toEqual(true);
144154
});
145155
});
@@ -163,8 +173,8 @@ describe('gridstack engine', function() {
163173
];
164174
});
165175

166-
it('should\'n be called if _updateCounter > 0', function() {
167-
engine._updateCounter = 1;
176+
it('should\'n be called if _batchMode true', function() {
177+
engine._batchMode = true;
168178
engine._notify();
169179

170180
expect(spy.callback).not.toHaveBeenCalled();

spec/gridstack-spec.js

Lines changed: 58 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ describe('gridstack', function() {
1515
' </div>' +
1616
' </div>' +
1717
'</div>';
18+
// generic widget with no param
19+
var widgetHTML = '<div class="grid-stack-item"><div class="grid-stack-item-content"> hello </div></div>';
1820

1921
beforeEach(function() {
2022
w = window;
@@ -31,13 +33,12 @@ describe('gridstack', function() {
3133

3234
it('should set default params correctly.', function() {
3335
e.call(w);
34-
expect(w.column).toBeUndefined();
36+
expect(w.column).toEqual(12);
3537
expect(w.float).toBe(false);
3638
expect(w.maxRow).toEqual(0);
3739
expect(w.nodes).toEqual([]);
3840
expect(typeof w.onchange).toBe('function');
39-
expect(w._updateCounter).toEqual(0);
40-
expect(w._float).toEqual(w.float);
41+
expect(w._batchMode).toBeFalse();
4142
});
4243

4344
it('should set params correctly.', function() {
@@ -50,23 +51,19 @@ describe('gridstack', function() {
5051
expect(w.maxRow).toEqual(2);
5152
expect(w.nodes).toEqual(arr);
5253
expect(w.onchange).toEqual(fkt);
53-
expect(w._updateCounter).toEqual(0);
54-
expect(w._float).toEqual(w.float);
54+
expect(w._batchMode).toBeFalse();
5555
});
56-
57-
5856
});
5957

6058
describe('batch update', function() {
6159

62-
it('should set float and counter when calling batchUpdate.', function() {
60+
it('should set float and batchMode when calling batchUpdate.', function() {
6361
e.prototype.batchUpdate.call(w);
6462
expect(w.float).toBe(true);
65-
expect(w._updateCounter).toEqual(1);
63+
expect(w._batchMode).toBeTrue();
6664
});
6765

6866
//test commit function
69-
7067
});
7168

7269
describe('sorting of nodes', function() {
@@ -914,7 +911,6 @@ describe('gridstack', function() {
914911
it('should keep all widget options the same (autoPosition off', function() {
915912
$('.grid-stack').gridstack({float: true});
916913
var grid = $('.grid-stack').data('gridstack');
917-
var widgetHTML = '<div class="grid-stack-item"><div class="grid-stack-item-content"></div></div>';
918914
var widget = grid.addWidget(widgetHTML, 6, 7, 2, 3, false, 1, 4, 2, 5, 'coolWidget');
919915
var $widget = $(widget);
920916
expect(parseInt($widget.attr('data-gs-x'), 10)).toBe(6);
@@ -927,6 +923,35 @@ describe('gridstack', function() {
927923
expect(parseInt($widget.attr('data-gs-min-height'), 10)).toBe(2);
928924
expect(parseInt($widget.attr('data-gs-max-height'), 10)).toBe(5);
929925
expect($widget.attr('data-gs-id')).toBe('coolWidget');
926+
927+
// should move widget to top with float=false
928+
expect(grid.float()).toBe(true);
929+
grid.float(false);
930+
expect(grid.float()).toBe(false);
931+
expect(parseInt($widget.attr('data-gs-x'), 10)).toBe(6);
932+
expect(parseInt($widget.attr('data-gs-y'), 10)).toBe(4); // <--- from 7 to 4 below second original widget
933+
expect(parseInt($widget.attr('data-gs-width'), 10)).toBe(2);
934+
expect(parseInt($widget.attr('data-gs-height'), 10)).toBe(3);
935+
expect($widget.attr('data-gs-auto-position')).toBe(undefined);
936+
expect(parseInt($widget.attr('data-gs-min-width'), 10)).toBe(1);
937+
expect(parseInt($widget.attr('data-gs-max-width'), 10)).toBe(4);
938+
expect(parseInt($widget.attr('data-gs-min-height'), 10)).toBe(2);
939+
expect(parseInt($widget.attr('data-gs-max-height'), 10)).toBe(5);
940+
expect($widget.attr('data-gs-id')).toBe('coolWidget');
941+
942+
// should not move again (no-op)
943+
grid.float(true);
944+
expect(grid.float()).toBe(true);
945+
expect(parseInt($widget.attr('data-gs-x'), 10)).toBe(6);
946+
expect(parseInt($widget.attr('data-gs-y'), 10)).toBe(4);
947+
expect(parseInt($widget.attr('data-gs-width'), 10)).toBe(2);
948+
expect(parseInt($widget.attr('data-gs-height'), 10)).toBe(3);
949+
expect($widget.attr('data-gs-auto-position')).toBe(undefined);
950+
expect(parseInt($widget.attr('data-gs-min-width'), 10)).toBe(1);
951+
expect(parseInt($widget.attr('data-gs-max-width'), 10)).toBe(4);
952+
expect(parseInt($widget.attr('data-gs-min-height'), 10)).toBe(2);
953+
expect(parseInt($widget.attr('data-gs-max-height'), 10)).toBe(5);
954+
expect($widget.attr('data-gs-id')).toBe('coolWidget');
930955
});
931956
});
932957

@@ -940,7 +965,6 @@ describe('gridstack', function() {
940965
it('should change x, y coordinates for widgets.', function() {
941966
$('.grid-stack').gridstack({float: true});
942967
var grid = $('.grid-stack').data('gridstack');
943-
var widgetHTML = '<div class="grid-stack-item"><div class="grid-stack-item-content"></div></div>';
944968
var widget = grid.addWidget(widgetHTML, 9, 7, 2, 3, true);
945969
var $widget = $(widget);
946970
expect(parseInt($widget.attr('data-gs-x'), 10)).not.toBe(9);
@@ -958,7 +982,6 @@ describe('gridstack', function() {
958982
it('should keep all widget options the same (autoPosition off)', function() {
959983
$('.grid-stack').gridstack();
960984
var grid = $('.grid-stack').data('gridstack');
961-
var widgetHTML = '<div class="grid-stack-item"><div class="grid-stack-item-content"></div></div>';
962985
var widget = grid.addWidget(widgetHTML, {x: 8, height: 2, id: 'optionWidget'});
963986
var $widget = $(widget);
964987
expect(parseInt($widget.attr('data-gs-x'), 10)).toBe(8);
@@ -983,7 +1006,6 @@ describe('gridstack', function() {
9831006
it('should use default', function() {
9841007
$('.grid-stack').gridstack();
9851008
var grid = $('.grid-stack').data('gridstack');
986-
var widgetHTML = '<div class="grid-stack-item"><div class="grid-stack-item-content"></div></div>';
9871009
var widget = grid.addWidget(widgetHTML, {x: 'foo', height: null, width: 'bar', height: ''});
9881010
var $widget = $(widget);
9891011
expect(parseInt($widget.attr('data-gs-x'), 10)).toBe(8);
@@ -1011,6 +1033,28 @@ describe('gridstack', function() {
10111033
});
10121034
});
10131035

1036+
describe('method float()', function() {
1037+
beforeEach(function() {
1038+
document.body.insertAdjacentHTML('afterbegin', gridstackHTML);
1039+
});
1040+
afterEach(function() {
1041+
document.body.removeChild(document.getElementById('gs-cont'));
1042+
});
1043+
it('should match true/false only', function() {
1044+
$('.grid-stack').gridstack({float: true});
1045+
var grid = $('.grid-stack').data('gridstack');
1046+
expect(grid.float()).toBe(true);
1047+
grid.float(0);
1048+
expect(grid.float()).toBe(false);
1049+
grid.float(null);
1050+
expect(grid.float()).toBe(false);
1051+
grid.float(undefined);
1052+
expect(grid.float()).toBe(false);
1053+
grid.float(false);
1054+
expect(grid.float()).toBe(false);
1055+
});
1056+
});
1057+
10141058
describe('grid.destroy', function() {
10151059
beforeEach(function() {
10161060
document.body.insertAdjacentHTML('afterbegin', gridstackHTML);

src/gridstack.d.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,19 @@ interface GridStack {
142142
*/
143143
enableResize(doEnable: boolean, includeNewWidgets: boolean): void;
144144

145+
/**
146+
* enable/disable floating widgets (default: `false`) See [example](http://gridstackjs.com/demo/float.html)
147+
* @param mode
148+
*/
149+
float(mode: boolean): void;
150+
151+
/**
152+
* get the current float mode
153+
*/
154+
float(): boolean;
155+
156+
157+
145158
/**
146159
* Get the position of the cell under a pixel on screen.
147160
* @param position the position of the pixel to resolve in
@@ -263,8 +276,9 @@ interface GridStack {
263276

264277
/**
265278
* (Experimental) Modify number of columns in the grid. Will attempt to update existing widgets
266-
* to conform to new number of columns. Requires `gridstack-extra.css` or `gridstack-extra.min.css`.
267-
* @param column - Integer between 1 and 12.
279+
* to conform to new number of columns. Requires `gridstack-extra.css` or `gridstack-extra.min.css` for [1-11],
280+
* else you will need to generate correct CSS (see https://github.com/gridstack/gridstack.js#change-grid-columns)
281+
* @param column - Integer > 0 (default 12).
268282
* @param doNotPropagate if true existing widgets will not be updated (optional)
269283
*/
270284
setColumn(column: number, doNotPropagate ? : boolean): void;

0 commit comments

Comments
 (0)