123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- import {
- forEach
- } from 'min-dash';
- import {
- snapTo
- } from './SnapUtil';
- /**
- * A snap context, containing the (possibly incomplete)
- * mappings of drop targets (to identify the snapping)
- * to computed snap points.
- */
- export default function SnapContext() {
- /**
- * Map<String, SnapPoints> mapping drop targets to
- * a list of possible snappings.
- *
- * @type {Object}
- */
- this._targets = {};
- /**
- * Map<String, Point> initial positioning of element
- * regarding various snap directions.
- *
- * @type {Object}
- */
- this._snapOrigins = {};
- /**
- * List of snap locations
- *
- * @type {Array<String>}
- */
- this._snapLocations = [];
- /**
- * Map<String, Array<Point>> of default snapping locations
- *
- * @type {Object}
- */
- this._defaultSnaps = {};
- }
- SnapContext.prototype.getSnapOrigin = function(snapLocation) {
- return this._snapOrigins[snapLocation];
- };
- SnapContext.prototype.setSnapOrigin = function(snapLocation, initialValue) {
- this._snapOrigins[snapLocation] = initialValue;
- if (this._snapLocations.indexOf(snapLocation) === -1) {
- this._snapLocations.push(snapLocation);
- }
- };
- SnapContext.prototype.addDefaultSnap = function(type, point) {
- var snapValues = this._defaultSnaps[type];
- if (!snapValues) {
- snapValues = this._defaultSnaps[type] = [];
- }
- snapValues.push(point);
- };
- /**
- * Return a number of initialized snaps, i.e. snap locations such as
- * top-left, mid, bottom-right and so forth.
- *
- * @return {Array<String>} snapLocations
- */
- SnapContext.prototype.getSnapLocations = function() {
- return this._snapLocations;
- };
- /**
- * Set the snap locations for this context.
- *
- * The order of locations determines precedence.
- *
- * @param {Array<String>} snapLocations
- */
- SnapContext.prototype.setSnapLocations = function(snapLocations) {
- this._snapLocations = snapLocations;
- };
- /**
- * Get snap points for a given target
- *
- * @param {Element|String} target
- */
- SnapContext.prototype.pointsForTarget = function(target) {
- var targetId = target.id || target;
- var snapPoints = this._targets[targetId];
- if (!snapPoints) {
- snapPoints = this._targets[targetId] = new SnapPoints();
- snapPoints.initDefaults(this._defaultSnaps);
- }
- return snapPoints;
- };
- /**
- * Creates the snap points and initializes them with the
- * given default values.
- *
- * @param {Object<String, Array<Point>>} [defaultPoints]
- */
- function SnapPoints(defaultSnaps) {
- /**
- * Map<String, Map<(x|y), Array<Number>>> mapping snap locations,
- * i.e. top-left, bottom-right, center to actual snap values.
- *
- * @type {Object}
- */
- this._snapValues = {};
- }
- SnapPoints.prototype.add = function(snapLocation, point) {
- var snapValues = this._snapValues[snapLocation];
- if (!snapValues) {
- snapValues = this._snapValues[snapLocation] = { x: [], y: [] };
- }
- if (snapValues.x.indexOf(point.x) === -1) {
- snapValues.x.push(point.x);
- }
- if (snapValues.y.indexOf(point.y) === -1) {
- snapValues.y.push(point.y);
- }
- };
- SnapPoints.prototype.snap = function(point, snapLocation, axis, tolerance) {
- var snappingValues = this._snapValues[snapLocation];
- return snappingValues && snapTo(point[axis], snappingValues[axis], tolerance);
- };
- /**
- * Initialize a number of default snapping points.
- *
- * @param {Object} defaultSnaps
- */
- SnapPoints.prototype.initDefaults = function(defaultSnaps) {
- var self = this;
- forEach(defaultSnaps || {}, function(snapPoints, snapLocation) {
- forEach(snapPoints, function(point) {
- self.add(snapLocation, point);
- });
- });
- };
|