1
0
mirror of https://github.com/kremalicious/blog.git synced 2025-02-14 21:10:25 +01:00

back to revealing module pattern

This commit is contained in:
Matthias Kretschmann 2017-09-09 19:22:04 +02:00
parent 26e33532be
commit c3abc79d2d
Signed by: m
GPG Key ID: 606EEEF3C479A91F
5 changed files with 238 additions and 177 deletions

View File

@ -1,78 +1,78 @@
/* global ga */ const krlcAnalytics = (() => { // eslint-disable-line no-unused-vars
//
// // Track Responsive Breakpoints
// Track Responsive Breakpoints //
// // stolen & adapted from
// stolen & adapted from // http://philipwalton.com/articles/measuring-your-sites-responsive-breakpoint-usage/
// http://philipwalton.com/articles/measuring-your-sites-responsive-breakpoint-usage/ //
// const gaBreakpoints = () => {
const gaBreakpoints = () => { // Do nothing in browsers that don't support `window.matchMedia`.
// Do nothing in browsers that don't support `window.matchMedia`. if (!window.matchMedia) {
if (!window.matchMedia) { return
return
}
// Prevent rapid breakpoint changes for all firing at once.
let timeout
const breakpoints = {
xxs: '(max-width: 479px)',
xs: '(min-width: 480px) and (max-width: 767px)',
sm: '(min-width: 768px) and (max-width: 991px)',
md: '(min-width: 992px) and (max-width: 1199px)',
lg: '(min-width: 1200px) and (max-width: 1599px)',
hg: '(min-width: 1600px)'
}
Object.keys(breakpoints).forEach(breakpoint => {
const mql = window.matchMedia(breakpoints[breakpoint])
// Set the initial breakpoint on page load.
if (mql.matches) {
ga('set', 'dimension1', breakpoint)
} }
// Update the breakpoint as the matched media changes // Prevent rapid breakpoint changes for all firing at once.
mql.addListener(() => { let timeout
const breakpoints = {
xxs: '(max-width: 479px)',
xs: '(min-width: 480px) and (max-width: 767px)',
sm: '(min-width: 768px) and (max-width: 991px)',
md: '(min-width: 992px) and (max-width: 1199px)',
lg: '(min-width: 1200px) and (max-width: 1599px)',
hg: '(min-width: 1600px)'
}
Object.keys(breakpoints).forEach(breakpoint => {
const mql = window.matchMedia(breakpoints[breakpoint])
// Set the initial breakpoint on page load.
if (mql.matches) { if (mql.matches) {
clearTimeout(timeout) window.ga('set', 'dimension1', breakpoint)
timeout = setTimeout(() => {
ga('set', 'dimension1', breakpoint)
}, 1000)
} }
// Update the breakpoint as the matched media changes
mql.addListener(() => {
if (mql.matches) {
clearTimeout(timeout)
timeout = setTimeout(() => {
window.ga('set', 'dimension1', breakpoint)
}, 1000)
}
})
}) })
}) }
}
// //
// Track Viewport // Track Viewport
// //
const gaViewport = () => { const gaViewport = () => {
const width = Math.max(document.documentElement.clientWidth, window.innerWidth || 0) const width = Math.max(document.documentElement.clientWidth, window.innerWidth || 0)
const height = Math.max(document.documentElement.clientHeight, window.innerHeight || 0) const height = Math.max(document.documentElement.clientHeight, window.innerHeight || 0)
const dimensions = width + 'x' + height const dimensions = width + 'x' + height
ga('set', 'dimension2', dimensions) window.ga('set', 'dimension2', dimensions)
} }
// //
// Track Pixel Density // Track Pixel Density
// //
const gaPixelDensity = () => { const gaPixelDensity = () => {
// Heads up! // Heads up!
// window.devicePixelRatio doesn't work correctly in IE but whatever // window.devicePixelRatio doesn't work correctly in IE but whatever
const pixeldensity = window.devicePixelRatio const pixeldensity = window.devicePixelRatio
ga('set', 'dimension3', pixeldensity) window.ga('set', 'dimension3', pixeldensity)
} }
const dnt = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack return {
init: [
if (dnt !== 'yes' && dnt !== '1') { gaBreakpoints(),
gaBreakpoints() gaViewport(),
gaViewport() gaPixelDensity()
gaPixelDensity() ]
} }
})(); // eslint-disable-line semi

View File

@ -1,27 +1,39 @@
$(document).ready(() => { /* exported krlcMenu */
const thesite = $('.site')
const thelink = $('.menu-btn')
const thepop = $('.nav-popover')
thelink.on('click', e => { const krlcMenu = (() => { // eslint-disable-line no-unused-vars
e.preventDefault() const _config = {
thesite: $('.site'),
thelink: $('.menu-btn'),
thepop: $('.nav-popover')
}
// Toggle menu const _private = {
thesite.toggleClass('has-menu-open') toggleMenu() {
_config.thelink.on('click', e => {
e.preventDefault()
// Bind the hide controls // Toggle menu
$(document).bind('click.hidethepop', () => { _config.thesite.toggleClass('has-menu-open')
thesite.removeClass('has-menu-open')
// Unbind the hide controls
$(document).unbind('click.hidethepop')
})
// Dont close thepop when you click on thepop // Bind the hide controls
thepop.on('click', e => { $(document).bind('click.hidethepop', () => {
e.stopPropagation() _config.thesite.removeClass('has-menu-open')
}) // Unbind the hide controls
$(document).unbind('click.hidethepop')
})
// And dont close thepop now // Dont close thepop when you click on thepop
e.stopPropagation() _config.thepop.on('click', e => {
}) e.stopPropagation()
}) })
// And dont close thepop now
e.stopPropagation()
})
}
}
return {
init: _private.toggleMenu
}
})(); // eslint-disable-line semi

View File

@ -0,0 +1,35 @@
//
// Vex modals
//
/* global vex */
/* exported krlcModals */
/* eslint-disable spaced-comment */
//=require vex-js/dist/js/vex.combined.js
/* eslint-enable spaced-comment */
const krlcModals = (() => { // eslint-disable-line no-unused-vars
const _config = {
vexTriggers: document.querySelectorAll('.js-vex-btc'),
modalContent: '<h3 class="vex__title">My Bitcoin Address</h3>\n <img src="/assets/img/btc-qr.png" />\n <pre class="highlight"><code class="nt">171qDmKEXm9YBgBLXyGjjPvopP5o9htQ1V</code></pre>'
}
const _private = {
initVex() {
_config.vexTriggers.forEach(el => {
el.addEventListener('click', e => {
e.preventDefault()
vex.defaultOptions.className = 'vex-theme-kremalicious vex-bitcoin'
vex.dialog.buttons.YES.text = 'Close'
vex.open({unsafeContent: _config.modalContent})
}, false)
})
}
}
return {
init: _private.initVex
}
})(); // eslint-disable-line semi

View File

@ -1,84 +1,100 @@
/* global SimpleJekyllSearch */ /* global SimpleJekyllSearch */
/* exported krlcSearch */
/* eslint-disable spaced-comment */ /* eslint-disable spaced-comment */
//=require simple-jekyll-search/dest/simple-jekyll-search.js //=require simple-jekyll-search/dest/simple-jekyll-search.js
/* eslint-enable spaced-comment */ /* eslint-enable spaced-comment */
$(document).ready(() => { const krlcSearch = (() => { // eslint-disable-line no-unused-vars
const searchlink = $('.search-btn, .js-search-init') const _config = {
const searcharea = $('.search-area') searchlink: $('.search-btn, .js-search-init'),
const searchfield = $('#search-input') searcharea: $('.search-area'),
const searchpop = $('#search-popover') searchfield: $('#search-input'),
searchpop: $('#search-popover')
const searchReset = () => {
// Revert all search elements
searcharea
.removeClass('animation-slidedown')
.addClass('animation-bounceOutUp')
.on('animationend webkitAnimationEnd oAnimationEnd', () => {
$('body').removeClass('has-search-open')
})
searchpop.addClass('hide')
} }
searchlink.on('click', e => { const _private = {
e.preventDefault() searchHide() {
// Revert all search elements
_config.searcharea
.removeClass('animation-slidedown')
.addClass('animation-bounceOutUp')
.on('animationend webkitAnimationEnd oAnimationEnd', () => {
$('body').removeClass('has-search-open')
})
_config.searchpop.addClass('hide')
},
// Show search field searchClose() {
searcharea $('.search-close').on('click', e => {
.removeClass('is-ready animation-bounceOutUp') e.preventDefault()
.addClass('is-ready animation-slidedown')
.on('animationend webkitAnimationEnd oAnimationEnd', () => { _private.searchHide()
$('body').addClass('has-search-open')
// Empty search field
_config.searchfield.val('').blur()
})
},
searchShow() {
_config.searchlink.on('click', e => {
e.preventDefault()
// Show search field
_config.searcharea
.removeClass('is-ready animation-bounceOutUp')
.addClass('is-ready animation-slidedown')
.on('animationend webkitAnimationEnd oAnimationEnd', () => {
$('body').addClass('has-search-open')
})
_config.searchfield.focus()
SimpleJekyllSearch({ // eslint-disable-line new-cap
searchInput: document.getElementById('search-input'),
resultsContainer: document.getElementById('search-results'),
json: '/api/search.json',
searchResultTemplate: '<a class="search-link" href="{url}">{title}</a>',
fuzzy: false
})
// Hide menu too just in case
if ($('body').hasClass('has-menu-open')) {
$('body').removeClass('has-menu-open')
}
// Bind the hide controls
$(document).bind('click.hidethepop', () => {
_private.searchHide()
// Unbind the hide controls
$(document).unbind('click.hidethepop')
})
// Dont close thepop when click on thepop
_config.searchpop.on('click', e => {
e.stopPropagation()
})
// Dont close thepop when click on search field
_config.searchfield.on('click', e => {
e.stopPropagation()
})
// And dont close thepop now
e.stopPropagation()
}) })
searchfield.focus() // Show popup upon first keypress
_config.searchfield.on('keyup', () => {
_config.searchpop.removeClass('hide')
})
SimpleJekyllSearch({ // eslint-disable-line new-cap // Listen for close icon
searchInput: document.getElementById('search-input'), _private.searchClose()
resultsContainer: document.getElementById('search-results'),
json: '/api/search.json',
searchResultTemplate: '<a class="search-link" href="{url}">{title}</a>',
fuzzy: false
})
// Hide menu too just in case
if ($('body').hasClass('has-menu-open')) {
$('body').removeClass('has-menu-open')
} }
}
// Bind the hide controls return {
$(document).bind('click.hidethepop', () => { init: _private.searchShow
searchReset() }
})(); // eslint-disable-line semi
// Unbind the hide controls
$(document).unbind('click.hidethepop')
})
// Dont close thepop when click on thepop
searchpop.on('click', e => {
e.stopPropagation()
})
// Dont close thepop when click on search field
searchfield.on('click', e => {
e.stopPropagation()
})
// And dont close thepop now
e.stopPropagation()
})
// Show popup upon first keypress
searchfield.on('keyup', () => {
searchpop.removeClass('hide')
})
$('.search-close').on('click', e => {
e.preventDefault()
searchReset()
// Empty search field
searchfield.val('').blur()
})
})

View File

@ -1,40 +1,38 @@
/* global svg4everybody, vex */ /* global krlcMenu, krlcSearch, krlcModals, krlcAnalytics, svg4everybody */
/* eslint-disable spaced-comment */ /* eslint-disable spaced-comment */
//=require webcomponents.js/CustomElements.js //=require webcomponents.js/CustomElements.js
//=require svg4everybody/dist/svg4everybody.js //=require svg4everybody/dist/svg4everybody.js
//=require jquery/dist/jquery.js //=require jquery/dist/jquery.js
//=require time-elements/time-elements.js //=require time-elements/time-elements.js
//=require vex-js/dist/js/vex.combined.js
/* eslint-disable spaced-comment */
//=include _analytics.js
//=include _search.js
//=include _menu.js //=include _menu.js
//=include _search.js
//=include _modals.js
//=include _analytics.js
/* eslint-enable spaced-comment */ /* eslint-enable spaced-comment */
// //
// Vex modals // Init jQuery-based modules
// //
const vexInit = () => { $(document).ready(() => {
const vexTriggers = document.querySelectorAll('.js-vex-btc') krlcMenu.init()
krlcSearch.init()
})
vexTriggers.forEach(el => {
el.addEventListener('click', e => {
e.preventDefault()
vex.defaultOptions.className = 'vex-theme-kremalicious vex-bitcoin' //
vex.dialog.buttons.YES.text = 'Close' // Init js modules
vex.open({ //
unsafeContent: '<h3 class="vex__title">My Bitcoin Address</h3>\n <img src="/assets/img/btc-qr.png" />\n <pre class="highlight"><code class="nt">171qDmKEXm9YBgBLXyGjjPvopP5o9htQ1V</code></pre>' krlcModals.init()
})
}, false) const dnt = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack
})
if (dnt !== 'yes' && dnt !== '1') {
krlcAnalytics.init()
} }
vexInit()
svg4everybody({ svg4everybody({
nosvg: false nosvg: false
}) })