Implement table of content.
[hacks/simpleWebSlides.git] / simpleWebSlides.js
index cd19624..f42811d 100644 (file)
@@ -293,8 +293,9 @@ SWS.Presentation = new function () {
         if (_initialized) return;
         //jQuery does not seem to work well
         //on a partial DOM
-
-        var slide_num = $(".sws-slide").length - 1;
+        var slides = $(".sws-slide");
+        var h1s = $("body").children("h1");
+        var slide_num = slides.add(h1s).length - 1;
 
         SWS.Utils.push2(_slide_callbacks, slide_num,{ 'fn': f, 'frame': i });
 
@@ -490,6 +491,53 @@ SWS.Presentation = new function () {
 
     }
 
+    self.buildFullTOC = function () {
+
+        var build_sections = function (doc) {
+            var res = [];
+            var h1s = doc.find("body").first().children("h1");
+            var slides = doc.find("body").first().children(".sws-slide");
+            var slide_num = 1;
+            var collection = h1s.add(slides);
+            collection.each (function () {
+                if ($(this).is("h1")) {
+                    res.push({ 'title' : $(this).text(),
+                               'slide' : slide_num });
+                } else {
+                    slide_num++;
+                }
+            });
+            return res;
+        };
+
+        var toc = [];
+
+        var append = function (a,e) {
+            return a.push(e);
+        };
+        var prepend = function (a,e) {
+            return a.unshift(e);
+        };
+
+        var loop = function (doc, dir, add, ignoreFirst) {
+            if (ignoreFirst !== true) {
+                var this_toc = { 'title' : doc.find("title").first().text(),
+                                 'sections' : build_sections(doc) };
+                add(toc, this_toc);
+            };
+            var url = doc.find(dir).first().attr("href");
+            if (!SWS.Utils.isUndefined(url) && url != "") {
+                $.ajax({ 'url' : url, 'async' : false ,'success' : function (page) {
+                    loop ($(page), dir, add, false);
+                }});
+            };
+        };
+        loop ($(document), ".sws-previous", prepend, false);
+        return toc;
+
+    };
+
+
 
     var _xstart = 0;
     self.inputHandler = function (event) {
@@ -706,11 +754,38 @@ SWS.Presentation = new function () {
         $("html").addClass("sws-display");
         //$(window).resize(self.redraw);
 
+        var slides = $(".sws-slide");
+        var h1s = $("body").children("h1");
+        var slide_num = slides.add(h1s).length - 1;
 
-
-        _total_slides = $(".sws-slide").length;
+        _total_slides = $(".sws-slide").add($("body").children("h1")).length;
 
         var cur = self.getCurrentSlide();
+        var toc = self.buildFullTOC();
+        var common_html = "<div class='sws-slide sws-toc'><h1>Plan</h1><ul style='list-style-type:none'>";
+        var i;
+        for (i= 0; i < toc.length - 1; i++)
+            common_html += "<li class='done'>" + (i+1) +
+            ' ' + toc[i].title + "</li>";
+
+        common_html += "<li>" + toc.length + ' ' + toc[toc.length - 1].title;
+        common_html += "<ul style='list-style-type:none' >";
+        var sections = toc[toc.length - 1].sections;
+        $("body").children("h1").each(function (i) {
+            var this_html = common_html;
+            var j;
+            var secnum = toc.length + '.';
+            for (j = 0; j < i; ++j)
+                this_html += "<li class='done'>" + secnum + (j+1) + ' ' + 
+                sections[j].title + "</li>";
+            this_html += "<li class='hl'>" + secnum + (i+1) + ' ' +
+                sections[i].title + "</li>";
+            for (j = i+1; j < sections.length; j++)
+                this_html += "<li>" + secnum + (j+1) + ' ' 
+                +sections[j].title + "</li>";
+            this_html += "</ul></li></ul></div>";
+            $(this).after(this_html);
+        });
 
         $(".sws-slide").each(function (i) {
 
@@ -786,6 +861,8 @@ SWS.Presentation = new function () {
 
         _slide_callbacks = null; /* avoid a leak */
         var passed_theme = SWS.Utils.getParameterByName("theme");
+
+
         //workaround weird chrome CSS loading bug
         var f =
             function () {