Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@susanndelgado
Created May 30, 2013 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save susanndelgado/5678079 to your computer and use it in GitHub Desktop.
Save susanndelgado/5678079 to your computer and use it in GitHub Desktop.
Practicing Modular on my website
var SLIDER = (function(self){
function __init(){
//run self init stuff here
request();
_setKeys();
// console.log(_globals.slides);
}
var _globals = {
slides: [],
slidesAlt: [],
details: [],
timer: 5000,
playpause: true,
animation: null,
current: 0,
total: 0,
};
/* =======================================
REQUEST and LOAD IMAGES
========================================== */
function request(){
var src= 'http://www.sdelgado.com/wp-content/themes/susanMarch2013wp/assets/js/banner.json';
var request = new XMLHttpRequest();
request.open("GET", src);
request.onload = function (){
success(request.responseText);
}
request.onerror = function(){
console.log("ERROR");
};
request.send();
}
function success(response){
//console.log("success");
var content = response;
var obj = JSON.parse(content);
//console.log(obj);
for(var i=0;i<obj.length;i++){
_globals.slides[i] = obj[i].src;
_globals.slidesAlt[i] = obj[i].alt;
_globals.details[i] = obj[i].infoText;
//console.log(_globals.slides);
}
_globals.total = _globals.slides.length;
startAnimate();
}
/* =======================================
KEYS
========================================== */
function _setKeys(){
_keysBinds().setKeys();
}
function nextSlide(){
//console.log("next");
if (_globals.current < _globals.total-1) {
_globals.current++;
setAnimation();
} else {
_globals.current = 0;
setAnimation();
}
}
function prevSlide(){
if (_globals.current > 0) {
_globals.current--;
setAnimation();
} else {
_globals.current = _globals.total-1;
setAnimation();
}
}
function _keysBinds() {
return {
setKeys: function(e) {
$(".prev").click(function() {
//e.preventDefault();
prevSlide();
});
$(".next").click(function(e) {
//e.preventDefault();
nextSlide();
});
}
}
};
/* =======================================
ANIMATIONS
========================================== */
function setAnimation(type){
if(_globals.playpause == true){
if(type==="fade"){
animation().fade();
}else if(type==="swap"){
animation().swap();
}else{
animation().defaultType();
}
}else{
//paused
console.log("paused");
}
}
function animation(){
return {
fade: function(){
// console.log("fade");
$('#banner #content-slider img').fadeOut('slow', function() {
$('#banner #content-slider img').attr('src', getSrc());
$('#banner #content-slider img').attr('alt', getAlt());
$('#banner #content-slider .description').html(getDetails());
$('#banner #content-slider img').fadeIn('slow');
});
},
swap: function(){
$('#banner #content-slider img').attr('src', getSrc());
},
defaultType: function(){
$('#banner #content-slider img').fadeOut('slow', function() {
$('#banner #content-slider img').attr('src', getSrc());
$('#banner #content-slider img').attr('alt', getAlt());
$('#banner #content-slider .description').html(getDetails());
$('#banner #content-slider img').fadeIn('slow');
});
}
}
}
function startAnimate(){
// console.log("start auto animation");
_globals.playpause = true;
_globals.animation = setInterval(nextSlide,_globals.timer);
}
function stopAnimate(){
// console.log("stop auto animation");
_globals.playpause = false;
clearInterval(_globals.animation);
console.log(_globals.playpause);
}
/* =======================================
ADDITIONAL HELPER FUNCTIONS
========================================== */
function slideTo(index,type) {
if(index > 0 && index < _globals.total-1 ){
_globals.current = index;
setAnimation(type);
} else{
// console.log("index of "+index+" does not exist in slider. There are only " +_globals.total+" slides in the slider." );
}
}
function getSrc(){
var Src= _globals.slides[_globals.current];
return Src;
}
function getAlt(){
var Alt= _globals.slidesAlt[_globals.current];
return Alt;
}
function getDetails(){
var details= _globals.details[_globals.current];
return details;
}
__init();
/*Reveal module API methods*/
return {
init: __init,
getSrc: getSrc,
startAnimate: startAnimate,
stopAnimate: stopAnimate,
slideTo: slideTo,
nextSlide: nextSlide,
prevSlide: prevSlide
};
})(SLIDER || {});
var mslidesSrc=[],
mcurrentIndex= 0;
//run self init stuff here
$(document).on('click','a[data-openpostid]',function(e){
var postId = $(this).attr('data-openpostid');
var passThisObj = {//Object containin my parameters
mypostid : postId
}
$.ajax({
url : 'http://www.sdelgado.com/wp-content/themes/susanMarch2013wp/get.php',
data : passThisObj,//This is the object above
dataType : 'json', //html
success : function(response){
var mycontent = response.post_content;
var details = response.post_excerpt;
var title = response.post_title;
//console.log(response);
mslidesSrc = [];
mcurrentIndex = 0;
$('.modalContent').html(mycontent); //'<img src=\"' + mslidesSrc[mcurrentIndex] + '\" alt=\"'+ title +' project image\"/>';
$('.modalBody .info').html(details);
// console.log(details + " Details");
$(mycontent).filter('img').each(function(i) {
mslidesSrc[i] = this.src;
});
$("#modal").fadeIn();
modal();
},
error : function(response){
console.error(response);
}
});
});//end Ajax Load on click'
function modal(){
$('.gallery').fadeOut('slow');
$(".modalClose").click(function() {
$("#modal").fadeOut();
mslidesSrc = [];
mcurrentIndex =0;
$('.gallery').fadeIn('fast');
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment