/*!
* Angular Material Design
* https://github.com/angular/material
* @license MIT
* v0.7.0-rc3
*/
(function() {
'use strict';
/**
* @private
* @ngdoc module
* @name material.components.switch
*/
angular.module('material.components.switch', [
'material.core',
'material.components.checkbox'
])
.directive('mdSwitch', MdSwitch);
/**
* @private
* @ngdoc directive
* @module material.components.switch
* @name mdSwitch
* @restrict E
*
* The switch directive is used very much like the normal [angular checkbox](https://docs.angularjs.org/api/ng/input/input%5Bcheckbox%5D).
*
* @param {string} ng-model Assignable angular expression to data-bind to.
* @param {string=} name Property name of the form under which the control is published.
* @param {expression=} ng-true-value The value to which the expression should be set when selected.
* @param {expression=} ng-false-value The value to which the expression should be set when not selected.
* @param {string=} ng-change Angular expression to be executed when input changes due to user interaction with the input element.
* @param {boolean=} md-no-ink Use of attribute indicates use of ripple ink effects.
* @param {string=} aria-label Publish the button label used by screen-readers for accessibility. Defaults to the switch's text.
*
* @usage
*
*
* Finished ?
*
*
*
* No Ink Effects
*
*
*
* Disabled
*
*
*
*/
function MdSwitch(mdCheckboxDirective, $mdTheming, $mdUtil, $document, $mdConstant, $parse, $$rAF) {
var checkboxDirective = mdCheckboxDirective[0];
return {
restrict: 'E',
transclude: true,
template:
'
' +
'' +
'
',
require: '?ngModel',
compile: compile
};
function compile(element, attr) {
var checkboxLink = checkboxDirective.compile(element, attr);
// no transition on initial load
element.addClass('md-dragging');
return function (scope, element, attr, ngModel) {
ngModel = ngModel || $mdUtil.fakeNgModel();
var disabledGetter = $parse(attr.ngDisabled);
var thumbContainer = angular.element(element[0].querySelector('.md-thumb-container'));
var switchContainer = angular.element(element[0].querySelector('.md-container'));
// no transition on initial load
$$rAF(function() {
element.removeClass('md-dragging');
});
// Tell the checkbox we don't want a click listener.
// Our drag listener tells us everything, using more granular events.
attr.mdNoClick = true;
checkboxLink(scope, element, attr, ngModel);
$mdUtil.attachDragBehavior(scope, switchContainer);
// These events are triggered by setup drag
switchContainer.on('$md.dragstart', onDragStart)
.on('$md.drag', onDrag)
.on('$md.dragend', onDragEnd);
function onDragStart(ev, drag) {
// Don't go if ng-disabled===true
if (disabledGetter(scope)) return ev.preventDefault();
drag.width = thumbContainer.prop('offsetWidth');
element.addClass('md-dragging');
}
function onDrag(ev, drag) {
var percent = drag.distance / drag.width;
//if checked, start from right. else, start from left
var translate = ngModel.$viewValue ? 1 - percent : -percent;
// Make sure the switch stays inside its bounds, 0-1%
translate = Math.max(0, Math.min(1, translate));
thumbContainer.css($mdConstant.CSS.TRANSFORM, 'translate3d(' + (100*translate) + '%,0,0)');
drag.translate = translate;
}
function onDragEnd(ev, drag) {
if (disabledGetter(scope)) return false;
element.removeClass('md-dragging');
thumbContainer.css($mdConstant.CSS.TRANSFORM, '');
// We changed if there is no distance (this is a click a click),
// or if the drag distance is >50% of the total.
var isChanged = Math.abs(drag.distance || 0) < 2 ||
(ngModel.$viewValue ? drag.translate < 0.5 : drag.translate > 0.5);
if (isChanged) {
scope.$apply(function() {
ngModel.$setViewValue(!ngModel.$viewValue);
ngModel.$render();
});
}
}
};
}
}
MdSwitch.$inject = ["mdCheckboxDirective", "$mdTheming", "$mdUtil", "$document", "$mdConstant", "$parse", "$$rAF"];
})();