PageRenderTime 38ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/src/xfbml/tags/comments.js

https://bitbucket.org/jaredstanley/facebook-js-sdk
JavaScript | 120 lines | 49 code | 8 blank | 63 comment | 4 complexity | 8b9be0526354fcc546099d4e81a6f096 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. * @provides fb.xfbml.comments
  17. * @layer xfbml
  18. * @requires fb.type fb.xfbml.iframewidget fb.auth
  19. */
  20. /**
  21. * Implementation for fb:comments tag.
  22. *
  23. * @class FB.XFBML.Comments
  24. * @extends FB.XFBML.IframeWidget
  25. * @private
  26. */
  27. FB.subclass('XFBML.Comments', 'XFBML.IframeWidget', null, {
  28. /**
  29. * Make the iframe visible only when we get the initial resize message.
  30. */
  31. _visibleAfter: 'resize',
  32. /**
  33. * Refresh the iframe on auth.statusChange events.
  34. */
  35. _refreshOnAuthChange: true,
  36. /**
  37. * Do initial attribute processing.
  38. */
  39. setupAndValidate: function() {
  40. // query parameters to the comments iframe
  41. var attr = {
  42. channel_url : this.getChannelUrl(),
  43. css : this.getAttribute('css'),
  44. notify : this.getAttribute('notify'),
  45. numposts : this.getAttribute('num-posts', 10),
  46. quiet : this.getAttribute('quiet'),
  47. reverse : this.getAttribute('reverse'),
  48. simple : this.getAttribute('simple'),
  49. title : this.getAttribute('title', document.title),
  50. url : this.getAttribute('url', document.URL),
  51. width : this._getPxAttribute('width', 550),
  52. xid : this.getAttribute('xid')
  53. };
  54. // default xid to current URL
  55. if (!attr.xid) {
  56. // We always want the URL minus the hash "#" also note the encoding here
  57. // and down below when the url is built. This is intentional, so the
  58. // string received by the server is url-encoded and thus valid.
  59. var index = document.URL.indexOf('#');
  60. if (index > 0) {
  61. attr.xid = encodeURIComponent(document.URL.substring(0, index));
  62. }
  63. else {
  64. attr.xid = encodeURIComponent(document.URL);
  65. }
  66. }
  67. this._attr = attr;
  68. return true;
  69. },
  70. /**
  71. * Setup event handlers.
  72. */
  73. oneTimeSetup: function() {
  74. this.subscribe('xd.addComment', FB.bind(this._handleCommentMsg, this));
  75. },
  76. /**
  77. * Get the initial size.
  78. *
  79. * @return {Object} the size
  80. */
  81. getSize: function() {
  82. return { width: this._attr.width, height: 200 };
  83. },
  84. /**
  85. * Get the URL bits for the iframe.
  86. *
  87. * @return {Object} the iframe URL bits
  88. */
  89. getUrlBits: function() {
  90. return { name: 'comments', params: this._attr };
  91. },
  92. /**
  93. * Invoked by the iframe when a comment is added. Note, this feature needs to
  94. * be enabled by specifying the notify=true attribute on the tag. This is in
  95. * order to improve performance by only requiring this overhead when a
  96. * developer explicitly said they want it.
  97. *
  98. * @param message {Object} the message received via XD
  99. */
  100. _handleCommentMsg: function(message) {
  101. //TODO (naitik) what should we be giving the developers here? is there a
  102. // comment_id they can get?
  103. if (!this.isValid()) {
  104. return;
  105. }
  106. FB.Event.fire('comments.add', {
  107. post: message.post,
  108. user: message.user,
  109. widget: this
  110. });
  111. }
  112. });