Commits

Shlomi Fish committed df4c888

Start writing calc_jqtree_data_from_html_w_nav_menu_json.

Comments (0)

Files changed (1)

Tests/js/jqtree-from-h-w-json-nav-menu-test.js

 };
 }
 
+if (!Array.prototype.map)
+{
+  Array.prototype.map = function(fun /*, thisp */)
+  {
+    "use strict";
+
+    if (this === void 0 || this === null)
+      throw new TypeError();
+
+    var t = Object(this);
+    var len = t.length >>> 0;
+    if (typeof fun !== "function")
+      throw new TypeError();
+
+    var res = new Array(len);
+    var thisp = arguments[1];
+    for (var i = 0; i < len; i++)
+    {
+      if (i in t)
+        res[i] = fun.call(thisp, t[i], i, t);
+    }
+
+    return res;
+  };
+}
+
 function get_base_relative_path (args) {
     var base = args.base;
     var current = args.current;
     return get_base_relative_path(args) + args.to;
 }
 
+function calc_jqtree_data_from_html_w_nav_menu_json (args) {
+    var base = args.base;
+    var current = args.current;
+
+    var _get_rel = function (href) {
+        return get_relative_path(
+            {
+                base: base,
+                current: current,
+                to: href,
+            }
+        );
+    };
+
+    // TODO : XSS - cross site scripting.
+    var _recurse;
+
+    _recurse = function(sub_tree) {
+        if ($.isArray(sub_tree)) {
+            return sub_tree.map(_recurse);
+        }
+
+        var ret = {
+            label: ("<a href=\"" +
+                _get_rel(sub_tree['url']) + "\">" + sub_tree['text'] + "</a>"),
+        };
+
+        if ('subs' in sub_tree) {
+            ret.children = _recurse(sub_tree.subs);
+        }
+
+        return ret;
+    };
+
+    return _recurse(args.input);
+}
+
 function test_nav_menu_generation()
 {
     module("NavMenu.Main.ToJQTree");
 
     test("NavMenu main test", function () {
-        expect(8);
+        expect(9);
 
         // TEST
         ok (true, "True is, well, true.");
             ),
             './../../humour/TheEnemy/te-heb.html'
         );
+
+        {
+            // TODO : test for title.
+            var input = [
+            {
+                "text": 'Shlomi Fish',
+                "url": "",
+                "subs": [
+                    {
+                        "text": "About Myself",
+                        "url": "me/"
+                    },
+                    {
+                        "text": "Humour",
+                        "url": "humour/"
+                    }
+                ]
+            }
+            ];
+
+            // TEST
+            var expected = [
+            {
+                label: '<a href="./../">Shlomi Fish</a>',
+                children: [
+                {
+                    label: '<a href="./../me/">About Myself</a>'
+                },
+                {
+                    label: '<a href="./../humour/">Humour</a>'
+                }
+                ],
+            },
+            ];
+
+            deepEqual (
+                calc_jqtree_data_from_html_w_nav_menu_json(
+                    {
+                        input: input,
+                        base: 'http://www.shlomifish.org/',
+                        current: 'http://www.shlomifish.org/humour/'
+                    }
+                ),
+                expected,
+                'Basic tree test No. 1'
+            );
+        }
     });
 }