X-Git-Url: http://git.nguyen.vg/gitweb/?a=blobdiff_plain;f=simpleWebSlides.js;h=26ea8e21fcbd5a743fcbaa131f3171b9dfddbf9f;hb=f55977d9f090ee6bbace278733377ff50b7bec3a;hp=71f8e642b2b78481ec6b770302456ac631520ac0;hpb=b1f6566ac20a78b0de684e0dc2ff296fed24a195;p=hacks%2FsimpleWebSlides.git diff --git a/simpleWebSlides.js b/simpleWebSlides.js index 71f8e64..26ea8e2 100644 --- a/simpleWebSlides.js +++ b/simpleWebSlides.js @@ -1,104 +1,359 @@ -var SimpleWebSlides = new function () { - var _self = this; +/* + Namespace object. +*/ + +var SWS = {}; + +/* Utility functions */ +SWS.isUndefined = function (o) { return typeof o == "undefined"; }; + +/* + 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])); + }; + + }; + self.add(x, y) { + if (SWS.isUndefined(y) && isFinite(x)) self.add(x,x); + + + + + }; +5B + + + }; +}; +*/ + +SWS.Presentation = new function () { + + + var self = this; + + var templates = {}; + + //TODO: clean-up; var _total_slides; - _self.getNumSlides = function () { return _total_slides; }; + var _defered_custom = new Array(); + var _initialized = false; + var _animations_running = false; + + //Utility functions + + + + function push_following(t, i, v) { + if ((typeof t[i]) == 'undefined') { + t[i] = new Array(); + }; + var l = t[i].length; + if (l == 0 || !($.contains(t[i][l-1].get()[0], v.get()[0]))) { + t[i][l] = v; + }; + }; + + function push(t, i, v) { + if ((typeof t[i]) == 'undefined') { + t[i] = new Array(); + }; + var l = t[i].length; + t[i][l] = v; + }; + + + function init_canvas(canvas) { + var cur_frame = 0; + var last_frame = canvas.find(".sws-pause").length; + //Add all regular elements to the frame list + canvas.find("*").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); + }; + }); + + canvas.data("sws-frame-info", { current: 0, + last: last_frame, + custom: new Array() }); + + }; + + + //Public methods + self.setTemplate = function (tn, fn) { templates[tn] = fn; }; + + self.getNumSlides = function () { return _total_slides; }; + + + self.registerCustom = function (i, f) { + if (_initialized) return; + //jQuery does not seem to work well + //on partal doms + + var scripts = document.getElementsByTagName("script"); + var current = scripts[scripts.length-1]; + + var slide = $(current).parents(".sws-slide"); + if (slide.length == 0) { + console.log("no parent ?"); + return; + } + _defered_custom[_defered_custom.length] = { src: slide[0], + fn: f, + frame: i }; + $(current).remove(); + + }; if (typeof(Storage)!=="undefined"){ - _self.getCurrentSlide = function () { + self.getCurrentSlide = function () { //unary + casts to integer var i = +(sessionStorage.current_slide); - if (!(i >= 0 && i < _self.getNumSlides())){ + if (!(i >= 0 && i < self.getNumSlides())){ return 0; } else { return i; }; }; - _self.setCurrentSlide = function (i) { + self.setCurrentSlide = function (i) { sessionStorage.current_slide = i; }; } else { var _current_slide = 0; - _self.getCurrentSlide = function () { return current_slide; }; - _self.setCurrentSlide = function (i) { _current_slide = i; }; - - }; - _self.firstSlide = function () { return 0; }; - _self.lastSlide = function () { return _self.getNumSlides() - 1; }; - _self.refresh = function () { - var cur = _self.getCurrentSlide(); - $(".slide").each (function (i) { - if (i == cur){ - $(this).removeClass("other_slides").addClass("current_slide"); - } else if ($(this).hasClass("current_slide")) { - $(this).removeClass("current_slide").addClass("other_slides"); + self.getCurrentSlide = function () { return current_slide; }; + self.setCurrentSlide = function (i) { _current_slide = i; }; + + }; + self.firstSlide = function () { return 0; }; + self.lastSlide = function () { return self.getNumSlides() - 1; }; + + self.refresh = function () { + var to_slide = $(".sws-canvas")[self.getCurrentSlide()]; + var from_slide = $(".sws-active-slide")[0]; + + if (!(to_slide === from_slide)) { + templates['sws-template-slide-deactivate']( + $(from_slide), + function (o){ + o.removeClass("sws-active-slide") + .addClass("sws-inactive-slide"); + templates['sws-template-slide-activate']( + $(to_slide).removeClass("sws-inactive-slide") + .addClass("sws-active-slide"), + function (_){ + }); + }); + + }; + var info = $(to_slide).data("sws-frame-info"); + var cur = info.current; + var custom = info.custom; + + $(to_slide).find("*").each (function (i) { + var fcur = "f" + cur; + var frameset = $(this).data("sws-frame-set") || {}; + if (frameset[fcur]) { + if (!($(this).hasClass("sws-active-frame"))) { + templates['sws-template-frame-activate']( + $(this).removeClass("sws-inactive-frame") + .addClass("sws-active-frame"), + function (_){ }); + + }; + + } else { + if (!($(this).hasClass("sws-inactive-frame"))) { + templates['sws-template-frame-deactivate']( + $(this), + function (o){ + o.removeClass("sws-active-frame") + .addClass("sws-inactive-frame"); + }); + + }; + + }; }); + _animation_running = true; + var callbacks; + + if (callbacks = custom[self.getCurrentFrame()]){ + for (var k = 0; k < callbacks.length; k++) + callbacks[k]($(to_slide)); + }; + + $(to_slide).find("*").promise().done(function() { + _animation_running = false; + }); + }; + self.nextSlide = function () { + self.setCurrentSlide(Math.min(self.getCurrentSlide()+1, + self.lastSlide())); + }; + + self.previousSlide = function () { + self.setCurrentSlide(Math.max(self.getCurrentSlide()-1, + self.firstSlide())); + }; + + self.getFrameInfo = function () { + + var i = self.getCurrentSlide(); + var canvas = $($(".sws-canvas")[i]); + var infos = canvas.data("sws-frame-info"); + return infos; + }; + self.getCurrentFrame = function () { return self.getFrameInfo().current; }; + self.setCurrentFrame = function (i) { self.getFrameInfo().current = i; }; + self.firstFrame = function () { return 0; }; + self.lastFrame = function () { return self.getFrameInfo().last; }; + + self.nextFrame = function () { + self.setCurrentFrame(Math.min(self.getCurrentFrame()+1, + self.lastFrame())); + + }; + self.previousFrame = function () { + self.setCurrentFrame(Math.max(self.getCurrentFrame()-1, + self.firstFrame())); }; - _self.nextSlide = function () { - _self.setCurrentSlide(Math.min(_self.getCurrentSlide()+1, - _self.lastSlide())); + self.next = function () { + var i = self.getCurrentFrame(); + if (i == self.lastFrame()) + self.nextSlide(); + else + self.nextFrame(); }; - _self.previousSlide = function () { - _self.setCurrentSlide(Math.max(_self.getCurrentSlide()-1, - _self.firstSlide())); + self.previous = function () { + var i = self.getCurrentFrame(); + if (i == self.firstFrame()) + self.previousSlide(); + else + self.previousFrame(); }; - - _self.keyboardHandler = function (event) { + self.inputHandler = function (event) { + if (_animation_running) return; switch (event.which) { case 36:/* Home */ - _self.firstSlide(); + self.firstSlide(); break; case 35:/* End */ - _self.lastSlide(); + self.lastSlide(); break; case 32: /* space */ - case 34: /* PgDown */ case 39: /* -> */ + case 1: /* mouse button 1 */ + self.next(); + break; + case 34: /* PgDown */ case 78: /* n */ - _self.nextSlide(); + self.nextSlide(); break; case 8: /* backspace */ - case 33: /* PgUp */ case 37: /* <- */ + self.previous(); + break; + case 33: /* PgUp */ case 80: /* p */ - _self.previousSlide(); + self.previousSlide(); break; default: return; }; - _self.refresh(); + self.refresh(); }; - _self.init = function () { - _total_slides = $(".slide").length; - $(document).keydown(_self.keyboardHandler); + function default_footer_template() { + var elem = '
'; + return $(elem); + }; + + self.setTemplate('sws-template-footer', function (x) { }); + self.setTemplate('sws-template-frame-activate', function (x, f) {f(x);}); + self.setTemplate('sws-template-frame-deactivate', function (x, f) {f(x);}); + self.setTemplate('sws-template-slide-activate', function (x, f) {f(x);}); + self.setTemplate('sws-template-slide-deactivate', function (x, f) {f(x);}); + + + + function init () { + + _total_slides = $(".sws-slide").length; + + $(document).keydown(self.inputHandler); + $(document).mousedown(self.inputHandler); - var cur = _self.getCurrentSlide(); - $(".slide").each (function (i) { + var cur = self.getCurrentSlide(); + $(".sws-slide").each (function (i) { + if (!($(this).hasClass("sws-option-nofooter"))) { + $(this).append(templates['sws-template-footer']()); + }; + var par = $(this).parent(); + $(this).remove(); + var canvas = $(''); + canvas.append($(this)); + par.append(canvas); if (i == cur) { - $(this).addClass("current_slide").removeClass("other_slides"); + canvas + .addClass("sws-active-slide") + .removeClass("sws-inacitve-slide"); } else { - $(this).addClass("other_slides").removeClass("current_slide"); - }; - var elem = ' '; - if (!($(this).hasClass("nofooter"))) { - $(this).append($(elem)); + canvas + .addClass("sws-inactive-slide") + .removeClass("sws-active-slide"); }; + init_canvas(canvas); + }); - }; + for (var i = 0; i < _defered_custom.length; i++){ -}; + var custom = _defered_custom[i]; + var canvas = $(custom.src).parents(".sws-canvas"); + if (canvas.length == 0) continue; + canvas = $(canvas[0]); + + var info = canvas.data("sws-frame-info"); + push(info.custom, custom.frame, custom.fn); + }; + self.refresh(); + _initialized = true; + + }; -$(document).ready(SimpleWebSlides.init); + self.init = function () { + $(document).ready(init); + }; + +};