File size: 5,711 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
// @flow
import type { Position } from "./utils";
export type PositionParams = {
margin: [number, number],
containerPadding: [number, number],
containerWidth: number,
cols: number,
rowHeight: number,
maxRows: number
};
// Helper for generating column width
export function calcGridColWidth(positionParams: PositionParams): number {
const { margin, containerPadding, containerWidth, cols } = positionParams;
return (
(containerWidth - margin[0] * (cols - 1) - containerPadding[0] * 2) / cols
);
}
// This can either be called:
// calcGridItemWHPx(w, colWidth, margin[0])
// or
// calcGridItemWHPx(h, rowHeight, margin[1])
export function calcGridItemWHPx(
gridUnits: number,
colOrRowSize: number,
marginPx: number
): number {
// 0 * Infinity === NaN, which causes problems with resize contraints
if (!Number.isFinite(gridUnits)) return gridUnits;
return Math.round(
colOrRowSize * gridUnits + Math.max(0, gridUnits - 1) * marginPx
);
}
/**
* Return position on the page given an x, y, w, h.
* left, top, width, height are all in pixels.
* @param {PositionParams} positionParams Parameters of grid needed for coordinates calculations.
* @param {Number} x X coordinate in grid units.
* @param {Number} y Y coordinate in grid units.
* @param {Number} w W coordinate in grid units.
* @param {Number} h H coordinate in grid units.
* @return {Position} Object containing coords.
*/
export function calcGridItemPosition(
positionParams: PositionParams,
x: number,
y: number,
w: number,
h: number,
state: ?Object
): Position {
const { margin, containerPadding, rowHeight } = positionParams;
const colWidth = calcGridColWidth(positionParams);
const out = {};
// If resizing, use the exact width and height as returned from resizing callbacks.
if (state && state.resizing) {
out.width = Math.round(state.resizing.width);
out.height = Math.round(state.resizing.height);
}
// Otherwise, calculate from grid units.
else {
out.width = calcGridItemWHPx(w, colWidth, margin[0]);
out.height = calcGridItemWHPx(h, rowHeight, margin[1]);
}
// If dragging, use the exact width and height as returned from dragging callbacks.
if (state && state.dragging) {
out.top = Math.round(state.dragging.top);
out.left = Math.round(state.dragging.left);
} else if (
state &&
state.resizing &&
typeof state.resizing.top === "number" &&
typeof state.resizing.left === "number"
) {
out.top = Math.round(state.resizing.top);
out.left = Math.round(state.resizing.left);
}
// Otherwise, calculate from grid units.
else {
out.top = Math.round((rowHeight + margin[1]) * y + containerPadding[1]);
out.left = Math.round((colWidth + margin[0]) * x + containerPadding[0]);
}
return out;
}
/**
* Translate x and y coordinates from pixels to grid units.
* @param {PositionParams} positionParams Parameters of grid needed for coordinates calculations.
* @param {Number} top Top position (relative to parent) in pixels.
* @param {Number} left Left position (relative to parent) in pixels.
* @param {Number} w W coordinate in grid units.
* @param {Number} h H coordinate in grid units.
* @return {Object} x and y in grid units.
*/
export function calcXY(
positionParams: PositionParams,
top: number,
left: number,
w: number,
h: number
): { x: number, y: number } {
const { margin, containerPadding, cols, rowHeight, maxRows } = positionParams;
const colWidth = calcGridColWidth(positionParams);
// left = containerPaddingX + x * (colWidth + marginX)
// x * (colWidth + marginX) = left - containerPaddingX
// x = (left - containerPaddingX) / (colWidth + marginX)
let x = Math.round((left - containerPadding[0]) / (colWidth + margin[0]));
let y = Math.round((top - containerPadding[1]) / (rowHeight + margin[1]));
// Capping
x = clamp(x, 0, cols - w);
y = clamp(y, 0, maxRows - h);
return { x, y };
}
/**
* Given a height and width in pixel values, calculate grid units.
* @param {PositionParams} positionParams Parameters of grid needed for coordinates calcluations.
* @param {Number} height Height in pixels.
* @param {Number} width Width in pixels.
* @param {Number} x X coordinate in grid units.
* @param {Number} y Y coordinate in grid units.
* @param {String} handle Resize Handle.
* @return {Object} w, h as grid units.
*/
export function calcWH(
positionParams: PositionParams,
width: number,
height: number,
x: number,
y: number,
handle: string
): { w: number, h: number } {
const { margin, maxRows, cols, rowHeight } = positionParams;
const colWidth = calcGridColWidth(positionParams);
// width = colWidth * w - (margin * (w - 1))
// ...
// w = (width + margin) / (colWidth + margin)
let w = Math.round((width + margin[0]) / (colWidth + margin[0]));
let h = Math.round((height + margin[1]) / (rowHeight + margin[1]));
// Capping
let _w = clamp(w, 0, cols - x);
let _h = clamp(h, 0, maxRows - y);
if (["sw", "w", "nw"].indexOf(handle) !== -1) {
_w = clamp(w, 0, cols);
}
if (["nw", "n", "ne"].indexOf(handle) !== -1) {
_h = clamp(h, 0, maxRows);
}
return { w: _w, h: _h };
}
// Similar to _.clamp
export function clamp(
num: number,
lowerBound: number,
upperBound: number
): number {
return Math.max(Math.min(num, upperBound), lowerBound);
}
|