From b28028ac4082842143b0f528d6bc539da6ccb419 Mon Sep 17 00:00:00 2001 From: Pliable Pixels Date: Thu, 21 Sep 2017 12:49:18 -0400 Subject: mega changes, including updates and X --- www/lib/ng-mfb/mfb/src/mfb.js | 94 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 www/lib/ng-mfb/mfb/src/mfb.js (limited to 'www/lib/ng-mfb/mfb/src/mfb.js') diff --git a/www/lib/ng-mfb/mfb/src/mfb.js b/www/lib/ng-mfb/mfb/src/mfb.js new file mode 100644 index 00000000..286b63ab --- /dev/null +++ b/www/lib/ng-mfb/mfb/src/mfb.js @@ -0,0 +1,94 @@ +/** + * Material floating button + * By: Nobita + * Repo and docs: https://github.com/nobitagit/material-floating-button + * + * License: MIT + */ + +;(function ( window, document, undefined ) { + + 'use strict'; + + /** + * Some defaults + */ + var clickOpt = 'click', + hoverOpt = 'hover', + toggleMethod = 'data-mfb-toggle', + menuState = 'data-mfb-state', + isOpen = 'open', + isClosed = 'closed', + mainButtonClass = 'mfb-component__button--main'; + + /** + * Internal references + */ + var elemsToClick, + elemsToHover, + mainButton, + target, + currentState; + + /** + * For every menu we need to get the main button and attach the appropriate evt. + */ + function attachEvt( elems, evt ){ + for( var i = 0, len = elems.length; i < len; i++ ){ + mainButton = elems[i].querySelector('.' + mainButtonClass); + mainButton.addEventListener( evt , toggleButton, false); + } + } + + /** + * Remove the hover option, set a click toggle and a default, + * initial state of 'closed' to menu that's been targeted. + */ + function replaceAttrs( elems ){ + for( var i = 0, len = elems.length; i < len; i++ ){ + elems[i].setAttribute( toggleMethod, clickOpt ); + elems[i].setAttribute( menuState, isClosed ); + } + } + + function getElemsByToggleMethod( selector ){ + return document.querySelectorAll('[' + toggleMethod + '="' + selector + '"]'); + } + + /** + * The open/close action is performed by toggling an attribute + * on the menu main element. + * + * First, check if the target is the menu itself. If it's a child + * keep walking up the tree until we found the main element + * where we can toggle the state. + */ + function toggleButton( evt ){ + + target = evt.target; + while ( target && !target.getAttribute( toggleMethod ) ){ + target = target.parentNode; + if(!target) { return; } + } + + currentState = target.getAttribute( menuState ) === isOpen ? isClosed : isOpen; + + target.setAttribute(menuState, currentState); + + } + + /** + * On touch enabled devices we assume that no hover state is possible. + * So, we get the menu with hover action configured and we set it up + * in order to make it usable with tap/click. + **/ + if ( window.Modernizr && Modernizr.touch ){ + elemsToHover = getElemsByToggleMethod( hoverOpt ); + replaceAttrs( elemsToHover ); + } + + elemsToClick = getElemsByToggleMethod( clickOpt ); + + attachEvt( elemsToClick, 'click' ); + +})( window, document ); -- cgit v1.2.3