facebook-ext.js 3.3 KB

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