Skip to content

Commit

Permalink
Create offside factory not relying on global window object
Browse files Browse the repository at this point in the history
  • Loading branch information
toomuchdesign committed Mar 26, 2018
1 parent 6c431ba commit 2e9b18e
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 44 deletions.
12 changes: 6 additions & 6 deletions demos/typical/index.html
Expand Up @@ -40,7 +40,7 @@
<li><a href="#">...</a></li>
</ul>
</nav>

<!-- Site Overlay -->
<div class="site-overlay"></div>

Expand All @@ -53,7 +53,7 @@
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</a>

<a href="#" class="icon icon--hamburger menu-btn-2 h--right">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
Expand Down Expand Up @@ -84,7 +84,7 @@ <h1>Overlay and close button</h1>
var offsideMenu1 = offside( '#menu-1', {

slidingElementsSelector: '#container, #results',
debug: true,
debug: true,
buttonsSelector: '.menu-btn-1, .menu-btn-1--close',
slidingSide: 'left',
beforeOpen: function(){},
Expand All @@ -94,20 +94,20 @@ <h1>Overlay and close button</h1>
var offsideMenu2 = offside( '#menu-2', {

slidingElementsSelector: '#container, #results',
debug: true,
debug: true,
buttonsSelector: '.menu-btn-2, .menu-btn-2--close',
slidingSide: 'right',
beforeOpen: function(){},
beforeClose: function(){},
});

var overlay = document.querySelector('.site-overlay')
.addEventListener( 'click', window.offside.factory.closeOpenOffside );
.addEventListener( 'click', offside.factory.closeOpenOffside );

console.log(offsideMenu1);
console.log(offsideMenu2);

</script>

</body>
</html>
</html>
38 changes: 19 additions & 19 deletions dist/offside.js
@@ -1,4 +1,4 @@
/* offside-js 1.3.1 22-05-2016
/* offside-js 1.3.1 25-03-2018
* Minimal JavaScript kit without library dependencies to push things off-canvas using just class manipulation
* https://github.com/toomuchdesign/offside.git
*
Expand All @@ -7,16 +7,6 @@
*/

;(function ( window, document, undefined ) {

/*
* The first time Offside is called, it creates a singleton-factory object
* and place it into "window.offside.factory".
*
* Offside factory serves the following purposes:
* - DOM initialization
* - Centralized Offside instances management and initialization
*/

'use strict';

// Self-invoking function returning the object which contains
Expand Down Expand Up @@ -255,7 +245,7 @@

// Offside instance private properties
var offside = domEl, // Hello, I'm the Offside instance
offsideButtons = getDomElements( offsideSettings.buttonsSelector ), // Offside toggle buttons
offsideButtons = getDomElements( offsideSettings.buttonsSelector ), // Offside toggle buttons
slidingSide = offsideSettings.slidingSide,
offsideClass = 'offside', // Class added to Offside instance when initialized
offsideSideClass = offsideClass + '--' + slidingSide, // Class added to Offside instance when initialized (eg. offside offside--left)
Expand Down Expand Up @@ -347,7 +337,7 @@
return id;
}
*/

// Set up and initialize a new Offside instance
_initOffside = function() {

Expand Down Expand Up @@ -469,20 +459,30 @@

} // initOffsideFactory() end

return {
var singleton = {

// Get the Singleton instance if one exists
// or create one if it doesn't
getInstance: function ( el, options ) {

if ( !window.offside.factory ) {
window.offside.factory = initOffsideFactory( options );
/*
* When Offside is called for the first time,
* inject a singleton-factory object
* as a static method in "offside.factory".
*
* Offside factory serves the following purposes:
* - DOM initialization
* - Centralized Offside instances management and initialization
*/
if ( !singleton.getInstance.factory ) {
singleton.getInstance.factory = initOffsideFactory( options );
}

return window.offside.factory.getOffsideInstance( el, options );
return singleton.getInstance.factory.getOffsideInstance( el, options );
}
};


return singleton;
})();

// Store in window a reference to the Offside singleton factory
Expand All @@ -492,4 +492,4 @@
window.offside = offside.getInstance;
}

})( window, document );
})( window, document );
2 changes: 1 addition & 1 deletion dist/offside.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 18 additions & 18 deletions src/offside.js
Expand Up @@ -7,16 +7,6 @@
*/

;(function ( window, document, undefined ) {

/*
* The first time Offside is called, it creates a singleton-factory object
* and place it into "window.offside.factory".
*
* Offside factory serves the following purposes:
* - DOM initialization
* - Centralized Offside instances management and initialization
*/

'use strict';

// Self-invoking function returning the object which contains
Expand Down Expand Up @@ -255,7 +245,7 @@

// Offside instance private properties
var offside = domEl, // Hello, I'm the Offside instance
offsideButtons = getDomElements( offsideSettings.buttonsSelector ), // Offside toggle buttons
offsideButtons = getDomElements( offsideSettings.buttonsSelector ), // Offside toggle buttons
slidingSide = offsideSettings.slidingSide,
offsideClass = 'offside', // Class added to Offside instance when initialized
offsideSideClass = offsideClass + '--' + slidingSide, // Class added to Offside instance when initialized (eg. offside offside--left)
Expand Down Expand Up @@ -347,7 +337,7 @@
return id;
}
*/

// Set up and initialize a new Offside instance
_initOffside = function() {

Expand Down Expand Up @@ -469,20 +459,30 @@

} // initOffsideFactory() end

return {
var singleton = {

// Get the Singleton instance if one exists
// or create one if it doesn't
getInstance: function ( el, options ) {

if ( !window.offside.factory ) {
window.offside.factory = initOffsideFactory( options );
/*
* When Offside is called for the first time,
* inject a singleton-factory object
* as a static method in "offside.factory".
*
* Offside factory serves the following purposes:
* - DOM initialization
* - Centralized Offside instances management and initialization
*/
if ( !singleton.getInstance.factory ) {
singleton.getInstance.factory = initOffsideFactory( options );
}

return window.offside.factory.getOffsideInstance( el, options );
return singleton.getInstance.factory.getOffsideInstance( el, options );
}
};


return singleton;
})();

// Store in window a reference to the Offside singleton factory
Expand All @@ -492,4 +492,4 @@
window.offside = offside.getInstance;
}

})( window, document );
})( window, document );

0 comments on commit 2e9b18e

Please sign in to comment.