Image zoom bugs solved

This commit is contained in:
Juan 2018-03-01 19:35:32 +01:00
parent e713ce01b4
commit 98388dde85
5 changed files with 66 additions and 65 deletions

View File

@ -1,5 +1,4 @@
describe('Directive zoomImage', () => { describe('Directive zoomImage', () => {
let idContainer = 'zoomImage';
let compile; let compile;
let scope; let scope;
let srcDefault = 'http://default.img.jpg/'; let srcDefault = 'http://default.img.jpg/';
@ -16,7 +15,6 @@ describe('Directive zoomImage', () => {
})); }));
afterEach(() => { afterEach(() => {
findContainer = document.getElementById(idContainer);
if (findContainer) { if (findContainer) {
findContainer.parentNode.removeChild(findContainer); findContainer.parentNode.removeChild(findContainer);
findContainer = undefined; findContainer = undefined;
@ -33,7 +31,8 @@ describe('Directive zoomImage', () => {
it('should create zoom container when click into image', () => { it('should create zoom container when click into image', () => {
let image = getCompiledImage(`<img src="${srcDefault}" zoom-image>`); let image = getCompiledImage(`<img src="${srcDefault}" zoom-image>`);
image[0].click(); image[0].click();
findContainer = document.getElementById(idContainer);
findContainer = document.querySelector('.vn-zoom-image');
expect(findContainer).not.toBeNull(); expect(findContainer).not.toBeNull();
}); });
@ -42,10 +41,10 @@ describe('Directive zoomImage', () => {
let image = getCompiledImage(`<img src="${srcDefault}" zoom-image>`); let image = getCompiledImage(`<img src="${srcDefault}" zoom-image>`);
image[0].click(); image[0].click();
let findOutsideImage = document.querySelector('.zoomImage-background'); findContainer = document.querySelector('.vn-zoom-image');
findOutsideImage.click(); findContainer.click();
findContainer = document.getElementById(idContainer); findContainer = document.querySelector('.vn-zoom-image');
expect(findContainer).toBeNull(); expect(findContainer).toBeNull();
}); });
@ -53,18 +52,20 @@ describe('Directive zoomImage', () => {
it('should create new image, into zoom container, with src as original image src', () => { it('should create new image, into zoom container, with src as original image src', () => {
let image = getCompiledImage(`<img src="${srcDefault}" zoom-image>`); let image = getCompiledImage(`<img src="${srcDefault}" zoom-image>`);
image[0].click(); image[0].click();
findContainer = document.getElementById(idContainer); findContainer = document.querySelector('.vn-zoom-image');
let findNewImage = findContainer.querySelector('.zoomImage-background');
expect(findNewImage.style.backgroundImage).toEqual(`url("${srcDefault}")`); let zoomedImg = findContainer.querySelector('img');
expect(zoomedImg.src).toEqual(srcDefault);
}); });
it('should create new image, into zoom container, with src likes zoomImage value', () => { it('should create new image, into zoom container, with src likes zoomImage value', () => {
let image = getCompiledImage(`<img src="${srcDefault}" zoom-image="${srcZoom}">`); let image = getCompiledImage(`<img src="${srcDefault}" zoom-image="${srcZoom}">`);
image[0].click(); image[0].click();
findContainer = document.getElementById(idContainer); findContainer = document.querySelector('.vn-zoom-image');
let findNewImage = findContainer.querySelector('.zoomImage-background');
expect(findNewImage.style.backgroundImage).toEqual(`url("${srcZoom}")`); let zoomedImg = findContainer.querySelector('img');
expect(zoomedImg.src).toEqual(srcZoom);
}); });
}); });

View File

@ -1,66 +1,56 @@
import ngModule from '../module'; import ngModule from '../module';
export function directive($timeout) { export function directive($timeout) {
let idContainer = 'zoomImage';
let container; let container;
let background; let keyDownHandler;
function createContainers(src) { function createContainers(src) {
if (document.getElementById(idContainer)) {
destroyContainers(); destroyContainers();
}
container = document.createElement('div');
container.id = idContainer;
background = document.createElement('div'); container = document.createElement('div');
background.className = 'zoomImage-background'; container.className = 'vn-zoom-image';
background.style.backgroundImage = `url(${src})`; container.addEventListener('click', destroyContainers);
container.appendChild(background);
let wrapper = document.createElement('div');
container.appendChild(wrapper);
let background = document.createElement('img');
background.src = src;
wrapper.appendChild(background);
document.body.appendChild(container); document.body.appendChild(container);
keyDownHandler = e => onKeyDown(e);
document.addEventListener('keydown', keyDownHandler);
} }
function addListeners() { function onKeyDown(event) {
background.addEventListener('click', destroyContainers);
document.addEventListener('keydown', e => keyDownHandler(e));
}
function removeListeners() {
if (container) {
background.removeEventListener('click', destroyContainers);
document.removeEventListener('keydown', e => keyDownHandler(e));
}
}
function keyDownHandler(event) {
if (event.keyCode === 27) { if (event.keyCode === 27) {
destroyContainers(); destroyContainers();
} }
} }
function destroyContainers() { function destroyContainers() {
if (document.getElementById(idContainer)) { if (!container) return;
removeListeners();
container.parentNode.removeChild(container);
}
container.removeEventListener('click', destroyContainers);
container.parentNode.removeChild(container);
container = undefined; container = undefined;
background = undefined;
document.removeEventListener('keydown', keyDownHandler);
keyDownHandler = undefined;
} }
return { return {
restrict: 'A', restrict: 'A',
priority: 9999,
link: function($scope, $element, $attrs) { link: function($scope, $element, $attrs) {
$element.on('click', function(event) { $element.on('click', function(event) {
event.preventDefault();
let src = $attrs.zoomImage || $attrs.src; let src = $attrs.zoomImage || $attrs.src;
if (src) { if (src) {
createContainers(src); createContainers(src);
addListeners();
} else } else
throw new Error('No image source detected'); throw new Error('No image source detected');
event.preventDefault();
}); });
} }
}; };

View File

@ -1,5 +1,5 @@
import './mdl-override.css'; import './mdl-override.css';
import './mdi-override.css'; import './mdi-override.css';
import './zoom-image.css'; import './zoom-image.scss';
import './fontello-head.css'; import './fontello-head.css';
import './fontello-icons.css'; import './fontello-icons.css';

View File

@ -1,19 +0,0 @@
img[zoom-image]{
cursor: zoom-in;
}
div#zoomImage, div#zoomImage .zoomImage-background {
width: 100%;
height: 100%;
position: fixed;
top: 0;
z-index: 11;
}
div#zoomImage .zoomImage-background{
background-color: rgba(1, 1, 1, 0.6);
cursor: zoom-out;
background-repeat: no-repeat;
background-position: center center;
background-size: auto 98%;
}

View File

@ -0,0 +1,29 @@
img[zoom-image]{
cursor: zoom-in;
}
.vn-zoom-image {
width: 100%;
height: 100%;
position: fixed;
top: 0;
z-index: 11;
background-color: rgba(1, 1, 1, 0.6);
& > div {
display: flex;
justify-content: center;
align-items: center;
width: inherit;
height: inherit;
box-sizing: border-box;
padding: 1em;
& > img {
cursor: zoom-out;
max-height: 100%;
max-width: 100%;
border-radius: .2em;
}
}
}