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

  1. /*!
  2. * Angular Material Design
  3. * https://github.com/angular/material
  4. * @license MIT
  5. * v1.1.1
  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. *
  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: 210, // Run before ngAria
  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: '?ngModel',
  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, ngModel) {
  79. ngModel = ngModel || $mdUtil.fakeNgModel();
  80. var disabledGetter = null;
  81. if (attr.disabled != null) {
  82. disabledGetter = function() { return true; };
  83. } else if (attr.ngDisabled) {
  84. disabledGetter = $parse(attr.ngDisabled);
  85. }
  86. var thumbContainer = angular.element(element[0].querySelector('.md-thumb-container'));
  87. var switchContainer = angular.element(element[0].querySelector('.md-container'));
  88. // no transition on initial load
  89. $$rAF(function() {
  90. element.removeClass('md-dragging');
  91. });
  92. checkboxLink(scope, element, attr, ngModel);
  93. if (disabledGetter) {
  94. scope.$watch(disabledGetter, function(isDisabled) {
  95. element.attr('tabindex', isDisabled ? -1 : 0);
  96. });
  97. }
  98. // These events are triggered by setup drag
  99. $mdGesture.register(switchContainer, 'drag');
  100. switchContainer
  101. .on('$md.dragstart', onDragStart)
  102. .on('$md.drag', onDrag)
  103. .on('$md.dragend', onDragEnd);
  104. var drag;
  105. function onDragStart(ev) {
  106. // Don't go if the switch is disabled.
  107. if (disabledGetter && disabledGetter(scope)) return;
  108. ev.stopPropagation();
  109. element.addClass('md-dragging');
  110. drag = {width: thumbContainer.prop('offsetWidth')};
  111. }
  112. function onDrag(ev) {
  113. if (!drag) return;
  114. ev.stopPropagation();
  115. ev.srcEvent && ev.srcEvent.preventDefault();
  116. var percent = ev.pointer.distanceX / drag.width;
  117. //if checked, start from right. else, start from left
  118. var translate = ngModel.$viewValue ? 1 + percent : percent;
  119. // Make sure the switch stays inside its bounds, 0-1%
  120. translate = Math.max(0, Math.min(1, translate));
  121. thumbContainer.css($mdConstant.CSS.TRANSFORM, 'translate3d(' + (100*translate) + '%,0,0)');
  122. drag.translate = translate;
  123. }
  124. function onDragEnd(ev) {
  125. if (!drag) return;
  126. ev.stopPropagation();
  127. element.removeClass('md-dragging');
  128. thumbContainer.css($mdConstant.CSS.TRANSFORM, '');
  129. // We changed if there is no distance (this is a click a click),
  130. // or if the drag distance is >50% of the total.
  131. var isChanged = ngModel.$viewValue ? drag.translate < 0.5 : drag.translate > 0.5;
  132. if (isChanged) {
  133. applyModelValue(!ngModel.$viewValue);
  134. }
  135. drag = null;
  136. // Wait for incoming mouse click
  137. scope.skipToggle = true;
  138. $timeout(function() {
  139. scope.skipToggle = false;
  140. }, 1);
  141. }
  142. function applyModelValue(newValue) {
  143. scope.$apply(function() {
  144. ngModel.$setViewValue(newValue);
  145. ngModel.$render();
  146. });
  147. }
  148. };
  149. }
  150. }
  151. ngmaterial.components.switch = angular.module("material.components.switch");