CanvasJSSDKBindings.jslib 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /**
  2. * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
  3. *
  4. * You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
  5. * copy, modify, and distribute this software in source code or binary form for use
  6. * in connection with the web services and APIs provided by Facebook.
  7. *
  8. * As with any software that integrates with the Facebook platform, your use of
  9. * this software is subject to the Facebook Developer Principles and Policies
  10. * [http://developers.facebook.com/policy/]. This copyright notice shall be
  11. * included in all copies or substantial portions of the software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  15. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  16. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  17. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  18. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. */
  20. var FBUnityLib = {
  21. $FBUnity: {
  22. init: function(connectFacebookUrl, locale, debug, initParams, status) {
  23. // make element for js sdk
  24. if (!document.getElementById('fb-root')) {
  25. var fbroot = document.createElement('div');
  26. fbroot.id = 'fb-root';
  27. var body = document.getElementsByTagName('body')[0];
  28. body.insertBefore(fbroot, body.children[0]);
  29. }
  30. // load js sdk
  31. var js, id = 'facebook-jssdk', ref = document.getElementsByTagName('script')[0];
  32. if (document.getElementById(id)) {return;}
  33. js = document.createElement('script'); js.id = id; js.async = true;
  34. js.src = connectFacebookUrl + '/' + locale + '/sdk' + (debug ? '/debug' : '') + '.js';
  35. ref.parentNode.insertBefore(js, ref);
  36. // once jssdk is loaded, init
  37. window.fbAsyncInit = function() {
  38. initParams = JSON.parse(initParams);
  39. initParams.hideFlashCallback = FBUnity.onHideUnity;
  40. FB.init(initParams);
  41. // send url to unity - needed for deep linking
  42. FBUnity.sendMessage('OnUrlResponse', location.href);
  43. if (status) {
  44. FBUnity.onInitWithStatus();
  45. } else {
  46. FBUnity.onInit();
  47. }
  48. };
  49. },
  50. initScreenPosition: function() {
  51. if (!screenPosition) {
  52. var body = document.getElementsByTagName('body')[0];
  53. var screenPosition = {omo : body.onmouseover || function(){}, iframeX: 0, iframeY: 0};
  54. body.onmouseover = function(e) {
  55. // Distance from top of screen to top of client area
  56. screenPosition.iframeX = e.screenX - e.clientX;
  57. screenPosition.iframeY = e.screenY - e.clientY;
  58. screenPosition.omo(e);
  59. }
  60. }
  61. },
  62. sendMessage: function(method, param) {
  63. SendMessage('FacebookJsBridge', method, param);
  64. },
  65. login: function(scope, callback_id) {
  66. FB.login(FBUnity.loginCallback.bind(null, callback_id), scope ? {scope: scope, auth_type: 'rerequest', return_scopes: true} : {return_scopes: true});
  67. },
  68. loginCallback: function(callback_id, response) {
  69. response = {'callback_id': callback_id, 'response': response};
  70. FBUnity.sendMessage('OnLoginComplete', JSON.stringify(response));
  71. },
  72. onInitWithStatus: function() {
  73. var timeoutHandler = setTimeout(function() { requestFailed(); }, 3000);
  74. function requestFailed() {
  75. FBUnity.onInit();
  76. }
  77. // try to get the login status right after init'ing
  78. FB.getLoginStatus(function(response) {
  79. clearTimeout(timeoutHandler);
  80. FBUnity.onInit(response);
  81. });
  82. },
  83. onInit: function(response) {
  84. var jsonResponse = '';
  85. if (response && response.authResponse) {
  86. jsonResponse = JSON.stringify(response);
  87. }
  88. FBUnity.sendMessage('OnInitComplete', jsonResponse);
  89. FB.Event.subscribe('auth.authResponseChange', function(r){ FBUnity.onAuthResponseChange(r) });
  90. FBUnity.logLoadingTime(response);
  91. },
  92. logLoadingTime: function(response) {
  93. FB.Canvas.setDoneLoading(
  94. function(result) {
  95. // send implicitly event to log the time from the canvas pages load to facebook init being called.
  96. FBUnity.logAppEvent('fb_canvas_time_till_init_complete', result.time_delta_ms / 1000, null);
  97. }
  98. );
  99. },
  100. onAuthResponseChange: function(response) {
  101. FBUnity.sendMessage('OnFacebookAuthResponseChange', response ? JSON.stringify(response) : '');
  102. },
  103. apiCallback: function(query, response) {
  104. response = {'query': query, 'response': response};
  105. FBUnity.sendMessage('OnFacebookAPIResponse', JSON.stringify(response));
  106. },
  107. api: function(query) {
  108. FB.api(query, FBUnity.apiCallback.bind(null, query));
  109. },
  110. activateApp: function() {
  111. FB.AppEvents.activateApp();
  112. },
  113. uiCallback: function(uid, callbackMethodName, response) {
  114. response = {'callback_id': uid, 'response': response};
  115. FBUnity.sendMessage(callbackMethodName, JSON.stringify(response));
  116. },
  117. logout: function() {
  118. FB.logout();
  119. },
  120. logAppEvent: function(eventName, valueToSum, parameters) {
  121. FB.AppEvents.logEvent(
  122. eventName,
  123. valueToSum,
  124. JSON.parse(parameters)
  125. );
  126. },
  127. logPurchase: function(purchaseAmount, currency, parameters) {
  128. FB.AppEvents.logPurchase(
  129. purchaseAmount,
  130. currency,
  131. JSON.parse(parameters)
  132. );
  133. },
  134. ui: function(x, uid, callbackMethodName) {
  135. x = JSON.parse(x);
  136. FB.ui(x, FBUnity.uiCallback.bind(null, uid, callbackMethodName));
  137. },
  138. hideUnity: function(direction) {
  139. direction = direction || 'hide';
  140. //TODO support this for webgl
  141. var unityDiv = jQuery(u.getUnity());
  142. if (direction == 'hide') {
  143. FBUnity.sendMessage('OnFacebookFocus', 'hide');
  144. } else /*show*/ {
  145. FBUnity.sendMessage('OnFacebookFocus', 'show');
  146. if (FBUnity.showScreenshotBackground.savedBackground) {
  147. /*
  148. if(fbShowScreenshotBackground.savedBackground == 'sentinel') {
  149. jQuery('body').css('background', null);
  150. } else {
  151. jQuery('body').css('background', fbShowScreenshotBackground.savedBackground);
  152. }
  153. */
  154. }
  155. hideUnity.savedCSS = FBUnity.showScreenshotBackground.savedBackground = null;
  156. }
  157. },
  158. showScreenshotBackground: function(pngbytes) /*and hide unity*/ {
  159. // window.screenxX and window.screenY = browser position
  160. // window.screen.height and window.screen.width = screen size
  161. // findPos, above, locates the iframe within the browser
  162. /*
  163. if (!fbShowScreenshotBackground.savedBackground)
  164. fbShowScreenshotBackground.savedBackground = jQuery('body').css('background') || 'sentinel';
  165. jQuery('body').css('background-image', 'url(data:image/png;base64,'+pngbytes+')');
  166. jQuery('body').css(
  167. 'background-position',
  168. -(screenPosition.iframeX)+'px '+
  169. -(screenPosition.iframeY)+'px'
  170. );
  171. jQuery('body').css('background-size', '100%');
  172. jquery('body').css('background-repeat', 'no-repeat');
  173. // TODO: Zoom detection
  174. */
  175. },
  176. onHideUnity: function(info) {
  177. if (info.state == 'opened') {
  178. FBUnity.sendMessage('OnFacebookFocus', 'hide');
  179. } else {
  180. FBUnity.sendMessage('OnFacebookFocus', 'show');
  181. }
  182. }
  183. },
  184. init: function(connectFacebookUrl, locale, debug, initParams, status) {
  185. var connectFacebookUrlString = UTF8ToString(connectFacebookUrl);
  186. var localeString = UTF8ToString(locale);
  187. var initParamsString = UTF8ToString(initParams);
  188. FBUnity.init(connectFacebookUrlString, localeString, debug, initParamsString, status);
  189. },
  190. initScreenPosition: function() {
  191. FBUnity.initScreenPosition();
  192. },
  193. login: function(scope, callback_id) {
  194. var scopeString = UTF8ToString(scope);
  195. var scopeArray = JSON.parse(scopeString);
  196. var callback_idString = UTF8ToString(callback_id);
  197. FBUnity.login(scopeArray, callback_idString);
  198. },
  199. activateApp: function() {
  200. FBUnity.activateApp();
  201. },
  202. logout: function() {
  203. FBUnity.logout();
  204. },
  205. logAppEvent: function(eventName, valueToSum, parameters) {
  206. var eventNameString = UTF8ToString(eventName);
  207. var parametersString = UTF8ToString(parameters);
  208. FBUnity.logAppEvent(eventNameString, valueToSum, parametersString);
  209. },
  210. logAppEventWithoutValue: function(eventName, parameters) {
  211. var eventNameString = UTF8ToString(eventName);
  212. var parametersString = UTF8ToString(parameters);
  213. FBUnity.logAppEvent(eventNameString, null, parametersString);
  214. },
  215. logPurchase: function(purchaseAmount, currency, parameters) {
  216. var currencyString = UTF8ToString(currency);
  217. var parametersString = UTF8ToString(parameters);
  218. FBUnity.logPurchase(purchaseAmount, currencyString, parametersString);
  219. },
  220. ui: function(x, uid, callbackMethodName) {
  221. var xString = UTF8ToString(x);
  222. var uidString = UTF8ToString(uid);
  223. var callbackMethodNameString = UTF8ToString(callbackMethodName);
  224. FBUnity.ui(xString, uidString, callbackMethodNameString);
  225. }
  226. };
  227. autoAddDeps(LibraryManager.library, '$FBUnity');
  228. mergeInto(LibraryManager.library, FBUnityLib);