-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfacebook-square-crop.coffee
79 lines (65 loc) · 1.84 KB
/
facebook-square-crop.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
class FacebookSquareCrop
# Wait threshold for debouncing.
threshold: 100
# Animation duration time after a facebook response.
duration: 300
# Deferred object for when the sdk has been initialized.
dfd: null
constructor: (dfd) ->
@ids = []
@dfd = dfd
# Instantly queries the facebook api.
instant: (id) ->
@pre id
@query id
# Debounced function for querying the facebook api.
# Gathers ids for a single request.
debounce: (id) ->
@pre id
@ids.push id if $.inArray id, @ids >= 0
delayed = =>
@query @ids
@ids = []
@timeout = null
if @timeout?
clearTimeout @timeout
@timeout = setTimeout delayed, @threshold
# Queries facebook.
query: (id) ->
if $.isArray(id)
selector = "id IN ("+id.join(",")+")"
else
selector = "id = "+id
@dfd.done =>
FB.api
method: "fql.query"
query: "SELECT id, pic_crop FROM profile WHERE "+selector
(data) =>
@crop data
# Positions the image correctly.
crop: (data) ->
for item in data
crop = item.pic_crop
if crop.width > crop.height
set = Math.min((crop.width / crop.height) * (crop.left / crop.bottom), 1 - (crop.height / (crop.width - 1)))
style =
"left": -set*100+"%"
"height": "100%"
"min-height": "100%"
else if crop.height > crop.width
set = Math.min(crop.top, 1 - (crop.width / (crop.height - 1)))
style =
"top": -set*100+"%"
"height": "auto"
"width": "100%"
else
style =
"height": "auto"
"width": "100%"
$(".fbImage" + item.id).css style
@post item.id
pre: (id) ->
$(".fbImage" + id).css "display", "none"
post: (id) ->
$(".fbImage" + id).fadeIn @duration
window.FacebookSquareCrop = FacebookSquareCrop