Skip to content

Instantly share code, notes, and snippets.

@joseluisq
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joseluisq/db7321357e3f35261d72 to your computer and use it in GitHub Desktop.
Save joseluisq/db7321357e3f35261d72 to your computer and use it in GitHub Desktop.
FB Extended Javascript Library 1.0
/**
* FB Extended
* Jose Luis Quintana <www.joseluisquintana.pe>
* Reference: https://developers.facebook.com/docs/javascript/reference/FB.api
*/
function FbApiHandler(options, fn) {
var ready = false;
this.options = null;
this.response = null;
this.initialize = function(options, fn) {
this.options = options;
ready = !!(window.FB || false);
if (ready) {
this.onReady(fn);
} else {
window.fbAsyncInit = function() {
this.onReady(fn);
}.bind(this);
this.requireJSDK();
}
};
this.isReady = function() {
return ready;
};
this.onReady = function(fn) {
FB.init(options);
ready = true;
fn.apply(this, [ready]);
};
this.connect = function(fn) {
if (ready) {
FB.getLoginStatus(function(response) {
if (response.status === 'not_authorized') {
this.login(fn);
} else {
if (response.status === 'connected') {
this.getProfile(response, fn);
} else if (response.status === 'not_authorized') {
// the user is logged in to Facebook,
// but has not authenticated your app
_callback(response, 'The user is logged in to Facebook, but has not authenticated your app.', fn);
} else {
this.login(fn);
}
}
}.bind(this));
} else {
this.error("Facebook JS SDK is not loaded !");
}
};
this.getProfile = function(header, fn) {
FB.api('/me', function(response) {
_callback(header, response, fn);
});
};
// https://developers.facebook.com/docs/facebook-login/permissions/
// publish_actions
this.login = function(fn) {
console.log('Login..');
FB.login(function(response) {
if (response.authResponse) {
this.getProfile(response, fn);
} else {
_callback(response, null, fn);
}
}.bind(this), {
scope: 'email'
});
};
this.logout = function(fn) {
FB.logout(function() {
_callback(null, null, fn);
});
};
this.getProfileBy = function(id, fn) {
FB.api('/' + id, function(response) {
_callback(response, null, fn);
});
};
this.error = function(str) {
if (window.console) {
window.console.error(str);
}
};
this.post = function(message, fn) {
FB.api('/me/feed', 'post', {message: message}, function(response) {
_callback(response, null, fn);
});
};
// method: 'feed',
// name: '',
// link: '',
// picture: '',
// caption: '',
// description: ''
this.share = function(options, fn) {
FB.ui(options, function(response) {
_callback(null, response, fn);
});
};
_callback = function(header, response, fn) {
this.response = {"header": header, "response": response};
if (fn) {
fn.apply(this, [this.response]);
}
}.bind(this);
this.requireJSDK = function() {
if (this.options.requireJSDK) {
var fbroot = document.createElement('div');
fbroot.id = 'fb-root';
document.body.insertBefore(fbroot, document.body.firstChild);
(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/es_ES/all.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
}
};
this.initialize(options, fn);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment