ncase.me-trust/js/core/Slideshow.js

142 lines
2.8 KiB
JavaScript
Raw Normal View History

2017-06-26 19:52:49 +00:00
var SLIDES = [];
2017-06-20 11:10:41 +00:00
function Slideshow(config){
var self = this;
self.config = config;
// DOM
self.dom = config.dom;
// Slide information
self.slides = config.slides;
// Reset: INITIAL VARIABLES
self.reset = function(){
2017-07-12 13:42:17 +00:00
// On End?
2017-07-18 18:39:53 +00:00
if(self.currentSlide){
if(self.currentSlide.onend) self.currentSlide.onend(self);
unlisten(_); // hax
2017-07-12 13:42:17 +00:00
}
2017-07-13 18:49:49 +00:00
// CLEAR
if(self.clear) self.clear();
2017-07-12 13:42:17 +00:00
// Reset
2017-06-20 11:10:41 +00:00
self.dom.innerHTML = "";
self.slideIndex = -1;
self.currentSlide = null;
2017-07-10 18:06:22 +00:00
self.objects = {};
2017-07-12 13:42:17 +00:00
2017-06-20 11:10:41 +00:00
};
self.reset();
2017-07-10 18:06:22 +00:00
//////////////////////////////////////////////////
/////////////// GO TO NEXT SLIDE /////////////////
//////////////////////////////////////////////////
2017-07-10 15:38:20 +00:00
2017-06-20 11:10:41 +00:00
// Go to next slide
2017-07-13 18:49:49 +00:00
self.nextSlide = function(){
2017-06-20 11:10:41 +00:00
2017-07-10 18:06:22 +00:00
// On End?
if(self.currentSlide && self.currentSlide.onend){
self.currentSlide.onend(self);
2017-07-10 15:38:20 +00:00
}
2017-06-20 11:10:41 +00:00
// Update the information
if(self.slideIndex >= self.slides.length-1) return;
self.slideIndex++;
self.currentSlide = self.slides[self.slideIndex];
2017-07-10 18:06:22 +00:00
// On Start
if(self.currentSlide.onstart){
self.currentSlide.onstart(self);
2017-06-20 11:10:41 +00:00
}
// Send out message!
2017-07-12 13:42:17 +00:00
publish("slideshow/slideChange", [self.currentSlide.id]);
2017-06-20 11:10:41 +00:00
};
// Subscribe to "next slide" message...
subscribe("slideshow/next", function(){
self.nextSlide();
});
2017-07-10 18:06:22 +00:00
//////////////////////////////////////////////////
///////////// SLIDESHOW OBJECTS //////////////////
//////////////////////////////////////////////////
2017-06-20 11:10:41 +00:00
// Objects!
self.objects = {};
// Add Object
2017-07-13 18:49:49 +00:00
self.add = function(objectConfig){
2017-06-20 11:10:41 +00:00
// Create object
var Classname = window[objectConfig.type];
2017-07-13 15:07:33 +00:00
objectConfig.slideshow = self;
2017-06-20 11:10:41 +00:00
var obj = new Classname(objectConfig);
obj.slideshow = self;
// Remember it
self.objects[objectConfig.id] = obj;
// Add it for real!
2017-07-13 18:49:49 +00:00
return obj.add();
2017-06-20 11:10:41 +00:00
};
// Remove Object
2017-07-13 18:49:49 +00:00
self.remove = function(objectID){
2017-06-20 11:10:41 +00:00
// Find it...
2017-07-10 18:06:22 +00:00
var obj = self.objects[objectID];
2017-06-20 11:10:41 +00:00
// Remove from memory & DOM
2017-07-10 18:06:22 +00:00
delete self.objects[objectID];
2017-07-13 18:49:49 +00:00
return obj.remove();
2017-06-20 11:10:41 +00:00
};
2017-07-10 18:06:22 +00:00
// Clear: Remove ALL objects
2017-07-13 18:49:49 +00:00
self.clear = function(){
for(var id in self.objects) self.remove(id);
2017-07-10 18:06:22 +00:00
};
//////////////////////////////////////////////////
///////////// FORCE GO TO SLIDE //////////////////
//////////////////////////////////////////////////
// FORCE go to a certain slide
2017-07-12 13:42:17 +00:00
self.gotoSlide = function(id){
2017-07-10 18:06:22 +00:00
2017-07-18 18:39:53 +00:00
// RESET IT ALL.
2017-07-10 18:06:22 +00:00
self.reset();
2017-07-12 13:42:17 +00:00
// Slide & SlideIndex
self.currentSlide = self.slides.find(function(slide){
return slide.id==id;
});
self.slideIndex = self.slides.indexOf(self.currentSlide);
// On JUMP & on Start
if(self.currentSlide.onjump) self.currentSlide.onjump(self);
if(self.currentSlide.onstart) self.currentSlide.onstart(self);
// Send out message!
publish("slideshow/slideChange", [self.currentSlide.id]);
2017-07-10 18:06:22 +00:00
};
// Subscribe to the "force goto" message...
subscribe("slideshow/goto", function(id){
self.gotoSlide(id);
2017-07-12 13:42:17 +00:00
});
2017-07-10 18:06:22 +00:00
2017-06-20 11:10:41 +00:00
}