7 /* Utility functions */
8 SWS.isUndefined = function (o) { return typeof o == "undefined"; };
11 Representation of intervals of positive integers.
13 SWS.Interval = new function (init) {
16 self.data = new Array();
18 var elems = s.split("_");
19 for(var i = 0; i < elems.length; i++){
20 var bounds = elems[i].split("-");
21 if (bounds.length > 2) continue;
22 if (SWS.isUndefined(bounds[1])) bounds[1] = bounds[0];
23 if (!(isFinite(bounds[0]) && isFinite(bounds[1]))) continue;
25 self.add(+(bounds[0]), +(bounds[1]));
30 if (SWS.isUndefined(y) && isFinite(x)) self.add(x,x);
43 SWS.Presentation = new function () {
52 var _defered_custom = new Array();
53 var _initialized = false;
54 var _animations_running = false;
60 function push_following(t, i, v) {
61 if ((typeof t[i]) == 'undefined') {
65 if (l == 0 || !($.contains(t[i][l-1].get()[0], v.get()[0]))) {
70 function push(t, i, v) {
71 if ((typeof t[i]) == 'undefined') {
79 function init_canvas(canvas) {
81 var last_frame = canvas.find(".sws-pause").length;
82 //Add all regular elements to the frame list
83 canvas.find("*").each(function(i) {
84 if ($(this).filter('*[class*="sws-onslide-"]').length) {
86 if ($(this).hasClass("sws-pause")) cur_frame++;
88 for (var j = cur_frame; j <= last_frame; j++)
90 $(this).data("sws-frame-set", o);
94 canvas.data("sws-frame-info", { current: 0,
96 custom: new Array() });
102 self.setTemplate = function (tn, fn) { templates[tn] = fn; };
104 self.getNumSlides = function () { return _total_slides; };
107 self.registerCustom = function (i, f) {
108 if (_initialized) return;
109 //jQuery does not seem to work well
112 var scripts = document.getElementsByTagName("script");
113 var current = scripts[scripts.length-1];
115 var slide = $(current).parents(".sws-slide");
116 if (slide.length == 0) {
117 console.log("no parent ?");
120 _defered_custom[_defered_custom.length] = { src: slide[0],
127 if (typeof(Storage)!=="undefined"){
128 self.getCurrentSlide = function () {
129 //unary + casts to integer
130 var i = +(sessionStorage.current_slide);
131 if (!(i >= 0 && i < self.getNumSlides())){
138 self.setCurrentSlide = function (i) {
139 sessionStorage.current_slide = i;
143 var _current_slide = 0;
144 self.getCurrentSlide = function () { return current_slide; };
145 self.setCurrentSlide = function (i) { _current_slide = i; };
148 self.firstSlide = function () { return 0; };
149 self.lastSlide = function () { return self.getNumSlides() - 1; };
151 self.refresh = function () {
152 var to_slide = $(".sws-canvas")[self.getCurrentSlide()];
153 var from_slide = $(".sws-active-slide")[0];
155 if (!(to_slide === from_slide)) {
156 templates['sws-template-slide-deactivate'](
159 o.removeClass("sws-active-slide")
160 .addClass("sws-inactive-slide");
161 templates['sws-template-slide-activate'](
162 $(to_slide).removeClass("sws-inactive-slide")
163 .addClass("sws-active-slide"),
169 var info = $(to_slide).data("sws-frame-info");
170 var cur = info.current;
171 var custom = info.custom;
173 $(to_slide).find("*").each (function (i) {
174 var fcur = "f" + cur;
175 var frameset = $(this).data("sws-frame-set") || {};
176 if (frameset[fcur]) {
177 if (!($(this).hasClass("sws-active-frame"))) {
178 templates['sws-template-frame-activate'](
179 $(this).removeClass("sws-inactive-frame")
180 .addClass("sws-active-frame"),
186 if (!($(this).hasClass("sws-inactive-frame"))) {
187 templates['sws-template-frame-deactivate'](
190 o.removeClass("sws-active-frame")
191 .addClass("sws-inactive-frame");
199 _animation_running = true;
202 if (callbacks = custom[self.getCurrentFrame()]){
203 for (var k = 0; k < callbacks.length; k++)
204 callbacks[k]($(to_slide));
207 $(to_slide).find("*").promise().done(function() {
208 _animation_running = false;
212 self.nextSlide = function () {
213 self.setCurrentSlide(Math.min(self.getCurrentSlide()+1,
217 self.previousSlide = function () {
218 self.setCurrentSlide(Math.max(self.getCurrentSlide()-1,
222 self.getFrameInfo = function () {
224 var i = self.getCurrentSlide();
225 var canvas = $($(".sws-canvas")[i]);
226 var infos = canvas.data("sws-frame-info");
229 self.getCurrentFrame = function () { return self.getFrameInfo().current; };
230 self.setCurrentFrame = function (i) { self.getFrameInfo().current = i; };
231 self.firstFrame = function () { return 0; };
232 self.lastFrame = function () { return self.getFrameInfo().last; };
234 self.nextFrame = function () {
235 self.setCurrentFrame(Math.min(self.getCurrentFrame()+1,
239 self.previousFrame = function () {
240 self.setCurrentFrame(Math.max(self.getCurrentFrame()-1,
244 self.next = function () {
245 var i = self.getCurrentFrame();
246 if (i == self.lastFrame())
252 self.previous = function () {
253 var i = self.getCurrentFrame();
254 if (i == self.firstFrame())
255 self.previousSlide();
257 self.previousFrame();
260 self.inputHandler = function (event) {
261 if (_animation_running) return;
262 switch (event.which) {
273 case 1: /* mouse button 1 */
276 case 34: /* PgDown */
280 case 8: /* backspace */
286 self.previousSlide();
295 function default_footer_template() {
296 var elem = '<div class="sws-footer"><span class="sws-current-slide-num">' + (i+1);
297 elem += '</span><span class="sws-slide-num-sep"/><span class="sws-total-slide-num">';
298 elem += self.getNumSlides() + '</span></div>';
302 self.setTemplate('sws-template-footer', function (x) { });
303 self.setTemplate('sws-template-frame-activate', function (x, f) {f(x);});
304 self.setTemplate('sws-template-frame-deactivate', function (x, f) {f(x);});
305 self.setTemplate('sws-template-slide-activate', function (x, f) {f(x);});
306 self.setTemplate('sws-template-slide-deactivate', function (x, f) {f(x);});
312 _total_slides = $(".sws-slide").length;
314 $(document).keydown(self.inputHandler);
315 $(document).mousedown(self.inputHandler);
317 var cur = self.getCurrentSlide();
318 $(".sws-slide").each (function (i) {
319 if (!($(this).hasClass("sws-option-nofooter"))) {
320 $(this).append(templates['sws-template-footer']());
322 var par = $(this).parent();
324 var canvas = $('<div class="sws-canvas"/>');
325 canvas.append($(this));
329 .addClass("sws-active-slide")
330 .removeClass("sws-inacitve-slide");
333 .addClass("sws-inactive-slide")
334 .removeClass("sws-active-slide");
339 for (var i = 0; i < _defered_custom.length; i++){
341 var custom = _defered_custom[i];
342 var canvas = $(custom.src).parents(".sws-canvas");
343 if (canvas.length == 0) continue;
344 canvas = $(canvas[0]);
346 var info = canvas.data("sws-frame-info");
348 push(info.custom, custom.frame, custom.fn);
355 self.init = function () {
356 $(document).ready(init);