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