-
Notifications
You must be signed in to change notification settings - Fork 2
/
Columns.js
200 lines (168 loc) · 5.49 KB
/
Columns.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
const { CUSTOM_PROP_REGEX } = require('./src/collectTidyRuleParams');
/**
* Columns class
* Calculate column and offset values based on processed options.
*
* @param {Object} options The options for the current rule.
*/
class Columns {
/**
* Round the given number to the specified number of decimal places.
*
* @param {Number} toRound The number to round.
* @param {Number} decimalPlaces The number of decimal places to round `toRound` to.
*
* @return {Number}
*/
static roundToPrecision(toRound, decimalPlaces) {
const precision = `1${'0'.repeat(decimalPlaces)}`;
return (0 === toRound) ? 0 : Math.round((toRound + 0.00001) * precision) / precision;
}
/**
* Separate a CSS length value's number from its units.
*
* @param {String} value A CSS length value.
*
* @return {Array}
*/
static splitCssUnit(value) {
return ('string' === typeof value)
? [parseFloat(value), value.replace(/[\d.]/g, '')]
: value;
}
constructor(options = {}) {
this.options = options;
// Collect siteMaxValues to be used in column and offset calc() functions.
this.siteMaxValues = ['100vw'];
if (undefined !== this.options.siteMax) {
this.siteMaxValues.push(this.options.siteMax);
}
this.fullWidthRule = null;
this.nonValues = [undefined, 0];
// Bind class methods.
this.getSharedGap = this.getSharedGap.bind(this);
this.getEdges = this.getEdges.bind(this);
this.getSingleColumn = this.getSingleColumn.bind(this);
this.buildCalcFunction = this.buildCalcFunction.bind(this);
this.spanCalc = this.spanCalc.bind(this);
this.offsetCalc = this.offsetCalc.bind(this);
this.edges = this.getEdges();
this.sharedGap = this.getSharedGap();
/**
* Suppress the `calc` string in the column output.
*
* @type {Boolean}
*/
this.suppressCalc = false;
}
/**
* Calculate the shared gap amount to be removed from each column.
*
* @return {String|Number}
*/
getSharedGap() {
const { gap, columns } = this.options;
if (CUSTOM_PROP_REGEX.test(gap)) {
return `(${gap} / ${columns} * (${columns} - 1))`;
}
if (!this.nonValues.includes(gap)) {
const [value, units] = this.constructor.splitCssUnit(gap);
const sharedGap = (value / columns) * (columns - 1);
return `${this.constructor.roundToPrecision(sharedGap, 4)}${units}`;
}
return 0;
}
/**
* Calculate the total edge spacing.
*
* @return {String|Number}
*/
getEdges() {
const { edge } = this.options;
return this.nonValues.includes(edge) ? 0 : `${edge} * 2`;
}
/**
* Build the column division for the appropriate siteMax and gaps.
*
* @param {String} siteMax The current siteMax size.
*
* @return {String}
*/
getSingleColumn(siteMax) {
const { columns } = this.options;
// 100vw : (100vw - 10px * 2)
const siteMaxSize = this.nonValues.includes(this.edges)
? siteMax
: `(${siteMax} - ${this.edges})`;
// 12 - 9.1667px : 12
const columnReduction = (this.sharedGap)
? `${columns} - ${this.sharedGap}`
: `${columns}`;
return `${siteMaxSize} / ${columnReduction}`;
}
/**
* Complete the calc() function.
*
* @param {String} siteMax The current siteMax size.
* @param {Number} colSpan The number of columns to span.
* @param {Number} gapSpan The number of gaps to span.
*
* @return {String}
*/
buildCalcFunction(siteMax, colSpan, gapSpan) {
const { gap } = this.options;
// The base calc() equation.
let cssCalcEquation = this.getSingleColumn(siteMax);
// Only multiply columns if there are more than one.
if (1 !== colSpan) {
cssCalcEquation = `(${cssCalcEquation}) * ${colSpan}`;
}
/**
* Check for gaps before adding the math for them.
* Only multiply gaps if there are more than one.
*/
if (!this.nonValues.includes(gap) && !this.nonValues.includes(gapSpan)) {
const gapSpanCalc = (1 === gapSpan) ? gap : `${gap} * ${gapSpan}`;
cssCalcEquation = `(${cssCalcEquation}) + ${gapSpanCalc}`;
}
return `${this.suppressCalc ? '' : 'calc'}(${cssCalcEquation})`;
}
/**
* Create the column `calc()` function declaration for each siteMax.
*
* @param {String|Number} colSpan The number of columns to span.
*
* @return {Object}
*/
spanCalc(colSpan) {
const columnSpan = parseFloat(colSpan, 10);
const [fluid, full] = this.siteMaxValues.map((siteMax) => {
/**
* Subtract from columnSpan, then round up to account for fractional columns.
* We are *always* spanning one more column than gap.
* Ensure we maintain the sign of the columnSpan value.
*/
const gapSpan = Math.ceil(columnSpan + (Math.sign(columnSpan) * -1));
return this.buildCalcFunction(siteMax, columnSpan, gapSpan);
});
return { fluid, full };
}
/**
* Create the offset `calc()` function declaration for each siteMax.
*
* @param {String|Number} colSpan The number of columns to offset.
* @param {Boolean} suppressCalc Suppress the `calc` string in the output.
*
* @return {Object}
*/
offsetCalc(colSpan) {
const columnSpan = parseFloat(colSpan, 10);
const [fluid, full] = this.siteMaxValues.map((siteMax) => {
// Round columnSpan down to account for fractional columns.
const gapSpan = Math.floor(columnSpan);
return this.buildCalcFunction(siteMax, columnSpan, gapSpan);
});
return { fluid, full };
}
}
module.exports = Columns;