forked from shprink/BttrLazyLoading
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BttrLazyLoading.coffee
301 lines (246 loc) · 8.73 KB
/
BttrLazyLoading.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
"use strict"
$ = jQuery
class BttrLazyLoading
@dpr = 1
constructor: (img, options = {}) ->
@$img = $(img)
@loaded = false
@loading = false
defaultOptions = $.extend true, {}, $.bttrlazyloading.constructor.options
@options = $.extend true, defaultOptions, options
@ranges = $.bttrlazyloading.constructor.ranges
@$container = $(@options.container)
@constructor.dpr = window.devicePixelRatio if typeof window.devicePixelRatio == 'number'
@whiteList = ['lg', 'md', 'sm', 'xs']
@blackList = []
_setOptionsFromData.call @
@$wrapper = $ '<span class="bttrlazyloading-wrapper"></span>'
@$wrapper.addClass @options.wrapperClasses if @options.wrapperClasses and typeof @options.wrapperClasses is 'string'
@$img.before @$wrapper
# The easier way to simulate a responsive image is to use canvas
@$clone = $ '<canvas class="bttrlazyloading-clone"></canvas>'
_updateCanvasSize.call @
@$wrapper.append @$clone
@$img.hide()
@$wrapper.append @$img
@$wrapper.css 'background-color', @options.backgroundcolor if @options.backgroundcolor
_setupEvents.call @, 'on'
setTimeout () =>
_update.call @
, 100
###
Private Functions
###
_updateCanvasSize = () ->
imgObject = _getImgObject.call @
@$clone.attr 'width', imgObject.width
@$clone.attr 'height', imgObject.height
_setOptionsFromData = () ->
# Set options based on Jquery Data available
$.each @$img.data(), (i, v) =>
if v
# making sure we only use bttrlazyloading data
if i.indexOf('bttrlazyloading') isnt 0
false
i = i.replace('bttrlazyloading', '').replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase().split '-'
if i.length > 1
@options[i[0]][i[1]] = v if typeof @options[i[0]][i[1]] isnt 'undefined'
else
if typeof v is 'object'
$.extend(@options[i[0]], v)
else
@options[i[0]] = v if typeof @options[i[0]] isnt 'undefined'
_setupEvents = (onOrOff) ->
onLoad = () =>
@$clone.hide()
@$img.show()
@$img.addClass 'bttrlazyloading-loaded'
@$img.addClass 'animated ' + @options.animation if @options.animation
@loaded = @$img.attr 'src'
@$img.trigger 'bttrlazyloading.afterLoad'
@$img[onOrOff] 'load', onLoad
onBttrLoad = (e) =>
if !@loading
@loading = true
imgObject = _getImgObject.call @
if !@loaded
@$wrapper.css 'background-image', "url('" + @options.placeholder + "')"
else
@$img.removeClass 'bttrlazyloading-loaded'
@$img.removeClass 'animated ' + @options.animation if @options.animation
@$img.removeAttr 'src'
@$img.hide()
@$clone.attr 'width', imgObject.width
@$clone.attr 'height', imgObject.height
@$clone.show()
setTimeout () =>
@$img.trigger 'bttrlazyloading.beforeLoad'
@$img.data 'bttrlazyloading.range', imgObject.range
@$img.attr 'src', _getImageSrc.call @, imgObject.src, imgObject.range
@loading = false
, @options.delay
@$img[onOrOff] 'bttrlazyloading.load', onBttrLoad
onError = (e) =>
src = @$img.attr 'src'
range = @$img.data 'bttrlazyloading.range'
if @constructor.dpr >= 2 && @options.retina && src.match(/@2x/gi)
@blackList.push range + '@2x'
else
@blackList.push range
@whiteList.splice @whiteList.indexOf(range), 1
if @whiteList.length is 0
@$img.trigger 'bttrlazyloading.error'
return false
@$img.trigger 'bttrlazyloading.load'
@$img[onOrOff] 'error', onError
update = (e)=>
_update.call @
@$container[onOrOff] @options.event, update
# making sure we laod image within a container not in the viewport
$(window)[onOrOff] @options.event, update if @options.container isnt window
$(window)[onOrOff] "resize", update
_getRangeFromScreenSize = () ->
ww = window.innerWidth
if ww <= @ranges.xs
'xs'
else if @ranges.sm <= ww < @ranges.md
'sm'
else if @ranges.md <= ww < @ranges.lg
'md'
else if @ranges.lg <= ww
'lg'
_getImgObject = () ->
@range = _getRangeFromScreenSize.call @
_getLargestImgObject.call @
_getImageSrc = (src, range)->
# check if retina and not in black list
if @constructor.dpr >= 2 && @options.retina && @blackList.indexOf(range + '@2x') is -1
src.replace /\.\w+$/, (match)->
'@2x' + match
else
src
_getImgObjectPerRange = (range)->
if typeof @options[range].src isnt 'undefined' and @options[range].src isnt null
return @options[range]
return null
_getLargestImgObject = () ->
index = @whiteList.indexOf(@range)
# Check if the right img exist, otherwise we find a bigger one
if index > -1
src = _getImgObjectPerRange.call @, @range
if src
src.range = @range
return src
for range, index in @whiteList
src = _getImgObjectPerRange.call @, range
if src
src.range = range
return src
return ''
_isUpdatable = () ->
if !@loaded && @options.triggermanually
return false
if @loaded && @options.updatemanually
return false
imgObject = _getImgObject.call @
if !imgObject.src or @loaded is _getImageSrc.call @, imgObject.src, imgObject.range
return false
threshold = 0
if !@loaded
threshold = @options.threshold
isWithinWindowViewport = _isWithinViewport.call @, $(window),
top : $(window).scrollTop() + threshold
left : $(window).scrollLeft()
if @options.container isnt window
return isWithinWindowViewport and _isWithinViewport.call @, @$container,
top : @$container.offset().top + threshold
left : @$container.offset().left
return isWithinWindowViewport
# http://upshots.org/javascript/jquery-test-if-element-is-in-viewport-visible-on-screen
_isWithinViewport = ($container, viewport = {}) ->
viewport.right = viewport.left + $container.width()
viewport.bottom = viewport.top + $container.height()
bounds = @$wrapper.offset()
bounds.right = bounds.left + @$wrapper.outerWidth()
bounds.bottom = bounds.top + @$wrapper.outerHeight()
return (!(viewport.right < bounds.left || viewport.left > bounds.right || viewport.bottom < bounds.top || viewport.top > bounds.bottom))
_update = () ->
# If the range changed (window resize) we update the canvas size
if @range isnt _getRangeFromScreenSize.call @
_updateCanvasSize.call @
if _isUpdatable.call @
@$img.trigger 'bttrlazyloading.load'
###
Public Functions
###
get$Img: () ->
@$img
get$Clone: () ->
@$clone
get$Wrapper: () ->
@$wrapper
destroy : () ->
@$wrapper.before @$img
@$wrapper.remove()
_setupEvents.call @, 'off'
@$img.off 'bttrlazyloading'
@$img.removeClass 'bttrlazyloading-loaded'
@$img.removeClass 'animated ' + @options.animation if @options.animation
@$img.removeData 'bttrlazyloading'
return @$img
$.fn.extend
bttrlazyloading: (options) ->
return this.each () ->
$this = $(this)
data = $this.data('bttrlazyloading')
# Already instantiated?
if typeof data is 'undefined'
data = new BttrLazyLoading this, options
$this.data 'bttrlazyloading', data
# Ability to call public methods directly
# using .bttrlazyloading('methodName')
if typeof options is 'string' and typeof data[options] isnt 'undefined'
data[options].call data
$.fn.bttrlazyloading.Constructor = BttrLazyLoading
class BttrLazyLoadingGlobal
version : '1.0.3'
@ranges =
xs : 767
sm : 768
md : 992
lg : 1200
@options =
xs :
src : null
width : 100
height : 100
sm :
src : null
width : 100
height : 100
md :
src : null
width : 100
height : 100
lg :
src : null
width : 100
height : 100
retina : false
animation: 'bounceIn'
delay: 0
event : 'scroll'
container : window
threshold : 0
triggermanually: false
updatemanually: false
wrapperClasses: null
backgroundcolor: '#EEE'
placeholder : 'data:image/gif;base64,R0lGODlhEAALAPQAAP/391tbW+bf3+Da2vHq6l5dXVtbW3h2dq6qqpiVldLMzHBvb4qHh7Ovr5uYmNTOznNxcV1cXI2Kiu7n5+Xf3/fw8H58fOjh4fbv78/JycG8vNzW1vPs7AAAAAAAAAAAACH/C05FVFNDQVBFMi4wAwEAAAAh/hpDcmVhdGVkIHdpdGggYWpheGxvYWQuaW5mbwAh+QQJCwAAACwAAAAAEAALAAAFLSAgjmRpnqSgCuLKAq5AEIM4zDVw03ve27ifDgfkEYe04kDIDC5zrtYKRa2WQgAh+QQJCwAAACwAAAAAEAALAAAFJGBhGAVgnqhpHIeRvsDawqns0qeN5+y967tYLyicBYE7EYkYAgAh+QQJCwAAACwAAAAAEAALAAAFNiAgjothLOOIJAkiGgxjpGKiKMkbz7SN6zIawJcDwIK9W/HISxGBzdHTuBNOmcJVCyoUlk7CEAAh+QQJCwAAACwAAAAAEAALAAAFNSAgjqQIRRFUAo3jNGIkSdHqPI8Tz3V55zuaDacDyIQ+YrBH+hWPzJFzOQQaeavWi7oqnVIhACH5BAkLAAAALAAAAAAQAAsAAAUyICCOZGme1rJY5kRRk7hI0mJSVUXJtF3iOl7tltsBZsNfUegjAY3I5sgFY55KqdX1GgIAIfkECQsAAAAsAAAAABAACwAABTcgII5kaZ4kcV2EqLJipmnZhWGXaOOitm2aXQ4g7P2Ct2ER4AMul00kj5g0Al8tADY2y6C+4FIIACH5BAkLAAAALAAAAAAQAAsAAAUvICCOZGme5ERRk6iy7qpyHCVStA3gNa/7txxwlwv2isSacYUc+l4tADQGQ1mvpBAAIfkECQsAAAAsAAAAABAACwAABS8gII5kaZ7kRFGTqLLuqnIcJVK0DeA1r/u3HHCXC/aKxJpxhRz6Xi0ANAZDWa+kEAA7AAAAAAAAAAAA'
setOptions : (object = {}) ->
$.extend true, this.constructor.options, object
this
setRanges : (object = {}) ->
$.extend true, this.constructor.ranges, object
this
$.bttrlazyloading = new BttrLazyLoadingGlobal()