|
"use strict"; |
|
|
|
(function() { |
|
/** |
|
* window.FB extended class |
|
* Jose Luis Quintana <www.joseluisquintana.pe> | MIT License |
|
* Reference: https://developers.facebook.com/docs/javascript/reference/FB.api |
|
* |
|
* @param {Object} options - Options |
|
* @param {Function} fn - Callback |
|
*/ |
|
function FBXnd(options, fn) { |
|
if (!(this instanceof FBXnd)) { |
|
return new FBXnd(options, fn); |
|
} |
|
|
|
this.fb = null; |
|
this.ready = false; |
|
this.response = null; |
|
this.options = options; |
|
this.initialize(fn); |
|
} |
|
|
|
function callback(header, response, fn) { |
|
if (fn) { |
|
fn({ |
|
"header": header, |
|
"response": response |
|
}); |
|
} |
|
} |
|
|
|
/** |
|
* Initialize method |
|
* @param {Function} fn - Callback |
|
*/ |
|
FBXnd.prototype.initialize = function(fn) { |
|
var me = this; |
|
this.ready = !!(window.FB || false); |
|
|
|
if (this.ready) { |
|
this.onReady(fn); |
|
} else { |
|
window.fbAsyncInit = function() { |
|
me.onReady(fn); |
|
}; |
|
|
|
this.injectJSDK(); |
|
} |
|
}; |
|
|
|
/** |
|
* Checks if window.FB SDK is ready |
|
* @return {[type]} [description] |
|
*/ |
|
FBXnd.prototype.isReady = function() { |
|
return this.ready; |
|
}; |
|
|
|
/** |
|
* Ready callback |
|
* @param {Function} fn - Callback |
|
*/ |
|
FBXnd.prototype.onReady = function(fn) { |
|
this.fb = window.FB; |
|
this.ready = true; |
|
this.fb.init(this.options); |
|
fn(this.ready); |
|
}; |
|
|
|
/** |
|
* Facebook connect |
|
* @param {Function} fn - Callback |
|
*/ |
|
FBXnd.prototype.connect = function(fn) { |
|
if (this.ready) { |
|
var me = this; |
|
|
|
this.fb.getLoginStatus(function(response) { |
|
if (response.status === "not_authorized") { |
|
me.login(fn); |
|
} else { |
|
if (response.status === "connected") { |
|
me.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 { |
|
me.login(fn); |
|
} |
|
} |
|
}); |
|
} else { |
|
this.error("Facebook JS SDK is not loaded !"); |
|
} |
|
}; |
|
|
|
/** |
|
* Get facebook profile |
|
* @param {Object} header - Object header |
|
* @param {Function} fn - Callback |
|
*/ |
|
FBXnd.prototype.getProfile = function(header, fn) { |
|
this.fb.api("/me", function(response) { |
|
callback(header, response, fn); |
|
}); |
|
}; |
|
|
|
/** |
|
* Faceboook login |
|
* https://developers.facebook.com/docs/facebook-login/permissions/ |
|
* publish_actions |
|
* @param {Function} fn Callback |
|
*/ |
|
FBXnd.prototype.login = function(fn) { |
|
var me = this; |
|
|
|
this.fb.login(function(response) { |
|
if (response.authResponse) { |
|
me.getProfile(response, fn); |
|
} else { |
|
callback(response, null, fn); |
|
} |
|
}, { |
|
scope: "email" |
|
}); |
|
}; |
|
|
|
/** |
|
* Facebook logout |
|
* @param {Function} fn Callback |
|
*/ |
|
FBXnd.prototype.logout = function(fn) { |
|
this.fb.logout(function() { |
|
callback(null, null, fn); |
|
}); |
|
}; |
|
|
|
/** |
|
* Get facebook profile by Id |
|
* @param {Number} id - Facebook Id |
|
* @param {Function} fn Callback |
|
*/ |
|
FBXnd.prototype.getProfileBy = function(id, fn) { |
|
this.fb.api("/" + id, function(response) { |
|
callback(response, null, fn); |
|
}); |
|
}; |
|
|
|
/** |
|
* Error output via console |
|
* @param {string} str - String to output |
|
*/ |
|
FBXnd.prototype.error = function(str) { |
|
if (window.console) { |
|
window.console.error(str); |
|
} |
|
}; |
|
|
|
/** |
|
* Create a facebook post |
|
* @param {String} message - String message |
|
* @param {Function} fn - Callback |
|
*/ |
|
FBXnd.prototype.post = function(message, fn) { |
|
this.fb.api("/me/feed", "post", { |
|
message: message |
|
}, function(response) { |
|
callback(response, null, fn); |
|
}); |
|
}; |
|
|
|
/** |
|
* Share on facebook |
|
* @param {Object} options - Options for share |
|
* Options: |
|
* { |
|
* method: "feed", |
|
* name: "", |
|
* link: "", |
|
* picture: "", |
|
* caption: "", |
|
* description: "" |
|
* } |
|
* @param {Function} fn - Callback |
|
*/ |
|
FBXnd.prototype.share = function(options, fn) { |
|
this.fb.ui(options, function(response) { |
|
callback(null, response, fn); |
|
}); |
|
}; |
|
|
|
/** |
|
* Inject facebook JSDK |
|
*/ |
|
FBXnd.prototype.injectJSDK = function() { |
|
if (this.options.injectJSDK) { |
|
var fbroot = document.createElement("div"); |
|
fbroot.id = "fb-root"; |
|
document.body.insertBefore(fbroot, document.body.firstChild); |
|
|
|
(function(d, s, id) { |
|
var js; |
|
var 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")); |
|
} |
|
}; |
|
|
|
window.FBXnd = FBXnd; |
|
}()); |