Reorganization of globals

- Simplified globals usage in App
- Removed global App
This commit is contained in:
Dominic Lord
2017-03-02 09:41:03 -05:00
parent e22b938741
commit c9c3950f8b
5 changed files with 39 additions and 21 deletions

View File

@@ -3,7 +3,7 @@ import { $document } from './utils/environment';
import { getNodeData } from './utils/html'; import { getNodeData } from './utils/html';
// Global functions and tools // Global functions and tools
import globals from './utils/globals'; import globals from './globals';
import { isFunction } from './utils/is'; import { isFunction } from './utils/is';
// Basic modules // Basic modules
@@ -17,7 +17,7 @@ class App {
$document.on('initModules.App', (event) => { $document.on('initModules.App', (event) => {
this.initGlobals(event.firstBlood) this.initGlobals(event.firstBlood)
.deleteModules() .deleteModules()
.initModules(); .initModules(event);
}); });
$document.on('initScopedModules.App', (event) => { $document.on('initScopedModules.App', (event) => {
@@ -55,10 +55,10 @@ class App {
/** /**
* Find modules and initialize them * Find modules and initialize them
* @param {object} event The event being triggered. * @param {Object} event The event being triggered.
* @return {object} Self (allows chaining) * @return {Object} Self (allows chaining)
*/ */
initModules() { initModules(event) {
// Elements with module // Elements with module
let moduleEls; let moduleEls;
@@ -116,8 +116,8 @@ class App {
// IIFE for loading the application // IIFE for loading the application
// ========================================================================== // ==========================================================================
(function() { (function() {
window.App = new App(); new App();
$document.trigger({ $document.triggerHandler({
type: 'initModules.App', type: 'initModules.App',
firstBlood: true firstBlood: true
}); });

10
assets/scripts/globals.js Normal file
View File

@@ -0,0 +1,10 @@
/* jshint esnext: true */
import TransitionManager from './transitions/TransitionManager';
export default function(firstBlood) {
svg4everybody();
if (firstBlood) {
const transitionManager = new TransitionManager();
}
}

View File

@@ -6,4 +6,6 @@ const $window = $(window);
const $html = $(document.documentElement); const $html = $(document.documentElement);
const $body = $(document.body); const $body = $(document.body);
export { $document, $window, $html, $body, APP_NAME, DATA_API_KEY }; const isDebug = !!$html.data('debug');
export { APP_NAME, DATA_API_KEY, $document, $window, $html, $body, isDebug };

View File

@@ -1,5 +0,0 @@
/* jshint esnext: true */
export default function() {
svg4everybody();
}

View File

@@ -1,7 +1,9 @@
/* jshint esnext: true */ /* jshint esnext: true */
var isAnimating = false; import { isNumeric } from './is'
var defaults = { let isAnimating = false;
const defaults = {
easing: 'swing', easing: 'swing',
headerOffset: 60, headerOffset: 60,
speed: 300 speed: 300
@@ -14,7 +16,7 @@ var defaults = {
* @param {object} options * @param {object} options
*/ */
export function scrollTo($element, options) { export function scrollTo($element, options) {
var deferred = $.Deferred(); const deferred = $.Deferred();
// Drop everything if this ain't a jQuery object // Drop everything if this ain't a jQuery object
if ($element instanceof jQuery && $element.length > 0) { if ($element instanceof jQuery && $element.length > 0) {
@@ -27,21 +29,30 @@ export function scrollTo($element, options) {
isAnimating = true; isAnimating = true;
// Default container that we'll be scrolling // Default container that we'll be scrolling
var $container = $('html, body'); let $container = $('html, body');
var elementOffset = 0; let elementOffset = 0;
// Testing container in options for jQuery-ness // Testing container in options for jQuery-ness
// If we're not using a custom container, we take the top document offset // If we're not using a custom container, we take the top document offset
// If we are, we use the elements position relative to the container // If we are, we use the elements position relative to the container
if (typeof options.$container !== 'undefined' && options.$container instanceof jQuery && options.$container.length > 0) { if (typeof options.$container !== 'undefined' && options.$container instanceof jQuery && options.$container.length > 0) {
$container = options.$container; $container = options.$container;
elementOffset = $element.position().top
if (typeof options.scrollTop !== 'undefined' && isNumeric(options.scrollTop) && options.scrollTop !== 0) {
scrollTop = options.scrollTop;
} else {
scrollTop = $element.position().top - options.headerOffset;
}
} else { } else {
elementOffset = $element.offset().top if (typeof options.scrollTop !== 'undefined' && isNumeric(options.scrollTop) && options.scrollTop !== 0) {
scrollTop = options.scrollTop;
} else {
scrollTop = $element.offset().top - options.headerOffset;
}
} }
$container.animate({ $container.animate({
scrollTop: elementOffset - options.headerOffset scrollTop: scrollTop
}, options.speed, options.easing, function() { }, options.speed, options.easing, function() {
isAnimating = false; isAnimating = false;
deferred.resolve(); deferred.resolve();