Permalink
Browse files

[Fix #65] TagLib update (scripts are only included once in a page eve…

…n if tags are used more than once)
  • Loading branch information...
1 parent 6c489a0 commit a202f490258d1931a76ee2b9633f2ecc113d0185 @benorama benorama committed Apr 4, 2014
@@ -53,6 +53,7 @@ class FacebookJSTagLib {
attrs.each { key, value ->
model[key] = value
}
+ includeScriptOnce('init-js', model)
out << render(template: '/tags/init-js', model: model, plugin: 'facebook-sdk')
}
@@ -71,6 +72,7 @@ class FacebookJSTagLib {
attrs.each { key, value ->
model[key] = value
}
+ includeScriptOnce('add-to-page-link', model)
out << render(template: '/tags/add-to-page-link', model: model, plugin: 'facebook-sdk')
}
@@ -89,6 +91,7 @@ class FacebookJSTagLib {
attrs.each { key, value ->
model[key] = value
}
+ includeScriptOnce('login-link', model)
out << render(template: '/tags/login-link', model: model, plugin: 'facebook-sdk')
}
@@ -104,6 +107,7 @@ class FacebookJSTagLib {
attrs.each { key, value ->
model[key] = value
}
+ includeScriptOnce('logout-link', model)
out << render(template: '/tags/logout-link', model: model, plugin: 'facebook-sdk')
}
@@ -140,6 +144,7 @@ class FacebookJSTagLib {
attrs.each { key, value ->
model[key] = value
}
+ includeScriptOnce('picture', model)
out << render(template: '/tags/picture', model: model, plugin: 'facebook-sdk')
}
@@ -164,6 +169,7 @@ class FacebookJSTagLib {
attrs.each { key, value ->
model[key] = value
}
+ includeScriptOnce('invite-link', model)
out << render(template: '/tags/invite-link', model: model, plugin: 'facebook-sdk')
}
@@ -187,6 +193,7 @@ class FacebookJSTagLib {
attrs.each { key, value ->
model[key] = value
}
+ includeScriptOnce('publish-link', model)
out << render(template: '/tags/publish-link', model: model, plugin: 'facebook-sdk')
}
@@ -209,7 +216,20 @@ class FacebookJSTagLib {
attrs.each { key, value ->
model[key] = value
}
+ includeScriptOnce('send-link', model)
out << render(template: '/tags/send-link', model: model, plugin: 'facebook-sdk')
}
+ // PRIVATE
+
+ private void includeScriptOnce(String tagName, Map model) {
+ if (!request.facebookSdkIncludedScripts) {
+ request.facebookSdkIncludedScripts = [:]
+ }
+ if (!request.facebookSdkIncludedScripts[tagName]) {
+ request.facebookSdkIncludedScripts[tagName] = true
+ model.includeScript = true
+ }
+ }
+
}
@@ -1,24 +1,26 @@
-<script type="text/javascript">
- $(function() {
- $('a.fb-sdk-add-to-page-link').click(function() {
- var link = $(this);
- var options = {
- method: 'pagetab'
- };
- if (link.data('display') != undefined) options['display'] = link.data('display');
- if (link.data('redirect_uri') != undefined) options['redirect_uri'] = link.data('redirect_uri');
- FB.ui(options, function(response) {
- if (link.data('callback') != undefined) {
- var callback = window[link.data('callback')];
- if (typeof callback === 'function') {
- callback(response);
+<g:if test="${includeScript}">
+ <script type="text/javascript">
+ $(function() {
+ $('a.fb-sdk-add-to-page-link').click(function() {
+ var link = $(this);
+ var options = {
+ method: 'pagetab'
+ };
+ if (link.data('display') != undefined) options['display'] = link.data('display');
+ if (link.data('redirect_uri') != undefined) options['redirect_uri'] = link.data('redirect_uri');
+ FB.ui(options, function(response) {
+ if (link.data('callback') != undefined) {
+ var callback = window[link.data('callback')];
+ if (typeof callback === 'function') {
+ callback(response);
+ }
}
- }
+ });
+ return false;
});
- return false;
});
- });
-</script>
+ </script>
+</g:if>
<a <g:if test="${elementId}">id="${elementId}"</g:if>
class="<g:if test="${elementClass}">${elementClass} </g:if>fb-sdk-add-to-page-link fb-sdk-link"
<g:if test="${callback}">data-callback="${callback}"</g:if>
@@ -1,37 +1,38 @@
<%@page expressionCodec="raw" %>
-<div id="fb-root"></div>
-<script type="text/javascript">
- // Disable all fb sdk links until FB JS SDK is initialized
- $(function() {
- if (!window.FB) {
- $('.fb-sdk-link').attr('disabled', 'disabled').addClass('disabled');
- }
- });
+<g:if test="${includeScript}">
+ <div id="fb-root"></div>
+ <script type="text/javascript">
+ // Disable all fb sdk links until FB JS SDK is initialized
+ $(function() {
+ if (!window.FB) {
+ $('.fb-sdk-link').attr('disabled', 'disabled').addClass('disabled');
+ }
+ });
- window.fbAsyncInit = function() {
- FB.init({
- appId: "${appId}",
- <g:if test="${channelUrl}">channelUrl: "${channelUrl}", // Custom channel URL</g:if>
- cookie: <g:if test="${cookie}">true</g:if><g:else>false</g:else>, // enable cookies to allow the server to access the session
- oauth: true, // enables OAuth 2.0
- status: <g:if test="${status}">true</g:if><g:else>false</g:else>, // check login status
- xfbml: <g:if test="${xfbml}">true</g:if><g:else>false</g:else>, // parse XFBML
- frictionlessRequests: <g:if test="${frictionlessRequests}">true</g:if><g:else>false</g:else> // to enable frictionless requests
- });
- $('.fb-sdk-link').removeAttr('disabled').removeClass('disabled');
+ window.fbAsyncInit = function() {
+ FB.init({
+ appId: "${appId}",
+ <g:if test="${channelUrl}">channelUrl: "${channelUrl}", // Custom channel URL</g:if>
+ cookie: <g:if test="${cookie}">true</g:if><g:else>false</g:else>, // enable cookies to allow the server to access the session
+ oauth: true, // enables OAuth 2.0
+ status: <g:if test="${status}">true</g:if><g:else>false</g:else>, // check login status
+ xfbml: <g:if test="${xfbml}">true</g:if><g:else>false</g:else>, // parse XFBML
+ frictionlessRequests: <g:if test="${frictionlessRequests}">true</g:if><g:else>false</g:else> // to enable frictionless requests
+ });
+ $('.fb-sdk-link').removeAttr('disabled').removeClass('disabled');
- <g:if test="${autoGrow}">
- FB.Canvas.setAutoGrow();
- </g:if>
+ <g:if test="${autoGrow}">
+ FB.Canvas.setAutoGrow();
+ </g:if>
- ${body}
- };
-
- (function() {
- var e = document.createElement("script");
- e.src = document.location.protocol + "//connect.facebook.net/${locale}/all.js";
- e.async = true;
- document.getElementById("fb-root").appendChild(e);
- }());
-</script>
+ ${body}
+ };
+ (function() {
+ var e = document.createElement("script");
+ e.src = document.location.protocol + "//connect.facebook.net/${locale}/all.js";
+ e.async = true;
+ document.getElementById("fb-root").appendChild(e);
+ }());
+ </script>
+</g:if>
@@ -1,31 +1,33 @@
<%@ page import="grails.converters.JSON" %>
-<script type="text/javascript">
- $(function() {
- $('a.fb-sdk-invite-link').click(function() {
- var link = $(this);
- var options = {
- method: 'apprequests',
- message: link.data('message')
- };
- if (link.data('data') != undefined) options['data'] = link.data('data');
- if (link.data('display') != undefined) options['display'] = link.data('display');
- if (link.data('exclude_ids') != undefined) options['exclude_ids'] = link.data('exclude_ids');
- if (link.data('filters') != undefined) options['filters'] = link.data('filters');
- if (link.data('max_recipients') != undefined) options['max_recipients'] = link.data('max_recipients');
- if (link.data('title') != undefined) options['title'] = link.data('title');
- if (link.data('to') != undefined) options['to'] = link.data('to');
- FB.ui(options, function(response) {
- if (link.data('callback') != undefined) {
- var callback = window[link.data('callback')];
- if (typeof callback === 'function') {
- callback(response);
+<g:if test="${includeScript}">
+ <script type="text/javascript">
+ $(function() {
+ $('a.fb-sdk-invite-link').click(function() {
+ var link = $(this);
+ var options = {
+ method: 'apprequests',
+ message: link.data('message')
+ };
+ if (link.data('data') != undefined) options['data'] = link.data('data');
+ if (link.data('display') != undefined) options['display'] = link.data('display');
+ if (link.data('exclude_ids') != undefined) options['exclude_ids'] = link.data('exclude_ids');
+ if (link.data('filters') != undefined) options['filters'] = link.data('filters');
+ if (link.data('max_recipients') != undefined) options['max_recipients'] = link.data('max_recipients');
+ if (link.data('title') != undefined) options['title'] = link.data('title');
+ if (link.data('to') != undefined) options['to'] = link.data('to');
+ FB.ui(options, function(response) {
+ if (link.data('callback') != undefined) {
+ var callback = window[link.data('callback')];
+ if (typeof callback === 'function') {
+ callback(response);
+ }
}
- }
+ });
+ return false;
});
- return false;
});
- });
-</script>
+ </script>
+</g:if>
<a <g:if test="${elementId}">id="${elementId}"</g:if>
class="<g:if test="${elementClass}">${elementClass} </g:if>fb-sdk-invite-link fb-sdk-link"
data-message="${message.encodeAsHTML()}"
@@ -1,33 +1,35 @@
-<script type="text/javascript">
- $(function() {
- $('a.fb-sdk-login-link').click(function(event) {
- var link = $(this);
- link.attr('disabled', 'disabled');
- FB.login(function(response) {
- if (link.data('callback') != undefined) {
- var callback = window[link.data('callback')];
- if (typeof callback === 'function') {
- callback(response, event.target);
- }
- } else if (response.authResponse) {
- // user is logged
- if (link.data('return_url')) {
- window.location.href = link.data('return_url');
+<g:if test="${includeScript}">
+ <script type="text/javascript">
+ $(function() {
+ $('a.fb-sdk-login-link').click(function(event) {
+ var link = $(this);
+ link.attr('disabled', 'disabled');
+ FB.login(function(response) {
+ if (link.data('callback') != undefined) {
+ var callback = window[link.data('callback')];
+ if (typeof callback === 'function') {
+ callback(response, event.target);
+ }
+ } else if (response.authResponse) {
+ // user is logged
+ if (link.data('return_url')) {
+ window.location.href = link.data('return_url');
+ } else {
+ window.location.reload();
+ }
+ } else if (link.data('cancel_url')) {
+ window.location.href = link.data('cancel_url');
} else {
- window.location.reload();
+ link.removeAttr('disabled');
}
- } else if (link.data('cancel_url')) {
- window.location.href = link.data('cancel_url');
- } else {
- link.removeAttr('disabled');
- }
- }, {
- scope: link.data('permissions')
+ }, {
+ scope: link.data('permissions')
+ });
+ return false;
});
- return false;
});
- });
-</script>
+ </script>
+</g:if>
<a <g:if test="${elementId}">id="${elementId}"</g:if>
class="<g:if test="${elementClass}">${elementClass} </g:if>fb-sdk-login-link fb-sdk-link"
data-permissions="${appPermissions instanceof List ? appPermissions.join(',') : appPermissions}"
@@ -1,11 +1,24 @@
-<script type="text/javascript">
- $(function() {
- $('a.fb-sdk-logout-link').click(function() {
- var link = $(this);
- link.attr('disabled', 'disabled');
- FB.getLoginStatus(function(response) {
- if (response.authResponse) {
- FB.logout(function(response) {
+<g:if test="${includeScript}">
+ <script type="text/javascript">
+ $(function() {
+ $('a.fb-sdk-logout-link').click(function() {
+ var link = $(this);
+ link.attr('disabled', 'disabled');
+ FB.getLoginStatus(function(response) {
+ if (response.authResponse) {
+ FB.logout(function(response) {
+ if (link.data('callback') != undefined) {
+ var callback = window[link.data('callback')];
+ if (typeof callback === 'function') {
+ callback(response);
+ }
+ } else if (link.data('next_url')) {
+ window.location.href = link.data('next_url');
+ } else {
+ window.location.reload();
+ }
+ });
+ } else {
if (link.data('callback') != undefined) {
var callback = window[link.data('callback')];
if (typeof callback === 'function') {
@@ -16,25 +29,14 @@
} else {
window.location.reload();
}
- });
- } else {
- if (link.data('callback') != undefined) {
- var callback = window[link.data('callback')];
- if (typeof callback === 'function') {
- callback(response);
- }
- } else if (link.data('next_url')) {
- window.location.href = link.data('next_url');
- } else {
- window.location.reload();
}
- }
+ });
+ return false;
});
- return false;
- });
- });
-</script>
+ });
+ </script>
+</g:if>
<a <g:if test="${elementId}">id="${elementId}"</g:if>
class="<g:if test="${elementClass}">${elementClass} </g:if>fb-sdk-logout-link fb-sdk-link"
<g:if test="${nextUrl}">data-next_url="${nextUrl}"</g:if>
Oops, something went wrong.

0 comments on commit a202f49

Please sign in to comment.