Browse Source

fb.ui.fbIntl added, splitReplace added to useful.js

Herbert Vojčík 13 years ago
parent
commit
fa806bb339
2 changed files with 77 additions and 20 deletions
  1. 52 20
      lib-app/facebook-ext.js
  2. 25 0
      lib-support/useful.js

+ 52 - 20
lib-app/facebook-ext.js

@@ -3,7 +3,7 @@
 // Copyright (c) 2009, 2010, Herbert Vojčík
 // Licensed by MIT license (http://www.opensource.org/licenses/mit-license.php)
 
-import("facebook", "storage");
+import("facebook", "storage", "lib-support/useful");
 
 // TODO new FBML tags
 importTags(this, [
@@ -38,25 +38,57 @@ fb.requireLogin = function (perms) {
 
 /** Place for convenience extension functions for UI */
 fb.ui = {
-/**
- * Creates tabs according to items parameter.
- * If actual url matches one of them, it is selected.
- * Next example renders tabs for .../callback/profile, .../callback/invite and .../callback/play.
- @example
- print(fb.ui.tabs("profile_Profile", "invite_Invite friends", "play_Play!"));
- */
-    tabs: function (items) {
-        var menu = FB_TABS();
-        var lastSegment = request.path.split("/").pop();
-        items.forEach (function (item) {
-            var split = item.split("_");
-            menu.push(FB_TAB_ITEM({
-                href: split[0],
-                title: split[1],
-                selected: lastSegment === split[0]
-            }));
-        });
-        return menu;
+  /**
+   * Creates tabs according to items parameter.
+   * If actual url matches one of them, it is selected.
+   * Next example renders tabs for .../callback/profile, .../callback/invite and .../callback/play.
+   @example
+   print(fb.ui.tabs("profile_Profile", "invite_Invite friends", "play_Play!"));
+   */
+  tabs: function (items) {
+      var menu = FB_TABS();
+      var lastSegment = request.path.split("/").pop();
+      items.forEach (function (item) {
+          var split = item.split("_");
+          menu.push(FB_TAB_ITEM({
+              href: split[0],
+              title: split[1],
+              selected: lastSegment === split[0]
+          }));
+      });
+      return menu;
+  },
+  /**
+   * Creates FB_INTL from supplied element.
+   * Element should have desc (description),
+   * body (text that may contain {tokens}),
+   * tokens (comma-separated list of token names
+   * to be inserted as FB_INTL_TOKEN, optional)
+   * and multiline (if defined, contains delimiter
+   * that is replaced with BR()s, optional).
+   * fbIntl.tokens contains functions that populate token bodies,
+   * you should populate it if you want to have FB_INTL_TOKENs inserted.
+   * If replacer function does not exist, the token is not inserted
+   * and no warning or error is raised.
+   */
+  fbIntl: function (element) {
+    var fbIntl = FB_INTL({desc: element.desc});
+    if (!element.multiline && !element.tokens) {
+      fbIntl.push(element.body);
+      return fbIntl;
     }
+    var parts = [ element.body ];
+    if (element.multiline) { parts = splitReplace(parts, element.multiline, BR()); }
+    if (element.tokens) {
+      element.tokens.split(",").forEach(function (token) {
+        var f = fbIntl.tokens[token];
+        if (f) { parts.push(FB_INTL_TOKEN({name:token}, f(token, element))); }
+      });
+    }
+    fbIntl.push.apply(fbIntl, parts);
+    return fbIntl;
+  },
 };
 
+fb.ui.fbIntl.tokens = {};
+

+ 25 - 0
lib-support/useful.js

@@ -136,6 +136,29 @@ function etagCaching(etag) {
     }
 }
 
+/**
+  * Splits all elements of an array by delimiter
+  * and insert o in places where split occurred.
+  * Returns modified array with split parts and delimiting o's.
+  */
+function splitReplace(array, delimiter, o) {
+  var i, j, k, l = 0, m = array.length;
+  array.forEach(function (v, k, a) {
+    var split = a[k] = v.split(delimiter);
+    l += 2 * split.length - 1;
+  });
+  array.length = l;
+  for (var i = m-1, j = l-1; i >= 0; i--) {
+    var parts = array[i];
+    for (var k = parts.length - 1; k > 0; --k) {
+      array[j--] = parts[k--];
+      array[j--] = o;
+    }
+    array[j--] = parts[0];
+  }
+  return array;
+}
+
 /* appjet:server */
 import("lib-support/jsunit");
 var _l = import({}, "lib-support/useful");
@@ -314,6 +337,8 @@ page.testSuite = [
 	    assertEquals("Stub returns incorrect value 1", "hi", r1);
 	    assertEquals("Stub returns incorrect value 2", "hi", r2);
 	},
+  
+  //TODO tests for splitReplace
 
 ];