Add focus previous/next overlay buttons.

This commit is contained in:
Timothy Farrell 2017-12-29 22:27:27 -06:00
parent fa88b36c76
commit b1f4bb0e52
3 changed files with 99 additions and 27 deletions

View File

@ -7,6 +7,7 @@ import {
defineElement as el defineElement as el
} from '../utils/domvm.js'; } from '../utils/domvm.js';
import { router } from '../services/router.js';
import { ImageType } from '../data/image.js'; import { ImageType } from '../data/image.js';
import { pouchDocHash, pick } from '../utils/conversion.js'; import { pouchDocHash, pick } from '../utils/conversion.js';
import { AttachmentImageView } from './components/attachmentImage.js'; import { AttachmentImageView } from './components/attachmentImage.js';
@ -17,15 +18,16 @@ import { error } from '../services/console.js';
import { CLICKABLE } from './styles.js'; import { CLICKABLE } from './styles.js';
export function FocusView(vm, params, key, { appbar }) { export function FocusView(vm, params, key, { appbar }) {
const id = params.vars.id; const id = prop();
const doc = prop(null, pouchDocHash);
const { body } = document; const { body } = document;
const windowSize = prop({}, o => (o ? `${o.width}x${o.height}` : '')); const windowSize = prop({}, o => (o ? `${o.width}x${o.height}` : ''));
const nextLink = prop();
const prevLink = prop();
const extractWindowSize = () => const extractWindowSize = () =>
windowSize({ width: window.innerWidth, height: window.innerHeight }); windowSize({ width: window.innerWidth, height: window.innerHeight });
const doc = container({}, pouchDocHash);
const imageStyle = computed( const imageStyle = computed(
({ width: iw, height: ih }, { width: vw, height: vh }) => { ({ width: iw, height: ih }, { width: vw, height: vh }) => {
const imageRatio = iw / ih; const imageRatio = iw / ih;
@ -47,7 +49,7 @@ export function FocusView(vm, params, key, { appbar }) {
); );
async function goBack() { async function goBack() {
history.go(-1); router.goto('home');
} }
function navBack() { function navBack() {
@ -56,7 +58,7 @@ export function FocusView(vm, params, key, { appbar }) {
} }
async function clickTrash() { async function clickTrash() {
await ImageType.delete(id); await ImageType.delete(id());
navBack(); navBack();
} }
@ -76,43 +78,98 @@ export function FocusView(vm, params, key, { appbar }) {
]; ];
} }
// Set the appbar title.
appbar.pushState({ title: '', buttons: renderAppBarButtons, style: { position: 'fixed' } });
// Prime our window size // Prime our window size
extractWindowSize(); extractWindowSize();
window.addEventListener('resize', extractWindowSize); window.addEventListener('resize', extractWindowSize);
// Set the appbar title.
appbar.pushState({ title: '', buttons: renderAppBarButtons, style: { position: 'fixed' } });
// Look for our image and set it.
ImageType.find(id)
.then(d => {
doc.src = d.sizes.full || d.sizes.preview || d.sizes.thumbnail;
doc.width = d.width;
doc.height = d.height;
doc._id = d._id;
})
.catch(error);
// Subscribe to our changables. // Subscribe to our changables.
subscribeToRender( subscribeToRender(
vm, vm,
[doc, imageStyle], [doc, imageStyle, nextLink, prevLink],
[appbar.subscribe(goBack), () => window.removeEventListener('resize', extractWindowSize)] [
appbar.subscribe(goBack),
// Keep up with the window resizing
() => window.removeEventListener('resize', extractWindowSize),
// Look for our image and set it.
id.subscribe(async _id => {
if (!_id) {
return;
}
const image = await ImageType.find(_id);
doc(image);
Promise.all([
ImageType.find({ _id: { $lt: _id } }, { limit: 2, sort: [{ _id: 'desc' }] }),
ImageType.find({ _id: { $gt: _id } }, { limit: 2 })
]).then(([prev, next]) => {
nextLink(next.length ? router.href('focus', { id: next[0]._id }) : null);
prevLink(prev.length ? router.href('focus', { id: prev[0]._id }) : null);
});
})
]
); );
// Watch for focus changes
vm.config({ hooks: { willUpdate: (vm, { vars }) => id(vars.id) } });
// Start navigation
id(params.vars.id);
return function() { return function() {
if (!doc._id) { const _id = doc() && doc()._id;
if (!_id) {
return Overlay('Loading...'); return Overlay('Loading...');
} }
return focusContainer([ return focusContainer([
AttachmentImageView({ prevLink()
src: doc._id ? doc.src : null, ? prevClickZone({ href: prevLink() }, [
style: imageStyle() Icon({
name: 'chevron_left',
size: 0.75
}) })
])
: null,
AttachmentImageView({
src: _id ? doc().sizes.full : null,
style: imageStyle()
}),
nextLink()
? nextClickZone({ href: nextLink() }, [
Icon({
name: 'chevron_right',
size: 0.75
})
])
: null
]); ]);
}; };
} }
const WIDE = injectStyle({ width: '100%' });
const TALL = injectStyle({ height: '100%' });
const CSS_CLICK_ZONE = {
position: 'absolute',
width: '33%',
height: '70%',
display: 'flex',
alignItems: 'center',
padding: '2em',
top: '15%',
transition: 'opacity .13s cubic-bezier(0.0,0.0,0.2,1)',
opacity: 0,
cursor: 'pointer',
':hover': {
opacity: 1
}
};
const trashButtonContainer = styled( const trashButtonContainer = styled(
{ {
marginRight: '1em' marginRight: '1em'
@ -128,5 +185,20 @@ const focusContainer = styled({
overflow: 'hidden' overflow: 'hidden'
}); });
const WIDE = injectStyle({ width: '100%' }); const nextClickZone = styled(
const TALL = injectStyle({ height: '100%' }); 'a',
{
right: '0px',
justifyContent: 'flex-end'
},
CSS_CLICK_ZONE
);
const prevClickZone = styled(
'a',
{
left: '0px',
justifyContent: 'flex-start'
},
CSS_CLICK_ZONE
);

View File

@ -44,7 +44,7 @@ export function GalleryView(vm) {
renderSwitch( renderSwitch(
{ {
photos: [AllImagesView, {}, 'allImages', context], photos: [AllImagesView, {}, 'allImages', context],
focus: [FocusView, routeParams(), 'focus', context] focus: [FocusView, routeParams(), `focus_${routeParams() && routeParams().id}`, context]
}, },
routeName() routeName()
) )

View File

@ -13,7 +13,7 @@ export function pouchDocArrayComparator(a, b) {
} }
export function isObject(obj) { export function isObject(obj) {
return typeof obj === 'object' && !Array.isArray(obj); return typeof obj === 'object' && !Array.isArray(obj) && obj !== null;
} }
export function isString(obj) { export function isString(obj) {