Skip to content

Commit

Permalink
fix: window not defined error
Browse files Browse the repository at this point in the history
  • Loading branch information
sparanoid committed Jun 30, 2021
1 parent 33aa20a commit d4564a0
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 53 deletions.
49 changes: 23 additions & 26 deletions dist/lightense.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ function _defineProperty(obj, key, value) { if (key in obj) { Object.definePrope
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

var Lightense = function Lightense() {
'use strict'; // Save some bytes

var w = window;
var d = document; // default options
'use strict'; // default options

var defaults = {
time: 300,
Expand Down Expand Up @@ -72,7 +69,7 @@ var Lightense = function Lightense() {
throw 'You need to pass an element!';

case 'string':
return d.querySelectorAll(elements);
return document.querySelectorAll(elements);

case 'object':
return elements;
Expand Down Expand Up @@ -101,7 +98,7 @@ var Lightense = function Lightense() {
// If Command (macOS) or Ctrl (Windows) key pressed, stop processing
// and open the image in a new tab
if (event.metaKey || event.ctrlKey) {
return w.open(element.src, '_blank');
return window.open(element.src, '_blank');
}
} // Init instance

Expand All @@ -112,20 +109,20 @@ var Lightense = function Lightense() {
}

function insertCss(styleId, styleContent) {
var head = d.head || d.getElementsByTagName('head')[0]; // Remove existing instance
var head = document.head || document.getElementsByTagName('head')[0]; // Remove existing instance

if (d.getElementById(styleId)) {
d.getElementById(styleId).remove();
if (document.getElementById(styleId)) {
document.getElementById(styleId).remove();
} // Create new instance


var styleEl = d.createElement('style');
var styleEl = document.createElement('style');
styleEl.id = styleId; // Check if content exists

if (styleEl.styleSheet) {
styleEl.styleSheet.cssText = styleContent;
} else {
styleEl.appendChild(d.createTextNode(styleContent));
styleEl.appendChild(document.createTextNode(styleContent));
}

head.appendChild(styleEl);
Expand All @@ -137,12 +134,12 @@ var Lightense = function Lightense() {
}

function createBackdrop() {
if (d.querySelector('.lightense-backdrop') === null) {
config.container = d.createElement('div');
if (document.querySelector('.lightense-backdrop') === null) {
config.container = document.createElement('div');
config.container.className = 'lightense-backdrop';
d.body.appendChild(config.container);
document.body.appendChild(config.container);
} else {
config.container = d.querySelector('.lightense-backdrop');
config.container = document.querySelector('.lightense-backdrop');
}
}

Expand All @@ -151,12 +148,12 @@ var Lightense = function Lightense() {
var naturalWidth = img.width;
var naturalHeight = img.height; // Calc zoom ratio

var scrollTop = w.pageYOffset || d.documentElement.scrollTop || 0;
var scrollLeft = w.pageXOffset || d.documentElement.scrollLeft || 0;
var scrollTop = window.pageYOffset || document.documentElement.scrollTop || 0;
var scrollLeft = window.pageXOffset || document.documentElement.scrollLeft || 0;
var targetImage = config.target.getBoundingClientRect();
var maxScaleFactor = naturalWidth / targetImage.width;
var viewportWidth = w.innerWidth || d.documentElement.clientWidth || 0;
var viewportHeight = w.innerHeight || d.documentElement.clientHeight || 0;
var viewportWidth = window.innerWidth || document.documentElement.clientWidth || 0;
var viewportHeight = window.innerHeight || document.documentElement.clientHeight || 0;
var viewportPadding = config.target.getAttribute('data-lightense-padding') || config.target.getAttribute('data-padding') || config.padding;
var viewportWidthOffset = viewportWidth > viewportPadding ? viewportWidth - viewportPadding : viewportWidth - defaults.padding;
var viewportHeightOffset = viewportHeight > viewportPadding ? viewportHeight - viewportPadding : viewportHeight - defaults.padding;
Expand All @@ -183,7 +180,7 @@ var Lightense = function Lightense() {
function createViewer() {
config.target.classList.add('lightense-open'); // Create wrapper element

config.wrap = d.createElement('div');
config.wrap = document.createElement('div');
config.wrap.className = 'lightense-wrap'; // Apply zoom ratio to target image

setTimeout(function () {
Expand Down Expand Up @@ -233,7 +230,7 @@ var Lightense = function Lightense() {
}

function checkViewer() {
var scrollOffset = Math.abs(config.scrollY - w.scrollY);
var scrollOffset = Math.abs(config.scrollY - window.scrollY);

if (scrollOffset >= config.offset) {
removeViewer();
Expand All @@ -257,7 +254,7 @@ var Lightense = function Lightense() {

invokeCustomHook('beforeShow'); // Save current window scroll position for later use

config.scrollY = w.scrollY;
config.scrollY = window.scrollY;
once(config.target, 'transitionend', function () {
invokeCustomHook('afterShow');
});
Expand All @@ -273,14 +270,14 @@ var Lightense = function Lightense() {
}

function bindEvents() {
w.addEventListener('keyup', onKeyUp, false);
w.addEventListener('scroll', checkViewer, false);
window.addEventListener('keyup', onKeyUp, false);
window.addEventListener('scroll', checkViewer, false);
config.container.addEventListener('click', removeViewer, false);
}

function unbindEvents() {
w.removeEventListener('keyup', onKeyUp, false);
w.removeEventListener('scroll', checkViewer, false);
window.removeEventListener('keyup', onKeyUp, false);
window.removeEventListener('scroll', checkViewer, false);
config.container.removeEventListener('click', removeViewer, false);
} // Exit on excape (esc) key pressed

Expand Down
2 changes: 1 addition & 1 deletion dist/lightense.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d4564a0

Please sign in to comment.