ImageLoader: refactoring, little fixes

This commit is contained in:
ccd0 2014-09-07 17:02:03 -07:00
parent d0247840d1
commit d1fbe50319
2 changed files with 58 additions and 46 deletions

View File

@ -195,6 +195,12 @@ $.off = (el, events, handler) ->
el.removeEventListener event, handler, false el.removeEventListener event, handler, false
return return
$.one = (el, events, handler) ->
handler2 = (e) ->
$.off el, events, handler2
handler.call @, e
$.on el, events, handler2
$.event = (event, detail, root=d) -> $.event = (event, detail, root=d) ->
<% if (type === 'userscript') { %> <% if (type === 'userscript') { %>
if detail? and typeof cloneInto is 'function' if detail? and typeof cloneInto is 'function'

View File

@ -33,8 +33,7 @@ ImageLoader =
if @isClone and @file.videoThumb if @isClone and @file.videoThumb
ImageLoader.play thumb ImageLoader.play thumb
return if @isClone or @isHidden or @thread.isHidden or !(isImage or isVideo) return if @isClone or @isHidden or @thread.isHidden or !(isImage or isVideo)
{style} = thumb type = if (match = URL.match(/\.([^.]+)$/)[1].toUpperCase()) is 'JPEG' then 'JPG' else match
type = if (match = URL.match(/\.([^.]+)$/)[1].toUpperCase()) is 'JPEG' then 'JPG' else match
replace = Conf["Replace #{type}"] and !/spoiler/.test thumb.src replace = Conf["Replace #{type}"] and !/spoiler/.test thumb.src
prefetch = (Conf['prefetch'] and g.VIEW is 'thread') or force prefetch = (Conf['prefetch'] and g.VIEW is 'thread') or force
return unless replace or prefetch return unless replace or prefetch
@ -42,60 +41,67 @@ ImageLoader =
if replace if replace
if @file.isSpoiler if @file.isSpoiler
# Revealed spoilers do not have height/width set, this fixes the image's dimensions. # Revealed spoilers do not have height/width set, this fixes the image's dimensions.
style.maxHeight = style.maxWidth = if @isReply then '125px' else '250px' thumb.style.maxHeight = thumb.style.maxWidth = if @isReply then '125px' else '250px'
cb = => if isImage
$.off el, 'load loadeddata', cb $.on el, 'load', => ImageLoader.replaceImage @
ImageLoader.replace @, el else
$.on el, 'load loadeddata', cb $.one el, 'loadeddata', => ImageLoader.replaceVideo @, el
ImageLoader.queue.push [el, URL] ImageLoader.queueDownload el, URL
ImageLoader.next() unless ImageLoader.busy
busy: false queueDownload: do ->
queue: [] busy = false
items = []
loadend: -> load = (el, url) ->
$.off @, 'load loadeddata error', ImageLoader.loadend next = ->
ImageLoader.busy = false $.off el, 'load loadeddata error', next
ImageLoader.next() clearTimeout timeoutID
busy = false
next: -> if item = items.shift()
return if ImageLoader.busy [el, url] = item
if item = ImageLoader.queue.shift() load el, url
[el, url] = item $.on el, 'load loadeddata error', next
$.on el, 'load loadeddata error', ImageLoader.loadend timeoutID = setTimeout next, $.SECOND
setTimeout (-> ImageLoader.loadend.call el), $.SECOND
el.src = url el.src = url
ImageLoader.busy = true busy = true
(el, url) ->
if busy
items.push [el, url]
else
load el, url
replace: (post, el) -> replaceImage: (post) ->
# Replace thumbnail in this post and any clones added before the file was loaded.
for post in [post, post.clones...]
post.file.thumb.src = post.file.URL
return
replaceVideo: (post, video) ->
{file} = post {file} = post
{isVideo, thumb} = file {thumb} = file
{style} = thumb {style} = thumb
if !post.isClone if !post.isClone then for clone in post.clones
# Replace the thumbnail in clones added before the file was loaded. # Replace the thumbnail in clones added before the file was loaded.
for clone in post.clones video2 = $.el 'video'
ImageLoader.replace clone, el.cloneNode true $.one video2, 'loadeddata', => ImageLoader.replaceVideo clone, video2
if isVideo video2.src = video.src
el.textContent = thumb.alt video.textContent = thumb.alt
el.dataset.md5 = thumb.dataset.md5 video.dataset.md5 = thumb.dataset.md5
el.style.height = style.height video.style.height = style.height
el.style.width = style.width video.style.width = style.width
el.style.maxHeight = style.maxHeight video.style.maxHeight = style.maxHeight
el.style.maxWidth = style.maxWidth video.style.maxWidth = style.maxWidth
el.loop = true video.loop = true
el.className = thumb.className video.className = thumb.className
$.on el, 'mouseover', ImageHover.mouseover if Conf['Image Hover'] $.on video, 'mouseover', ImageHover.mouseover if Conf['Image Hover']
$.replace thumb, el $.replace thumb, video
file.videoThumb = true file.thumb = video
file.thumb = el file.videoThumb = true
ImageLoader.play el unless post.isFetchedQuote ImageLoader.play video unless post.isFetchedQuote
else
thumb.src = file.URL
play: (video) -> play: (video) ->
if Conf['Autoplay'] if Conf['Autoplay']
$.asap (-> doc.contains video), -> $.asap (-> doc.contains video), ->
if !d.hidden and Header.isNodeVisible video if Header.isNodeVisible video
video.play() video.play()
toggle: -> toggle: ->