forked from M1ke/easy-facebook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheasy-facebook.js
executable file
·157 lines (138 loc) · 3.28 KB
/
easy-facebook.js
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
// DEV we need to upgrade this to function() scope, remove use of prototype and use as singleton object
/* @include ../js-utils/function-q.js */
var easyFacebook = (function(){
var singleton = {};
var _url = ''
,_user = 0
,_fbLoginOptions = {scope:'email'}
,_classLoggedIn = 'logged-in'
,_loginEl = 'a.fb-login'
,_textLoggedIn = 'Logged in with Facebook'
,_loginOnInit = false
,_initFunctions = new functionQ;
function _setLoginEl(setLoginEl){
_loginEl = setLoginEl;
}
function _setInit(init){
_loginOnInit = init;
}
function _addToInit(fn){
_initFunctions.wrap(fn, singleton);
}
function _init(){
_initFunctions.run();
if (_loginOnInit){
_onClickLogin();
_getLoginStatus();
}
}
function _onClickLogin(){
$(document).on('click', _loginEl, _click_LoginEvent);
}
function _clickLogin($link){
$link.click(_click_LoginEvent);
}
function _click_LoginEvent(event){
event.preventDefault();
$(this).html('Connecting to Facebook…');
_fbLogin($(this));
}
function _fbLogin($link){
FB && FB.login(function(response){
if (response.status=='connected'){
singleton.userLoggedIn(response.authResponse.accessToken, $link);
}
else {
singleton.userNotLoggedIn(response.status, $link);
}
}, _fbLoginOptions);
}
function _dialog(request, callback){
FB && FB.ui(request, function(response){
callback && callback.call(null, response);
});
}
function _getLoginStatus(){
FB && FB.getLoginStatus(function(response){
if (response.status!=='connected'){
return false;
}
FB.api('me/permissions',function(response){
var perms = _fbLoginOptions.scope
,perm;
for (var i=0; i<response.data.length; i++){
perm = response.data[i];
if (perm.status=='granted'){
perms = perms.replace(perm.permission,'');
}
}
if (perms.replace(/,/g,'').length>0){
_fbLogin();
}
else {
singleton.userLoggedIn(FB.getAccessToken());
}
});
});
}
function _feed(link, caption, paramsOrCallback, callback){
switch (typeof paramsOrCallback){
case 'function':
callback = paramsOrCallback;
params = {};
break;
case 'object':
params = paramsOrCallback;
break;
default:
params = {};
}
params = $.extend({
method: 'feed'
,link: link
,caption: caption
,picture: null
,source: null
,name: null
,description: null
,properties: null
,actions: null
,ref: null
}, params);
_dialog(params, callback);
}
function _send(link, callback){
var params = {
method: 'send'
,link: link
}
_dialog(params, callback);
}
function _share(href, callback){
var params = {
method: 'share'
,href: href
}
_dialog(params, callback);
}
singleton = {
scope: _fbLoginOptions.scope
,init: _init
,setLoginEl: _setLoginEl
,setInit: _setInit
,clickLogin: _clickLogin
,click_Event: _click_LoginEvent
,dialog: _dialog
,feed: _feed
,send: _send
,share: _share
,userLoggedIn: function(token, $link){
}
,userNotLoggedIn: function(status, $link){
}
,friendRequest: function(){
throw "The friendRequest method is no longer available";
}
};
return singleton;
})();