-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculateBreakpoints2.js
376 lines (354 loc) · 8.62 KB
/
calculateBreakpoints2.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
import deriveViewportFromTable from "./deriveViewportFromTable.js";
// TODO: Validate this against the prior version
// TODO: Add a subroutine which attempts to "save" higher weight candidates by
// removing ones which fit with room to spare but have lower weights in
// order of their weights and seeing if that makes room for the higher
// weight one (this might reintroduce reappearing columns)
export default function* calculateBreakpoints(
/** @type {Column[]} */
tableColumns,
/** @type {Deadspace} */
deadspace
) {
// Clone the array to be able to remove from it without affecting the caller
const columns = [...tableColumns];
do {
// Calculate the total of the remaining columns' ratios
const columnsRatio = columns.reduce((a, c) => a + c.ratio, 0);
// Calculate the size of the table where it fits all the columns at their ratios
const tableSize = Math.max(
...columns.map(c => (columnsRatio / c.ratio) * c.limit)
);
// Calculate the viewport size resulting in the above table size including dead space
const viewportSize = deriveViewportFromTable(tableSize, deadspace);
// Calculate the column sizes at the table size above according to their ratios
const columnSizes = columns.map(c => (c.ratio / columnsRatio) * tableSize);
// Account for the arithmetic artifacts that result in some epsilon over perfect zero
const roundingArtifact = Math.min(
...columns.map((c, i) => columnSizes[i] - c.limit)
);
// Find the column which fits and is at its limit with the lowest weight (the one to go)
const columnToRemove = columns
// Find all columns whose size is equal to their limit accoundint for the artifact
.filter((c, i) => c.limit === columnSizes[i] - roundingArtifact)
// Order the columns which are at their limits on this breakpoint by their weights
// Select the one with the lowest weight for removal (the first one)
.sort((a, b) => b.weight - a.weight)[0];
// Remove the identified column which has the lowest weight of those at their limits
columns.splice(columns.indexOf(columnToRemove), 1);
// Report the table breakpoint, viewport breakpoint and the key of the removed column
// Note that `~~` truncates the decimal part of the number as the viewport size is integral
yield {
version: 2,
tableBreakpoint: ~~tableSize,
viewportBreakpoint: ~~viewportSize,
hideColumnKey: columnToRemove.key
};
} while (columns.length > 0);
}
/*
const columns = [
{ key: 1, ratio: 0.1, limit: 50, weight: 0 },
{ key: 2, ratio: 0.2, limit: 50, weight: 3 },
{ key: 3, ratio: 0.4, limit: 75, weight: 2 },
{ key: 4, ratio: 0.3, limit: 100, weight: 1 }
];
NO DEAD SPACE (deadspace = 0)
1st round:
columns
[ #1, #2, #3, #4 ]
ratioSumTotal
.1 + .2 + .4 + .3 = 1
tableFitSize
max([ (1 / .1) * 50, (1 / .2) * 50, (1 / .4) * 75, (1 / .3) * 100 ])
max([ 10 * 50, 5 * 50, 2.5 * 75, 3.33 * 100 ])
max([ 500, 250, 187.5, 333.33 ])
500
columnSizes
[ (.1 / 1) * 500, (.2 / 1) * 500, (.4 / 1) * 500, (.3 / 1) * 500 ]
[ .1 * 500, .2 * 500, .4 * 500, .3 * 500 ]
[ 50, 100, 200, 150 ]
columnToRemove
filter
[ #1 ]
sort
[ #1 ]
#1
columns.splice
[ #2, #3, #4 ]
yield
{ breakpoint: 500 - 1, columnToRemove: #1 }
2nd round:
columns
[ #2, #3, #4 ]
ratioSumTotal
.2 + .4 + .3 = .9
tableFitSize
max([ (.9 / .2) * 50, (.9 / .4) * 75, (.9 / .3) * 100 ])
max([ 4.5 * 50, 2.25 * 75, 3 * 100 ])
max([ 225, 168.75, 300 ])
300
columnSizes
[ (.2 / .9) * 300, (.4 / .9) * 300, (.3 / .9) * 300 ]
[ .22 * 300, .44 * 300, .33 * 300 ]
[ 66.66, 133.33, 100 ]
columnToRemove
filter
[ #4 ]
sort
[ #4 ]
#4
columns.splice
[ #2, #3 ]
yield
{ breakpoint: 300, columnToRemove: #4 }
3rd round:
columns
[ #2, #3 ]
ratioSumTotal
.2 + .4 = .6
tableFitSize
max([ (.6 / .2) * 50, (.6 / .4) * 75 ])
max([ 150, 150 ])
150
columnSizes
[ (.2 / .6) * 150, (.4 / .6) * 150 ]
[ .33 * 150, .66 * 150 ]
[ 50, 100 ]
columnToRemove
filter
[ #2 ]
sort
[ #2 ]
#2
columns.splice
[ #3 ]
yield
{ breakpoint: 150, columnToRemove: #3 }
4th round:
columns
[ #2 ]
ratioSumTotal
.2
tableFitSize
max([ (.2 / .2) * 50 ])
max([ 50 ])
50
columnSizes
[ (.2 / .2) * 50 ]
[ 50 ]
columnToRemove
filter
[ #2 ]
sort
[ #2 ]
#2
columns.splice
[]
yield
{ breakpoint: 50, columnToRemove: #4 }
STATIC DEAD SPACE (deadspace = 16)
1st round:
columns
[ #1, #2, #3, #4 ]
ratioSumTotal
.1 + .2 + .4 + .3 = 1
tableFitSize
max([ (1 / .1) * 50, (1 / .2) * 50, (1 / .4) * 75, (1 / .3) * 100 ])
max([ 10 * 50, 5 * 50, 2.5 * 75, 3.33 * 100 ])
max([ 500, 250, 187.5, 333.33 ])
500
viewportFitSize
516
columnSizes
[ (.1 / 1) * 500, (.2 / 1) * 500, (.4 / 1) * 500, (.3 / 1) * 500 ]
[ .1 * 500, .2 * 500, .4 * 500, .3 * 500 ]
[ 50, 100, 200, 150 ]
columnToRemove
filter
[ #1 ]
sort
[ #1 ]
#1
columns.splice
[ #2, #3, #4 ]
yield
{ tableBreakpoint: 500, viewportBreakpoint: 516 columnToRemove: #1 }
2nd round:
columns
[ #2, #3, #4 ]
ratioSumTotal
.2 + .4 + .3 = .9
tableFitSize
max([ (.9 / .2) * 50, (.9 / .4) * 75, (.9 / .3) * 100 ])
max([ 4.5 * 50, 2.25 * 75, 3 * 100 ])
max([ 225, 168.75, 300 ])
300
viewportFitSize
316
columnSizes
[ (.2 / .9) * 300, (.4 / .9) * 300, (.3 / .9) * 300 ]
[ .22 * 300, .44 * 300, .33 * 300 ]
[ 66.66, 133.33, 100 ]
columnToRemove
filter
[ #4 ]
sort
[ #4 ]
#4
columns.splice
[ #2, #3 ]
yield
{ tableBreakpoint: 300, viewportBreakpoint: 316, columnToRemove: #4 }
3rd round:
columns
[ #2, #3 ]
ratioSumTotal
.2 + .4 = .6
tableFitSize
max([ (.6 / .2) * 50, (.6 / .4) * 75 ])
max([ 150, 150 ])
150
viewportFitSize
166
columnSizes
[ (.2 / .6) * 150, (.4 / .6) * 150 ]
[ .33 * 150, .66 * 150 ]
[ 50, 100 ]
columnToRemove
filter
[ #2 ]
sort
[ #2 ]
#2
columns.splice
[ #3 ]
yield
{ tableBreakpoint: 150, viewportBreakpoint: 166, columnToRemove: #3 }
4th round:
columns
[ #2 ]
ratioSumTotal
.2
tableFitSize
max([ (.2 / .2) * 50 ])
max([ 50 ])
50
viewportFitSize
66
columnSizes
[ (.2 / .2) * 50 ]
[ 50 ]
columnToRemove
filter
[ #2 ]
sort
[ #2 ]
#2
columns.splice
[]
yield
{ tableBreakpoint: 50, viewportBreakpoint: 66, columnToRemove: #4 }
DYNAMIC DEAD SPACE (deadspace = { 270: 0, _: 16 })
1st round:
columns
[ #1, #2, #3, #4 ]
ratioSumTotal
.1 + .2 + .4 + .3 = 1
tableFitSize
max([ (1 / .1) * 50, (1 / .2) * 50, (1 / .4) * 75, (1 / .3) * 100 ])
max([ 10 * 50, 5 * 50, 2.5 * 75, 3.33 * 100 ])
max([ 500, 250, 187.5, 333.33 ])
500
viewportFitSize
516
columnSizes
[ (.1 / 1) * 500, (.2 / 1) * 500, (.4 / 1) * 500, (.3 / 1) * 500 ]
[ .1 * 500, .2 * 500, .4 * 500, .3 * 500 ]
[ 50, 100, 200, 150 ]
columnToRemove
filter
[ #1 ]
sort
[ #1 ]
#1
columns.splice
[ #2, #3, #4 ]
yield
{ tableBreakpoint: 500, viewportBreakpoint: 516 columnToRemove: #1 }
2nd round:
columns
[ #2, #3, #4 ]
ratioSumTotal
.2 + .4 + .3 = .9
tableFitSize
max([ (.9 / .2) * 50, (.9 / .4) * 75, (.9 / .3) * 100 ])
max([ 4.5 * 50, 2.25 * 75, 3 * 100 ])
max([ 225, 168.75, 300 ])
300
viewportFitSize
316
columnSizes
[ (.2 / .9) * 300, (.4 / .9) * 300, (.3 / .9) * 300 ]
[ .22 * 300, .44 * 300, .33 * 300 ]
[ 66.66, 133.33, 100 ]
columnToRemove
filter
[ #4 ]
sort
[ #4 ]
#4
columns.splice
[ #2, #3 ]
yield
{ tableBreakpoint: 300, viewportBreakpoint: 316, columnToRemove: #4 }
3rd round:
columns
[ #2, #3 ]
ratioSumTotal
.2 + .4 = .6
tableFitSize
max([ (.6 / .2) * 50, (.6 / .4) * 75 ])
max([ 150, 150 ])
150
viewportFitSize
150
columnSizes
[ (.2 / .6) * 150, (.4 / .6) * 150 ]
[ .33 * 150, .66 * 150 ]
[ 50, 100 ]
columnToRemove
filter
[ #2 ]
sort
[ #2 ]
#2
columns.splice
[ #3 ]
yield
{ tableBreakpoint: 150, viewportBreakpoint: 150, columnToRemove: #3 }
4th round:
columns
[ #2 ]
ratioSumTotal
.2
tableFitSize
max([ (.2 / .2) * 50 ])
max([ 50 ])
50
viewportFitSize
50
columnSizes
[ (.2 / .2) * 50 ]
[ 50 ]
columnToRemove
filter
[ #2 ]
sort
[ #2 ]
#2
columns.splice
[]
yield
{ tableBreakpoint: 50, viewportBreakpoint: 50, columnToRemove: #4 }
*/