facebook-ext.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /* appjet:version 0.1 */
  2. /* appjet:library */
  3. // Copyright (c) 2009, 2010, Herbert Vojčík
  4. // Licensed by MIT license (http://www.opensource.org/licenses/mit-license.php)
  5. import("facebook", "storage");
  6. // TODO new FBML tags
  7. importTags(this, [
  8. "FB:INTL",
  9. "FB:INTL-TOKEN",
  10. "FB:TAG-ATTRIBUTE",
  11. "FB:TAG-BODY",
  12. "FB:TAG",
  13. ]);
  14. /**
  15. * Contains uid usable in not-logged canvas scenarios as well.
  16. * Either it contains fb.uid, canvas user or
  17. * "loggedinuser"
  18. */
  19. fb.any_uid = fb.uid;
  20. if (fb.any_uid == -1) {
  21. fb.any_uid = request.params.fb_sig_canvas_user;
  22. if (!fb.any_uid) fb.any_uid = "loggedinuser";
  23. }
  24. fb.requireLogin = function (perms) {
  25. if (request.params.fb_sig_added != '1') {
  26. fb.redirect("http://www.facebook.com/login.php?"
  27. +"v=1.0&"
  28. +"canvas=1&"
  29. +(perms?"req_perms="+perms.join()+"&":"")
  30. +"api_key="+storage.facebooklib.apiKey+"&"
  31. +"next="+encodeURIComponent(fb.fullCanvasUrl));
  32. }
  33. };
  34. /** Place for convenience extension functions for UI */
  35. fb.ui = {
  36. /**
  37. * Creates tabs according to items parameter.
  38. * If actual url matches one of them, it is selected.
  39. * Next example renders tabs for .../callback/profile, .../callback/invite and .../callback/play.
  40. @example
  41. print(fb.ui.tabs("profile_Profile", "invite_Invite friends", "play_Play!"));
  42. */
  43. tabs: function (items) {
  44. var menu = FB_TABS();
  45. var lastSegment = request.path.split("/").pop();
  46. items.forEach (function (item) {
  47. var split = item.split("_");
  48. menu.push(FB_TAB_ITEM({
  49. href: split[0],
  50. title: split[1],
  51. selected: lastSegment === split[0]
  52. }));
  53. });
  54. return menu;
  55. },
  56. /**
  57. * Creates FB_INTL from supplied element.
  58. * Element should have desc (description),
  59. * body (text that may contain {tokens}),
  60. * tokens (comma-separated list of token names
  61. * to be inserted as FB_INTL_TOKEN, optional)
  62. * and multiline (if defined, contains delimiter token name
  63. * that is inserted as <br />, optional).
  64. * fbIntl.tokens contains functions that populate token bodies,
  65. * you should populate it if you want to have FB_INTL_TOKENs inserted.
  66. * If replacer function does not exist, the token is not inserted
  67. * and no warning or error is raised.
  68. */
  69. fbIntl: function fbIntl (element) {
  70. var result = FB_INTL({desc: element.desc});
  71. result.push(element.body);
  72. var tokens = element.tokens ? element.tokens.split(",") : [],
  73. tokenFuns = fbIntl.tokens;
  74. if (element.multiline) {
  75. tokens.push(element.multiline);
  76. tokenFuns = object(tokenFuns);
  77. var br = BR();
  78. tokenFuns[element.multiline] = function () { return br; };
  79. }
  80. tokens.forEach(function (token) {
  81. var f = tokenFuns[token];
  82. if (f) { result.push(FB_INTL_TOKEN({name:token}, f(token, element))); }
  83. });
  84. return result;
  85. },
  86. };
  87. fb.ui.fbIntl.tokens = {};