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.3 KiB

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