switch.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*!
  2. * Angular Material Design
  3. * https://github.com/angular/material
  4. * @license MIT
  5. * v0.11.4
  6. */
  7. (function( window, angular, undefined ){
  8. "use strict";
  9. /**
  10. * @private
  11. * @ngdoc module
  12. * @name material.components.switch
  13. */
  14. angular.module('material.components.switch', [
  15. 'material.core',
  16. 'material.components.checkbox'
  17. ])
  18. .directive('mdSwitch', MdSwitch);
  19. /**
  20. * @private
  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 {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) {
  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);
  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. element.removeClass('transition');
  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. }
  137. function applyModelValue(newValue) {
  138. scope.$apply(function() {
  139. ngModel.$setViewValue(newValue);
  140. ngModel.$render();
  141. });
  142. }
  143. };
  144. }
  145. }
  146. MdSwitch.$inject = ["mdCheckboxDirective", "$mdUtil", "$mdConstant", "$parse", "$$rAF", "$mdGesture"];
  147. })(window, window.angular);