Created
July 25, 2018 07:55
-
-
Save AndreKelling/dd2d8f1f8862579d87ef30c6efa5be6d to your computer and use it in GitHub Desktop.
module pattern example with jquery
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var $ = require('jquery'); | |
var Product = (function($) { | |
var plugin = {}; | |
var settings = { | |
stickyBasket: '[data-sticky-basket]', | |
quickLinks: '.product__quicklinks-list li a', | |
cssAccordion: $('.css-accordion__input'), | |
offerExtra: '[data-offer-extra]' | |
}; | |
plugin.init = function() { | |
$('[data-product]').each(function () { | |
plugin[this.dataset.product](this); | |
}) | |
plugin.accordionCalc(); | |
plugin.offerLinkQuantity(); | |
}; | |
plugin.productImage = function(el) { | |
$(el).slick({ | |
slidesToShow: 1, | |
slidesToScroll: 1, | |
fade: true, | |
prevArrow: certeo.slick.arrow('left'), | |
nextArrow: certeo.slick.arrow('right'), | |
asNavFor: '[data-product="productImageNav"]' | |
}); | |
}; | |
plugin.accordionCalc = function() { | |
settings.cssAccordion.change(function() { | |
plugin.stickyBasketRecalc(); | |
}); | |
}; | |
plugin.productImageNav = function(el) { | |
$(el).slick({ | |
slidesToShow: 3, | |
slidesToScroll: 1, | |
prevArrow: certeo.slick.arrow('left'), | |
nextArrow: certeo.slick.arrow('right'), | |
asNavFor: '[data-product="productImage"]', | |
centerMode: true, | |
centerPadding: '10%', | |
focusOnSelect: true | |
}); | |
}; | |
plugin.stickyBasket = function() { | |
$(settings.stickyBasket).stick_in_parent({ | |
offset_top: 70 | |
}); | |
}; | |
plugin.stickyBasketRecalc = function() { | |
$(settings.stickyBasket).trigger('sticky_kit:recalc'); | |
}; | |
plugin.productQuicklinks = function() { | |
$(settings.quickLinks).on('click', function() { | |
var accId = $(this).attr('href'); | |
$(accId).find('.css-accordion__input').prop('checked', true); | |
}); | |
}; | |
plugin.offerLinkQuantity = function() { | |
$(settings.offerExtra).on('click', function(e) { | |
e.preventDefault(); | |
var qtyInputForm = 1, | |
qtyAmount = 1, | |
qtyValue; | |
if($(window).width() < certeo.tools.getMediaQuery('plarge')) { | |
qtyInputForm = 0; | |
} | |
qtyValue = $('input.qty__input').eq(qtyInputForm).val(); | |
if(qtyValue === null) { | |
qtyValue = qtyAmount; | |
} | |
var qtyUrl = $(this).attr('href')+qtyValue; | |
window.location = qtyUrl; | |
return false; | |
}); | |
}; | |
return { | |
init: plugin.init, | |
stickyBasketRecalc: plugin.stickyBasketRecalc | |
}; | |
}(jQuery)); | |
module.exports = Product; | |
Product.init($); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment