forked from strues/retinajs
/
retina_image_path.coffee
51 lines (40 loc) · 1.64 KB
/
retina_image_path.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
class RetinaImagePath
# Class variable [Array]
# cache of files we've checked on the server
@confirmed_paths = []
constructor: (@path) ->
# Split the file extension off the image path,
# and put it back together with @2x before the extension.
# "/path/to/image.png" => "/path/to/image@2x.png"
path_segments = @path.split('.')
path_without_extension = path_segments.slice(0, (path_segments.length - 1)).join(".")
extension = path_segments[path_segments.length - 1]
@at_2x_path = "#{path_without_extension}@2x.#{extension}"
# Function to test if image is external
is_external: ->
!!( @path.match(/^https?\:/i) and !@path.match('//' + document.domain) )
has_2x_variant: ->
# If the image path is on an external server,
# exit early to avoid cross-domain ajax errors
if @is_external()
return false
# If we have already checked and confirmed that
# the @2x variant exists, then just return true
else if @at_2x_path in RetinaImagePath.confirmed_paths
return true
# Otherwise, prepare an AJAX request for the HEAD only.
# We don't need a full request because we're only
# checking to see if the @2x version exists on the server
else
http = new XMLHttpRequest
http.open('HEAD', @at_2x_path, false)
http.send()
# If we get an A-OK from the server,
# push file path onto array of confirmed files
if http.status in [200..399]
RetinaImagePath.confirmed_paths.push @at_2x_path
return true
else
return false
root = exports ? window
root.RetinaImagePath = RetinaImagePath