facebook-ext.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. import("lib-support/useful");
  35. /** Place for convenience extension functions for UI */
  36. fb.ui = {
  37. /**
  38. * Creates tabs according to items parameter.
  39. * If actual url matches one of them, it is selected.
  40. * Next example renders tabs for .../callback/profile, .../callback/invite and .../callback/play.
  41. @example
  42. print(fb.ui.tabs("profile_Profile", "invite_Invite friends", "play_Play!"));
  43. */
  44. tabs: function (items) {
  45. var menu = FB_TABS();
  46. var lastSegment = request.path.split("/").pop();
  47. items.forEach (function (item) {
  48. var split = item.split("_");
  49. menu.push(FB_TAB_ITEM({
  50. href: split[0],
  51. title: split[1],
  52. selected: lastSegment === split[0]
  53. }));
  54. });
  55. return menu;
  56. },
  57. /**
  58. * Creates FB_INTL from supplied element.
  59. * Element should have desc (description),
  60. * body (text that may contain {tokens}),
  61. * tokens (comma-separated list of token names
  62. * to be inserted as FB_INTL_TOKEN, optional)
  63. * and multiline (if defined, contains delimiter token name
  64. * that is inserted as <br />, optional).
  65. * fbIntl.tokens contains functions that populate token bodies,
  66. * you should populate it if you want to have FB_INTL_TOKENs inserted.
  67. * If replacer function does not exist, the token is not inserted
  68. * and no warning or error is raised.
  69. */
  70. fbIntl: function fbIntl (element) {
  71. var result = FB_INTL({desc: element.desc});
  72. var body = [element.body];
  73. var tokens = element.tokens ? element.tokens.split(",") : [],
  74. tokenFuns = fbIntl.tokens;
  75. if (element.multiline) {
  76. tokens.push(element.multiline);
  77. tokenFuns = object(tokenFuns);
  78. var br = BR();
  79. tokenFuns[element.multiline] = function () { return br; };
  80. }
  81. tokens.forEach(function (token) {
  82. var f = tokenFuns[token];
  83. if (f) {
  84. if (f.direct) {
  85. splitReplace(body, "{"+token+"}", f(token, element));
  86. } else {
  87. result.push(FB_INTL_TOKEN({name:token}, f(token, element)));
  88. }
  89. }
  90. });
  91. result.unshift.apply(result, body);
  92. return result;
  93. },
  94. };
  95. fb.ui.fbIntl.tokens = {};