9 SWS.Utils = new function () {
12 self.isUndefined = function (o) { return typeof o == "undefined"; };
13 self.push2 = function (t, i, v) {
14 if ((typeof t[i]) == 'undefined') {
21 self.isEmpty = function (o) {
22 for(var _ in o) return false;
26 self.parseFrameSpec = function (s) {
27 var elems = s.split("_");
31 for(var i = 0; i < elems.length; i++){
32 var bounds = elems[i].split("-");
33 if (bounds.length > 2 || bounds.length == 0) return {};
34 if (bounds.length == 1) bounds[1] = bounds[0];
35 var a = parseInt(bounds[0]);
36 var b = parseInt(bounds[1])
37 if (!isFinite(a) || !isFinite(b)) return {};
38 a = Math.min(a, 1000); // don't allow more than 1000 frames/slide
39 b = Math.min(b, 1000);
40 if (b > max_value) max_value = b;
41 for (var j = a; j <= b; j++)
53 SWS.Templates = new function () {
55 self.controlPanel = "<div id='sws-control-panel-canvas'><div id='sws-control-panel'>\
56 <div id='sws-control-panel-title-bar'>\
57 <a title='Toggle fullscreen' id='sws-control-panel-fullscreen' class='sws-symbol' onclick='SWS.Presentation.toggleFullScreen();'></a>\
58 <a title='Close panel' id='sws-control-panel-close' onclick='$(\"#sws-control-panel-canvas\").toggle();'>✖</a>\
60 <div id='sws-control-panel-options'>\
61 <span title='Change the aspect ratio' class='sws-symbol' >💻</span><select id='sws-aspect-select' onchange='SWS.Presentation.changeAspect();'>\
62 <option value='sws-aspect-4-3'>4:3</option>\
63 <option value='sws-aspect-16-9'>16:9</option>\
64 <option value='sws-aspect-16-10'>16:10</option>\
66 <span title='Change the theme' class='sws-symbol'>🎨</span><select id='sws-theme-select' onchange='SWS.Presentation.changeTheme();'></select>\
68 <div id='sws-control-panel-navigation'>\
69 <a title='First slide' class='sws-symbol' onclick='SWS.Presentation.goToSlide(SWS.Presentation.firstSlide());' >⏮</a>\
70 <a title='Previous slide' class='sws-symbol' onclick='SWS.Presentation.previousSlide();SWS.Presentation.refresh();'>⏪</a>\
71 <a title='Previous step' class='sws-symbol' style='-webkit-transform: scaleX(-1);' onclick='SWS.Presentation.previous();SWS.Presentation.refresh();'>▶</a>\
72 <a title='Next step' class='sws-symbol' onclick='SWS.Presentation.next();SWS.Presentation.refresh();'>▶</a>\
73 <a title='Next slide' class='sws-symbol' onclick='SWS.Presentation.nextSlide();SWS.Presentation.refresh();'>⏩</a>\
74 <a title='Last slide' class='sws-symbol' onclick='SWS.Presentation.goToSlide(SWS.Presentation.lastSlide());'>⏭</a>\
76 <span class='sws-symbol'></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>\
77 <input type='range' title='Navigate the presentation' id='sws-control-panel-navigation-bar' onchange='SWS.Presentation.navigate();' step='1'></input>\
81 self.slideActivate = function (o) {
82 if (!(o.hasClass("sws-active-slide"))){
83 o.removeClass("sws-inactive-slide").addClass("sws-active-slide");
87 self.slideDeactivate = function (o) {
88 if (!(o.hasClass("sws-inactive-slide"))){
89 o.removeClass("sws-active-slide").addClass("sws-inactive-slide");
93 self.slideChange = function (from, to) {
94 var canvas = $(".sws-canvas");
95 self.slideDeactivate($(canvas[from]));
96 self.slideActivate($(canvas[to]));
99 self.objectActivate = function (o) {
100 if (!(o.hasClass("sws-active-object"))){
101 o.removeClass("sws-inactive-object").addClass("sws-active-object");
102 o.css({'visibility':'visible'});
108 self.objectDeactivate = function (o) {
109 if (!(o.hasClass("sws-inactive-object"))){
110 o.addClass("sws-inactive-object").removeClass("sws-active-object");
116 self.updateFooter = function (o) {
117 var footer = o.find(".sws-footer");
118 if (footer.length && (footer.children("*").length == 0)) {
119 var i = SWS.Presentation.getCurrentSlide();
120 var cur = $( "<span class='sws-current-slide-number'>"
123 var sep = $( "<span class='sws-slide-num-sep' />");
124 var tot = $( "<span class='sws-last-slide-number'>"
125 + (SWS.Presentation.getNumSlides())
127 footer.append(cur).append(sep).append(tot);
130 self.updateHeader = function (o) {};
132 SWS.ConfigBuilder = function () {
134 self['sws-object-activate'] = SWS.Templates.objectActivate;
135 self['sws-object-deactivate'] = SWS.Templates.objectDeactivate;
136 self['sws-slide-change'] = SWS.Templates.slideChange;
137 self['sws-update-footer'] = SWS.Templates.updateFooter;
138 self['sws-update-header'] = SWS.Templates.updateHeader;
141 SWS.Defaults = new SWS.ConfigBuilder ();
143 SWS.Config = new SWS.ConfigBuilder ();
146 SWS.Effects = new function () {
149 self.objectDeactivateFadeOut = function (o) {
150 o.animate({'opacity': '0'}, 200,
151 function () { SWS.Templates.objectDeactivate(o)});
154 self.objectActivateFadeIn = function (o) {
156 if (SWS.Templates.objectActivate(o)){
157 o.animate({'opacity': '1' }, 200);
162 self.slideChangeHorizontalFlip = function (from, to){
163 var f = SWS.Presentation.getSlide(from);
164 var t = SWS.Presentation.getSlide(to);
165 f.animate({ 'left': '50%', 'width': '0pt', 'opacity':'0' }, 150,
167 SWS.Templates.slideDeactivate(f);
168 f.css({'left':'0%', 'width': '100%'});
169 t.css({ 'left': '50%', 'width': '0pt','opacity':'0' });
170 SWS.Templates.slideActivate(t);
171 t.animate({'left':'0%', 'width': '100%','opacity':'1'});
174 self.slideChangeFadeOutIn = function (from, to) {
175 var f = SWS.Presentation.getSlide(from);
176 var t = SWS.Presentation.getSlide(to);
177 f.animate({ 'opacity': '0'}, 150,
178 function () { SWS.Templates.slideDeactivate(f);
179 SWS.Templates.slideActivate(t);
180 t.css('opacity', '0');
181 t.animate({ 'opacity': '1'}, 150);
184 self.slideChangeHorizontalSlide = function (from, to) {
185 var f = SWS.Presentation.getSlide(from);
186 var t = SWS.Presentation.getSlide(to);
188 t.css('left', '100%');
189 SWS.Templates.slideActivate(t);
190 f.animate({ 'left': '-100%' }, 250, function () { SWS.Templates.slideDeactivate(f); });
191 t.animate({ 'left': '0%' }, 250);
193 t.css('left', '-100%');
194 SWS.Templates.slideActivate(t);
195 f.animate({ 'left': '100%' }, 250, function () { SWS.Templates.slideDeactivate(f); });
196 t.animate({ 'left': '0%' }, 250);
201 self.slideChangeVerticalSlide = function (from, to) {
202 var f = SWS.Presentation.getSlide(from);
203 var t = SWS.Presentation.getSlide(to);
205 t.css('top', '100%');
206 SWS.Templates.slideActivate(t);
207 f.animate({ 'top': '-100%' }, 250, function () { SWS.Templates.slideDeactivate(f); });
208 t.animate({ 'top': '0%' }, 250);
210 t.css('top', '-100%');
211 SWS.Templates.slideActivate(t);
212 f.animate({ 'top': '100%' }, 250, function () { SWS.Templates.slideDeactivate(f); });
213 t.animate({ 'top': '0%' }, 250);
219 SWS.Fullscreen = new function () {
222 if (SWS.Utils.isUndefined(document.fullScreen)) {
223 if (SWS.Utils.isUndefined(document.mozfullScreen)) {
224 self.status = function () { return document.webkitIsFullScreen; };
225 self.enter = function(e) {
226 e.webkitRequestFullScreen();
228 self.exit = function () {
229 document.webkitCancelFullScreen();
233 self.status = function () { return document.mozfullScreen; };
234 self.enter = function(e) {
235 e.mozRequestFullScreen();
237 self.exit = function () {
238 document.mozCancelFullScreen();
243 self.status = function () { return document.fullScreen; };
244 self.enter = function(e) {
245 e.requestFullScreen();
247 self.exit = function () {
248 document.cancelFullScreen();
256 SWS.Presentation = new function () {
261 //TODO move outside of the Presentation object
265 var _initialized = false;
266 var _disable_input_events = false;
268 var _slide_callbacks = new Array ();
271 self.getNumSlides = function () { return _total_slides; };
273 self.getSlide = function(i) {
274 return $($(".sws-canvas")[i]);
277 self.registerCallback = function (i, f) {
278 if (_initialized) return;
279 //jQuery does not seem to work well
282 var slide_num = $(".sws-slide").length - 1;
284 SWS.Utils.push2(_slide_callbacks, slide_num,{ 'fn': f, 'frame': i });
288 if (typeof(Storage)!=="undefined"){
289 self.getCurrentSlide = function () {
290 //unary + casts to integer
291 var i = +(sessionStorage.getItem("current_slide"));
292 if (!(i >= 0 && i < self.getNumSlides())){
299 self.setCurrentSlide = function (i) {
300 sessionStorage.setItem("current_slide", i);
304 var _current_slide = 0;
305 self.getCurrentSlide = function () { return _current_slide; };
306 self.setCurrentSlide = function (i) { _current_slide = i; };
309 self.firstSlide = function () { return 0; };
310 self.lastSlide = function () { return self.getNumSlides() - 1; };
311 self.refresh = function () {
312 /* block upcoming input event until all animations are finished */
313 _disable_input_events = true;
315 var canvas = $(".sws-canvas");
316 var from_slide_num = canvas.index($(".sws-active-slide"));
317 var to_slide_num = self.getCurrentSlide();
318 var watch_slide_anim = false;
319 var to_slide = $(canvas[to_slide_num]);
320 var from_slide = $(canvas[from_slide_num]);
321 var slide_change = (from_slide_num != to_slide_num);
323 var info = to_slide.data("sws-frame-info");
324 SWS.Config['sws-update-header'](to_slide);
325 SWS.Config['sws-update-footer'](to_slide);
328 //Launch a slide transition:
329 SWS.Config['sws-slide-change'](from_slide_num, to_slide_num);
330 watch_slide_anim = true;
331 for (var i = 0; i < info.callbacks.at_slide.length;i++){
332 info.callbacks.at_slide[i](to_slide);
337 var cur = info.current;
338 var custom = info.custom;
339 var real_slide = to_slide.find(".sws-slide");
341 real_slide.find("*").andSelf().each(function (i){
342 var frameset = $(this).data("sws-frame-set") || {};
344 SWS.Config['sws-object-activate']($(this));
346 SWS.Config['sws-object-deactivate']($(this));
350 if (callbacks = info.callbacks.at_frame[self.getCurrentFrame()]){
351 for (var k = 0; k < callbacks.length; k++)
352 callbacks[k]($(to_slide));
355 var all = $(from_slide).add(to_slide);
356 all.find("*").addBack().promise().done(function() {
357 _disable_input_events = false;
361 self.nextSlide = function () {
362 self.setCurrentSlide(Math.min(self.getCurrentSlide()+1,
364 self.setCurrentFrame(self.firstFrame());
367 self.previousSlide = function () {
368 self.setCurrentSlide(Math.max(self.getCurrentSlide()-1,
370 self.setCurrentFrame(self.firstFrame());
373 self.getFrameInfo = function () {
375 var i = self.getCurrentSlide();
376 var canvas = $($(".sws-canvas")[i]);
377 var infos = canvas.data("sws-frame-info");
380 self.getCurrentFrame = function () { return self.getFrameInfo().current; };
381 self.setCurrentFrame = function (i) { self.getFrameInfo().current = i; };
382 self.firstFrame = function () { return 0; };
383 self.lastFrame = function () { return self.getFrameInfo().last; };
385 self.nextFrame = function () {
386 self.setCurrentFrame(Math.min(self.getCurrentFrame()+1,
390 self.previousFrame = function () {
391 self.setCurrentFrame(Math.max(self.getCurrentFrame()-1,
395 self.next = function () {
396 var i = self.getCurrentFrame();
397 if (i == self.lastFrame()) {
399 self.setCurrentFrame(self.firstFrame());
404 self.previous = function () {
405 var i = self.getCurrentFrame();
406 if (i == self.firstFrame()){
407 self.previousSlide();
408 self.setCurrentFrame(self.lastFrame());
411 self.previousFrame();
414 self.goToSlide = function (s, f) {
415 if (SWS.Utils.isUndefined(f))
417 if (!(s >= self.firstSlide() && s <= self.lastSlide())) return;
418 self.setCurrentSlide(s);
419 if (!(f >= self.firstFrame() && f <= self.lastFrame())) f = 0;
420 self.setCurrentFrame(f);
424 self.cycleStyle = function() {
425 var styles = $("head").children('link[rel$="stylesheet"][title]');
426 var j = styles.index(styles.filter(':not(:disabled)'));
427 styles[j].disabled = true;
428 if (++j == styles.length) j = 0;
429 styles[j].disabled = false;
432 self.inputHandler = function (event) {
433 if (_disable_input_events) return;
434 switch (event.which) {
436 self.setCurrentSlide(self.firstSlide());
440 self.setCurrentSlide(self.lastSlide());
448 case 34: /* PgDown */
452 case 8: /* backspace */
458 self.previousSlide();
464 $("#sws-control-panel-canvas").toggle();
473 function init_canvas(canvas, custom) {
475 var last_frame = canvas.find(".sws-pause").length;
476 //Add all regular elements to the frame list
477 var slide = $(canvas.find(".sws-slide")[0]);
479 var callbacks = { at_slide : new Array(),
480 at_frame : new Array() }
482 if (SWS.Utils.isUndefined(custom)) {
483 custom = new Array ();
486 for (var i = 0; i < custom.length; i++) {
487 if (isFinite(custom[i].frame)){
488 var num = +(custom[i].frame);
489 if (num > last_frame) last_frame = num;
490 SWS.Utils.push2(callbacks.at_frame, num, custom[i].fn);
491 } else if (custom[i].frame == "slide")
492 callbacks.at_slide.push(custom[i].fn);
494 var frame_set = SWS.Utils.parseFrameSpec(custom[i].frame);
495 for(var f in frame_set){
496 if (f > last_frame) last_frame = f;
497 SWS.Utils.push2(callbacks.at_frame, +(f), custom[i].fn);
504 slide.find('*[class*="sws-onframe-"]').each(function(_){
505 var cls = $(this).attr('class');
506 var idx = cls.indexOf("sws-onframe-");
508 var end = cls.indexOf(" ", idx);
509 end = (end == -1) ? cls.length : end;
510 var spec = cls.substring(idx+12, end);
511 var o = SWS.Utils.parseFrameSpec(spec);
513 if (f > last_frame) last_frame = f;
514 $(this).find("*").andSelf().each(function(_){
515 if (!SWS.Utils.isEmpty(o))
516 $(this).data("sws-frame-set", o);
518 specials.add($(this));
525 slide.find("*").andSelf().not(specials).each(function(i) {
526 if ($(this).hasClass("sws-pause")) cur_frame++;
528 for (var j = cur_frame; j <= last_frame; j++)
530 if (!SWS.Utils.isEmpty(o))
531 $(this).data("sws-frame-set", o);
534 canvas.data("sws-frame-info", { current: 0,
541 /* Forces redrawing the page without reloading */
542 self.redraw = function () {
547 self.changeAspect = function() {
548 $("html").removeClass("sws-aspect-4-3")
549 .removeClass("sws-aspect-16-9")
550 .removeClass("sws-aspect-16-10")
551 .addClass($("#sws-aspect-select").val());
556 self.changeTheme = function () {
557 var theme_name = $("#sws-theme-select").val();
558 $("link.sws-theme").each (function (i) {
560 var title = e.attr("title");
561 e[0].disabled = (title != theme_name);
567 var _fullscreen_icon_on = "";
568 var _fullscreen_icon_off = "";
570 self.toggleFullScreen = function () {
571 if (SWS.Fullscreen.status()) {
572 SWS.Fullscreen.exit();
573 $("a#sws-control-panel-fullscreen")
574 .html(_fullscreen_icon_off);
579 SWS.Fullscreen.enter($("body")[0]);
580 $("a#sws-control-panel-fullscreen")
581 .html(_fullscreen_icon_on);
584 function _update_ui() {
585 var nav = $('#sws-control-panel-navigation-bar');
586 nav.val(SWS.Presentation.getCurrentSlide() + 1);
587 $('#sws-control-panel-slide-input').val(nav.val());
589 self.navigate = function () {
590 self.goToSlide($("#sws-control-panel-navigation-bar").val()-1);
595 self.init = function () {
599 $(window).resize(self.redraw);
602 $(window).bind('storage', function (e) {
606 _total_slides = $(".sws-slide").length;
608 $(document).keydown(self.inputHandler);
611 var cur = self.getCurrentSlide();
612 $(".sws-slide").each (function (i) {
613 var par = $(this).parent();
616 var canvas = $('<div class="sws-canvas"/>');
618 if (!($(this).hasClass("sws-option-noheader"))) {
619 canvas.append($('<div class="sws-header"/><br/>'));
622 var inner = $('<div class="sws-inner-canvas"/>');
623 var content = $('<div class="sws-content"/>');
624 var title = $('<div class="sws-title"/>');
625 var h1 = $(this).children("h1");
630 canvas.append(title);
631 canvas.append($("<br/>"));
632 $(this).find('script[type="text/javascript"]').remove();
633 content.append($(this));
634 inner.append(content);
635 inner.append($('<span class="sws-vertical-align"> </span><br/>'));
636 canvas.append(inner);
638 if (!($(this).hasClass("sws-option-nofooter"))) {
639 canvas.append($('<div class="sws-footer"/>'));
646 .addClass("sws-active-slide")
647 .removeClass("sws-inactive-slide");
650 .addClass("sws-inactive-slide")
651 .removeClass("sws-active-slide");
653 init_canvas(canvas,_slide_callbacks[i]);
659 // Initialize the control panel
660 $("body").append($(SWS.Templates.controlPanel));
661 // Fill the theme switcher
662 $("link.sws-theme").each (function (i) {
664 var opt = "<option value='";
665 opt += e.attr("title");
667 if (e.attr("rel") == "stylesheet") {
668 opt+= "selected='selected'";
670 opt += ">" + e.attr("title") + "</option>";
671 $("#sws-theme-select").append($(opt));
673 // Set the fullscreen icon
674 if (SWS.Fullscreen.status()) {
675 $("a#sws-control-panel-fullscreen")
676 .html(_fullscreen_icon_on);
678 $("a#sws-control-panel-fullscreen")
679 .html(_fullscreen_icon_off);
681 // Put the navigation range at the correct position
682 var nav = $('#sws-control-panel-navigation-bar');
683 nav.attr("min", SWS.Presentation.firstSlide() + 1);
684 nav.attr("max", SWS.Presentation.lastSlide() + 1);
685 $('#sws-control-panel-total-slides').text('/' + SWS.Presentation.getNumSlides());
688 _slide_callbacks = null; /* avoid a leak */