facebook-ext.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. "FB:DATE",
  14. ]);
  15. /**
  16. * Contains uid usable in not-logged canvas scenarios as well.
  17. * Either it contains fb.uid, canvas user or
  18. * "loggedinuser"
  19. */
  20. fb.any_uid = fb.uid;
  21. if (fb.any_uid == -1) {
  22. fb.any_uid = request.params.fb_sig_canvas_user;
  23. if (!fb.any_uid) fb.any_uid = "loggedinuser";
  24. }
  25. fb.requireLogin = function (perms) {
  26. if (request.params.fb_sig_added != '1') {
  27. fb.redirect("http://www.facebook.com/login.php?"
  28. +"v=1.0&"
  29. +"canvas=1&"
  30. +(perms?"req_perms="+perms.join()+"&":"")
  31. +"api_key="+storage.facebooklib.apiKey+"&"
  32. +"next="+encodeURIComponent(fb.fullCanvasUrl));
  33. }
  34. };
  35. import("lib-support/useful");
  36. /** Place for convenience extension functions for UI */
  37. fb.ui = {
  38. /**
  39. * Creates tabs according to items parameter.
  40. * If actual url matches one of them, it is selected.
  41. * Next example renders tabs for .../callback/profile, .../callback/invite and .../callback/play.
  42. @example
  43. print(fb.ui.tabs("profile_Profile", "invite_Invite friends", "play_Play!"));
  44. */
  45. tabs: function (items) {
  46. var menu = FB_TABS();
  47. var lastSegment = request.path.split("/").pop();
  48. items.forEach (function (item) {
  49. var split = item.split("_");
  50. menu.push(FB_TAB_ITEM({
  51. href: split[0],
  52. title: split[1],
  53. selected: lastSegment === split[0]
  54. }));
  55. });
  56. return menu;
  57. },
  58. /**
  59. * Creates FB_INTL from supplied element.
  60. * Element should have desc (description),
  61. * body (text that may contain {tokens}),
  62. * tokens (comma-separated list of token names
  63. * to be inserted as FB_INTL_TOKEN, optional)
  64. * and multiline (if defined, contains delimiter token name
  65. * that is inserted as <br />, optional).
  66. * fbIntl.tokens contains functions that populate token bodies,
  67. * you should populate it if you want to have FB_INTL_TOKENs inserted.
  68. * If replacer function does not exist, the token is not inserted
  69. * and no warning or error is raised.
  70. */
  71. fbIntl: function fbIntl (element) {
  72. var result = FB_INTL({desc: element.desc});
  73. var body = [element.body];
  74. var tokens = element.tokens ? element.tokens.split(",") : [],
  75. tokenFuns = fbIntl.tokens;
  76. if (element.multiline) {
  77. tokens.push(element.multiline);
  78. tokenFuns = object(tokenFuns);
  79. var br = BR();
  80. tokenFuns[element.multiline] = function () { return br; };
  81. }
  82. tokens.forEach(function (token) {
  83. var f = tokenFuns[token];
  84. if (f) {
  85. if (f.direct) {
  86. splitReplace(body, "{"+token+"}", f(token, element));
  87. } else {
  88. result.push(FB_INTL_TOKEN({name:token}, f(token, element)));
  89. }
  90. }
  91. });
  92. result.unshift.apply(result, body);
  93. return result;
  94. },
  95. };
  96. fb.ui.fbIntl.tokens = {};