facebook-ext.js 3.4 KB

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