123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351 |
- import _toConsumableArray from 'babel-runtime/helpers/toConsumableArray';
- import _Object$getPrototypeOf from 'babel-runtime/core-js/object/get-prototype-of';
- import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
- import _createClass from 'babel-runtime/helpers/createClass';
- import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
- import _get from 'babel-runtime/helpers/get';
- import _inherits from 'babel-runtime/helpers/inherits';
-
-
- import onsElements from '../ons/elements';
- import util from '../ons/util';
- import autoStyle from '../ons/autostyle';
- import ModifierUtil from '../ons/internal/modifier-util';
- import BaseCheckboxElement from './base/base-checkbox';
- import contentReady from '../ons/content-ready';
- import GestureDetector from '../ons/gesture-detector';
-
- var scheme = {
- '': 'switch--*',
- '.switch__input': 'switch--*__input',
- '.switch__handle': 'switch--*__handle',
- '.switch__toggle': 'switch--*__toggle'
- };
-
- var locations = {
- ios: [1, 21],
- material: [0, 16]
- };
-
-
-
- var SwitchElement = function (_BaseCheckboxElement) {
- _inherits(SwitchElement, _BaseCheckboxElement);
-
- function SwitchElement() {
- _classCallCheck(this, SwitchElement);
-
- var _this = _possibleConstructorReturn(this, (SwitchElement.__proto__ || _Object$getPrototypeOf(SwitchElement)).call(this));
-
- contentReady(_this, function () {
- _this.attributeChangedCallback('modifier', null, _this.getAttribute('modifier'));
- });
-
- _this._onChange = _this._onChange.bind(_this);
- _this._onRelease = _this._onRelease.bind(_this);
- _this._lastTimeStamp = 0;
- return _this;
- }
-
- _createClass(SwitchElement, [{
- key: '_getPosition',
-
-
-
-
- value: function _getPosition(e) {
- var l = this._locations;
- return Math.min(l[1], Math.max(l[0], this._startX + e.gesture.deltaX));
- }
- }, {
- key: '_emitChangeEvent',
- value: function _emitChangeEvent() {
- util.triggerElementEvent(this, 'change', {
- value: this.checked,
- switch: this,
- isInteractive: true
- });
- }
- }, {
- key: '_onChange',
- value: function _onChange(event) {
- if (event && event.stopPropagation) {
- event.stopPropagation();
- }
-
- this._emitChangeEvent();
- }
- }, {
- key: '_onClick',
- value: function _onClick(ev) {
- if (ev.target.classList.contains(this.defaultElementClass + '__touch') || ev.timeStamp - this._lastTimeStamp < 50
- ) {
- ev.preventDefault();
- }
- this._lastTimeStamp = ev.timeStamp;
- }
- }, {
- key: '_onHold',
- value: function _onHold(e) {
- if (!this.disabled) {
- ModifierUtil.addModifier(this, 'active');
- document.addEventListener('release', this._onRelease);
- }
- }
- }, {
- key: '_onDragStart',
- value: function _onDragStart(e) {
- if (this.disabled || ['left', 'right'].indexOf(e.gesture.direction) === -1) {
- ModifierUtil.removeModifier(this, 'active');
- return;
- }
-
- e.consumed = true;
-
- ModifierUtil.addModifier(this, 'active');
- this._startX = this._locations[this.checked ? 1 : 0];
-
- this.addEventListener('drag', this._onDrag);
- document.addEventListener('release', this._onRelease);
- }
- }, {
- key: '_onDrag',
- value: function _onDrag(e) {
- e.stopPropagation();
- this._handle.style.left = this._getPosition(e) + 'px';
- }
- }, {
- key: '_onRelease',
- value: function _onRelease(e) {
- var l = this._locations;
- var position = this._getPosition(e);
- var previousValue = this.checked;
-
- this.checked = position >= (l[0] + l[1]) / 2;
-
- if (this.checked !== previousValue) {
- this._emitChangeEvent();
- }
-
- this.removeEventListener('drag', this._onDrag);
- document.removeEventListener('release', this._onRelease);
-
- this._handle.style.left = '';
- ModifierUtil.removeModifier(this, 'active');
- }
- }, {
- key: 'click',
- value: function click() {
- var ev = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
-
- if (!this.disabled) {
- this.checked = !this.checked;
- this._emitChangeEvent();
- this._lastTimeStamp = ev.timeStamp || 0;
- }
- }
- }, {
- key: 'connectedCallback',
- value: function connectedCallback() {
- var _this2 = this;
-
- contentReady(this, function () {
- _this2._input.addEventListener('change', _this2._onChange);
- });
-
- this.addEventListener('dragstart', this._onDragStart);
- this.addEventListener('hold', this._onHold);
- this.addEventListener('tap', this.click);
- this.addEventListener('click', this._onClick);
- this._gestureDetector = new GestureDetector(this, { dragMinDistance: 1, holdTimeout: 251, passive: true });
- }
- }, {
- key: 'disconnectedCallback',
- value: function disconnectedCallback() {
- var _this3 = this;
-
- contentReady(this, function () {
- _this3._input.removeEventListener('change', _this3._onChange);
- });
-
- this.removeEventListener('dragstart', this._onDragStart);
- this.removeEventListener('hold', this._onHold);
- this.removeEventListener('tap', this.click);
- this.removeEventListener('click', this._onClick);
- if (this._gestureDetector) {
- this._gestureDetector.dispose();
- }
- }
- }, {
- key: 'attributeChangedCallback',
- value: function attributeChangedCallback(name, last, current) {
- if (name === 'modifier') {
- var md = (current || '').indexOf('material') !== -1;
- this._locations = locations[md ? 'material' : 'ios'];
- }
-
- _get(SwitchElement.prototype.__proto__ || _Object$getPrototypeOf(SwitchElement.prototype), 'attributeChangedCallback', this).call(this, name, last, current);
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- }, {
- key: '_scheme',
- get: function get() {
- return scheme;
- }
- }, {
- key: '_defaultClassName',
- get: function get() {
- return 'switch';
- }
- }, {
- key: '_template',
- get: function get() {
- return '\n <input type="' + this.type + '" class="' + this._defaultClassName + '__input">\n <div class="' + this._defaultClassName + '__toggle">\n <div class="' + this._defaultClassName + '__handle">\n <div class="' + this._defaultClassName + '__touch"></div>\n </div>\n </div>\n ';
- }
- }, {
- key: 'type',
- get: function get() {
- return 'checkbox';
- }
- }, {
- key: '_handle',
- get: function get() {
- return this.querySelector('.' + this._defaultClassName + '__handle');
- }
- }, {
- key: 'checkbox',
- get: function get() {
- return this._input;
- }
- }], [{
- key: 'observedAttributes',
- get: function get() {
- return [].concat(_toConsumableArray(_get(SwitchElement.__proto__ || _Object$getPrototypeOf(SwitchElement), 'observedAttributes', this)), ['modifier']);
- }
- }]);
-
- return SwitchElement;
- }(BaseCheckboxElement);
-
- export default SwitchElement;
-
-
- onsElements.Switch = SwitchElement;
- customElements.define('ons-switch', SwitchElement);
|