Refine image ratios and widths to prevent wrapping

This commit is contained in:
Timothy Farrell 2018-01-03 15:53:55 -06:00
parent dec9ed3248
commit fa41359cd3
3 changed files with 49 additions and 18 deletions

View File

@ -4,7 +4,7 @@ import {
subscribeToRender,
defineView,
nodeParentWithType,
viewportSize,
availableViewportSize,
defineElement as el
} from '../utils/domvm.js';
@ -42,7 +42,7 @@ export function FocusView(vm, params, key, { appbar }) {
};
}
},
[doc, viewportSize]
[doc, availableViewportSize]
);
function navBack() {

View File

@ -6,7 +6,7 @@ import {
patchRefStyle,
patchNodeStyle,
subscribeToRender,
viewportSize
availableViewportSize
} from '../utils/domvm.js';
import { injectStyle, styled } from '../services/style.js';
import { DEFAULT_TRANSITION, CLICKABLE, IMAGE_MARGIN, CONTENT_MARGIN } from './styles.js';
@ -18,8 +18,9 @@ const OPTIMAL_IMAGE_HEIGHT = 140;
const ROW_HEIGHT_CUTOFF_MODIFIER = 2;
const IMAGE_MARGIN_WIDTH = 2 * IMAGE_MARGIN;
const CONTENT_MARGIN_WIDTH = 2 * CONTENT_MARGIN;
const aspectRatio = (img, margin = 0) => (img.width + margin) / (img.height + margin);
const aspectRatio = img => img.width / img.height;
export function SectionView(vm, params, key, context) {
const { appbar } = context;
@ -27,12 +28,9 @@ export function SectionView(vm, params, key, context) {
const sectionSelectButtonRef = `secSel${key}`;
function calculateSections(photos) {
const { width: vw } = viewportSize();
const availableWidth = vw - CONTENT_MARGIN;
const totalImageRatio = photos.reduce(
(acc, img) => acc + aspectRatio(img, IMAGE_MARGIN_WIDTH),
0
);
const { width: vw } = availableViewportSize();
const availableWidth = vw - CONTENT_MARGIN_WIDTH;
const totalImageRatio = photos.reduce((acc, img) => acc + aspectRatio(img), 0);
const rowCount = Math.ceil(totalImageRatio * OPTIMAL_IMAGE_HEIGHT / availableWidth);
const rowRatios = partition(photos.map(aspectRatio), rowCount);
@ -59,7 +57,7 @@ export function SectionView(vm, params, key, context) {
return result;
}
subscribeToRender(vm, [viewportSize]);
subscribeToRender(vm, [availableViewportSize]);
return function render(vm, params) {
const { selectedIds, selectMode } = params;

View File

@ -1,7 +1,7 @@
// export * from 'domvm/dist/dev/domvm.dev.js';
export * from 'domvm/dist/mini/domvm.mini.js';
import { defineView } from 'domvm/dist/mini/domvm.mini.js';
import { prop, call } from 'frptools';
import { prop, computed, call } from 'frptools';
import { deepAssign } from './conversion.js';
import { error } from '../services/console.js';
@ -52,13 +52,46 @@ export function renderSwitch(renderMap, switchValue) {
}
// Expose viewport size in a subscribable.
const SCROLLBAR_SIZE = 20;
export const viewportSize = prop({}, o => (o ? `${o.width}x${o.height}` : ''));
export const scrollbarSize = prop(0);
export const fullViewportSize = prop(
{ width: window.innerWidth, height: window.innerHeight },
o => (o ? `${o.width}x${o.height}` : '')
);
export const availableViewportSize = computed(
(ss, vs) => ({
width: vs.width - ss,
height: vs.height - ss
}),
[scrollbarSize, fullViewportSize]
);
(function getScrollbarSize() {
const outer = document.createElement('div');
const inner = document.createElement('div');
outer.style.visibility = 'hidden';
outer.style.width = '100px';
outer.style.msOverflowStyle = 'scrollbar'; // needed for WinJS apps
document.body.appendChild(outer);
const widthNoScroll = outer.offsetWidth;
// force scrollbars
outer.style.overflow = 'scroll';
// add innerdiv
inner.style.width = '100%';
outer.appendChild(inner);
const widthWithScroll = inner.offsetWidth;
// remove divs
outer.parentNode.removeChild(outer);
scrollbarSize(widthNoScroll - widthWithScroll);
})();
const extractWindowSize = () =>
viewportSize({
width: window.innerWidth - SCROLLBAR_SIZE,
height: window.innerHeight - SCROLLBAR_SIZE
});
fullViewportSize({ width: window.innerWidth, height: window.innerHeight });
window.addEventListener('resize', extractWindowSize);
// Prime our window size
extractWindowSize();