-
Notifications
You must be signed in to change notification settings - Fork 0
/
facebook.js
131 lines (110 loc) · 3.11 KB
/
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
angular.module('facebook', [])
.factory('facebook', ['$rootScope', '$q', '$fb', function ($rootScope, $q, $fb) {
var login, ensureUserLoggedIn, api;
login = function () {
return $fb.login().then(function (response) {
if (response.authResponse) {
// resolve promise and propagate via digest
return response;
} else {
// reject promise and propagate via digest
return $q.reject(response);
}
});
};
checkLoggedIn = function(response) {
if (response.status === 'connected') {
return response;
} else {
return $q.reject(response);
}
}
isLoggedIn = function() {
return $fb.getLoginStatus().then(checkLoggedIn);
}
ensureUserLoggedIn = function () {
return isLoggedIn()
.then( null, function () {
// not_authorized || not_logged_in
return login();
}
);
};
api = function () {
return $fb.api.apply(null, arguments)
.then(function (response) {
if(response.error)
return $q.reject(response);
else
return response;
});
}
return {
api: api
, isLoggedIn: isLoggedIn
, ensureUserLoggedIn: ensureUserLoggedIn
}
}])
.provider('$fb', function () {
var initConfig;
return {
initialize: function(config) {
initConfig = config;
}
, $get: ['$window', '$q', '$rootScope', '$timeout', function($window, $q, $rootScope, $timeout) {
var $fb = {}
, fbPromise
, getFB = function() {
var deferred = $q.defer();
// TODO: Maybe facebook is down?
$window.fbAsyncInit = function () {
if(!initConfig) {
console.error('did you forget to initialize fb?');
}
$window.FB.init(initConfig);
$rootScope.$apply(function() {
deferred.resolve($window.FB);
});
}
loadFBSdkAsync();
return deferred.promise;
}
fbPromise = getFB();
function resolveFBMethod(methodName) {
$fb[methodName] = function() {
var deferred = $q.defer()
, asyncFlag = true
, args = Array.prototype.slice.call(arguments); //get arguments to sdk function
function fbMethodHandler(response) {
deferred.resolve(response);
if(asyncFlag)
$rootScope.$apply();
}
function callMethod (FB) {
asyncFlag = false;
FB[methodName].apply(null, args);
asyncFlag = true;
}
args.push(fbMethodHandler);
fbPromise.then(callMethod);
return deferred.promise;
}
}
angular.forEach(
['api', 'ui', 'getLoginStatus', 'getAuthResponse', 'login', 'logout']
, resolveFBMethod
);
return $fb;
}]
}
});
function loadFBSdkAsync() {
// Load the SDK asynchronously
(function(d, s, id){
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {return;}
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/all.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
}