Skip to content

Instantly share code, notes, and snippets.

@ambercouch
Last active January 25, 2018 09:36
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 ambercouch/d037bf3f4f7eda8007d2f2c744c80b5d to your computer and use it in GitHub Desktop.
Save ambercouch/d037bf3f4f7eda8007d2f2c744c80b5d to your computer and use it in GitHub Desktop.
Javascript template. Execute based on page template etc.
/**
* Created by Richard on 19/09/2016.
*/
//console.log('ACTIMBER');
ACSCRIPT = {
common: {
init: function () {
'use strict';
//uncomment to debug
console.log('common');
//add js class
jQuery('body').removeClass('no-js');
jQuery('body').addClass('js')
// Scroll events
ACSCRIPT.event.acScroll();
// $(window).on('scrolling', function () {
// console.log('SCROLLING event')
// });
// $(window).on('scrollend', function () {
// console.log('END S event')
// });
// $(window).on('scrollstart', function () {
// console.log('START S event')
// });
//FITVIDS
//$("[data-fitvid]").fitVids();
//open some drawers
$('[some-list-of-elemens]').each(function () {
//TD Needs to work on hover
var showButton = $( this);
var container = $('[some-container]', this);
ACSCRIPT.fn.open(container, showButton);
});
//open a single drawer
var showButton = $( '[data-slider-open], [data-slider-close]');
var container = document.getElementById('slideGallery');
ACSCRIPT.fn.open(container, showButton);
}
},
page: {
init: function () {
//uncomment to debug
console.log('pages');
}
},
post: {
init: function () {
//uncomment to debug
//console.log('posts');
}
},
event:{
acResized: function () {
var event = new Event('resized');
$(window).resize(function() {
clearTimeout(window.resizedFinished);
window.resizedFinished = setTimeout(function(){
console.log('Resized finished.');
window.dispatchEvent(event);
}, 250);
});
},
acScroll: function () {
var scrollTimer = null;
var acScrolling = false;
var eventScrollStart = new Event('scrollstart');
var eventScrollEnd = new Event('scrollend');
var eventScrolling = new Event('scrolling');
$(window).scroll(function () {
setTimeout(function () {
if (acScrolling === false){
window.dispatchEvent(eventScrollStart);
acScrolling = true;
}else {
window.dispatchEvent(eventScrolling);
}
}, 200);
if (scrollTimer) {
clearTimeout(scrollTimer); // clear any previous pending timer
}
scrollTimer = setTimeout(function () {
window.dispatchEvent(eventScrollEnd);
acScrolling = false;
}, 200); // set new timer
});
},
fn: {
acElDimensions: function (selector) {
if (selector == undefined){
selector = 'html'
}
return {
width : $(selector).outerWidth(),
height : $(selector).outerHeight(),
}
},
acScrollTo : function (offSet) {
if(offSet == undefined){
offSet = 0;
}
$('a[href*="#"]:not([href="#"])').click(function () {
if (location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') && location.hostname == this.hostname) {
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) + ']');
if (target.length) {
$('html, body').animate({
scrollTop: target.offset().top - offSet
}, 1000);
return false;
}
}
});
},
open: function (container, showButton, parent, listParent) {
var elState = showButton.attr('data-state');
var eventActOpen = new Event('actOpen');
var eventActClose = new Event('actClose');
showButton.on('click', function(e){
e.preventDefault();
console.log('clicker');
elState = showButton.attr('data-state');
if ('off' === elState ) {
showButton.attr('data-state', 'on');
$(container).attr('data-state', 'on');
$(parent).attr('data-state', 'on');
$(container).addClass('ac-on');
container.dispatchEvent(eventActOpen);
} else {
$(showButton).attr('data-state', 'off');
$(container).attr('data-state', 'off');
$(parent).attr('data-state', 'off');
$(container).removeClass('ac-on');
container.dispatchEvent(eventActClose);
}
});
},
defer: function(successMethod, failMethod, testMethod, pause, attempts) {
var defTest = function () {
return $('#ISR_popup_container').length == 1
};
var defFail = function () {
$( "#ISR_button" ).click();
console.log('deft clicking it');
}
var defSuccess = function () {
Cookies.remove('openStockReminder');
console.log('return def success');;
}
attempts = (attempts == 'undefined')? false : attempts;
pause = (pause == 'undefined')? 50 : pause;
testMethod = (testMethod == 'undefined')? defTest : testMethod;
failMethod = (failMethod == 'undefined')? defFail : failMethod;
successMethod = (successMethod == 'undefined')? defSuccess : successMethod;
if (testMethod()) {
defSuccess();
} else {
failMethod();
if(attempts === false || attempts > 0) {
setTimeout(function () {
attempts = (attempts === false )? attempts : attempts = attempts - 1;
ACSHOPIFY.ac_fn.defer(successMethod, failMethod, testMethod, pause, attempts)
}, pause);
}
}
}
},
settings: {
acScrolling: false,
acMasthead:{
'height' : false
}
}
};
UTIL = {
exec: function (template, handle) {
var ns = ACSCRIPT,
handle = (handle === undefined) ? "init" : handle;
if (template !== '' && ns[template] && typeof ns[template][handle] === 'function') {
ns[template][handle]();
}
},
init: function () {
var body = document.body,
template = body.getAttribute('data-post-type'),
handle = body.getAttribute('data-post-slug');
UTIL.exec('common');
UTIL.exec(template);
UTIL.exec(template, handle);
}
};
jQuery(window).load(UTIL.init);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment