simplify caching of last hovered/expanded file

This commit is contained in:
ccd0 2014-09-26 22:58:56 -07:00
parent 62a242e100
commit 9fe9b373a2
4 changed files with 30 additions and 37 deletions

View File

@ -813,10 +813,10 @@ span.hide-announcement {
.expanded-image > .post > .file > .fileThumb > img[data-md5] {
display: none;
}
.full-image:not(#ihover) {
.full-image {
display: none;
}
.expanded-image > .post > .file > .fileThumb > .full-image:not(#ihover) {
.expanded-image > .post > .file > .fileThumb > .full-image {
display: inline;
}
.expanded-image {
@ -825,13 +825,13 @@ span.hide-announcement {
.expanding {
opacity: .5;
}
:root.fit-height .full-image:not(#ihover) {
:root.fit-height .full-image {
max-height: 100vh;
}
:root.fit-width .full-image:not(#ihover) {
:root.fit-width .full-image {
max-width: 100%;
}
:root.gecko.fit-width .full-image:not(#ihover) {
:root.gecko.fit-width .full-image {
width: 100%;
}
.fileThumb > .warning {

View File

@ -134,12 +134,15 @@ ImageExpand =
window.scrollBy 0, d.body.clientHeight - oldHeight
if el = file.fullImage
delete file.fullImage
$.off el, 'error', ImageExpand.error
if file.isVideo
el.pause()
for eventName, cb of ImageExpand.videoCB
$.off el, eventName, cb
TrashQueue.add el, post
$.rm el
$.rmClass el, 'full-image'
ImageCommon.cache = el
expand: (post, src) ->
# Do not expand images of hidden/filtered replies, or already expanded pictures.
@ -150,18 +153,23 @@ ImageExpand =
$.addClass thumb, 'expanding'
file.isExpanding = true
el = file.fullImage or $.el (if isVideo then 'video' else 'img'), className: 'full-image'
$.on el, 'error', ImageExpand.error
if file.fullImage
# Expand already-loaded/ing picture.
TrashQueue.remove el
el = file.fullImage
else if ImageCommon.cache?.dataset.fullID is post.fullID
el = file.fullImage = ImageCommon.cache
delete ImageCommon.cache
unless file.isHovered
$.queueTask(-> el.src = el.src) if /\.gif$/.test el.src
el.currentTime = 0 if isVideo and el.readyState >= el.HAVE_METADATA
$.on el, 'error', ImageExpand.error
else
el = file.fullImage = $.el (if isVideo then 'video' else 'img')
el.dataset.fullID = post.fullID
$.on el, 'error', ImageExpand.error
el.src = src or file.URL
$.after thumb, el
file.fullImage = el
el.className = 'full-image'
$.after thumb, el
if isVideo
# add contract link to file info

View File

@ -15,24 +15,24 @@ ImageHover =
{isVideo} = file
return if file.isExpanding or file.isExpanded
file.isHovered = true
if el = file.fullImage
el.id = 'ihover'
TrashQueue.remove el
if ImageCommon.cache?.dataset.fullID is post.fullID
el = ImageCommon.cache
delete ImageCommon.cache
$.queueTask(-> el.src = el.src) if /\.gif$/.test el.src
el.currentTime = 0 if isVideo and el.readyState >= el.HAVE_METADATA
else
file.fullImage = el = $.el (if isVideo then 'video' else 'img'),
className: 'full-image'
id: 'ihover'
el = $.el (if isVideo then 'video' else 'img')
el.dataset.fullID = post.fullID
$.on el, 'error', ImageHover.error
el.src = file.URL
$.after file.thumb, el
el.id = 'ihover'
$.after Header.hover, el
if isVideo
el.loop = true
el.controls = false
el.play() if Conf['Autoplay']
[width, height] = file.dimensions.split('x').map (x) -> +x
{left, right} = file.thumb.getBoundingClientRect()
{left, right} = @getBoundingClientRect()
padding = 16
maxWidth = Math.max left, doc.clientWidth - right
maxHeight = doc.clientHeight - padding
@ -50,9 +50,10 @@ ImageHover =
cb: ->
if isVideo
el.pause()
TrashQueue.add el, post
$.rm el
el.removeAttribute 'id'
el.removeAttribute 'style'
ImageCommon.cache = el
$.queueTask -> delete file.isHovered
error: ->
post = Get.postFromNode @
@ -64,7 +65,5 @@ ImageHover =
@src = URL + if @src is URL then '?' + Date.now() else ''
else
$.rm @
delete post.file.fullImage
else
$.rm @
delete post.file.fullImage

View File

@ -1,14 +0,0 @@
TrashQueue =
init: -> return
add: (video, post) ->
if @killNext and video isnt @killNext
delete @killNextPost?.file?.fullImage
$.rm @killNext
@killNext = video
@killNextPost = post
remove: (video) ->
if video is @killNext
delete @killNext