2018-02-13 12:15:03 +00:00
|
|
|
import ngModule from '../module';
|
2019-10-04 22:16:57 +00:00
|
|
|
import './zoom-image.scss';
|
2018-02-13 12:15:03 +00:00
|
|
|
|
|
|
|
export function directive($timeout) {
|
|
|
|
let container;
|
2018-03-01 18:35:32 +00:00
|
|
|
let keyDownHandler;
|
2018-02-13 12:15:03 +00:00
|
|
|
|
|
|
|
function createContainers(src) {
|
2018-03-01 18:35:32 +00:00
|
|
|
destroyContainers();
|
2018-02-13 12:15:03 +00:00
|
|
|
|
2018-03-01 18:35:32 +00:00
|
|
|
container = document.createElement('div');
|
|
|
|
container.className = 'vn-zoom-image';
|
|
|
|
container.addEventListener('click', destroyContainers);
|
2018-02-13 12:15:03 +00:00
|
|
|
|
2018-03-01 18:35:32 +00:00
|
|
|
let wrapper = document.createElement('div');
|
|
|
|
container.appendChild(wrapper);
|
2018-02-13 12:15:03 +00:00
|
|
|
|
2018-03-01 18:35:32 +00:00
|
|
|
let background = document.createElement('img');
|
|
|
|
background.src = src;
|
|
|
|
wrapper.appendChild(background);
|
2018-02-13 12:15:03 +00:00
|
|
|
|
2018-03-01 18:35:32 +00:00
|
|
|
document.body.appendChild(container);
|
|
|
|
keyDownHandler = e => onKeyDown(e);
|
|
|
|
document.addEventListener('keydown', keyDownHandler);
|
2018-02-13 12:15:03 +00:00
|
|
|
}
|
|
|
|
|
2018-03-01 18:35:32 +00:00
|
|
|
function onKeyDown(event) {
|
2019-10-04 22:16:57 +00:00
|
|
|
if (event.keyCode === 27)
|
2018-02-13 12:15:03 +00:00
|
|
|
destroyContainers();
|
|
|
|
}
|
|
|
|
|
|
|
|
function destroyContainers() {
|
2018-03-01 18:35:32 +00:00
|
|
|
if (!container) return;
|
2018-02-13 12:15:03 +00:00
|
|
|
|
2018-03-01 18:35:32 +00:00
|
|
|
container.removeEventListener('click', destroyContainers);
|
|
|
|
container.parentNode.removeChild(container);
|
2018-02-13 12:15:03 +00:00
|
|
|
container = undefined;
|
2018-03-01 18:35:32 +00:00
|
|
|
|
|
|
|
document.removeEventListener('keydown', keyDownHandler);
|
|
|
|
keyDownHandler = undefined;
|
2018-02-13 12:15:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
restrict: 'A',
|
|
|
|
link: function($scope, $element, $attrs) {
|
|
|
|
$element.on('click', function(event) {
|
2018-03-01 18:35:32 +00:00
|
|
|
event.preventDefault();
|
|
|
|
|
2018-02-13 12:15:03 +00:00
|
|
|
let src = $attrs.zoomImage || $attrs.src;
|
2019-10-04 22:16:57 +00:00
|
|
|
if (src)
|
2018-02-13 12:15:03 +00:00
|
|
|
createContainers(src);
|
2019-10-04 22:16:57 +00:00
|
|
|
else
|
2018-02-13 12:15:03 +00:00
|
|
|
throw new Error('No image source detected');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
directive.$inject = ['$timeout'];
|
|
|
|
|
|
|
|
ngModule.directive('zoomImage', directive);
|