switch.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*!
  2. * Angular Material Design
  3. * https://github.com/angular/material
  4. * @license MIT
  5. * v0.11.4
  6. */
  7. goog.provide('ng.material.components.switch');
  8. goog.require('ng.material.components.checkbox');
  9. goog.require('ng.material.core');
  10. /**
  11. * @private
  12. * @ngdoc module
  13. * @name material.components.switch
  14. */
  15. angular.module('material.components.switch', [
  16. 'material.core',
  17. 'material.components.checkbox'
  18. ])
  19. .directive('mdSwitch', MdSwitch);
  20. /**
  21. * @private
  22. * @ngdoc directive
  23. * @module material.components.switch
  24. * @name mdSwitch
  25. * @restrict E
  26. *
  27. * The switch directive is used very much like the normal [angular checkbox](https://docs.angularjs.org/api/ng/input/input%5Bcheckbox%5D).
  28. *
  29. * As per the [material design spec](http://www.google.com/design/spec/style/color.html#color-ui-color-application)
  30. * the switch is in the accent color by default. The primary color palette may be used with
  31. * the `md-primary` class.
  32. *
  33. * @param {string} ng-model Assignable angular expression to data-bind to.
  34. * @param {string=} name Property name of the form under which the control is published.
  35. * @param {expression=} ng-true-value The value to which the expression should be set when selected.
  36. * @param {expression=} ng-false-value The value to which the expression should be set when not selected.
  37. * @param {string=} ng-change Angular expression to be executed when input changes due to user interaction with the input element.
  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) {
  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);
  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. element.removeClass('transition');
  112. }
  113. function onDrag(ev) {
  114. if (!drag) return;
  115. ev.stopPropagation();
  116. ev.srcEvent && ev.srcEvent.preventDefault();
  117. var percent = ev.pointer.distanceX / drag.width;
  118. //if checked, start from right. else, start from left
  119. var translate = ngModel.$viewValue ? 1 + percent : percent;
  120. // Make sure the switch stays inside its bounds, 0-1%
  121. translate = Math.max(0, Math.min(1, translate));
  122. thumbContainer.css($mdConstant.CSS.TRANSFORM, 'translate3d(' + (100*translate) + '%,0,0)');
  123. drag.translate = translate;
  124. }
  125. function onDragEnd(ev) {
  126. if (!drag) return;
  127. ev.stopPropagation();
  128. element.removeClass('md-dragging');
  129. thumbContainer.css($mdConstant.CSS.TRANSFORM, '');
  130. // We changed if there is no distance (this is a click a click),
  131. // or if the drag distance is >50% of the total.
  132. var isChanged = ngModel.$viewValue ? drag.translate > 0.5 : drag.translate < 0.5;
  133. if (isChanged) {
  134. applyModelValue(!ngModel.$viewValue);
  135. }
  136. drag = null;
  137. }
  138. function applyModelValue(newValue) {
  139. scope.$apply(function() {
  140. ngModel.$setViewValue(newValue);
  141. ngModel.$render();
  142. });
  143. }
  144. };
  145. }
  146. }
  147. MdSwitch.$inject = ["mdCheckboxDirective", "$mdUtil", "$mdConstant", "$parse", "$$rAF", "$mdGesture"];
  148. ng.material.components.switch = angular.module("material.components.switch");