Restructuring of scripts for better use of ES6 concepts
- Optimized use of common properties (, , ...) - Dispatching events in an attempt to de-globalize certain variables used in the past - Attempt to organize files in a more logical manner
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
/* jshint esnext: true */
|
||||
import globals from './utils/globals';
|
||||
import * as modules from './modules';
|
||||
|
||||
class App {
|
||||
constructor(options) {
|
||||
constructor() {
|
||||
this.modules = modules;
|
||||
this.currentModules = [];
|
||||
}
|
||||
@@ -12,7 +13,7 @@ class App {
|
||||
* @return {Object}
|
||||
*/
|
||||
initGlobals() {
|
||||
this.globals = new this.modules.Globals();
|
||||
globals();
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -22,7 +23,7 @@ class App {
|
||||
*/
|
||||
initModules() {
|
||||
// Elements with module
|
||||
const moduleEls = document.querySelectorAll('[data-module]');
|
||||
var moduleEls = document.querySelectorAll('[data-module]');
|
||||
|
||||
// Loop through elements
|
||||
var i = 0;
|
||||
@@ -65,8 +66,8 @@ class App {
|
||||
|
||||
/**
|
||||
* Get element data attributes
|
||||
* @param {DOMElement} el
|
||||
* @return {Array} data
|
||||
* @param {DOMElement} el
|
||||
* @return {Array} data
|
||||
*/
|
||||
getElemData(el) {
|
||||
// All attributes
|
||||
@@ -108,8 +109,6 @@ class App {
|
||||
}
|
||||
}
|
||||
|
||||
// Document ready
|
||||
// =========================================================================
|
||||
$(function() {
|
||||
window.app = new App();
|
||||
window.app.init();
|
||||
|
||||
4
assets/scripts/global/svg.js
Normal file
4
assets/scripts/global/svg.js
Normal file
@@ -0,0 +1,4 @@
|
||||
/* jshint esnext: true */
|
||||
export default function() {
|
||||
svg4everybody();
|
||||
}
|
||||
@@ -1,4 +1,3 @@
|
||||
/* jshint esnext: true */
|
||||
export {default as Globals} from './modules/Globals';
|
||||
export {default as Button} from './modules/Button';
|
||||
export {default as Title} from './modules/Title';
|
||||
|
||||
17
assets/scripts/modules/AbstractModule.js
Normal file
17
assets/scripts/modules/AbstractModule.js
Normal file
@@ -0,0 +1,17 @@
|
||||
import { $document, $window, $html, $body } from '../utils/environment';
|
||||
|
||||
/**
|
||||
* Abstract module
|
||||
* Gives access to generic jQuery nodes
|
||||
*/
|
||||
class AbstractModule {
|
||||
constructor(options) {
|
||||
this.$document = $document;
|
||||
this.$window = $window;
|
||||
this.$html = $html;
|
||||
this.$body = $body;
|
||||
this.$el = options.$el;
|
||||
}
|
||||
}
|
||||
|
||||
export default AbstractModule;
|
||||
@@ -1,21 +1,16 @@
|
||||
/* jshint esnext: true */
|
||||
import Module from './Module';
|
||||
import AbstractModule from './AbstractModule';
|
||||
|
||||
class Generic extends Module {
|
||||
export default class extends AbstractModule {
|
||||
constructor(options) {
|
||||
super();
|
||||
this.$el = options.$el;
|
||||
super(options);
|
||||
|
||||
this.$el.on('click', (event) => {
|
||||
this.$document.trigger('title.changeLabel', [$(event.currentTarget).val()]);
|
||||
});
|
||||
}
|
||||
|
||||
// Destroy
|
||||
// ==========================================================================
|
||||
destroy() {
|
||||
this.$el.off();
|
||||
this.$el.off('.Button');
|
||||
}
|
||||
}
|
||||
|
||||
export default Generic;
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
// ==========================================================================
|
||||
// Globals module
|
||||
// ==========================================================================
|
||||
import Svg from './Svg';
|
||||
|
||||
class Globals {
|
||||
constructor() {
|
||||
new Svg();
|
||||
}
|
||||
}
|
||||
|
||||
export default Globals;
|
||||
@@ -1,14 +0,0 @@
|
||||
// ==========================================================================
|
||||
// Module
|
||||
// ==========================================================================
|
||||
|
||||
class Module {
|
||||
constructor() {
|
||||
this.$document = $(document);
|
||||
this.$window = $(window);
|
||||
this.$html = $(document.documentElement);
|
||||
this.$body = $(document.body);
|
||||
}
|
||||
}
|
||||
|
||||
export default Module;
|
||||
@@ -1,15 +0,0 @@
|
||||
// ==========================================================================
|
||||
// Svg module
|
||||
// ==========================================================================
|
||||
import Module from './Module';
|
||||
|
||||
class Svg extends Module {
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
svg4everybody();
|
||||
}
|
||||
}
|
||||
|
||||
export default Svg;
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
/* jshint esnext: true */
|
||||
import Module from './Module';
|
||||
import AbstractModule from './AbstractModule';
|
||||
|
||||
class Title extends Module {
|
||||
export default class extends AbstractModule {
|
||||
constructor(options) {
|
||||
super();
|
||||
this.$el = options.$el;
|
||||
super(options);
|
||||
|
||||
this.$label = this.$el.find('.js-label');
|
||||
|
||||
this.$document.on('title.changeLabel', (event, value) => {
|
||||
@@ -16,12 +16,8 @@ class Title extends Module {
|
||||
this.$label.text(value);
|
||||
}
|
||||
|
||||
// Destroy
|
||||
// ==========================================================================
|
||||
destroy() {
|
||||
this.$document.off('title.changeLabel');
|
||||
this.$el.off();
|
||||
this.$el.off('.Title');
|
||||
}
|
||||
}
|
||||
|
||||
export default Title;
|
||||
|
||||
6
assets/scripts/utils/environment.js
Normal file
6
assets/scripts/utils/environment.js
Normal file
@@ -0,0 +1,6 @@
|
||||
const $document = $(document);
|
||||
const $window = $(window);
|
||||
const $html = $(document.documentElement);
|
||||
const $body = $(document.body);
|
||||
|
||||
export { $document, $window, $html, $body };
|
||||
6
assets/scripts/utils/globals.js
Normal file
6
assets/scripts/utils/globals.js
Normal file
@@ -0,0 +1,6 @@
|
||||
/* jshint esnext: true */
|
||||
import svg from '../global/svg';
|
||||
|
||||
export default function() {
|
||||
svg();
|
||||
}
|
||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user