interactjs
Version:
Drag and drop, resizing and multi-touch gestures with inertia and snapping for modern browsers (and also IE8+)
84 lines (68 loc) • 2.56 kB
JavaScript
// This module adds the options.resize.restrictSize setting which sets min and
// max width and height for the target being resized.
//
// interact(target).resize({
// edges: { top: true, left: true },
// restrictSize: {
// min: { width: -600, height: -600 },
// max: { width: 600, height: 600 },
// },
// });
const modifiers = require('./index');
const restrictEdges = require('./restrictEdges');
const utils = require('../utils');
const rectUtils = require('../utils/rect');
const defaultOptions = require('../defaultOptions');
const resize = require('../actions/resize');
const noMin = { width: -Infinity, height: -Infinity };
const noMax = { width: +Infinity, height: +Infinity };
const restrictSize = {
defaults: {
enabled: false,
endOnly: false,
min: null,
max: null,
},
setOffset: function ({ interaction }) {
return interaction.startOffset;
},
set: function (arg) {
const { interaction, options } = arg;
const edges = interaction.prepared.linkedEdges || interaction.prepared.edges;
if (!interaction.interacting() || !edges) {
return;
}
const rect = rectUtils.xywhToTlbr(interaction.resizeRects.inverted);
const minSize = rectUtils.tlbrToXywh(restrictEdges.getRestrictionRect(options.min, interaction)) || noMin;
const maxSize = rectUtils.tlbrToXywh(restrictEdges.getRestrictionRect(options.max, interaction)) || noMax;
arg.options = {
enabled: options.enabled,
endOnly: options.endOnly,
inner: utils.extend({}, restrictEdges.noInner),
outer: utils.extend({}, restrictEdges.noOuter),
};
if (edges.top) {
arg.options.inner.top = rect.bottom - minSize.height;
arg.options.outer.top = rect.bottom - maxSize.height;
}
else if (edges.bottom) {
arg.options.inner.bottom = rect.top + minSize.height;
arg.options.outer.bottom = rect.top + maxSize.height;
}
if (edges.left) {
arg.options.inner.left = rect.right - minSize.width;
arg.options.outer.left = rect.right - maxSize.width;
}
else if (edges.right) {
arg.options.inner.right = rect.left + minSize.width;
arg.options.outer.right = rect.left + maxSize.width;
}
restrictEdges.set(arg);
},
modifyCoords: restrictEdges.modifyCoords,
};
modifiers.restrictSize = restrictSize;
modifiers.names.push('restrictSize');
defaultOptions.perAction.restrictSize = restrictSize.defaults;
resize.defaults.restrictSize = restrictSize.defaults;
module.exports = restrictSize;