facebook-ext.js 3.4 KB

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