/** * @package JFBConnect * @copyright (C) 2009-2014 by Source Coast - All rights reserved * @website http://www.sourcecoast.com/ * @website http://www.sourcecoast.com/joomla-facebook/ * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL */ /** * Ensure global _gaq Google Anlaytics queue has be initialized. * @type {Array} */ ; var _gaq = _gaq || []; var jfbc = { base: null, return_url: null, token: 't', // have a default, but this should be filled in login: { show_modal: false, scope: null, // Permissions JFBConnect requests logout_facebook: false, logged_in: true, login_started: false, // Prevents multiple concurrent login attempts, mainly caused by the auto-login feature enabled use_popup: true, provider: function (name) { if ((name == 'facebook') && jfbc.login.use_popup) { jfbc.debug.log('using popup' + jfbc.login.use_popup); jfbc.login.facebook(); } else self.location = jfbc.base + 'index.php?option=com_jfbconnect&task=authenticate.login&provider=' + name + '&return=' + jfbc.return_url + '&' + jfbc.token + '=1'; }, // Special method for Facebook. Should not be called directly in case it changes. Use jfbc.login.provider('facebook') instead facebook: function () { FB.login(function (response) { if (!jfbc.login.logged_in) jfbc.login.facebook_onlogin(); else jfbc.permissions.fetch(); }, { scope: jfbc.login.scope }); }, // v5.2 - Move to jfbc.login.provider(xyz) google: function () { jfbc.login.provider('google'); }, // Deprecated. Use jfbc.login.login_custom(); login_custom: function () { jfbc.debug.log("jfbc.login.login_custom is deprecated. Please use jfbc.login.provider('facebook') instead"); jfbc.login.provider('facebook'); }, // Action to perform after authentication on FB has occurred facebook_onlogin: function () { if (!jfbc.login.login_started) { jfbc.login.login_started = true; FB.getLoginStatus(function (response) { if (response.status === 'connected') { jfbcJQuery(document).one("jfbc-permissions-fetched", function () { jfbc.debug.log("Login checks"); // if (jfbc.permissions.check('email')) // { jfbc.debug.log("Logging in"); if (jfbc.login.show_modal == '1') { // First, hide the SCLogin modal if it's there jfbcJQuery('#login-modal').modal('hide'); jfbcJQuery("#jfbcLoginModal").css({"margin-left": function () { return -(jfbcJQuery("#jfbcLoginModal").width() / 2) }}); jfbcJQuery("#jfbcLoginModal").modal(); } self.location = jfbc.base + 'index.php?option=com_jfbconnect&task=authenticate.login&provider=facebook&return=' + jfbc.return_url + '&' + jfbc.token + '=1'; // } jfbc.debug.log("Done with checks"); }); // Start the check_permissions asynchronous check. This will fire the code above. jfbc.permissions.fetch(); } }); } jfbc.login.login_started = false; }, logout_button_click: function () { if (jfbc.login.logout_facebook) { FB.getLoginStatus(function (response) { if (response.status === 'connected') { FB.logout(function (response) { jfbcJQuery(document).trigger("jfbc-provider-logout-done"); }); } else { jfbcJQuery(document).trigger("jfbc-provider-logout-done"); } }); } else { jfbcJQuery(document).trigger("jfbc-provider-logout-done"); } }, logout: function (redirect) { jfbcJQuery(document).one("jfbc-provider-logout-done", function () { window.location = jfbc.base + 'index.php?option=com_users&task=user.logout&return=' + redirect + '&' + jfbc.token + '=1'; }); jfbc.login.logout_button_click(); } }, permissions: { // scope: comma-separated list of permissions to check check: function (scope) { var c = jfbc.cookie.get('jfbconnect_permissions_granted'); if (c === null) return false; var permissions = []; permissions = jfbcJQuery.parseJSON(c); var checkScope = scope.split(','); var scopeFound = true; jfbcJQuery.each(checkScope, function (k, v) { if (jfbcJQuery.inArray(v, permissions) == -1) scopeFound = false; }); return scopeFound; }, // Called at initialization // If called independently, use the jfbcJQuery.one("jfbc-permissions-fetched", ..) code to check new values fetch: function () { jfbc.debug.log("permissions_fetch"); FB.api('/me/permissions', function (response) { var permissions = ""; if (response.data !== undefined && jfbcJQuery.isArray(response.data)) { jfbcJQuery.each(response.data, function (k, v) { // Check for v2.0 of Graph API if ('permission' in v) { if (v.status == "granted") permissions = permissions + '","' + v.permission; } else { jfbcJQuery.each(v, function (perm, value) { permissions = permissions + '","' + perm; }); } }); // Can't use JSON.stringify as it's incompatible with IE7 :( permissions = permissions + '"'; permissions = permissions.substring(2, permissions.length); jfbc.cookie.set('jfbconnect_permissions_granted', "[" + permissions + "]"); } jfbcJQuery(document).trigger("jfbc-permissions-fetched"); }); }, // newScope = comma-separated list of scope to request update_scope: function (newScope) { var jfbcScope = jfbc.login.scope.split(','); newScope = newScope.split(','); newScope = jfbcJQuery.merge(jfbcScope, newScope); newScope = jfbcJQuery.grep(newScope, function (v, k) { // Create a new scope array with no duplicates return jfbcJQuery.inArray(v, newScope) === k; }); jfbc.login.scope = newScope.join(','); jfbc.debug.log("update_scope: Now set to " + jfbc.login.scope); } }, social: { facebook: { comment: { create: function (response) { var url = 'option=com_jfbconnect&task=social.comment&type=create&href=' + encodeURIComponent(escape(response.href)) + '&commentID=' + response.commentID + '&title=' + document.title; jfbc.util.ajax(url, null); }, remove: function (response) { var url = 'option=com_jfbconnect&task=social.comment&type=remove&href=' + encodeURIComponent(escape(response.href)) + '&commentID=' + response.commentID + '&title=' + document.title; jfbc.util.ajax(url, null); } }, like: { create: function (response) { var url = 'option=com_jfbconnect&task=social.share&provider=facebook&share=like&type=create&href=' + encodeURIComponent(escape(response)) + '&title=' + document.title; jfbc.util.ajax(url, null); }, remove: function (response) { var url = 'option=com_jfbconnect&task=social.share&provider=facebook&share=like&type=remove&href=' + encodeURIComponent(escape(response)) + '&title=' + document.title; jfbc.util.ajax(url, null); } } }, google: { plusone: function (response) { var type; if (response.state == "on") type = "create"; else if (response.state == "off") type = "remove"; else return; var url = 'option=com_jfbconnect&task=social.share&provider=google&share=plusone&type=' + type + '&href=' + encodeURIComponent(escape(response.href)) + '&title=' + document.title; jfbc.util.ajax(url, null); } }, linkedin: { share: function () { var href = document.location.href; var url = 'option=com_jfbconnect&task=social.share&provider=linkedin&share=share&type=create&href=' + encodeURIComponent(escape(href)) + '&title=' + document.title; jfbc.util.ajax(url, null); } }, twitter: { tweet: function (intentEvent) { if (!intentEvent) return; // var id = intentEvent.tweet_id; // var username = intentEvent.screen_name; var href = document.location.href; var url = 'option=com_jfbconnect&task=social.share&provider=twitter&share=tweet&type=create&href=' + encodeURIComponent(escape(href)) + '&title=' + document.title; jfbc.util.ajax(url, null); } }, /** * Tracks Facebook likes, unlikes and sends by suscribing to the Facebook * JSAPI event model. Note: This will not track facebook buttons using the * iFrame method. */ googleAnalytics: { trackFacebook: function () { var opt_pageUrl = window.location; try { if (FB && FB.Event && FB.Event.subscribe) { FB.Event.subscribe('edge.create', function (targetUrl) { _gaq.push(['_trackSocial', 'facebook', 'like', targetUrl, opt_pageUrl]); }); FB.Event.subscribe('edge.remove', function (targetUrl) { _gaq.push(['_trackSocial', 'facebook', 'unlike', targetUrl, opt_pageUrl]); }); FB.Event.subscribe('message.send', function (targetUrl) { _gaq.push(['_trackSocial', 'facebook', 'send', targetUrl, opt_pageUrl]); }); FB.Event.subscribe('comment.create', function (targetUrl) { _gaq.push(['_trackSocial', 'facebook', 'comment', targetUrl, opt_pageUrl]); }); FB.Event.subscribe('comment.remove', function (targetUrl) { _gaq.push(['_trackSocial', 'facebook', 'uncomment', targetUrl, opt_pageUrl]); }); } } catch (e) { } } }, // Not published yet. Need to figure out the best way to incorporate this into pages feedPost: function (title, caption, description, url, picture) { // javascript:jfbc.social.feedPost('SourceCoast JE', 'My caption', 'Great extensions!', 'http://www.sourcecoast.com/', 'https://www.sourcecoast.com/images/stories/extensions/jfbconnect/home_jfbconn.png'); var obj = { method: 'feed', link: url, picture: picture, name: title, // page title? caption: caption, description: description }; function callback(response) { } FB.ui(obj, callback); }, share: function () { var element = jfbcJQuery('.jfbcsocialshare'); if (!element.length) element = jfbcJQuery('
').appendTo('body'); jfbcJQuery('.jfbcsocialshare').fadeIn(1000); } }, canvas: { checkFrame: function () { if (top == window) { // crude check for any frame if (window.location.search == "") top.location.href = window.location.href + '?jfbcCanvasBreakout=1'; else top.location.href = window.location.href + '&jfbcCanvasBreakout=1'; } } }, request: { currentId: null, popup: function (jfbcReqId) { jfbc.request.currentId = jfbcReqId; data = jfbcRequests[jfbcReqId]; FB.ui({method: 'apprequests', display: 'popup', message: data.message, title: data.title, data: jfbcReqId }, jfbc.request.fbCallback); }, fbCallback: function (response) { if (response != null) { var rId = response.request; var to = response.to; var toQuery = ""; for (var i = 0; i < to.length; i++) toQuery += "&to[]=" + to[i]; var query = 'option=com_jfbconnect&task=request.requestSent&requestId=' + rId + toQuery + '&jfbcId=' + jfbc.request.currentId; jfbc.util.ajax(query, jfbc.request.redirectToThanks); } }, redirectToThanks: function () { data = jfbcRequests[jfbc.request.currentId]; if (data.thanksUrl != "" && (window.location.href != data.thanksUrl)) window.location.href = data.thanksUrl; } }, opengraph: { triggerAction: function (action, href, params) { if (params != undefined) { params = {params: params}; // Wrap it in an extra parameters object so that the query params become an array keyed off params params = jfbcJQuery.param(params); } else params = ''; var url = 'option=com_jfbconnect&task=opengraph.ajaxAction&action=' + action + '&href=' + encodeURIComponent(href) + '&' + params; jfbc.debug.log('opengraph.triggerAction url: ' + url); jfbc.util.ajax(url, jfbc.opengraph.actionPopup); }, actionPopup: function (message) { if (message != '') { jfbc.debug.log("Open Graph Action response: " + message); var element = jfbcJQuery('#ogActionPopup'); if (!element.length) element = jfbcJQuery('
').appendTo('body'); element.html(message).hide(); jfbcJQuery('#ogActionPopup').fadeIn(1000); setTimeout("jfbcJQuery('#ogActionPopup').fadeOut(1000)", 15000); } } }, share: { messages: { action_text: "Post to Timeline", close: "Close", custom_message: "Message", custom_message_placeholder: "Write something about the %s", error: "Error", success: "Posted to Facebook!", view_on_facebook: "View %s story on Facebook" }, build_with_at_text: function () { var message_data = jfbcJQuery("#composer-message-data"); if (message_data.length === 0) { return; } var component = jfbcJQuery(""); if (jfbc.share.friends.tagged !== undefined && !jfbcJQuery.isEmptyObject(jfbc.share.friends.tagged)) { var friends = jfbcJQuery("").addClass("friends").text(" " + jfbc.share.friends.autocomplete.messages.intro_text + " "); jfbcJQuery.each(jfbc.share.friends.tagged, function (id, values) { friends.append(jfbcJQuery("").addClass("friend").attr({href: values.link, target: "_blank"}).text(values.name).data("fbid", id)); }); if (!friends.is(":empty")) { component.append(friends); } friends = null; } if (jfbc.share.place.tagged !== undefined) { component.append(jfbcJQuery("").addClass("place").text(" " + jfbc.share.place.autocomplete.messages.intro_text + " ").append(jfbcJQuery("").attr({href: jfbc.share.place.tagged.link, target: "_blank"}).text(jfbc.share.place.tagged.name))); } // clear any previously stored elements and their handlers message_data.empty(); if (component.is(":empty")) { // hide the extra component message_data.hide(); } else { message_data.show(); message_data.append(component.html()); } }, story_uri: function (story_id) { if (jfbc.user.link !== undefined) { return jfbc.user.link + "/activity/" + story_id; } else { return "https://www.facebook.com/" + story_id; } }, form_handler: function (e) { if (e.preventDefault) { e.preventDefault(); // Don't POST the form } var params = { message: jfbc.util.encode_data(jfbcJQuery.trim(jfbcJQuery("#composer-message").val())), explicitly_shared: jfbcJQuery.trim(jfbcJQuery("#explicitly_shared").val()) }; var actionId = jfbcJQuery("#jfbc-action-id").val() if (jfbc.share.place.tagged !== undefined && jfbc.share.place.tagged.id !== undefined) { params.place = jfbc.share.place.tagged.id; } if (jfbc.share.friends.tagged !== undefined && !jfbcJQuery.isEmptyObject(jfbc.share.friends.tagged)) { params.tags = Object.keys(jfbc.share.friends.tagged).join(","); } jfbcJQuery("#jfbc-share-modal").modal("hide"); jfbc.opengraph.triggerAction(actionId, window.location, params); // Disable automatically now. Need to do this only if the action is successful //jfbcJQuery('button#share-button').addClass('disabled'); return false; }, display_modal: function (actionId, capabilities) { jfbc.debug.log(capabilities); if (jfbc.login.logged_in) { if (jfbc.permissions.check('publish_actions')) // Check for permission, and request if not granted { jfbc.debug.log("display_modal: Permission found! Showing."); jfbc.share.show_composer(actionId, capabilities); return true; } else { jfbcJQuery(document).one("jfbc-permissions-fetched", function () { jfbc.debug.log("display_modal: Permission fetched and found! Showing."); if (jfbc.permissions.check('publish_actions')) // Check for permission, and request if not granted jfbc.share.show_composer(actionId, capabilities); }); } } // If we get here, we're either not logged in or don't have the right permissions. // Log the user in, or update their permissions and auto-show the modal (using the on-event above) jfbc.permissions.update_scope('publish_actions'); jfbc.login.provider('facebook'); return false; }, show_composer: function (actionId, capabilities) { jfbc.util.loadJQueryUi(); /* if (!jfbc.share.jquery_ui_loaded) { jfbcJQuery.getScript(jfbc.base + "media/sourcecoast/js/jquery-ui.min.js").done(function () { jfbc.share.jquery_ui_loaded = true; jfbc.share.add_composer(actionId, capabilities); }); } else*/ jfbc.share.add_composer(actionId, capabilities); }, // add the modal code to the page and pop it up. Do not call directly. Use display_modal above. add_composer: function (actionId, capabilities) { jfbc.share.friends.tagged = {}; jfbc.debug.log("add_composer: Action " + actionId + ", capabilities: " + capabilities); var form = jfbcJQuery("
").attr({id: "jfbc-share-composer"}).submit(jfbc.share.form_handler); form.append(jfbcJQuery("").attr({"id": "jfbc-action-id", "type": "hidden"}).val(actionId)); var modal_body = jfbcJQuery("
").addClass("modal-body"); if (jfbcJQuery.inArray("messages", capabilities) != -1) { modal_body.append(jfbcJQuery("
").attr("id", "composer-message-group").addClass("control-group") .append( jfbcJQuery("
").addClass("controls").append(jfbcJQuery("").addClass("input-xxlarge").attr({id: "composer-message", type: "text", maxlength: 1000, autocomplete: "off", placeholder: "Add a comment" })) ).append( jfbcJQuery("").attr("id", "composer-message-data").hide()) ); } if (jfbcJQuery.inArray("tags", capabilities) != -1) { modal_body.append(jfbcJQuery("
").attr("id", "autocomplete-fields").append( jfbc.share.friends.build_form_fields() ).append( jfbc.share.place.build_form_fields() )); modal_body.append(jfbcJQuery("
").addClass("btn-group").attr("id", "composer-buttons").append( jfbc.share.place.build_toggle() ).append( jfbc.share.friends.build_toggle() )); } if (jfbcJQuery.inArray("explicitly_shared", capabilities) != -1) form.append(jfbcJQuery("").attr({"id": "explicitly_shared", "type": "hidden"}).val("true")); else form.append(jfbcJQuery("").attr({"id": "explicitly_shared", "type": "hidden"}).val("false")); var dataDismiss; if (jfbc.jqcompat) dataDismiss = 'sc-modal'; else dataDismiss = 'modal'; modal = jfbcJQuery("
").addClass("sourcecoast modal").attr({id: "jfbc-share-modal", role: "dialog", "aria-labelledby": "modal-title"}); modal.append(jfbcJQuery("
").addClass("modal-header").append(jfbcJQuery("