From c6a6ad5210c47e8aea1e4440ea29d2c7cfcf480f Mon Sep 17 00:00:00 2001 From: Julien Chichignoud Date: Wed, 28 Jan 2015 16:04:30 +0100 Subject: [PATCH] add the get_cell_from_pixel() function --- README.md | 10 ++++++++++ src/gridstack.js | 11 +++++++++++ 2 files changed, 21 insertions(+) diff --git a/README.md b/README.md index fbf4b17..4bc3164 100644 --- a/README.md +++ b/README.md @@ -226,6 +226,16 @@ grid.cell_height(grid.cell_width() * 1.2); Gets current cell width. +### get_cell_from_pixel(position) + +Get the position of the cell under a pixel on screen. + +Parameters : + +- `position` - the position of the pixel to resolve in absolute coordinates, as an object with `top` and `left`properties + +Returns an object with properties `x` and `y` i.e. the column and row in the grid. + ### locked(el, val) Locks/unlocks widget. diff --git a/src/gridstack.js b/src/gridstack.js index bfce4c5..8accee8 100644 --- a/src/gridstack.js +++ b/src/gridstack.js @@ -714,6 +714,17 @@ return Math.ceil(o.outerWidth() / o.attr('data-gs-width')); }; + GridStack.prototype.get_cell_from_pixel = function(position) { + var containerPos = this.container.position(); + var relativeLeft = position.left - containerPos.left; + var relativeTop = position.top - containerPos.top; + + var column_width = Math.floor(this.container.width() / this.opts.width); + var row_height = this.opts.cell_height + this.opts.vertical_margin; + + return {x: Math.floor(relativeLeft / column_width), y: Math.floor(relativeTop / row_height)}; + }; + scope.GridStackUI = GridStack; scope.GridStackUI.Utils = Utils;