You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

189 lines
6.2 KiB

7 years ago
  1. /*!
  2. * Angular Material Design
  3. * https://github.com/angular/material
  4. * @license MIT
  5. * v1.1.3
  6. */
  7. (function( window, angular, undefined ){
  8. "use strict";
  9. /**
  10. * @ngdoc module
  11. * @name material.components.switch
  12. */
  13. MdSwitch['$inject'] = ["mdCheckboxDirective", "$mdUtil", "$mdConstant", "$parse", "$$rAF", "$mdGesture", "$timeout"];
  14. angular.module('material.components.switch', [
  15. 'material.core',
  16. 'material.components.checkbox'
  17. ])
  18. .directive('mdSwitch', MdSwitch);
  19. /**
  20. * @ngdoc directive
  21. * @module material.components.switch
  22. * @name mdSwitch
  23. * @restrict E
  24. *
  25. * The switch directive is used very much like the normal [angular checkbox](https://docs.angularjs.org/api/ng/input/input%5Bcheckbox%5D).
  26. *
  27. * As per the [material design spec](http://www.google.com/design/spec/style/color.html#color-ui-color-application)
  28. * the switch is in the accent color by default. The primary color palette may be used with
  29. * the `md-primary` class.
  30. *
  31. * @param {string} ng-model Assignable angular expression to data-bind to.
  32. * @param {string=} name Property name of the form under which the control is published.
  33. * @param {expression=} ng-true-value The value to which the expression should be set when selected.
  34. * @param {expression=} ng-false-value The value to which the expression should be set when not selected.
  35. * @param {string=} ng-change Angular expression to be executed when input changes due to user interaction with the input element.
  36. * @param {expression=} ng-disabled En/Disable based on the expression.
  37. * @param {boolean=} md-no-ink Use of attribute indicates use of ripple ink effects.
  38. * @param {string=} aria-label Publish the button label used by screen-readers for accessibility. Defaults to the switch's text.
  39. * @param {boolean=} md-invert When set to true, the switch will be inverted.
  40. *
  41. * @usage
  42. * <hljs lang="html">
  43. * <md-switch ng-model="isActive" aria-label="Finished?">
  44. * Finished ?
  45. * </md-switch>
  46. *
  47. * <md-switch md-no-ink ng-model="hasInk" aria-label="No Ink Effects">
  48. * No Ink Effects
  49. * </md-switch>
  50. *
  51. * <md-switch ng-disabled="true" ng-model="isDisabled" aria-label="Disabled">
  52. * Disabled
  53. * </md-switch>
  54. *
  55. * </hljs>
  56. */
  57. function MdSwitch(mdCheckboxDirective, $mdUtil, $mdConstant, $parse, $$rAF, $mdGesture, $timeout) {
  58. var checkboxDirective = mdCheckboxDirective[0];
  59. return {
  60. restrict: 'E',
  61. priority: $mdConstant.BEFORE_NG_ARIA,
  62. transclude: true,
  63. template:
  64. '<div class="md-container">' +
  65. '<div class="md-bar"></div>' +
  66. '<div class="md-thumb-container">' +
  67. '<div class="md-thumb" md-ink-ripple md-ink-ripple-checkbox></div>' +
  68. '</div>'+
  69. '</div>' +
  70. '<div ng-transclude class="md-label"></div>',
  71. require: ['^?mdInputContainer', '?ngModel', '?^form'],
  72. compile: mdSwitchCompile
  73. };
  74. function mdSwitchCompile(element, attr) {
  75. var checkboxLink = checkboxDirective.compile(element, attr).post;
  76. // No transition on initial load.
  77. element.addClass('md-dragging');
  78. return function (scope, element, attr, ctrls) {
  79. var containerCtrl = ctrls[0];
  80. var ngModel = ctrls[1] || $mdUtil.fakeNgModel();
  81. var formCtrl = ctrls[2];
  82. var disabledGetter = null;
  83. if (attr.disabled != null) {
  84. disabledGetter = function() { return true; };
  85. } else if (attr.ngDisabled) {
  86. disabledGetter = $parse(attr.ngDisabled);
  87. }
  88. var thumbContainer = angular.element(element[0].querySelector('.md-thumb-container'));
  89. var switchContainer = angular.element(element[0].querySelector('.md-container'));
  90. var labelContainer = angular.element(element[0].querySelector('.md-label'));
  91. // no transition on initial load
  92. $$rAF(function() {
  93. element.removeClass('md-dragging');
  94. });
  95. checkboxLink(scope, element, attr, ctrls);
  96. if (disabledGetter) {
  97. scope.$watch(disabledGetter, function(isDisabled) {
  98. element.attr('tabindex', isDisabled ? -1 : 0);
  99. });
  100. }
  101. attr.$observe('mdInvert', function(newValue) {
  102. var isInverted = $mdUtil.parseAttributeBoolean(newValue);
  103. isInverted ? element.prepend(labelContainer) : element.prepend(switchContainer);
  104. // Toggle a CSS class to update the margin.
  105. element.toggleClass('md-inverted', isInverted);
  106. });
  107. // These events are triggered by setup drag
  108. $mdGesture.register(switchContainer, 'drag');
  109. switchContainer
  110. .on('$md.dragstart', onDragStart)
  111. .on('$md.drag', onDrag)
  112. .on('$md.dragend', onDragEnd);
  113. var drag;
  114. function onDragStart(ev) {
  115. // Don't go if the switch is disabled.
  116. if (disabledGetter && disabledGetter(scope)) return;
  117. ev.stopPropagation();
  118. element.addClass('md-dragging');
  119. drag = {width: thumbContainer.prop('offsetWidth')};
  120. }
  121. function onDrag(ev) {
  122. if (!drag) return;
  123. ev.stopPropagation();
  124. ev.srcEvent && ev.srcEvent.preventDefault();
  125. var percent = ev.pointer.distanceX / drag.width;
  126. //if checked, start from right. else, start from left
  127. var translate = ngModel.$viewValue ? 1 + percent : percent;
  128. // Make sure the switch stays inside its bounds, 0-1%
  129. translate = Math.max(0, Math.min(1, translate));
  130. thumbContainer.css($mdConstant.CSS.TRANSFORM, 'translate3d(' + (100*translate) + '%,0,0)');
  131. drag.translate = translate;
  132. }
  133. function onDragEnd(ev) {
  134. if (!drag) return;
  135. ev.stopPropagation();
  136. element.removeClass('md-dragging');
  137. thumbContainer.css($mdConstant.CSS.TRANSFORM, '');
  138. // We changed if there is no distance (this is a click a click),
  139. // or if the drag distance is >50% of the total.
  140. var isChanged = ngModel.$viewValue ? drag.translate < 0.5 : drag.translate > 0.5;
  141. if (isChanged) {
  142. applyModelValue(!ngModel.$viewValue);
  143. }
  144. drag = null;
  145. // Wait for incoming mouse click
  146. scope.skipToggle = true;
  147. $timeout(function() {
  148. scope.skipToggle = false;
  149. }, 1);
  150. }
  151. function applyModelValue(newValue) {
  152. scope.$apply(function() {
  153. ngModel.$setViewValue(newValue);
  154. ngModel.$render();
  155. });
  156. }
  157. };
  158. }
  159. }
  160. })(window, window.angular);