Skip to content

Instantly share code, notes, and snippets.

@Thibzzz
Created September 4, 2017 14:51
Show Gist options
  • Save Thibzzz/fbe89c79832566b5cc8d0b646eff90b4 to your computer and use it in GitHub Desktop.
Save Thibzzz/fbe89c79832566b5cc8d0b646eff90b4 to your computer and use it in GitHub Desktop.
utils.js
import { config } from './../config';
const utils = {
Log (param) {
if (config.debug) {
console.log('¯\_(ツ)_/¯', param);
}
},
Warn (param) {
if (config.debug) {
console.warn('(づ。◕‿‿◕。)づ', param);
}
},
Err (param) {
if (config.debug) {
console.error('(ノಠ益ಠ)ノ彡┻━┻', param);
}
},
deviceIsMobile: null,
screenDimensions: {
h : null,
w : null
},
isMobile () {
window.requestAnimationFrame(function () {
utils.screenDimensions.w = window.innerWidth;
utils.screenDimensions.h = window.innerHeight;
var el = document.querySelector('body');
// utils.Log(['testing isMobile', utils.screenDimensions]);
if (utils.screenDimensions.w <= 768) {
utils.deviceIsMobile = true;
el.classList.add('is-mobile');
return true;
} else {
utils.deviceIsMobile = false;
el.classList.remove('is-mobile');
return false;
}
});
},
autoReflower (callback) {
// utils.Log('_utils.autoReflower start');
var timeout, delay, calls;
timeout = false; // holder for timeout id
delay = 100; // delay after event is "complete" to run callback
calls = 0; // counter for calls, uncomment it in the event listenner if you want to log performance & function calls
// window.resize event listener + performance management + debounce function + event throttling + callback management
window.addEventListener('resize', function() {
window.requestAnimationFrame(function () {
clearTimeout(timeout);
timeout = setTimeout(function () {
utils.isMobile();
if (callback) {
callback();
}
// calls += 1; utils.Log(['call from autoReflower : ', calls]);
}, delay);
});
});
// calls += 1; utils.Log(['call from autoReflower : ', calls]);
utils.isMobile();
}
};
export {utils};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment