Working and clean-up 'on-slide' specification.
[hacks/simpleWebSlides.git] / simpleWebSlides.js
index e592eb8..16de314 100644 (file)
@@ -18,6 +18,31 @@ SWS.Utils = new function () {
         t[i][l] = v;
     };
 
+    self.isEmpty = function (o) {
+        for(var _ in o) return false;
+        return true;
+    };
+
+    self.parseFrameSpec = function (s) {
+        var elems = s.split("_");
+        var result = {};
+        var min_last = 10000;
+        var max_value = -1;
+        for(var i = 0; i < elems.length; i++){
+            var bounds = elems[i].split("-");
+            if (bounds.length > 2 || bounds.length == 0) return {};
+            if (bounds.length == 1) bounds[1] = bounds[0];
+            var a = parseInt(bounds[0]);
+            var b = parseInt(bounds[1])
+            if (!isFinite(a) || !isFinite(b)) return {};
+            a = Math.min(a, 1000); // don't allow more than 1000 frames/slide
+            b = Math.min(b, 1000);
+            if (b > max_value) max_value = b;
+            for (var j = a; j <= b; j++)
+                result[j] = true;
+        };
+        return result;
+    };
 
     self.slideActivate = function (o) {
         if (!(o.hasClass("sws-active-slide"))){
@@ -41,14 +66,18 @@ SWS.Utils = new function () {
         if (!(o.hasClass("sws-active-object"))){
             o.removeClass("sws-inactive-object").addClass("sws-active-object");
             o.css({'visibility':'visible'});
+            return true;
         };
+        return false;
     };
 
     self.objectDeactivate = function (o) {
-        o.css({'visibility':'hidden', 'display':''});
         if (!(o.hasClass("sws-inactive-object"))){
             o.addClass("sws-inactive-object").removeClass("sws-active-object");
+            o.css({'visibility':'hidden'});
+            return true;
         };
+        return false;
     };
     self.updateFooter = function (o) {
         var footer = o.find(".sws-footer");
@@ -70,15 +99,42 @@ SWS.Utils = new function () {
 
 SWS.Effects = new function () {
     var self = this;
+
     self.objectDeactivateFadeOut = function (o) {
-        o.fadeOut(150, function () { SWS.Utils.objectDeactivate(o)});
+        o.animate({'opacity': '0'}, 150,
+                  function () { SWS.Utils.objectDeactivate(o)});
     };
 
     self.objectActivateFadeIn = function (o) {
-        SWS.Utils.objectActivate(o);
-        o.fadeIn(150);
+
+        if (SWS.Utils.objectActivate(o)){
+            o.animate({'opacity': '1' }, 150);
+        };
+
     };
 
+    self.slideChangeHorizontalFlip = function (from, to){
+        var f = SWS.Presentation.getSlide(from);
+        var t = SWS.Presentation.getSlide(to);
+        f.animate({ 'left': '50%', 'width': '0pt', 'opacity':'0' }, 150,
+                  function  () {
+                      SWS.Utils.slideDeactivate(f);
+                      f.css({'left':'0%', 'width': '100%'});
+                      t.css({ 'left': '50%', 'width': '0pt','opacity':'0' });
+                      SWS.Utils.slideActivate(t);
+                      t.animate({'left':'0%', 'width': '100%','opacity':'1'});
+                  });
+    };
+    self.slideChangeFadeOutIn = function (from, to) {
+        var f = SWS.Presentation.getSlide(from);
+        var t = SWS.Presentation.getSlide(to);
+        f.animate({ 'opacity': '0'}, 150,
+                  function () { SWS.Utils.slideDeactivate(f);
+                                SWS.Utils.slideActivate(t);
+                                t.css('opacity', '0');
+                                t.animate({ 'opacity': '1'}, 150);
+                              });
+    };
     self.slideChangeHorizontalSlide = function (from, to) {
         var f = SWS.Presentation.getSlide(from);
         var t = SWS.Presentation.getSlide(to);
@@ -114,51 +170,19 @@ SWS.Effects = new function () {
 
 };
 
-/*
-  Representation of intervals of positive integers.
-
-SWS.Interval = new function (init) {
-
-    var self = this;
-    self.data = new Array();
-    self.parseString(s) {
-        var elems = s.split("_");
-        for(var i = 0; i < elems.length; i++){
-            var bounds = elems[i].split("-");
-            if (bounds.length > 2) continue;
-            if (SWS.isUndefined(bounds[1])) bounds[1] = bounds[0];
-            if (!(isFinite(bounds[0]) && isFinite(bounds[1]))) continue;
-
-            self.add(+(bounds[0]), +(bounds[1]));
-        };
-7C
-    };
-    self.add(x, y) {
-        if (SWS.isUndefined(y) && isFinite(x)) self.add(x,x);
-
-
-
-
-    };
-5B
-
-
-    };
-};
-*/
 
 SWS.Presentation = new function () {
 
 
     var self = this;
 
+    //TODO move outside of the Presentation object
     var templates = {};
 
 
-    //TODO: clean-up;
     var _total_slides;
     var _initialized = false;
-    var _animations_running = false;
+    var _disable_input_events = false;
 
     var _slide_callbacks = new Array ();
 
@@ -212,15 +236,15 @@ SWS.Presentation = new function () {
     self.lastSlide = function () { return self.getNumSlides() - 1; };
 
     self.refresh = function () {
-        /* block upcoming input event until all animation are finished */
-        _animations_running = true;
+        /* block upcoming input event until all animations are finished */
+        _disable_input_events = true;
 
         var canvas = $(".sws-canvas");
         var from_slide_num = canvas.index($(".sws-active-slide"));
         var to_slide_num = self.getCurrentSlide();
         var watch_slide_anim = false;
         var to_slide = $(canvas[to_slide_num]);
-        var slide_change = !_initialized || (from_slide_num != to_slide_num);
+        var slide_change = (from_slide_num != to_slide_num);
 
         var info = to_slide.data("sws-frame-info");
         if (slide_change) {
@@ -238,10 +262,10 @@ SWS.Presentation = new function () {
         var cur = info.current;
         var custom = info.custom;
         var real_slide = to_slide.children(".sws-slide");
-        real_slide.find("*").add(real_slide).each (function (i) {
-            var fcur = "f" + cur;
+
+        to_slide.children(".sws-slide").find("*").andSelf().each(function (i){
             var frameset = $(this).data("sws-frame-set") || {};
-            if (frameset[fcur])
+            if (frameset[cur])
                 templates['sws-object-activate']($(this));
             else
                 templates['sws-object-deactivate']($(this));
@@ -259,7 +283,7 @@ SWS.Presentation = new function () {
         };
 
         to_watch.find("*").promise().done(function() {
-            _animations_running = false;
+            _disable_input_events = false;
         });
     };
 
@@ -312,14 +336,14 @@ SWS.Presentation = new function () {
     };
 
     self.inputHandler = function (event) {
-        if (_animations_running) return;
+        if (_disable_input_events) return;
         switch (event.which) {
         case 36:/* Home */
-            self.firstSlide();
+            self.setCurrentSlide(self.firstSlide());
             break;
 
         case 35:/* End */
-            self.lastSlide();
+            self.setCurrentSlide(self.lastSlide());
             break;
 
         case 32: /* space */
@@ -359,24 +383,49 @@ SWS.Presentation = new function () {
         if (SWS.Utils.isUndefined(custom)) {
             custom = new Array ();
         };
+
         for (var i = 0; i < custom.length; i++) {
-            if (isFinite(custom[i].frame))
-                SWS.Utils.push2(callbacks.at_frame, +(custom[i].frame), custom[i].fn);
-            else if (custom[i].frame == "slide")
+            if (isFinite(custom[i].frame)){
+                var num = +(custom[i].frame);
+                if (num > last_frame) last_frame = num;
+                SWS.Utils.push2(callbacks.at_frame, num, custom[i].fn);
+            } else if (custom[i].frame == "slide")
                 callbacks.at_slide.push(custom[i].fn);
+            else {
+                var frame_set = SWS.Utils.parseFrameSpec(custom[i].frame);
+                for(var f in frame_set){
+                    if (f > last_frame) last_frame = f;
+                    SWS.Utils.push2(callbacks.at_frame, +(f), custom[i].fn);
+                };
+            }
         };
 
-        slide.find("*").add(slide).each(function(i) {
-            if ($(this).filter('*[class*="sws-onslide-"]').length) {
-            } else {
-                if ($(this).hasClass("sws-pause"))  cur_frame++;
-                var o = {};
-                for (var j = cur_frame; j <= last_frame; j++)
-                    o[ "f" + j ] = true;
-                $(this).data("sws-frame-set", o);
+        var specials = slide.find('*[class*="sws-onslide-"]').find("*").andSelf();
+        specials.each(function(i) {
+            var cls = $(this).attr('class');
+            var idx = cls.indexOf("sws-onslide-");
+            if (idx >= 0) {
+                var end = cls.indexOf(" ", idx);
+                end = (end == -1) ? cls.length : end;
+                var spec = cls.substring(idx+12, end);
+                var o = SWS.Utils.parseFrameSpec(spec);
+                for(var f in o)
+                    if (f > last_frame) last_frame = f;
+
+                if (!SWS.Utils.isEmpty(o))
+                    $(this).data("sws-frame-set", o);
             };
         });
 
+        slide.find("*").andSelf().not(specials).each(function(i) {
+            if ($(this).hasClass("sws-pause"))  cur_frame++;
+            var o = {};
+            for (var j = cur_frame; j <= last_frame; j++)
+                o[ j ] = true;
+            if (!SWS.Utils.isEmpty(o))
+                $(this).data("sws-frame-set", o);
+        });
+
         canvas.data("sws-frame-info", { current: 0,
                                         last: last_frame,
                                         callbacks: callbacks