Add XPI course.
[hacks/simpleWebSlides.git] / simpleWebSlides.js
index 76cbeba..cc030cf 100644 (file)
@@ -105,7 +105,6 @@ SWS.Templates = new function () {
     self.objectActivate = function (o) {
         if (!(o.hasClass("sws-active-object"))){
             o.removeClass("sws-inactive-object").addClass("sws-active-object");
-            o.css('visibility','visible');
             return true;
         };
         return false;
@@ -154,7 +153,10 @@ SWS.Effects = new function () {
 
     self.objectDeactivateFadeOut = function (o) {
         o.animate({'opacity': '0'}, 200,
-                  function () { SWS.Templates.objectDeactivate(o)});
+                  function () {
+
+                      SWS.Templates.objectDeactivate(o);
+                  });
     };
 
     self.objectActivateFadeIn = function (o) {
@@ -291,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 });
 
@@ -351,8 +354,8 @@ SWS.Presentation = new function () {
         var cur = info.current;
         var custom = info.custom;
         var real_slide = to_slide.find(".sws-slide");
-
-        real_slide.find("*").andSelf().each(function (i){
+        var dont_touch = real_slide.find("sws-protect").find("*").addBack();
+        real_slide.find("*").addBack().not(dont_touch).each(function (i){
             var frameset = $(this).data("sws-frame-set") || {};
             if (frameset[cur])
                 SWS.Config['sws-object-activate']($(this));
@@ -360,15 +363,20 @@ SWS.Presentation = new function () {
                 SWS.Config['sws-object-deactivate']($(this));
         });
 
-        var callbacks;
-        if (callbacks = info.callbacks.at_frame[self.getCurrentFrame()]){
-            for (var k = 0; k < callbacks.length; k++)
-                callbacks[k]($(to_slide));
-        };
 
         var all = $(from_slide).add(to_slide);
         all.find("*").addBack().promise().done(function() {
-            _disable_input_events = false;
+            var callbacks;
+            //execute callbacks when all elements are finished transitioning
+            if (callbacks = info.callbacks.at_frame[self.getCurrentFrame()]){
+                for (var k = 0; k < callbacks.length; k++)
+                    callbacks[k]($(to_slide));
+            };
+            all.find("*").addBack().promise().done(function() {
+                //wait for all elements to finish transitionning, in case a callback animate something
+                //an denable _input_events again.
+                _disable_input_events = false;
+            });
         });
     };
 
@@ -398,7 +406,6 @@ SWS.Presentation = new function () {
         $(".sws-canvas").each(function(i) {
             var canvas = $($(".sws-canvas")[i]);
             var infos = canvas.data("sws-frame-info");
-            console.log(infos);
             _total_steps += infos.last + 1;
         });
         return _total_steps;
@@ -490,9 +497,76 @@ 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) {
-        if (_disable_input_events || _print_mode ) return;
-        switch (event.which) {
+        if (_disable_input_events || _print_mode) return;
+        var code = 0;
+        switch (event.type) {
+        case 'touchstart':
+            _xstart = event.changedTouches[0].clientX;
+            return;
+        case 'touchend':
+
+            var dist = event.changedTouches[0].clientX - _xstart;
+            if (dist > 20) code = 37
+            else if (dist < -20) code = 39
+            else if (!$("#sws-control-panel-canvas").is(":visible")) code = 67;
+            break;
+        case 'keydown':
+            code = event.which;
+            break;
+        default:
+            return;
+        };
+        switch (code) {
         case 36:/* Home */
             self.setCurrentSlide(self.firstSlide());
             break;
@@ -500,7 +574,6 @@ SWS.Presentation = new function () {
         case 35:/* End */
             self.setCurrentSlide(self.lastSlide());
             break;
-
         case 32: /* space */
         case 34: /* PgDown */
         case 39: /* -> */
@@ -520,15 +593,16 @@ SWS.Presentation = new function () {
             self.previousSlide();
             break;
         case 83: /* s */
-            self.cycleStyle();
+                self.cycleStyle();
             return;
         case 67: /* c */
             $("#sws-control-panel-canvas").toggle();
+
         default:
             return;
         };
         self.refresh();
-    };
+};
 
 
 
@@ -561,7 +635,7 @@ SWS.Presentation = new function () {
             }
         };
 
-        var specials = null;
+        var specials = $([]);
 
         slide.find('*[class*="sws-onframe-"]').each(function(_){
             var cls = $(this).attr('class');
@@ -574,12 +648,10 @@ SWS.Presentation = new function () {
                 for(var f in o)
                     if (f > last_frame) last_frame = f;
                 $(this).find("*").andSelf().each(function(_){
-                    if (!SWS.Utils.isEmpty(o))
+                    if (!SWS.Utils.isEmpty(o)){
                         $(this).data("sws-frame-set", o);
-                    if (specials)
-                        specials.add($(this));
-                    else
-                        specials = $(this);
+                    }
+                    specials = specials.add($(this));
                 });
             };
         });
@@ -617,9 +689,9 @@ SWS.Presentation = new function () {
         self.redraw();
     };
 
-    self.getCurrentTheme = function () { 
+    self.getCurrentTheme = function () {
         var l = $("link.sws-theme[rel='stylesheet']")[0];
-        console.log(l);
+
         if (l) {
             return  l.title;
         } else
@@ -632,9 +704,7 @@ SWS.Presentation = new function () {
             theme_name = $("#sws-theme-select").val()
         else
             theme_name = name;
-//        if (self.getCurrentTheme() == theme_name) {
-  //          return;
-    //    };
+
         _current_theme = theme_name;
         $("link.sws-theme").each (function (i) {
             var e = this;
@@ -688,35 +758,63 @@ SWS.Presentation = new function () {
 
 
         $("html").addClass("sws-display");
-        $(window).resize(self.redraw);
-
+        //$(window).resize(self.redraw);
 
-        $(window).bind('storage', function (e) {
-            console.log(e);
-        });
+        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;
 
-        $(document).keydown(self.inputHandler);
+        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) {
 
-        var cur = self.getCurrentSlide();
-        $(".sws-slide").each (function (i) {
             var par = $(this).parent();
 
+
             $(this).remove();
             var canvas = $('<div class="sws-canvas"/>');
 
             if (!($(this).hasClass("sws-option-noheader"))) {
-                canvas.append($('<div class="sws-header"/><br/>'));
+                canvas.append($('<div class="sws-header"/>'));
             };
-
+            if (!$(this).hasClass("sws-cover")) {
+                var title = $($(this).find("h1")[0]);
+                var title_div = $('<div class="sws-title" />');
+                title_div.append(title);
+                canvas.append(title_div);
+            }
             var inner = $('<div class="sws-inner-canvas"/>');
             var content = $('<div class="sws-content"/>');
             $(this).find('script[type="text/javascript"]').remove();
             content.append($(this));
             inner.append(content);
-            inner.append($('<span class="sws-vertical-align"> </span><br/>'));
             canvas.append(inner);
 
             if (!($(this).hasClass("sws-option-nofooter"))) {
@@ -738,8 +836,6 @@ SWS.Presentation = new function () {
 
         });
 
-
-
         // Initialize the control panel
         $("body").append($(SWS.Templates.controlPanel));
         // Fill the theme switcher
@@ -770,20 +866,28 @@ SWS.Presentation = new function () {
         _update_ui();
 
         _slide_callbacks = null; /* avoid a leak */
-
         var passed_theme = SWS.Utils.getParameterByName("theme");
-        if (passed_theme == "")
-            self.changeTheme();
-        else
-            self.changeTheme(passed_theme);
 
-        if (SWS.Utils.getParameterByName("mode") == "print") {
-           self.printMode();
-        }
-        else
-            self.refresh();
-        _initialized = true;
 
+        //workaround weird chrome CSS loading bug
+        var f =
+            function () {
+                if (passed_theme == "")
+                    self.changeTheme();
+                else
+                    self.changeTheme(passed_theme);
+                if (SWS.Utils.getParameterByName("mode") == "print") {
+                    self.printMode();
+                }
+                else
+                    self.refresh();
+                $(document).keydown(self.inputHandler);
+                document.body.addEventListener('touchstart',self.inputHandler, false);
+                document.body.addEventListener('touchend',self.inputHandler, false);
+
+                _initialized = true;
+            };
+        setTimeout(f, 100);
     };
 
 };