Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed IE10/11 support (#34) #35

Merged
merged 5 commits into from
Mar 4, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/demo/demo.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
(function() {
// Show placeholders for paragraphs
const paragraphs = Array.from(document.querySelectorAll('p.placeholder'))
var paragraphs = Array.apply(null, document.querySelectorAll('p.placeholder'))
paragraphs.forEach(function(p) {
var content = p.textContent
.split(' ')
.filter(function(text) {
return text.length > 4
})
.map(function(text) {
return `<span class="placeholder__word">${text}</span>`
return '<span class="placeholder__word">' + text + '</span>'
})
.join(' ')

Expand Down
12 changes: 8 additions & 4 deletions examples/demo/preview/medium-zoom.min.js

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

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@
"/examples/"
],
"globals": [
"NodeList",
"CustomEvent",
"Event",
"HTMLCollection",
"requestAnimationFrame",
"Event"
"NodeList",
"requestAnimationFrame"
]
},
"files": [
Expand Down
38 changes: 24 additions & 14 deletions src/medium-zoom.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const KEY_ESC = 27
const KEY_Q = 81
const CANCEL_KEYS = [KEY_ESC, KEY_Q]

const isSupported = img => SUPPORTED_FORMATS.includes(img.tagName)
const isSupported = img => SUPPORTED_FORMATS.indexOf(img.tagName) > -1

const isScaled = img => img.naturalWidth !== img.width

Expand Down Expand Up @@ -42,16 +42,16 @@ const mediumZoom = (
return Array.isArray(selector)
? selector.filter(isSupported)
: isListOrCollection(selector)
? [...selector].filter(isSupported)
? Array.apply(null, selector).filter(isSupported)
: isNode(selector)
? [selector].filter(isSupported)
: typeof selector === 'string'
? [...document.querySelectorAll(selector)].filter(isSupported)
: [
...document.querySelectorAll(
SUPPORTED_FORMATS.map(attr => attr.toLowerCase()).join(',')
)
].filter(isScaled)
? Array.apply(null, document.querySelectorAll(selector)).filter(isSupported)
: Array.apply(null,
document.querySelectorAll(
SUPPORTED_FORMATS.map(attr => attr.toLowerCase()).join(',')
)
).filter(isScaled)
} catch (err) {
throw new TypeError(
'The provided selector is invalid.\n' +
Expand Down Expand Up @@ -97,7 +97,7 @@ const mediumZoom = (
const zoom = () => {
if (!target.original) return

target.original.dispatchEvent(new Event('show'))
target.original.dispatchEvent(new ZoomEvent('show'))

scrollTop =
window.pageYOffset ||
Expand Down Expand Up @@ -164,7 +164,7 @@ const mediumZoom = (
const doZoomOut = () => {
if (isAnimating || !target.original) return

target.original.dispatchEvent(new Event('hide'))
target.original.dispatchEvent(new ZoomEvent('hide'))

isAnimating = true
document.body.classList.remove('medium-zoom--open')
Expand Down Expand Up @@ -225,7 +225,7 @@ const mediumZoom = (

const detach = () => {
const doDetach = () => {
const event = new Event('detach')
const event = new ZoomEvent('detach')

images.forEach(image => {
image.classList.remove('medium-zoom-image')
Expand Down Expand Up @@ -275,7 +275,7 @@ const mediumZoom = (
isAnimating = false
target.zoomed.removeEventListener('transitionend', onZoomEnd)

target.original.dispatchEvent(new Event('shown'))
target.original.dispatchEvent(new ZoomEvent('shown'))
}

const onZoomOutEnd = () => {
Expand All @@ -291,7 +291,7 @@ const mediumZoom = (
isAnimating = false
target.zoomed.removeEventListener('transitionend', onZoomOutEnd)

target.original.dispatchEvent(new Event('hidden'))
target.original.dispatchEvent(new ZoomEvent('hidden'))

target.original = null
target.zoomed = null
Expand All @@ -314,7 +314,7 @@ const mediumZoom = (
}

const onDismiss = event => {
if (CANCEL_KEYS.includes(event.keyCode || event.which)) {
if (CANCEL_KEYS.indexOf(event.keyCode || event.which) > -1) {
zoomOut()
}
}
Expand Down Expand Up @@ -390,6 +390,16 @@ const mediumZoom = (
target.zoomedHd && (target.zoomedHd.style.transform = transform)
}

const ZoomEvent = (event, params = { bubbles: false, cancelable: false, detail: undefined }) => {
if (typeof window.CustomEvent === 'function') {
return new CustomEvent(event, params)
} else {
const evt = document.createEvent('CustomEvent')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you name this variable something like customEvent?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep. :)

Do you prefer customEvent or createCustomEvent? The latter feels like a better match to the existing [verb][Noun] method names. Happy to go whichever way you prefer.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

customEvent makes more sense to me since it's an object, not a method. Perhaps customEventFactory is appropriate here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

evt.initCustomEvent(event, params.bubbles, params.cancelable, params.detail)
return evt
}
}

const options = {
margin,
background,
Expand Down