PageRenderTime 55ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/src/core/prelude.js

http://github.com/facebook/connect-js
JavaScript | 206 lines | 71 code | 12 blank | 123 comment | 16 complexity | ca5c6f86df74a3c69fc1db0fd427c64a MD5 | raw file
  1. /**
  2. * Copyright Facebook Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. *
  17. *
  18. * @provides fb.prelude
  19. */
  20. /**
  21. * Prelude.
  22. *
  23. * Namespaces are one honking great idea -- let's do more of those!
  24. * -- Tim Peters
  25. *
  26. * The Prelude is what keeps us from being messy. In order to co-exist with
  27. * arbitary environments, we need to control our footprint. The one and only
  28. * rule to follow here is that we need to limit the globals we introduce. The
  29. * only global we should every have is ``FB``. This is exactly what the prelude
  30. * enables us to do.
  31. *
  32. * The main method to take away from this file is `FB.copy()`_. As the name
  33. * suggests it copies things. Its powerful -- but to get started you only need
  34. * to know that this is what you use when you are augmenting the FB object. For
  35. * example, this is skeleton for how ``FB.Event`` is defined::
  36. *
  37. * FB.provide('Event', {
  38. * subscribe: function() { ... },
  39. * unsubscribe: function() { ... },
  40. * fire: function() { ... }
  41. * });
  42. *
  43. * This is similar to saying::
  44. *
  45. * FB.Event = {
  46. * subscribe: function() { ... },
  47. * unsubscribe: function() { ... },
  48. * fire: function() { ... }
  49. * };
  50. *
  51. * Except it does some housekeeping, prevents redefinition by default and other
  52. * goodness.
  53. *
  54. * .. _FB.copy(): #method_FB.copy
  55. *
  56. * @class FB
  57. * @static
  58. * @access private
  59. */
  60. if (!window.FB) {
  61. FB = {
  62. // use the init method to set these values correctly
  63. _apiKey : null,
  64. _session : null,
  65. _userStatus : 'unknown', // or 'notConnected' or 'connected'
  66. // logging is enabled by default. this is the logging shown to the
  67. // developer and not at all noisy.
  68. _logging: true,
  69. _inCanvas: (
  70. (window.location.search.indexOf('fb_sig_in_iframe=1') > -1) ||
  71. (window.location.search.indexOf('session=') > -1)),
  72. //
  73. // DYNAMIC DATA
  74. //
  75. // the various domains needed for using Connect
  76. _domain: {
  77. api : 'https://api.facebook.com/',
  78. api_read : 'https://api-read.facebook.com/',
  79. cdn : (window.location.protocol == 'https:'
  80. ? 'https://s-static.ak.fbcdn.net/'
  81. : 'http://static.ak.fbcdn.net/'),
  82. graph : 'https://graph.facebook.com/',
  83. staticfb : 'http://static.ak.facebook.com/',
  84. www : window.location.protocol + '//www.facebook.com/'
  85. },
  86. _locale: null,
  87. _localeIsRtl: false,
  88. /**
  89. * Copies things from source into target.
  90. *
  91. * @access private
  92. * @param target {Object} the target object where things will be copied
  93. * into
  94. * @param source {Object} the source object where things will be copied
  95. * from
  96. * @param overwrite {Boolean} indicate if existing items should be
  97. * overwritten
  98. * @param tranform {function} [Optional], transformation function for
  99. * each item
  100. */
  101. copy: function(target, source, overwrite, transform) {
  102. for (var key in source) {
  103. if (overwrite || typeof target[key] === 'undefined') {
  104. target[key] = transform ? transform(source[key]) : source[key];
  105. }
  106. }
  107. return target;
  108. },
  109. /**
  110. * Create a namespaced object.
  111. *
  112. * @access private
  113. * @param name {String} full qualified name ('Util.foo', etc.)
  114. * @param value {Object} value to set. Default value is {}. [Optional]
  115. * @return {Object} The created object
  116. */
  117. create: function(name, value) {
  118. var node = window.FB, // We will use 'FB' as root namespace
  119. nameParts = name ? name.split('.') : [],
  120. c = nameParts.length;
  121. for (var i = 0; i < c; i++) {
  122. var part = nameParts[i];
  123. var nso = node[part];
  124. if (!nso) {
  125. nso = (value && i + 1 == c) ? value : {};
  126. node[part] = nso;
  127. }
  128. node = nso;
  129. }
  130. return node;
  131. },
  132. /**
  133. * Copy stuff from one object to the specified namespace that
  134. * is FB.<target>.
  135. * If the namespace target doesn't exist, it will be created automatically.
  136. *
  137. * @access private
  138. * @param target {Object|String} the target object to copy into
  139. * @param source {Object} the source object to copy from
  140. * @param overwrite {Boolean} indicate if we should overwrite
  141. * @return {Object} the *same* target object back
  142. */
  143. provide: function(target, source, overwrite) {
  144. // a string means a dot separated object that gets appended to, or created
  145. return FB.copy(
  146. typeof target == 'string' ? FB.create(target) : target,
  147. source,
  148. overwrite
  149. );
  150. },
  151. /**
  152. * Generates a weak random ID.
  153. *
  154. * @access private
  155. * @return {String} a random ID
  156. */
  157. guid: function() {
  158. return 'f' + (Math.random() * (1<<30)).toString(16).replace('.', '');
  159. },
  160. /**
  161. * Logs a message for the developer if logging is on.
  162. *
  163. * @access private
  164. * @param args {Object} the thing to log
  165. */
  166. log: function(args) {
  167. if (FB._logging) {
  168. //TODO what is window.Debug, and should it instead be relying on the
  169. // event fired below?
  170. //#JSCOVERAGE_IF 0
  171. if (window.Debug && window.Debug.writeln) {
  172. window.Debug.writeln(args);
  173. } else if (window.console) {
  174. window.console.log(args);
  175. }
  176. //#JSCOVERAGE_ENDIF
  177. }
  178. // fire an event if the event system is available
  179. if (FB.Event) {
  180. FB.Event.fire('fb.log', args);
  181. }
  182. },
  183. /**
  184. * Shortcut for document.getElementById
  185. * @method $
  186. * @param {string} DOM id
  187. * @return DOMElement
  188. * @access private
  189. */
  190. $: function(id) {
  191. return document.getElementById(id);
  192. }
  193. };
  194. }