UNPKG

7.42 kBJavaScriptView Raw
1'use strict'
2
3module.exports = function(Region){
4
5 require('./Region.static')
6 require('./Region.proto')
7
8 var COMPUTE_ALIGN_REGION = require('./computeAlignRegion')
9
10 /**
11 * region-align module exposes methods for aligning {@link Element} and {@link Region} instances
12 *
13 * The #alignTo method aligns this to the target element/region using the specified positions. See #alignTo for a graphical example.
14 *
15 *
16 * var div = Element.select('div.first')
17 *
18 * div.alignTo(Element.select('body') , 'br-br')
19 *
20 * //aligns the div to be in the bottom-right corner of the body
21 *
22 * Other useful methods
23 *
24 * * {@link #alignRegions} - aligns a given source region to a target region
25 * * {@link #COMPUTE_ALIGN_REGION} - given a source region and a target region, and alignment positions, returns a clone of the source region, but aligned to satisfy the given alignments
26 */
27
28
29 /**
30 * Aligns sourceRegion to targetRegion. It modifies the sourceRegion in order to perform the correct alignment.
31 * See #COMPUTE_ALIGN_REGION for details and examples.
32 *
33 * This method calls #COMPUTE_ALIGN_REGION passing to it all its arguments. The #COMPUTE_ALIGN_REGION method returns a region that is properly aligned.
34 * If this returned region position/size differs from sourceRegion, then the sourceRegion is modified to be an exact copy of the aligned region.
35 *
36 * @inheritdoc #COMPUTE_ALIGN_REGION
37 * @return {String} the position used for alignment
38 */
39 Region.alignRegions = function(sourceRegion, targetRegion, positions, config){
40
41 var result = COMPUTE_ALIGN_REGION(sourceRegion, targetRegion, positions, config)
42 var alignedRegion = result.region
43
44 if ( !alignedRegion.equals(sourceRegion) ) {
45 sourceRegion.setRegion(alignedRegion)
46 }
47
48 return result.position
49
50 }
51
52 /**
53 *
54 * The #alignTo method aligns this to the given target region, using the specified alignment position(s).
55 * You can also specify a constrain for the alignment.
56 *
57 * Example
58 *
59 * BIG
60 * ________________________
61 * | _______ |
62 * | | | |
63 * | | A | |
64 * | | | _____ |
65 * | |_______| | | |
66 * | | B | |
67 * | | | |
68 * |_______________|_____|_|
69 *
70 * Assume the *BIG* outside rectangle is our constrain region, and you want to align the *A* rectangle
71 * to the *B* rectangle. Ideally, you'll want their tops to be aligned, and *A* to be placed at the right side of *B*
72 *
73 *
74 * //so we would align them using
75 *
76 * A.alignTo(B, 'tl-tr', { constrain: BIG })
77 *
78 * But this would result in
79 *
80 * BIG
81 * ________________________
82 * | |
83 * | |
84 * | |
85 * | _____ _|_____
86 * | | | . |
87 * | | B | . A |
88 * | | | . |
89 * |_______________|_____|_._____|
90 *
91 *
92 * Which is not what we want. So we specify an array of options to try
93 *
94 * A.alignTo(B, ['tl-tr', 'tr-tl'], { constrain: BIG })
95 *
96 * So by this we mean: try to align A(top,left) with B(top,right) and stick to the BIG constrain. If this is not possible,
97 * try the next option: align A(top,right) with B(top,left)
98 *
99 * So this is what we end up with
100 *
101 * BIG
102 * ________________________
103 * | |
104 * | |
105 * | |
106 * | _______ _____ |
107 * | | | | |
108 * | | A | B | |
109 * | | | | |
110 * |_______|_______|_____|_|
111 *
112 *
113 * Which is a lot better!
114 *
115 * @param {Element/Region} target The target to which to align this alignable.
116 *
117 * @param {String[]/String} positions The positions for the alignment.
118 *
119 * Example:
120 *
121 * 'br-tl'
122 * ['br-tl','br-tr','cx-tc']
123 *
124 * This method will try to align using the first position. But if there is a constrain region, that position might not satisfy the constrain.
125 * If this is the case, the next positions will be tried. If one of them satifies the constrain, it will be used for aligning and it will be returned from this method.
126 *
127 * If no position matches the contrain, the one with the largest intersection of the source region with the constrain will be used, and this alignable will be resized to fit the constrain region.
128 *
129 * @param {Object} config A config object with other configuration for this method
130 *
131 * @param {Array[]/Object[]/Object} config.offset The offset to use for aligning. If more that one offset is specified, then offset at a given index is used with the position at the same index.
132 *
133 * An offset can have the following form:
134 *
135 * [left_offset, top_offset]
136 * {left: left_offset, top: top_offset}
137 * {x: left_offset, y: top_offset}
138 *
139 * You can pass one offset or an array of offsets. In case you pass just one offset,
140 * it cannot have the array form, so you cannot call
141 *
142 * this.alignTo(target, positions, [10, 20])
143 *
144 * If you do, it will not be considered. Instead, please use
145 *
146 * this.alignTo(target, positions, {x: 10, y: 20})
147 *
148 * Or
149 *
150 * this.alignTo(target, positions, [[10, 20]] )
151 *
152 * @param {Boolean/Element/Region} config.constrain If boolean, target will be constrained to the document region, otherwise,
153 * getRegion will be called on this argument to determine the region we need to constrain to.
154 *
155 * @param {Boolean/Object} config.sync Either boolean or an object with {width, height}. If it is boolean,
156 * both width and height will be synced. If directions are specified, will only sync the direction which is specified as true
157 *
158 * @return {String}
159 *
160 */
161 Region.prototype.alignTo = function(target, positions, config){
162
163 config = config || {}
164
165 var sourceRegion = this
166 var targetRegion = Region.from(target)
167
168 var result = COMPUTE_ALIGN_REGION(sourceRegion, targetRegion, positions, config)
169 var resultRegion = result.region
170
171 if (!resultRegion.equalsSize(sourceRegion)){
172 this.setSize(resultRegion.getSize())
173 }
174 if (!resultRegion.equalsPosition(sourceRegion)){
175 this.setPosition(resultRegion.getPosition(), { absolute: !!config.absolute })
176 }
177
178 return result.position
179 }
180
181}
\No newline at end of file