123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564 |
- /*!
- * Angular Material Design
- * https://github.com/angular/material
- * @license MIT
- * v0.11.4
- */
- (function( window, angular, undefined ){
- "use strict";
- (function() {
- 'use strict';
- angular.module('material.components.fabShared', ['material.core'])
- .controller('FabController', FabController);
- function FabController($scope, $element, $animate, $mdUtil, $mdConstant) {
- var vm = this;
- // NOTE: We use async evals below to avoid conflicts with any existing digest loops
- vm.open = function() {
- $scope.$evalAsync("vm.isOpen = true");
- };
- vm.close = function() {
- // Async eval to avoid conflicts with existing digest loops
- $scope.$evalAsync("vm.isOpen = false");
- // Focus the trigger when the element closes so users can still tab to the next item
- $element.find('md-fab-trigger')[0].focus();
- };
- // Toggle the open/close state when the trigger is clicked
- vm.toggle = function() {
- $scope.$evalAsync("vm.isOpen = !vm.isOpen");
- };
- setupDefaults();
- setupListeners();
- setupWatchers();
- fireInitialAnimations();
- function setupDefaults() {
- // Set the default direction to 'down' if none is specified
- vm.direction = vm.direction || 'down';
- // Set the default to be closed
- vm.isOpen = vm.isOpen || false;
- // Start the keyboard interaction at the first action
- resetActionIndex();
- }
- var events = [];
- function setupListeners() {
- var eventTypes = [
- 'mousedown', 'mouseup', 'click', 'touchstart', 'touchend', 'focusin', 'focusout'
- ];
- // Add our listeners
- angular.forEach(eventTypes, function(eventType) {
- $element.on(eventType, parseEvents);
- });
- // Remove our listeners when destroyed
- $scope.$on('$destroy', function() {
- angular.forEach(eventTypes, function(eventType) {
- $element.off(eventType, parseEvents);
- });
- // remove any attached keyboard handlers in case element is removed while
- // speed dial is open
- disableKeyboard();
- });
- }
- function resetEvents() {
- events = [];
- }
- function equalsEvents(toCheck) {
- var isEqual, strippedCheck, moreToCheck;
- // Quick check to make sure we don't get stuck in an infinite loop
- var numTests = 0;
- do {
- // Strip out the question mark
- strippedCheck = toCheck.map(function(event) {
- return event.replace('?', '')
- });
- // Check if they are equal
- isEqual = angular.equals(events, strippedCheck);
- // If not, check to see if removing an optional event makes them equal
- if (!isEqual) {
- toCheck = removeOptionalEvent(toCheck);
- moreToCheck = (toCheck.length >= events.length && toCheck.length !== strippedCheck.length);
- }
- }
- while (numTests < 10 && !isEqual && moreToCheck);
- return isEqual;
- }
- function removeOptionalEvent(events) {
- var foundOptional = false;
- return events.filter(function(event) {
- // If we have not found an optional one, keep searching
- if (!foundOptional && event.indexOf('?') !== -1) {
- foundOptional = true;
- // If we find an optional one, remove only that one and keep going
- return false;
- }
- return true;
- });
- }
- function parseEvents(latestEvent) {
- events.push(latestEvent.type);
- // Handle desktop click
- if (equalsEvents(['mousedown', 'focusout?', 'focusin?', 'mouseup', 'click'])) {
- handleItemClick(latestEvent);
- resetEvents();
- return;
- }
- // Handle mobile click/tap (and keyboard enter)
- if (equalsEvents(['touchstart?', 'touchend?', 'click'])) {
- handleItemClick(latestEvent);
- resetEvents();
- return;
- }
- // Handle tab keys (focusin)
- if (equalsEvents(['focusin'])) {
- vm.open();
- resetEvents();
- return;
- }
- // Handle tab keys (focusout)
- if (equalsEvents(['focusout'])) {
- vm.close();
- resetEvents();
- return;
- }
- eventUnhandled();
- }
- /*
- * No event was handled, so setup a timeout to clear the events
- *
- * TODO: Use $mdUtil.debounce()?
- */
- var resetEventsTimeout;
- function eventUnhandled() {
- if (resetEventsTimeout) {
- window.clearTimeout(resetEventsTimeout);
- }
- resetEventsTimeout = window.setTimeout(function() {
- resetEvents();
- }, 250);
- }
- function resetActionIndex() {
- vm.currentActionIndex = -1;
- }
- function setupWatchers() {
- // Watch for changes to the direction and update classes/attributes
- $scope.$watch('vm.direction', function(newDir, oldDir) {
- // Add the appropriate classes so we can target the direction in the CSS
- $animate.removeClass($element, 'md-' + oldDir);
- $animate.addClass($element, 'md-' + newDir);
- // Reset the action index since it may have changed
- resetActionIndex();
- });
- var trigger, actions;
- // Watch for changes to md-open
- $scope.$watch('vm.isOpen', function(isOpen) {
- // Reset the action index since it may have changed
- resetActionIndex();
- // We can't get the trigger/actions outside of the watch because the component hasn't been
- // linked yet, so we wait until the first watch fires to cache them.
- if (!trigger || !actions) {
- trigger = getTriggerElement();
- actions = getActionsElement();
- }
- if (isOpen) {
- enableKeyboard();
- } else {
- disableKeyboard();
- }
- var toAdd = isOpen ? 'md-is-open' : '';
- var toRemove = isOpen ? '' : 'md-is-open';
- // Set the proper ARIA attributes
- trigger.attr('aria-haspopup', true);
- trigger.attr('aria-expanded', isOpen);
- actions.attr('aria-hidden', !isOpen);
- // Animate the CSS classes
- $animate.setClass($element, toAdd, toRemove);
- });
- }
- // Fire the animations once in a separate digest loop to initialize them
- function fireInitialAnimations() {
- $mdUtil.nextTick(function() {
- $animate.addClass($element, 'md-noop');
- });
- }
- function enableKeyboard() {
- angular.element(document).on('keydown', keyPressed);
- }
- function disableKeyboard() {
- angular.element(document).off('keydown', keyPressed);
- }
- function keyPressed(event) {
- switch (event.which) {
- case $mdConstant.KEY_CODE.SPACE: event.preventDefault(); return false;
- case $mdConstant.KEY_CODE.ESCAPE: vm.close(); event.preventDefault(); return false;
- case $mdConstant.KEY_CODE.LEFT_ARROW: doKeyLeft(event); return false;
- case $mdConstant.KEY_CODE.UP_ARROW: doKeyUp(event); return false;
- case $mdConstant.KEY_CODE.RIGHT_ARROW: doKeyRight(event); return false;
- case $mdConstant.KEY_CODE.DOWN_ARROW: doKeyDown(event); return false;
- }
- }
- function doActionPrev(event) {
- focusAction(event, -1);
- }
- function doActionNext(event) {
- focusAction(event, 1);
- }
- function focusAction(event, direction) {
- // Grab all of the actions
- var actions = getActionsElement()[0].querySelectorAll('.md-fab-action-item');
- // Disable all other actions for tabbing
- angular.forEach(actions, function(action) {
- angular.element(angular.element(action).children()[0]).attr('tabindex', -1);
- });
- // Increment/decrement the counter with restrictions
- vm.currentActionIndex = vm.currentActionIndex + direction;
- vm.currentActionIndex = Math.min(actions.length - 1, vm.currentActionIndex);
- vm.currentActionIndex = Math.max(0, vm.currentActionIndex);
- // Focus the element
- var focusElement = angular.element(actions[vm.currentActionIndex]).children()[0];
- angular.element(focusElement).attr('tabindex', 0);
- focusElement.focus();
- // Make sure the event doesn't bubble and cause something else
- event.preventDefault();
- event.stopImmediatePropagation();
- }
- function doKeyLeft(event) {
- if (vm.direction === 'left') {
- doActionNext(event);
- } else {
- doActionPrev(event);
- }
- }
- function doKeyUp(event) {
- if (vm.direction === 'down') {
- doActionPrev(event);
- } else {
- doActionNext(event);
- }
- }
- function doKeyRight(event) {
- if (vm.direction === 'left') {
- doActionPrev(event);
- } else {
- doActionNext(event);
- }
- }
- function doKeyDown(event) {
- if (vm.direction === 'up') {
- doActionPrev(event);
- } else {
- doActionNext(event);
- }
- }
- function isTrigger(element) {
- return $mdUtil.getClosest(element, 'md-fab-trigger');
- }
- function isAction(element) {
- return $mdUtil.getClosest(element, 'md-fab-actions');
- }
- function handleItemClick(event) {
- if (isTrigger(event.target)) {
- vm.toggle();
- }
- if (isAction(event.target)) {
- vm.close();
- }
- }
- function getTriggerElement() {
- return $element.find('md-fab-trigger');
- }
- function getActionsElement() {
- return $element.find('md-fab-actions');
- }
- }
- FabController.$inject = ["$scope", "$element", "$animate", "$mdUtil", "$mdConstant"];
- })();
- (function() {
- 'use strict';
- /**
- * @ngdoc module
- * @name material.components.fabSpeedDial
- */
- angular
- // Declare our module
- .module('material.components.fabSpeedDial', [
- 'material.core',
- 'material.components.fabShared',
- 'material.components.fabTrigger',
- 'material.components.fabActions'
- ])
- // Register our directive
- .directive('mdFabSpeedDial', MdFabSpeedDialDirective)
- // Register our custom animations
- .animation('.md-fling', MdFabSpeedDialFlingAnimation)
- .animation('.md-scale', MdFabSpeedDialScaleAnimation)
- // Register a service for each animation so that we can easily inject them into unit tests
- .service('mdFabSpeedDialFlingAnimation', MdFabSpeedDialFlingAnimation)
- .service('mdFabSpeedDialScaleAnimation', MdFabSpeedDialScaleAnimation);
- /**
- * @ngdoc directive
- * @name mdFabSpeedDial
- * @module material.components.fabSpeedDial
- *
- * @restrict E
- *
- * @description
- * The `<md-fab-speed-dial>` directive is used to present a series of popup elements (usually
- * `<md-button>`s) for quick access to common actions.
- *
- * There are currently two animations available by applying one of the following classes to
- * the component:
- *
- * - `md-fling` - The speed dial items appear from underneath the trigger and move into their
- * appropriate positions.
- * - `md-scale` - The speed dial items appear in their proper places by scaling from 0% to 100%.
- *
- * You may also easily position the trigger by applying one one of the following classes to the
- * `<md-fab-speed-dial>` element:
- * - `md-fab-top-left`
- * - `md-fab-top-right`
- * - `md-fab-bottom-left`
- * - `md-fab-bottom-right`
- *
- * These CSS classes use `position: absolute`, so you need to ensure that the container element
- * also uses `position: absolute` or `position: relative` in order for them to work.
- *
- * Additionally, you may use the standard `ng-mouseenter` and `ng-mouseleave` directives to
- * open or close the speed dial. However, if you wish to allow users to hover over the empty
- * space where the actions will appear, you must also add the `md-hover-full` class to the speed
- * dial element. Without this, the hover effect will only occur on top of the trigger.
- *
- * @usage
- * <hljs lang="html">
- * <md-fab-speed-dial md-direction="up" class="md-fling">
- * <md-fab-trigger>
- * <md-button aria-label="Add..."><md-icon icon="/img/icons/plus.svg"></md-icon></md-button>
- * </md-fab-trigger>
- *
- * <md-fab-actions>
- * <md-button aria-label="Add User">
- * <md-icon icon="/img/icons/user.svg"></md-icon>
- * </md-button>
- *
- * <md-button aria-label="Add Group">
- * <md-icon icon="/img/icons/group.svg"></md-icon>
- * </md-button>
- * </md-fab-actions>
- * </md-fab-speed-dial>
- * </hljs>
- *
- * @param {string} md-direction From which direction you would like the speed dial to appear
- * relative to the trigger element.
- * @param {expression=} md-open Programmatically control whether or not the speed-dial is visible.
- */
- function MdFabSpeedDialDirective() {
- return {
- restrict: 'E',
- scope: {
- direction: '@?mdDirection',
- isOpen: '=?mdOpen'
- },
- bindToController: true,
- controller: 'FabController',
- controllerAs: 'vm',
- link: FabSpeedDialLink
- };
- function FabSpeedDialLink(scope, element) {
- // Prepend an element to hold our CSS variables so we can use them in the animations below
- element.prepend('<div class="md-css-variables"></div>');
- }
- }
- function MdFabSpeedDialFlingAnimation() {
- function runAnimation(element) {
- var el = element[0];
- var ctrl = element.controller('mdFabSpeedDial');
- var items = el.querySelectorAll('.md-fab-action-item');
- // Grab our trigger element
- var triggerElement = el.querySelector('md-fab-trigger');
- // Grab our element which stores CSS variables
- var variablesElement = el.querySelector('.md-css-variables');
- // Setup JS variables based on our CSS variables
- var startZIndex = parseInt(window.getComputedStyle(variablesElement).zIndex);
- // Always reset the items to their natural position/state
- angular.forEach(items, function(item, index) {
- var styles = item.style;
- styles.transform = styles.webkitTransform = '';
- styles.transitionDelay = '';
- styles.opacity = 1;
- // Make the items closest to the trigger have the highest z-index
- styles.zIndex = (items.length - index) + startZIndex;
- });
- // Set the trigger to be above all of the actions so they disappear behind it.
- triggerElement.style.zIndex = startZIndex + items.length + 1;
- // If the control is closed, hide the items behind the trigger
- if (!ctrl.isOpen) {
- angular.forEach(items, function(item, index) {
- var newPosition, axis;
- var styles = item.style;
- switch (ctrl.direction) {
- case 'up':
- newPosition = item.scrollHeight * (index + 1);
- axis = 'Y';
- break;
- case 'down':
- newPosition = -item.scrollHeight * (index + 1);
- axis = 'Y';
- break;
- case 'left':
- newPosition = item.scrollWidth * (index + 1);
- axis = 'X';
- break;
- case 'right':
- newPosition = -item.scrollWidth * (index + 1);
- axis = 'X';
- break;
- }
- var newTranslate = 'translate' + axis + '(' + newPosition + 'px)';
- styles.transform = styles.webkitTransform = newTranslate;
- });
- }
- }
- return {
- addClass: function(element, className, done) {
- if (element.hasClass('md-fling')) {
- runAnimation(element);
- done();
- }
- },
- removeClass: function(element, className, done) {
- runAnimation(element);
- done();
- }
- }
- }
- function MdFabSpeedDialScaleAnimation() {
- var delay = 65;
- function runAnimation(element) {
- var el = element[0];
- var ctrl = element.controller('mdFabSpeedDial');
- var items = el.querySelectorAll('.md-fab-action-item');
- // Grab our element which stores CSS variables
- var variablesElement = el.querySelector('.md-css-variables');
- // Setup JS variables based on our CSS variables
- var startZIndex = parseInt(window.getComputedStyle(variablesElement).zIndex);
- // Always reset the items to their natural position/state
- angular.forEach(items, function(item, index) {
- var styles = item.style,
- offsetDelay = index * delay;
- styles.opacity = ctrl.isOpen ? 1 : 0;
- styles.transform = styles.webkitTransform = ctrl.isOpen ? 'scale(1)' : 'scale(0)';
- styles.transitionDelay = (ctrl.isOpen ? offsetDelay : (items.length - offsetDelay)) + 'ms';
- // Make the items closest to the trigger have the highest z-index
- styles.zIndex = (items.length - index) + startZIndex;
- });
- }
- return {
- addClass: function(element, className, done) {
- runAnimation(element);
- done();
- },
- removeClass: function(element, className, done) {
- runAnimation(element);
- done();
- }
- }
- }
- })();
- })(window, window.angular);
|