summaryrefslogtreecommitdiff
path: root/www/lib/ng-mfb/mfb/src/mfb.js
diff options
context:
space:
mode:
authorPliable Pixels <pliablepixels@gmail.com>2017-09-21 12:49:18 -0400
committerPliable Pixels <pliablepixels@gmail.com>2017-09-21 12:49:18 -0400
commitb28028ac4082842143b0f528d6bc539da6ccb419 (patch)
tree1e26ea969a781ed8e323fca4e3c76345113fc694 /www/lib/ng-mfb/mfb/src/mfb.js
parent676270d21beed31d767a06c89522198c77d5d865 (diff)
mega changes, including updates and X
Diffstat (limited to 'www/lib/ng-mfb/mfb/src/mfb.js')
-rw-r--r--www/lib/ng-mfb/mfb/src/mfb.js94
1 files changed, 94 insertions, 0 deletions
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 );