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.

176 lines
5.6 KiB

  1. /*!
  2. * Angular Material Design
  3. * https://github.com/angular/material
  4. * @license MIT
  5. * v1.1.1
  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. *
  40. * @usage
  41. * <hljs lang="html">
  42. * <md-switch ng-model="isActive" aria-label="Finished?">
  43. * Finished ?
  44. * </md-switch>
  45. *
  46. * <md-switch md-no-ink ng-model="hasInk" aria-label="No Ink Effects">
  47. * No Ink Effects
  48. * </md-switch>
  49. *
  50. * <md-switch ng-disabled="true" ng-model="isDisabled" aria-label="Disabled">
  51. * Disabled
  52. * </md-switch>
  53. *
  54. * </hljs>
  55. */
  56. function MdSwitch(mdCheckboxDirective, $mdUtil, $mdConstant, $parse, $$rAF, $mdGesture, $timeout) {
  57. var checkboxDirective = mdCheckboxDirective[0];
  58. return {
  59. restrict: 'E',
  60. priority: 210, // Run before ngAria
  61. transclude: true,
  62. template:
  63. '<div class="md-container">' +
  64. '<div class="md-bar"></div>' +
  65. '<div class="md-thumb-container">' +
  66. '<div class="md-thumb" md-ink-ripple md-ink-ripple-checkbox></div>' +
  67. '</div>'+
  68. '</div>' +
  69. '<div ng-transclude class="md-label"></div>',
  70. require: '?ngModel',
  71. compile: mdSwitchCompile
  72. };
  73. function mdSwitchCompile(element, attr) {
  74. var checkboxLink = checkboxDirective.compile(element, attr).post;
  75. // No transition on initial load.
  76. element.addClass('md-dragging');
  77. return function (scope, element, attr, ngModel) {
  78. ngModel = ngModel || $mdUtil.fakeNgModel();
  79. var disabledGetter = null;
  80. if (attr.disabled != null) {
  81. disabledGetter = function() { return true; };
  82. } else if (attr.ngDisabled) {
  83. disabledGetter = $parse(attr.ngDisabled);
  84. }
  85. var thumbContainer = angular.element(element[0].querySelector('.md-thumb-container'));
  86. var switchContainer = angular.element(element[0].querySelector('.md-container'));
  87. // no transition on initial load
  88. $$rAF(function() {
  89. element.removeClass('md-dragging');
  90. });
  91. checkboxLink(scope, element, attr, ngModel);
  92. if (disabledGetter) {
  93. scope.$watch(disabledGetter, function(isDisabled) {
  94. element.attr('tabindex', isDisabled ? -1 : 0);
  95. });
  96. }
  97. // These events are triggered by setup drag
  98. $mdGesture.register(switchContainer, 'drag');
  99. switchContainer
  100. .on('$md.dragstart', onDragStart)
  101. .on('$md.drag', onDrag)
  102. .on('$md.dragend', onDragEnd);
  103. var drag;
  104. function onDragStart(ev) {
  105. // Don't go if the switch is disabled.
  106. if (disabledGetter && disabledGetter(scope)) return;
  107. ev.stopPropagation();
  108. element.addClass('md-dragging');
  109. drag = {width: thumbContainer.prop('offsetWidth')};
  110. }
  111. function onDrag(ev) {
  112. if (!drag) return;
  113. ev.stopPropagation();
  114. ev.srcEvent && ev.srcEvent.preventDefault();
  115. var percent = ev.pointer.distanceX / drag.width;
  116. //if checked, start from right. else, start from left
  117. var translate = ngModel.$viewValue ? 1 + percent : percent;
  118. // Make sure the switch stays inside its bounds, 0-1%
  119. translate = Math.max(0, Math.min(1, translate));
  120. thumbContainer.css($mdConstant.CSS.TRANSFORM, 'translate3d(' + (100*translate) + '%,0,0)');
  121. drag.translate = translate;
  122. }
  123. function onDragEnd(ev) {
  124. if (!drag) return;
  125. ev.stopPropagation();
  126. element.removeClass('md-dragging');
  127. thumbContainer.css($mdConstant.CSS.TRANSFORM, '');
  128. // We changed if there is no distance (this is a click a click),
  129. // or if the drag distance is >50% of the total.
  130. var isChanged = ngModel.$viewValue ? drag.translate < 0.5 : drag.translate > 0.5;
  131. if (isChanged) {
  132. applyModelValue(!ngModel.$viewValue);
  133. }
  134. drag = null;
  135. // Wait for incoming mouse click
  136. scope.skipToggle = true;
  137. $timeout(function() {
  138. scope.skipToggle = false;
  139. }, 1);
  140. }
  141. function applyModelValue(newValue) {
  142. scope.$apply(function() {
  143. ngModel.$setViewValue(newValue);
  144. ngModel.$render();
  145. });
  146. }
  147. };
  148. }
  149. }
  150. })(window, window.angular);