Skip to content

Commit b79a603

Browse files
committed
TS: getCellFromPixel() fix and tests for #810 (roundoff error)
* forgot to update the test case for #810 changes * also found getCellFromPixel() was incorrect in rounding as well or delaing with margins (which don't affect cell size, just content)
1 parent 94e1341 commit b79a603

File tree

3 files changed

+34
-15
lines changed

3 files changed

+34
-15
lines changed

doc/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ gridstack.js API
4141
- [isAreaEmpty(x, y, width, height)](#isareaemptyx-y-width-height)
4242
- [locked(el, val)](#lockedel-val)
4343
- [makeWidget(el)](#makewidgetel)
44-
- [margin(value: number)](#marginvalue-number)
44+
- [margin(value: numberOrString)](#marginvalue-numberorstring)
4545
- [maxHeight(el, val)](#maxheightel-val)
4646
- [minHeight(el, val)](#minheightel-val)
4747
- [maxWidth(el, val)](#maxwidthel-val)

spec/gridstack-spec.ts

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -123,22 +123,38 @@ describe('gridstack', function() {
123123
afterEach(function() {
124124
document.body.removeChild(document.getElementById('gs-cont'));
125125
});
126-
it('should return {x: 2, y: 5}.', function() {
126+
it('should return {x: 4, y: 5}.', function() {
127+
let cellHeight = 80;
128+
let rectMargin = 8; // ??? top/left margin of 8 when calling getBoundingClientRect
127129
let options = {
128-
cellHeight: 80,
130+
cellHeight: cellHeight,
129131
margin: 5
130132
};
131133
let grid = GridStack.init(options);
132-
let pixel = {top: 500, left: 200};
134+
let pixel = {left: 4 * 800 / 12 + rectMargin, top: 5 * cellHeight + rectMargin};
133135
let cell = grid.getCellFromPixel(pixel);
134-
expect(cell.x).toBe(2);
136+
expect(cell.x).toBe(4);
135137
expect(cell.y).toBe(5);
136138
cell = grid.getCellFromPixel(pixel, false);
137-
expect(cell.x).toBe(2);
139+
expect(cell.x).toBe(4);
138140
expect(cell.y).toBe(5);
139141
cell = grid.getCellFromPixel(pixel, true);
140-
expect(cell.x).toBe(2);
142+
expect(cell.x).toBe(4);
141143
expect(cell.y).toBe(5);
144+
pixel = {left: 4 * 800 / 12 + rectMargin, top: 5 * cellHeight + rectMargin};
145+
146+
// now move 1 pixel in and get prev cell (we were on the edge)
147+
pixel.left--;
148+
pixel.top--;
149+
cell = grid.getCellFromPixel(pixel);
150+
expect(cell.x).toBe(3);
151+
expect(cell.y).toBe(4);
152+
cell = grid.getCellFromPixel(pixel, false);
153+
expect(cell.x).toBe(3);
154+
expect(cell.y).toBe(4);
155+
cell = grid.getCellFromPixel(pixel, true);
156+
expect(cell.x).toBe(3);
157+
expect(cell.y).toBe(4);
142158
});
143159
});
144160

@@ -149,14 +165,14 @@ describe('gridstack', function() {
149165
afterEach(function() {
150166
document.body.removeChild(document.getElementById('gs-cont'));
151167
});
152-
it('should return 1/12th of container width.', function() {
168+
it('should return 1/12th of container width (not rounded anymore).', function() {
153169
let options = {
154170
cellHeight: 80,
155171
margin: 5,
156172
column: 12
157173
};
158174
let grid = GridStack.init(options);
159-
let res = Math.round($('.grid-stack').outerWidth() / 12);
175+
let res = $('.grid-stack').outerWidth() / 12;
160176
expect(grid.cellWidth()).toBe(res);
161177
});
162178
it('should return 1/10th of container width.', function() {
@@ -193,21 +209,21 @@ describe('gridstack', function() {
193209
expect(grid.getRow()).toBe(rows);
194210

195211
expect(grid.getCellHeight()).toBe(cellHeight);
196-
expect(parseInt(container.css('height'))).toBe(rows * cellHeight + (rows-1) * margin);
212+
expect(parseInt(container.css('height'))).toBe(rows * cellHeight);
197213

198214
grid.cellHeight( grid.getCellHeight() ); // should be no-op
199215
expect(grid.getCellHeight()).toBe(cellHeight);
200-
expect(parseInt(container.css('height'))).toBe(rows * cellHeight + (rows-1) * margin);
216+
expect(parseInt(container.css('height'))).toBe(rows * cellHeight);
201217

202218
cellHeight = 120; // should change and CSS actual height
203219
grid.cellHeight( cellHeight );
204220
expect(grid.getCellHeight()).toBe(cellHeight);
205-
expect(parseInt(container.css('height'))).toBe(rows * cellHeight + (rows-1) * margin);
221+
expect(parseInt(container.css('height'))).toBe(rows * cellHeight);
206222

207223
cellHeight = 20; // should change and CSS actual height
208224
grid.cellHeight( cellHeight );
209225
expect(grid.getCellHeight()).toBe(cellHeight);
210-
expect(parseInt(container.css('height'))).toBe(rows * cellHeight + (rows-1) * margin);
226+
expect(parseInt(container.css('height'))).toBe(rows * cellHeight);
211227
});
212228

213229
it('should be square', function() {

src/gridstack.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -609,17 +609,20 @@ export class GridStack {
609609
*/
610610
public getCellFromPixel(position: MousePosition, useDocRelative = false): CellPosition {
611611
let box = this.el.getBoundingClientRect();
612+
// console.log(`getBoundingClientRect left: ${box.left} top: ${box.top} w: ${box.width} h: ${box.height}`)
612613
let containerPos;
613614
if (useDocRelative) {
614615
containerPos = {top: box.top + document.documentElement.scrollTop, left: box.left};
616+
// console.log(`getCellFromPixel scrollTop: ${document.documentElement.scrollTop}`)
615617
} else {
616618
containerPos = {top: this.el.offsetTop, left: this.el.offsetLeft}
619+
// console.log(`getCellFromPixel offsetTop: ${containerPos.left} offsetLeft: ${containerPos.top}`)
617620
}
618621
let relativeLeft = position.left - containerPos.left;
619622
let relativeTop = position.top - containerPos.top;
620623

621-
let columnWidth = Math.floor(box.width / this.opts.column);
622-
let rowHeight = Math.floor(box.height / parseInt(this.el.getAttribute('data-gs-current-row')));
624+
let columnWidth = (box.width / this.opts.column);
625+
let rowHeight = (box.height / parseInt(this.el.getAttribute('data-gs-current-row')));
623626

624627
return {x: Math.floor(relativeLeft / columnWidth), y: Math.floor(relativeTop / rowHeight)};
625628
}

0 commit comments

Comments
 (0)