1+ 'use strict' ;
2+
3+ Object . defineProperty ( exports , '__esModule' , {
4+ value : true
5+ } ) ;
6+
7+ function _defineProperty ( obj , key , value ) { if ( key in obj ) { Object . defineProperty ( obj , key , { value : value , enumerable : true , configurable : true , writable : true } ) ; } else { obj [ key ] = value ; } return obj ; }
8+
9+ var _platform = require ( './platform' ) ;
10+
11+ var _utils = require ( './utils' ) ;
12+
13+ /* Axes System
14+
15+ This allows us to at-will work in a different orientation
16+ without having to manually keep track of knowing if we should be using
17+ x or y positions. */
18+
19+ var axes = {
20+ row : { } ,
21+ column : { }
22+ } ;
23+
24+ axes . row . main = {
25+ start : 'x' ,
26+ end : 'x2' ,
27+ size : 'w'
28+ } ;
29+ axes . row . cross = {
30+ start : 'y' ,
31+ end : 'y2' ,
32+ size : 'h'
33+ } ;
34+ axes . column . main = axes . row . cross ;
35+ axes . column . cross = axes . row . main ;
36+
37+ var types = [ { name : 'side' , values : [ 'start' , 'end' ] } , { name : 'standing' , values : [ 'above' , 'right' , 'below' , 'left' ] } , { name : 'flow' , values : [ 'column' , 'row' ] } ] ;
38+
39+ var validTypeValues = types . reduce ( function ( xs , _ref2 ) {
40+ var values = _ref2 . values ;
41+ return xs . concat ( values ) ;
42+ } , [ ] ) ;
43+
44+ var centerOfSize = function centerOfSize ( flow , axis , size ) {
45+ return size [ axes [ flow ] [ axis ] . size ] / 2 ;
46+ } ;
47+
48+ var centerOfBounds = function centerOfBounds ( flow , axis , bounds ) {
49+ return bounds [ axes [ flow ] [ axis ] . start ] + bounds [ axes [ flow ] [ axis ] . size ] / 2 ;
50+ } ;
51+
52+ var centerOfBoundsFromBounds = function centerOfBoundsFromBounds ( flow , axis , boundsTo , boundsFrom ) {
53+ return centerOfBounds ( flow , axis , boundsTo ) - boundsFrom [ axes [ flow ] [ axis ] . start ] ;
54+ } ;
55+
56+ var place = function place ( flow , axis , align , bounds , size ) {
57+ var axisProps = axes [ flow ] [ axis ] ;
58+ return align === 'center' ? centerOfBounds ( flow , axis , bounds ) - centerOfSize ( flow , axis , size ) : align === 'end' ? bounds [ axisProps . end ] : align === 'start'
59+ /* DOM rendering unfolds leftward. Therefore if the slave is positioned before
60+ the master then the slave`s position must in addition be pulled back
61+ by its [the slave`s] own length. */
62+ ? bounds [ axisProps . start ] - size [ axisProps . size ] : null ;
63+ } ;
64+
65+ /* Element Layout Queries */
66+
67+ var El = { } ;
68+
69+ El . calcBounds = function ( el ) {
70+
71+ if ( el === _platform . window ) {
72+ return {
73+ x : 0 ,
74+ y : 0 ,
75+ x2 : el . innerWidth ,
76+ y2 : el . innerHeight ,
77+ w : el . innerWidth ,
78+ h : el . innerHeight
79+ } ;
80+ }
81+
82+ var b = el . getBoundingClientRect ( ) ;
83+
84+ return {
85+ x : b . left ,
86+ y : b . top ,
87+ x2 : b . right ,
88+ y2 : b . bottom ,
89+ w : b . right - b . left ,
90+ h : b . bottom - b . top
91+ } ;
92+ } ;
93+
94+ El . calcSize = function ( el ) {
95+ return el === _platform . window ? { w : el . innerWidth , h : el . innerHeight } : { w : el . offsetWidth , h : el . offsetHeight } ;
96+ } ;
97+
98+ El . calcScrollSize = function ( el ) {
99+ return el === _platform . window ? {
100+ w : el . scrollX || el . pageXOffset ,
101+ h : el . scrollY || el . pageYOffset
102+ } : { w : el . scrollLeft , h : el . scrollTop } ;
103+ } ;
104+
105+ /* Misc Utilities */
106+
107+ var getPreferenceType = function getPreferenceType ( preference ) {
108+ return types . reduce ( function ( found , type ) {
109+ return found ? found : type . values . indexOf ( preference ) !== - 1 ? type . name : null ;
110+ } , null ) ;
111+ } ;
112+
113+ /* Dimension Fit Checks */
114+
115+ var fitWithinChecker = function fitWithinChecker ( dimension ) {
116+ return function ( domainSize , itemSize ) {
117+ return domainSize [ dimension ] > itemSize [ dimension ] ;
118+ } ;
119+ } ;
120+
121+ var doesWidthFitWithin = fitWithinChecker ( 'w' ) ;
122+ var doesHeightFitWithin = fitWithinChecker ( 'h' ) ;
123+
124+ var doesFitWithin = function doesFitWithin ( domainSize , itemSize ) {
125+ return doesWidthFitWithin ( domainSize , itemSize ) && doesHeightFitWithin ( domainSize , itemSize ) ;
126+ } ;
127+
128+ /* Errors */
129+
130+ var createPreferenceError = function createPreferenceError ( givenValue ) {
131+ return new Error ( 'The given layout placement of "' + givenValue + '" is not a valid choice. Valid choices are: ' + validTypeValues . join ( ' | ' ) + '.' ) ;
132+ } ;
133+
134+ /* Algorithm for picking the best fitting zone for popover. The current technique will loop through all zones picking the last one that fits. If
135+ none fit the last one is selected.
136+
137+ TODO In the case that none fit we should pick the least-not-fitting zone. */
138+
139+ var pickZone = function pickZone ( opts , frameBounds , targetBounds , size ) {
140+ var t = targetBounds ;
141+ var f = frameBounds ;
142+ var zones = [ { side : 'start' , standing : 'above' , flow : 'column' , order : - 1 , w : f . x2 , h : t . y } , { side : 'end' , standing : 'right' , flow : 'row' , order : 1 , w : f . x2 - t . x2 , h : f . y2 } , { side : 'end' , standing : 'below' , flow : 'column' , order : 1 , w : f . x2 , h : f . y2 - t . y2 } , { side : 'start' , standing : 'left' , flow : 'row' , order : - 1 , w : t . x , h : f . y2 } ] ;
143+
144+ var availZones = zones . filter ( function ( zone ) {
145+ return doesFitWithin ( zone , size ) ;
146+ } ) ;
147+
148+ /* If a place is required pick it from the available zones if possible. */
149+
150+ if ( opts . place ) {
151+ var _ret = ( function ( ) {
152+ var type = getPreferenceType ( opts . place ) ;
153+ if ( ! type ) throw createPreferenceError ( opts . place ) ;
154+ var finder = function finder ( z ) {
155+ return z [ type ] === opts . place ;
156+ } ;
157+ return {
158+ v : ( 0 , _utils . find ) ( finder , availZones ) || ( 0 , _utils . find ) ( finder , zones )
159+ } ;
160+ } ) ( ) ;
161+
162+ if ( typeof _ret === 'object' ) return _ret . v ;
163+ }
164+
165+ /* If the preferred side is part of the available zones, use that otherwise
166+ pick the largest available zone. If there are no available zones, pick the
167+ largest zone. TODO: logic that executes picking based on largest option. */
168+
169+ if ( opts . preferPlace ) {
170+ var _ret2 = ( function ( ) {
171+ var preferenceType = getPreferenceType ( opts . preferPlace ) ;
172+ if ( ! preferenceType ) throw createPreferenceError ( opts . preferPlace ) ;
173+ var preferredAvailZones = availZones . filter ( function ( zone ) {
174+ return zone [ preferenceType ] === opts . preferPlace ;
175+ } ) ;
176+ if ( preferredAvailZones . length ) return {
177+ v : preferredAvailZones [ 0 ]
178+ } ;
179+ } ) ( ) ;
180+
181+ if ( typeof _ret2 === 'object' ) return _ret2 . v ;
182+ }
183+
184+ return availZones . length ? availZones [ 0 ] : zones [ 0 ] ;
185+ } ;
186+
187+ /* TODO Document this. */
188+
189+ var calcRelPos = function calcRelPos ( zone , masterBounds , slaveSize ) {
190+ var _ref ;
191+
192+ var _axes$zone$flow = axes [ zone . flow ] ;
193+ var main = _axes$zone$flow . main ;
194+ var cross = _axes$zone$flow . cross ;
195+
196+ /* TODO: The slave is hard-coded to align cross-center with master. */
197+ var crossAlign = 'center' ;
198+ var mainStart = place ( zone . flow , 'main' , zone . side , masterBounds , slaveSize ) ;
199+ var mainSize = slaveSize [ main . size ] ;
200+ var crossStart = place ( zone . flow , 'cross' , crossAlign , masterBounds , slaveSize ) ;
201+ var crossSize = slaveSize [ cross . size ] ;
202+
203+ return ( _ref = { } , _defineProperty ( _ref , main . start , mainStart ) , _defineProperty ( _ref , 'mainLength' , mainSize ) , _defineProperty ( _ref , main . end , mainStart + mainSize ) , _defineProperty ( _ref , cross . start , crossStart ) , _defineProperty ( _ref , 'crossLength' , crossSize ) , _defineProperty ( _ref , cross . end , crossStart + crossSize ) , _ref ) ;
204+ } ;
205+
206+ exports . types = types ;
207+ exports . validTypeValues = validTypeValues ;
208+ exports . calcRelPos = calcRelPos ;
209+ exports . place = place ;
210+ exports . pickZone = pickZone ;
211+ exports . axes = axes ;
212+ exports . centerOfSize = centerOfSize ;
213+ exports . centerOfBounds = centerOfBounds ;
214+ exports . centerOfBoundsFromBounds = centerOfBoundsFromBounds ;
215+ exports . doesFitWithin = doesFitWithin ;
216+ exports . equalCoords = _utils . equalRecords ;
217+ exports . El = El ;
0 commit comments