UNPKG

3.42 kBJavaScriptView Raw
1'use strict'
2
3var ALIGN_TO_NORMALIZED = require('./alignToNormalized')
4
5/**
6 * @localdoc Given source and target regions, and the given alignments required, returns a region that is the resulting allignment.
7 * Does not modify the sourceRegion.
8 *
9 * Example
10 *
11 * var sourceRegion = zippy.getInstance({
12 * alias : 'z.region',
13 * top : 10,
14 * left : 10,
15 * bottom : 40,
16 * right : 100
17 * })
18 *
19 * var targetRegion = zippy.getInstance({
20 * alias : 'z.region',
21 * top : 10,
22 * left : 10,
23 * bottom : 40,
24 * right : 100
25 * })
26 * //has top-left at (10,10)
27 * //and bottom-right at (40, 100)
28 *
29 * var alignRegion = alignable.COMPUTE_ALIGN_REGION(sourceRegion, targetRegion, 'tl-br')
30 *
31 * //alignRegion will be a clone of sourceRegion, but will have the
32 * //top-left corner aligned with bottom-right of targetRegion
33 *
34 * alignRegion.get() // => { top: 40, left: 100, bottom: 70, right: 190 }
35 *
36 * @param {Region} sourceRegion The source region to align to targetRegion
37 * @param {Region} targetRegion The target region to which to align the sourceRegion
38 * @param {String/String[]} positions A string ( delimited by "-" characters ) or an array of strings with the position to try, in the order of their priority.
39 * See Region#getPoint for a list of available positions. They can be combined in any way.
40 * @param {Object} config A config object with other configuration for the alignment
41 * @param {Object/Object[]} config.offset Optional offsets. Either an object or an array with a different offset for each position
42 * @param {Element/Region/Boolean} config.constrain The constrain to region or element. If the boolean true, Region.getDocRegion() will be used
43 * @param {Object/Boolean} config.sync A boolean object that indicates whether to sync sourceRegion and targetRegion sizes (width/height or both). Can be
44 *
45 * * true - in order to sync both width and height
46 * * { width: true } - to only sync width
47 * * { height: true } - to only sync height
48 * * { size: true } - to sync both width and height
49 *
50 * @return {Object} an object with the following keys:
51 *
52 * * position - the position where the alignment was made. One of the given positions
53 * * region - the region where the alignment is in place
54 * * positionChanged - boolean value indicating if the position of the returned region is different from the position of sourceRegion
55 * * widthChanged - boolean value indicating if the width of the returned region is different from the width of sourceRegion
56 * * heightChanged - boolean value indicating if the height of the returned region is different from the height of sourceRegion
57 */
58function COMPUTE_ALIGN_REGION(sourceRegion, targetRegion, positions, config){
59 sourceRegion = Region.from(sourceRegion)
60
61 var sourceClone = sourceRegion.clone()
62 var position = ALIGN_TO_NORMALIZED(sourceClone, targetRegion, positions, config)
63
64 return {
65 position : position,
66 region : sourceClone,
67 widthChanged : sourceClone.getWidth() != sourceRegion.getWidth(),
68 heightChanged : sourceClone.getHeight() != sourceRegion.getHeight(),
69 positionChanged : sourceClone.equalsPosition(sourceRegion)
70 }
71}
72
73
74module.exports = COMPUTE_ALIGN_REGION
\No newline at end of file