.Merge branch 'master' of ssh://git.nguyen.vg/hacks/simpleWebSlides
[hacks/simpleWebSlides.git] / simpleWebSlides.js
index c63bf25..7483d4e 100644 (file)
@@ -60,6 +60,7 @@ SWS.Templates = new function () {
     self.helpPanel = "<div id='sws-help-panel-canvas'>\
 <h1>Keyboard shortcuts</h1>\
 <table>\
+<tr ><td style='color:#f55;'>h</td><td style='color:#f55;'> toggle help</td></tr>\
 <tr><td>c</td><td> toggle the control panel</td></tr>\
 <tr><td>Left, PgUp,swipe left</td><td> previous step</td></tr>\
 <tr><td>Right, PgDown, Space, swipe right</td><td> next step</td></tr>\
@@ -67,7 +68,6 @@ SWS.Templates = new function () {
 <tr><td>n</td><td> next slide</td></tr>\
 <tr><td>Home</td><td> first slide</td></tr>\
 <tr><td>End</td><td> last slide</td></tr>\
-<tr><td>h</td><td> toggle help</td></tr>\
 </table>\
 </div>";
     self.controlPanel = "<div id='sws-control-panel-canvas'><div id='sws-control-panel'>\
@@ -77,7 +77,7 @@ SWS.Templates = new function () {
 </div>\
 <div id='sws-control-panel-options'>\
 <span title='Change the aspect ratio' class='sws-symbol' >&#x1f4bb;</span><select id='sws-aspect-select' onchange='SWS.Presentation.changeAspect();'>\
-<option value='sws-aspect-4-3'>4:3</option>\
+<option value='sws-aspect-4-3' selected='selected'>4:3</option>\
 <option value='sws-aspect-16-9'>16:9</option>\
 <option value='sws-aspect-16-10'>16:10</option>\
 </select>\
@@ -163,7 +163,7 @@ SWS.Config = new SWS.ConfigBuilder ();
 
 SWS.Effects = new function () {
     var self = this;
-
+    
     self.objectDeactivateFadeOut = function (o) {
        if (o.is("embed")) return;
         o.animate({'opacity': '0'}, 200,
@@ -310,7 +310,6 @@ SWS.Presentation = new function () {
         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 });
 
     };
@@ -395,7 +394,7 @@ SWS.Presentation = new function () {
             };
             all.find("*").addBack().promise().done(function() {
                 //wait for all elements to finish transitionning, in case a callback animate something
-                //an denable _input_events again.
+                //anenable _input_events again.
                 _disable_input_events = false;
             });
         });
@@ -487,6 +486,11 @@ SWS.Presentation = new function () {
 
     self.printMode = function () {
         _print_mode = true;
+
+       var old_fx_status = $.fx.off;
+       //disable animation while printing.
+
+       $.fx.off = true;
         var progress = $("<div style='position:fixed;top:0pt;left:0pt;background:white;color:black;width:100%;height:100vh;z-index:200;' id='sws-print-progress'>Rendering presentation: <span id='sws-percent-progress'></span>%</div>");
         $("body").append(progress);
 
@@ -513,6 +517,9 @@ SWS.Presentation = new function () {
                 $("#sws-percent-progress").text(100);
                 progress.remove();
                window.status = 'Ready';
+               $.fx.off = old_fx_status;
+               if (SWS.Utils.getParameterByName("dialog") != "off")
+                   $("body").ready( function () { window.print(); });
             }
         };
         loop();
@@ -549,7 +556,12 @@ SWS.Presentation = new function () {
 
         var loop = function (doc, dir, add, ignoreFirst) {
             if (ignoreFirst !== true) {
-                var this_toc = { 'title' : doc.find("title").first().text(),
+                var this_toc = { 'title' : doc.find("title").first().text()
+                                .replace ("&", "&amp;")
+                                .replace("'","&apos;")
+                                .replace('"', "&quot;")
+                                .replace("<", "&lt;")
+                                .replace(">", "&gt;"),
                                  'sections' : build_sections(doc) };
                 add(toc, this_toc);
             };
@@ -567,20 +579,25 @@ SWS.Presentation = new function () {
 
 
 
-    var _xstart = 0;
+    var _startTouch = null;
+
     self.inputHandler = function (event) {
         if (_disable_input_events || _print_mode) return;
         var code = 0;
         switch (event.type) {
         case 'touchstart':
-            _xstart = event.changedTouches[0].clientX;
+           _startTouch = event.changedTouches[0];
             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;
+           if (!_startTouch) return;
+           var _endTouch = event.changedTouches[0];
+           var Xdist = _endTouch.clientX - _startTouch.clientX;
+           var Ydist = _endTouch.clientY - _startTouch.clientY;
+           if (Xdist > 40) code = 39
+           else if (Xdist < -40) code = 37
+            else if (Ydist > 20 && !$("#sws-control-panel-canvas").is(":visible")) code = 67;
+           else if (Ydist < -20  && $("#sws-control-panel-canvas").is(":visible")) code = 67;
+           else code = 39;
             break;
         case 'keydown':
             code = event.which;
@@ -600,6 +617,8 @@ SWS.Presentation = new function () {
         case 32: /* space */
         case 34: /* PgDown */
         case 39: /* -> */
+       case 176: /* Multimedia skip forward */
+       case 179: /* Multimedia play/pause */
             if (self.getCurrentSlide() == self.lastSlide()
                 && self.getCurrentFrame() == self.lastFrame()) return;
             self.next();
@@ -610,6 +629,7 @@ SWS.Presentation = new function () {
         case 8: /* backspace */
         case 33: /* PgUp */
         case 37: /* <-   */
+       case 177: /* Multimedia skip backward */
             self.previous();
             break;
         case 80: /* p */
@@ -708,10 +728,22 @@ SWS.Presentation = new function () {
             });
     };
     self.changeAspect = function() {
+       if (_print_mode) return;
+       var newClass = $("#sws-aspect-select").val();
+       var args = newClass.split("-");
+       var targetRatio = (args[2] - 0) / (args[3] - 0);
+       var realRatio = window.innerWidth / window.innerHeight;
+       var byClass = (targetRatio > realRatio ) ? "sws-by-height" : "sws-by-width";
+       if ($("html").hasClass(newClass)
+           && $("html").hasClass(byClass))
+           return;
+
         $("html").removeClass("sws-aspect-4-3")
             .removeClass("sws-aspect-16-9")
             .removeClass("sws-aspect-16-10")
-            .addClass($("#sws-aspect-select").val());
+           .removeClass("sws-by-width")
+           .removeClass("sws-by-height")
+            .addClass(newClass).addClass(byClass);
         self.redraw();
     };
 
@@ -830,18 +862,28 @@ SWS.Presentation = new function () {
             if (!($(this).hasClass("sws-option-noheader"))) {
                 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);
             canvas.append(inner);
+           var that = this;
+           [ "sws-cover", "sws-toc" ].forEach(
+               function(v) {
+                   if ($(that).hasClass(v)) {
+                       inner.addClass(v);
+                       $(that).removeClass(v);
+                   }
+               });
 
             if (!($(this).hasClass("sws-option-nofooter"))) {
                 canvas.append($('<div class="sws-footer"/>'));
@@ -906,16 +948,18 @@ SWS.Presentation = new function () {
                     self.printMode();
                 }
                 else {
-                   if (self.showHelpAtStartup()) $("#sws-help-panel-canvas").show();
+                   if (self.showHelpAtStartup()) $("#sws-help-panel-canvas").show().delay(5000).hide();
+                   self.changeAspect();
                     self.refresh();
                };
                 $(document).keydown(self.inputHandler);
                 document.body.addEventListener('touchstart',self.inputHandler, false);
                 document.body.addEventListener('touchend',self.inputHandler, false);
-               
+               $(window).resize(self.changeAspect);
                 _initialized = true;
             };
-        setTimeout(f, 100);
+        //setTimeout(f, 100);
+       f();
     };
 
 };