Change the CSS for print mode.
[hacks/simpleWebSlides.git] / simpleWebSlides.js
index 9993a51..d0165f0 100644 (file)
@@ -52,13 +52,31 @@ SWS.Utils = new function () {
 
 SWS.Templates = new function () {
     var self = this;
-    self.controlPanel = "<div id='sws-control-panel'>\
-<a onclick='SWS.Presentation.goToSlide(SWS.Presentation.firstSlide());'>◀◀◀</a>\
-<a onclick='SWS.Presentation.previousSlide();SWS.Presentation.refresh();'>◀◀ </a>\
-<a onclick='SWS.Presentation.previous();SWS.Presentation.refresh();'>◀</a>\
-<a onclick='SWS.Presentation.next();SWS.Presentation.refresh();'>▶</a>\
-<a onclick='SWS.Presentation.nextSlide();SWS.Presentation.refresh();'>▶▶</a>\
-<a rel='Last slide' onclick='SWS.Presentation.goToSlide(SWS.Presentation.lastSlide());'>▶▶▶</a>\
+    self.controlPanel = "<div id='sws-control-panel-canvas'><div id='sws-control-panel'>\
+<div id='sws-control-panel-title-bar'>\
+<a title='Toggle fullscreen' id='sws-control-panel-fullscreen' class='sws-symbol' onclick='SWS.Presentation.toggleFullScreen();'></a>\
+<a title='Close panel' id='sws-control-panel-close' onclick='$(\"#sws-control-panel-canvas\").toggle();'>&#x2716;</a>\
+</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-16-9'>16:9</option>\
+<option value='sws-aspect-16-10'>16:10</option>\
+</select>\
+<span title='Change the theme' class='sws-symbol'>&#x1f3a8;</span><select id='sws-theme-select' onchange='SWS.Presentation.changeTheme();'></select>\
+</div>\
+<div id='sws-control-panel-navigation'>\
+<a title='First slide' class='sws-symbol' onclick='SWS.Presentation.goToSlide(SWS.Presentation.firstSlide());' >&#x23ee;</a>\
+<a title='Previous slide' class='sws-symbol' onclick='SWS.Presentation.previousSlide();SWS.Presentation.refresh();'>&#x23ea;</a>\
+<a title='Previous step' class='sws-symbol' style='-webkit-transform: scaleX(-1);' onclick='SWS.Presentation.previous();SWS.Presentation.refresh();'>&#x25b6;</a>\
+<a title='Next step' class='sws-symbol' onclick='SWS.Presentation.next();SWS.Presentation.refresh();'>&#x25b6;</a>\
+<a title='Next slide' class='sws-symbol' onclick='SWS.Presentation.nextSlide();SWS.Presentation.refresh();'>&#x23e9;</a>\
+<a title='Last slide' class='sws-symbol' onclick='SWS.Presentation.goToSlide(SWS.Presentation.lastSlide());'>&#x23ed;</a>\
+<br/>\
+<span class='sws-symbol'>&#xe4ae;</span><input type='text' id='sws-control-panel-slide-input' oninput='SWS.Presentation.goToSlide($(\"#sws-control-panel-slide-input\").val()-1);'></input><span id='sws-control-panel-total-slides'></span>\
+<input type='range' title='Navigate the presentation' id='sws-control-panel-navigation-bar' onchange='SWS.Presentation.navigate();' step='1'></input>\
+</div>\
+</div>\
 </div>";
     self.slideActivate = function (o) {
         if (!(o.hasClass("sws-active-slide"))){
@@ -103,7 +121,7 @@ SWS.Templates = new function () {
                          + (i + 1)
                          +"</span>");
             var sep = $( "<span class='sws-slide-num-sep' />");
-            var tot = $( "<span class='sws-current-slide-number'>"
+            var tot = $( "<span class='sws-last-slide-number'>"
                          + (SWS.Presentation.getNumSlides())
                          +"</span>");
             footer.append(cur).append(sep).append(tot);
@@ -168,13 +186,19 @@ SWS.Effects = new function () {
         var t = SWS.Presentation.getSlide(to);
         if (from < to) {
             t.css('left', '100%');
+            t.css('opacity', '1');
             SWS.Templates.slideActivate(t);
-            f.animate({ 'left': '-100%' }, 250, function () { SWS.Templates.slideDeactivate(f); });
-            t.animate({ 'left': '0%' }, 250);
+            f.animate({ 'left': '-100%' }, 250, function () { SWS.Templates.slideDeactivate(f);
+                                                              f.css('opacity', '0');
+                                                              t.animate({ 'left': '0%' }, 250);
+                                                            });
         } else {
             t.css('left', '-100%');
             SWS.Templates.slideActivate(t);
-            f.animate({ 'left': '100%' }, 250, function () { SWS.Templates.slideDeactivate(f); });
+            f.animate({ 'left': '100%' }, 250, function () { SWS.Templates.slideDeactivate(f);
+                                                             f.css('opacity', '0');
+                                                           });
+            t.css('opacity', '1');
             t.animate({ 'left': '0%' }, 250);
         };
     };
@@ -198,6 +222,43 @@ SWS.Effects = new function () {
 
 };
 
+SWS.Fullscreen = new function () {
+    var self = this;
+
+    if (SWS.Utils.isUndefined(document.fullScreen)) {
+        if (SWS.Utils.isUndefined(document.mozfullScreen)) {
+            self.status = function () { return document.webkitIsFullScreen; };
+            self.enter = function(e) {
+                e.webkitRequestFullScreen();
+            };
+            self.exit = function () {
+                document.webkitCancelFullScreen();
+            };
+
+        } else {
+            self.status = function () { return document.mozfullScreen; };
+            self.enter = function(e) {
+                e.mozRequestFullScreen();
+            };
+            self.exit = function () {
+                document.mozCancelFullScreen();
+            };
+
+        };
+    } else {
+            self.status = function () { return document.fullScreen; };
+            self.enter = function(e) {
+                e.requestFullScreen();
+            };
+            self.exit = function () {
+                document.cancelFullScreen();
+            };
+
+    };
+
+
+};
+
 SWS.Presentation = new function () {
 
 
@@ -262,15 +323,17 @@ SWS.Presentation = new function () {
         var to_slide_num = self.getCurrentSlide();
         var watch_slide_anim = false;
         var to_slide = $(canvas[to_slide_num]);
+        var from_slide = $(canvas[from_slide_num]);
         var slide_change = (from_slide_num != to_slide_num);
 
         var info = to_slide.data("sws-frame-info");
+        SWS.Config['sws-update-header'](to_slide);
+        SWS.Config['sws-update-footer'](to_slide);
+
         if (slide_change) {
             //Launch a slide transition:
             SWS.Config['sws-slide-change'](from_slide_num, to_slide_num);
             watch_slide_anim = true;
-            SWS.Config['sws-update-header'](to_slide);
-            SWS.Config['sws-update-footer'](to_slide);
             for (var i = 0; i < info.callbacks.at_slide.length;i++){
                 info.callbacks.at_slide[i](to_slide);
             };
@@ -279,9 +342,9 @@ SWS.Presentation = new function () {
 
         var cur = info.current;
         var custom = info.custom;
-        var real_slide = to_slide.children(".sws-slide");
+        var real_slide = to_slide.find(".sws-slide");
 
-        to_slide.children(".sws-slide").find("*").andSelf().each(function (i){
+        real_slide.find("*").andSelf().each(function (i){
             var frameset = $(this).data("sws-frame-set") || {};
             if (frameset[cur])
                 SWS.Config['sws-object-activate']($(this));
@@ -295,12 +358,8 @@ SWS.Presentation = new function () {
                 callbacks[k]($(to_slide));
         };
 
-        var to_watch = $(to_slide).find("*");
-        if (watch_slide_anim) {
-            to_watch = to_watch.add(to_slide).add($(canvas[from_slide_num]));
-        };
-
-        to_watch.find("*").promise().done(function() {
+        var all = $(from_slide).add(to_slide);
+        all.find("*").addBack().promise().done(function() {
             _disable_input_events = false;
         });
     };
@@ -308,11 +367,13 @@ SWS.Presentation = new function () {
     self.nextSlide = function () {
         self.setCurrentSlide(Math.min(self.getCurrentSlide()+1,
                                       self.lastSlide()));
+        self.setCurrentFrame(self.firstFrame());
     };
 
     self.previousSlide = function () {
         self.setCurrentSlide(Math.max(self.getCurrentSlide()-1,
                                       self.firstSlide()));
+        self.setCurrentFrame(self.firstFrame());
     };
 
     self.getFrameInfo = function () {
@@ -339,9 +400,10 @@ SWS.Presentation = new function () {
 
     self.next = function () {
         var i = self.getCurrentFrame();
-        if (i == self.lastFrame())
+        if (i == self.lastFrame()) {
             self.nextSlide();
-        else
+            self.setCurrentFrame(self.firstFrame());
+        } else
             self.nextFrame();
     };
 
@@ -360,11 +422,18 @@ SWS.Presentation = new function () {
             f = 0;
         if (!(s >= self.firstSlide() && s <= self.lastSlide())) return;
         self.setCurrentSlide(s);
-        if (!(f >= self.firstFrame() && s <= self.lastFrame())) f = 0;
+        if (!(f >= self.firstFrame() && f <= self.lastFrame())) f = 0;
         self.setCurrentFrame(f);
         self.refresh();
     };
 
+    self.cycleStyle = function() {
+        var styles = $("head").children('link[rel$="stylesheet"][title]');
+        var j = styles.index(styles.filter(':not(:disabled)'));
+        styles[j].disabled = true;
+        if (++j == styles.length) j = 0;
+        styles[j].disabled = false;
+    };
 
     self.inputHandler = function (event) {
         if (_disable_input_events) return;
@@ -379,7 +448,8 @@ SWS.Presentation = new function () {
 
         case 32: /* space */
         case 39: /* -> */
-
+            if (self.getCurrentSlide() == self.lastSlide()
+                && self.getCurrentFrame() == self.lastFrame()) return;
             self.next();
             break;
         case 34: /* PgDown */
@@ -394,8 +464,11 @@ SWS.Presentation = new function () {
         case 80: /* p */
             self.previousSlide();
             break;
+        case 83: /* s */
+            self.cycleStyle();
+            return;
         case 67: /* c */
-            $("#sws-control-panel").toggle();
+            $("#sws-control-panel-canvas").toggle();
         default:
             return;
         };
@@ -408,7 +481,7 @@ SWS.Presentation = new function () {
         var cur_frame = 0;
         var last_frame = canvas.find(".sws-pause").length;
         //Add all regular elements to the frame list
-        var slide = $(canvas.children(".sws-slide")[0]);
+        var slide = $(canvas.find(".sws-slide")[0]);
 
         var callbacks = { at_slide : new Array(),
                           at_frame : new Array() }
@@ -472,8 +545,67 @@ SWS.Presentation = new function () {
 
     };
 
+    /* Forces redrawing the page without reloading */
+    self.redraw = function () {
+        $("body").hide();
+        $("body").show();
+    };
+
+    self.changeAspect = function() {
+        $("html").removeClass("sws-aspect-4-3")
+            .removeClass("sws-aspect-16-9")
+            .removeClass("sws-aspect-16-10")
+            .addClass($("#sws-aspect-select").val());
+        self.redraw();
+    };
+
+
+    self.changeTheme = function () {
+        var theme_name = $("#sws-theme-select").val();
+        $("link.sws-theme").each (function (i) {
+            var e = $(this);
+            var title =  e.attr("title");
+            e[0].disabled = (title != theme_name);
+        });
+        self.redraw();
+    };
+
+
+    var _fullscreen_icon_on = "&#xe746;";
+    var _fullscreen_icon_off = "&#xe744;";
+
+    self.toggleFullScreen = function () {
+        if (SWS.Fullscreen.status()) {
+            SWS.Fullscreen.exit();
+            $("a#sws-control-panel-fullscreen")
+                .html(_fullscreen_icon_off);
+
+
+
+        } else {
+            SWS.Fullscreen.enter($("body")[0]);
+            $("a#sws-control-panel-fullscreen")
+                .html(_fullscreen_icon_on);
+        };
+    };
+    function _update_ui() {
+        var nav = $('#sws-control-panel-navigation-bar');
+        nav.val(SWS.Presentation.getCurrentSlide() + 1);
+        $('#sws-control-panel-slide-input').val(nav.val());
+    }
+    self.navigate = function () {
+        self.goToSlide($("#sws-control-panel-navigation-bar").val()-1);
+        _update_ui();
+    };
+
+
     self.init = function () {
-        console.log("inited");
+
+
+
+        $(window).resize(self.redraw);
+
+
         $(window).bind('storage', function (e) {
             console.log(e);
         });
@@ -481,7 +613,8 @@ SWS.Presentation = new function () {
         _total_slides = $(".sws-slide").length;
 
         $(document).keydown(self.inputHandler);
-        $("body").append($(SWS.Templates.controlPanel));
+
+
         var cur = self.getCurrentSlide();
         $(".sws-slide").each (function (i) {
             var par = $(this).parent();
@@ -490,19 +623,27 @@ SWS.Presentation = new function () {
             var canvas = $('<div class="sws-canvas"/>');
 
             if (!($(this).hasClass("sws-option-noheader"))) {
-                canvas.append($('<div class="sws-header"/>'));
+                canvas.append($('<div class="sws-header"/><br/>'));
             };
+
+            var inner = $('<div class="sws-inner-canvas"/>');
+            var content = $('<div class="sws-content"/>');
             $(this).find('script[type="text/javascript"]').remove();
-            canvas.append($(this));
+            content.append($(this));
+            inner.append(content);
+            inner.append($('<span class="sws-vertical-align"> </span><br/>'));
+            canvas.append(inner);
+
             if (!($(this).hasClass("sws-option-nofooter"))) {
                 canvas.append($('<div class="sws-footer"/>'));
             };
+
             par.append(canvas);
 
             if (i == cur) {
                 canvas
                     .addClass("sws-active-slide")
-                    .removeClass("sws-inacitve-slide");
+                    .removeClass("sws-inactive-slide");
             } else {
                 canvas
                     .addClass("sws-inactive-slide")
@@ -511,10 +652,43 @@ SWS.Presentation = new function () {
             init_canvas(canvas,_slide_callbacks[i]);
 
         });
+
+
+
+        // Initialize the control panel
+        $("body").append($(SWS.Templates.controlPanel));
+        // Fill the theme switcher
+        $("link.sws-theme").each (function (i) {
+            var e = $(this);
+            var opt = "<option value='";
+            opt += e.attr("title");
+            opt += "' ";
+            if (e.attr("rel") == "stylesheet") {
+                opt+= "selected='selected'";
+            };
+            opt += ">" + e.attr("title") + "</option>";
+            $("#sws-theme-select").append($(opt));
+        });
+        // Set the fullscreen icon
+        if (SWS.Fullscreen.status()) {
+            $("a#sws-control-panel-fullscreen")
+                .html(_fullscreen_icon_on);
+        } else {
+            $("a#sws-control-panel-fullscreen")
+                .html(_fullscreen_icon_off);
+        };
+        // Put the navigation range at the correct position
+        var nav = $('#sws-control-panel-navigation-bar');
+        nav.attr("min", SWS.Presentation.firstSlide() + 1);
+        nav.attr("max", SWS.Presentation.lastSlide() + 1);
+        $('#sws-control-panel-total-slides').text('/' + SWS.Presentation.getNumSlides());
+        _update_ui();
+
         _slide_callbacks = null; /* avoid a leak */
         self.refresh();
         _initialized = true;
 
+
     };
 
 };