facebook-ext.js 3.5 KB

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