1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.equalCoords = exports.doesFitWithin = exports.centerOfBoundsFromBounds = exports.centerOfBounds = exports.centerOfSize = exports.axes = exports.pickZone = exports.place = exports.calcRelPos = exports.validTypeValues = exports.types = exports.El = undefined;
|
7 |
|
8 | var _platform = require("./platform");
|
9 |
|
10 | var _utils = require("./utils");
|
11 |
|
12 | 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; }
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | var axes = {
|
21 | row: {},
|
22 | column: {}
|
23 | };
|
24 |
|
25 | axes.row.main = {
|
26 | start: "x",
|
27 | end: "x2",
|
28 | size: "w"
|
29 | };
|
30 | axes.row.cross = {
|
31 | start: "y",
|
32 | end: "y2",
|
33 | size: "h"
|
34 | };
|
35 | axes.column.main = axes.row.cross;
|
36 | axes.column.cross = axes.row.main;
|
37 |
|
38 | var types = [{ name: "side", values: ["start", "end"] }, { name: "standing", values: ["above", "right", "below", "left"] }, { name: "flow", values: ["column", "row"] }];
|
39 |
|
40 | var validTypeValues = types.reduce(function (xs, _ref) {
|
41 | var values = _ref.values;
|
42 | return xs.concat(values);
|
43 | }, []);
|
44 |
|
45 | var centerOfSize = function centerOfSize(flow, axis, size) {
|
46 | return size[axes[flow][axis].size] / 2;
|
47 | };
|
48 |
|
49 | var centerOfBounds = function centerOfBounds(flow, axis, bounds) {
|
50 | return bounds[axes[flow][axis].start] + bounds[axes[flow][axis].size] / 2;
|
51 | };
|
52 |
|
53 | var centerOfBoundsFromBounds = function centerOfBoundsFromBounds(flow, axis, boundsTo, boundsFrom) {
|
54 | return centerOfBounds(flow, axis, boundsTo) - boundsFrom[axes[flow][axis].start];
|
55 | };
|
56 |
|
57 | var place = function place(flow, axis, align, bounds, size) {
|
58 | var axisProps = axes[flow][axis];
|
59 | return align === "center" ? centerOfBounds(flow, axis, bounds) - centerOfSize(flow, axis, size) : align === "end" ? bounds[axisProps.end] : align === "start"
|
60 | |
61 |
|
62 |
|
63 | ? bounds[axisProps.start] - size[axisProps.size] : null;
|
64 | };
|
65 |
|
66 |
|
67 |
|
68 | var El = {};
|
69 |
|
70 | El.calcBounds = function (el) {
|
71 |
|
72 | if (el === _platform.window) {
|
73 | return {
|
74 | x: 0,
|
75 | y: 0,
|
76 | x2: el.innerWidth,
|
77 | y2: el.innerHeight,
|
78 | w: el.innerWidth,
|
79 | h: el.innerHeight
|
80 | };
|
81 | }
|
82 |
|
83 | var b = el.getBoundingClientRect();
|
84 |
|
85 | return {
|
86 | x: b.left,
|
87 | y: b.top,
|
88 | x2: b.right,
|
89 | y2: b.bottom,
|
90 | w: b.right - b.left,
|
91 | h: b.bottom - b.top
|
92 | };
|
93 | };
|
94 |
|
95 | El.calcSize = function (el) {
|
96 | return el === _platform.window ? { w: el.innerWidth, h: el.innerHeight } : { w: el.offsetWidth, h: el.offsetHeight };
|
97 | };
|
98 |
|
99 | El.calcScrollSize = function (el) {
|
100 | return el === _platform.window ? {
|
101 | w: el.scrollX || el.pageXOffset,
|
102 | h: el.scrollY || el.pageYOffset
|
103 | } : { w: el.scrollLeft, h: el.scrollTop };
|
104 | };
|
105 |
|
106 |
|
107 |
|
108 | var getPreferenceType = function getPreferenceType(preference) {
|
109 | return types.reduce(function (found, type) {
|
110 | return found ? found : type.values.indexOf(preference) !== -1 ? type.name : null;
|
111 | }, null);
|
112 | };
|
113 |
|
114 |
|
115 |
|
116 | var fitWithinChecker = function fitWithinChecker(dimension) {
|
117 | return function (domainSize, itemSize) {
|
118 | return domainSize[dimension] >= itemSize[dimension];
|
119 | };
|
120 | };
|
121 |
|
122 | var doesWidthFitWithin = fitWithinChecker("w");
|
123 | var doesHeightFitWithin = fitWithinChecker("h");
|
124 |
|
125 | var doesFitWithin = function doesFitWithin(domainSize, itemSize) {
|
126 | return doesWidthFitWithin(domainSize, itemSize) && doesHeightFitWithin(domainSize, itemSize);
|
127 | };
|
128 |
|
129 |
|
130 |
|
131 | var createPreferenceError = function createPreferenceError(givenValue) {
|
132 | return new Error("The given layout placement of \"" + givenValue + "\" is not a valid choice. Valid choices are: " + validTypeValues.join(" | ") + ".");
|
133 | };
|
134 |
|
135 |
|
136 |
|
137 |
|
138 | var pickZone = function pickZone(opts, frameBounds, targetBounds, size) {
|
139 | var t = targetBounds;
|
140 | var f = frameBounds;
|
141 | 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 }];
|
142 |
|
143 | |
144 |
|
145 |
|
146 |
|
147 | zones.forEach(function (z) {
|
148 |
|
149 |
|
150 | z.cutOff = -Math.max(0, Math.min(z.w, size.w)) * Math.max(0, Math.min(z.h, size.h));
|
151 | });
|
152 | zones.sort(function (a, b) {
|
153 | return a.cutOff - b.cutOff;
|
154 | });
|
155 |
|
156 | var availZones = zones.filter(function (zone) {
|
157 | return doesFitWithin(zone, size);
|
158 | });
|
159 |
|
160 |
|
161 |
|
162 | if (opts.place) {
|
163 | var type = getPreferenceType(opts.place);
|
164 | if (!type) throw createPreferenceError(opts.place);
|
165 | var finder = function finder(z) {
|
166 | return z[type] === opts.place;
|
167 | };
|
168 | return (0, _utils.find)(finder, availZones) || (0, _utils.find)(finder, zones);
|
169 | }
|
170 |
|
171 | |
172 |
|
173 |
|
174 |
|
175 | if (opts.preferPlace) {
|
176 | var preferenceType = getPreferenceType(opts.preferPlace);
|
177 | if (!preferenceType) throw createPreferenceError(opts.preferPlace);
|
178 |
|
179 |
|
180 | var preferredAvailZones = availZones.filter(function (zone) {
|
181 | return zone[preferenceType] === opts.preferPlace;
|
182 | });
|
183 | if (preferredAvailZones.length) return preferredAvailZones[0];
|
184 |
|
185 |
|
186 |
|
187 | var preferredZones = zones.filter(function (zone) {
|
188 | return zone[preferenceType] === opts.preferPlace;
|
189 | });
|
190 | if (!availZones.length && preferredZones.length) return preferredZones[0];
|
191 | }
|
192 |
|
193 |
|
194 | return availZones.length ? availZones[0] : zones[0];
|
195 | };
|
196 |
|
197 |
|
198 |
|
199 | var calcRelPos = function calcRelPos(zone, masterBounds, slaveSize) {
|
200 | var _ref2;
|
201 |
|
202 | var _axes$zone$flow = axes[zone.flow],
|
203 | main = _axes$zone$flow.main,
|
204 | cross = _axes$zone$flow.cross;
|
205 |
|
206 |
|
207 | var crossAlign = "center";
|
208 | var mainStart = place(zone.flow, "main", zone.side, masterBounds, slaveSize);
|
209 | var mainSize = slaveSize[main.size];
|
210 | var crossStart = place(zone.flow, "cross", crossAlign, masterBounds, slaveSize);
|
211 | var crossSize = slaveSize[cross.size];
|
212 |
|
213 | return _ref2 = {}, _defineProperty(_ref2, main.start, mainStart), _defineProperty(_ref2, "mainLength", mainSize), _defineProperty(_ref2, main.end, mainStart + mainSize), _defineProperty(_ref2, cross.start, crossStart), _defineProperty(_ref2, "crossLength", crossSize), _defineProperty(_ref2, cross.end, crossStart + crossSize), _ref2;
|
214 | };
|
215 |
|
216 | exports.default = {
|
217 | El: El,
|
218 | types: types,
|
219 | validTypeValues: validTypeValues,
|
220 | calcRelPos: calcRelPos,
|
221 | place: place,
|
222 | pickZone: pickZone,
|
223 | axes: axes,
|
224 | centerOfSize: centerOfSize,
|
225 | centerOfBounds: centerOfBounds,
|
226 | centerOfBoundsFromBounds: centerOfBoundsFromBounds,
|
227 | doesFitWithin: doesFitWithin,
|
228 | equalCoords: _utils.equalRecords
|
229 | };
|
230 | exports.El = El;
|
231 | exports.types = types;
|
232 | exports.validTypeValues = validTypeValues;
|
233 | exports.calcRelPos = calcRelPos;
|
234 | exports.place = place;
|
235 | exports.pickZone = pickZone;
|
236 | exports.axes = axes;
|
237 | exports.centerOfSize = centerOfSize;
|
238 | exports.centerOfBounds = centerOfBounds;
|
239 | exports.centerOfBoundsFromBounds = centerOfBoundsFromBounds;
|
240 | exports.doesFitWithin = doesFitWithin;
|
241 | exports.equalCoords = _utils.equalRecords; |
\ | No newline at end of file |