-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.coffee
58 lines (52 loc) · 1.47 KB
/
script.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# Called when page is loaded
init = ->
if window.verbose then console.log "loaded"
window.zoom = new Zoom "z"
addLinkListeners()
# Add listeners to each link
addLinkListeners = ->
fileTypes = [
".jpg",
".png",
".gif",
".jpeg",
".bmp",
".tiff",
".webp"
]
# Run through every link on the page
for a in document.getElementsByTagName("a")
# Check if link contains a filetype
for type in fileTypes
if a.getAttribute("href").match(type)
# See if the link has any children, and if it's an image
if a.childNodes.length isnt 0 and a.firstChild is a.getElementsByTagName("img")[0]
# Add the listeners (finally!)
a.addEventListener "mouseover", cacheImage, false
a.addEventListener "click", handleZoom, false
# Handles zoom on click
handleZoom = (e) ->
e.preventDefault()
# Check to see if the image is cached
if @loaded
window.zoom.zoom this
else
window.zoom.showLoadingIndicator()
image = document.createElement "img"
image.onload = =>
window.zoom.cache[image.src] = image
window.zoom.hideLoadingIndicator()
@loaded = true
window.zoom.zoom this
image.src = @getAttribute "href"
# Caches image
cacheImage = (e) ->
# Create an image, set its src, you know the rest
image = document.createElement "img"
url = @getAttribute "href"
image.onload = =>
@loaded = true
window.zoom.cache[url] = image
@removeEventListener "mouseover", cacheImage, false
image.src = url
window.addEventListener "DOMContentLoaded", init, false