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
|
# File 'app/helpers/facebook_rails_helper.rb', line 11
def facebook_initialization resize = :auto, app_id = FACEBOOK['app_id'], user_f8id = nil
begin
user ||= current_user_f8id
rescue Exception => e
logger.info "current_user_f8id exception #{ e.to_s }"
end
onload_resize = %{
var domLoaded = false;
var facebookLoaded = false;
var pageLoaded = false;
function pageLoad() {
if (domLoaded && facebookLoaded) {
if (!pageLoaded) {
pageLoaded = true;
$(document).trigger('page_loaded');
}
sizeToMain();
}
}
function sizeToMain() {
if (domLoaded && facebookLoaded) {
var height = $('#main').outerHeight();
if(height > 100) {
FB.Canvas.setSize({ width: 760, height: height + 200 });
}
}
}
$(document).ready(function () {
domLoaded = true;
pageLoad();
});
$(window).load(function() {
domLoaded = true; //not neccessary
pageLoad();
});
}
login_status = if user_f8id
%{
FB.getLoginStatus(function(response) {
if ((response.authResponse && response.authResponse.userID == '#{ user_f8id }') ||
(response.session && response.session.uid == '#{ user_f8id }')) {
// logged in and connected user - continue
$(document).trigger('got_facebook_status');
} else {
// no user session available or new user - clear old user's session
window.location.href = window.location.href + (window.location.href.indexOf('?') >= 0 ? '&' : '?') + 'clear_users_session=true';
}
}, true); //force session status reloading
}
else
%{
FB.getLoginStatus(function(response) {
$(document).trigger('got_facebook_status');
});
}
end
%{
<div id="fb-root"></div>
<script type="text/javascript">
#{ onload_resize if resize == :onload }
window.fbAsyncInit = function() {
FB.init({ appId: '#{ app_id }',
status: true,
cookie: true,
xfbml: true });
$(document).trigger('facebook_init');
#{ 'facebookLoaded = true; pageLoad();' if resize == :onload }
#{ 'FB.Canvas.setAutoGrow();' if resize == :auto }
#{ login_status }
};
(function() {
var e = document.createElement('script'); e.async = true;
e.src = document.location.protocol +
'//connect.facebook.net/#{ get_facebook_locale }/all.js';
document.getElementById('fb-root').appendChild(e);
}());
</script>
}.html_safe
end
|