20867
20867
20868
20868
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
20869
20869
20870
+ var componentDefaults = {
20871
+ width: 100,
20872
+ buttonColor: '#FFFFFF',
20873
+ passiveColor: '#FFFFFF',
20874
+ activeColor: '#13BF11'
20875
+ };
20876
+
20877
+ var stopEvent = function stopEvent(e) {
20878
+ e.preventDefault();
20879
+ e.stopPropagation();
20880
+ };
20881
+
20870
20882
var OnOff = function (_React$Component) {
20871
20883
_inherits(OnOff, _React$Component);
20872
20884
@@ -20876,15 +20888,11 @@
20876
20888
var _this = _possibleConstructorReturn(this, (OnOff.__proto__ || Object.getPrototypeOf(OnOff)).call(this, props));
20877
20889
20878
20890
var active = !!props.initialValue;
20879
- _this.state = {
20891
+ _this.state = _extends({}, componentDefaults, props, {
20880
20892
on: active, // false if not set
20881
- width: props.with || 100,
20882
20893
activeColorWidth: active ? 1 : 0.6,
20883
- buttonPosition: active ? 0.4 : 0,
20884
- buttonColor: props.buttonColor || '#FFFFFF',
20885
- passiveColor: props.passiveColor || '#FFFFFF',
20886
- activeColor: props.activeColor || '#13BF11'
20887
- };
20894
+ buttonPosition: active ? 0.4 : 0
20895
+ });
20888
20896
20889
20897
_this.handleChange = _this.handleChange.bind(_this);
20890
20898
_this.onChange = props.onChange || function () {};
@@ -20912,29 +20920,29 @@
20912
20920
}, {
20913
20921
key: 'getPointerCoords',
20914
20922
value: function getPointerCoords(e) {
20915
- return {
20916
- x: e.pageX || e.touches[0].pageX,
20917
- y: e.pageY || e.touches[0].pageY
20918
- };
20923
+ return e.pageX || e.touches[0].pageX;
20919
20924
}
20920
20925
}, {
20921
20926
key: 'onPointerDown',
20922
20927
value: function onPointerDown(e) {
20923
- this.touchDown = this.getPointerCoords(e.nativeEvent).x - parseInt(e.target.style.left, 10);
20928
+ stopEvent(e);
20929
+ this.touchDown = this.getPointerCoords(e.nativeEvent) - parseInt(e.target.style.left, 10);
20924
20930
window.addEventListener('ontouchend' in window ? 'touchend' : 'mouseup', this.onDragEnd);
20925
- if ('onmouseout' in window) e.target.addEventListener('mouseout', this.onDragEnd);
20926
20931
}
20927
20932
}, {
20928
20933
key: 'onDrag',
20929
20934
value: function onDrag(e) {
20930
- e.preventDefault();
20931
20935
if (!this.touchDown) return;else this.dragged = true;
20936
+ stopEvent(e);
20937
+
20938
+ // 0.4 and 0.6 are related to proportions where 1 is the width
20939
+ // 0.4 is the most left point of the buttonPosition
20940
+ // 0.6 is the most right point, so the active color fills the whole background behind the button
20932
20941
20933
- var positionNow = this.getPointerCoords(e.nativeEvent).x ;
20942
+ var positionNow = this.getPointerCoords(e.nativeEvent);
20934
20943
var diff = (positionNow - this.touchDown) / this.state.width;
20935
- // o.4 and 0.6 are related to proportions where 1 is the width
20936
- var max = 0.4;
20937
- if (diff < 0) diff = 0;else if (diff > max) diff = max;
20944
+ var maxDragDistance = 0.4;
20945
+ if (diff < 0) diff = 0;else if (diff > maxDragDistance) diff = maxDragDistance;
20938
20946
var pos = 0.6 + diff;
20939
20947
20940
20948
this.setState({
20945
20953
}, {
20946
20954
key: 'onDragEnd',
20947
20955
value: function onDragEnd(e) {
20956
+ stopEvent(e);
20957
+ if (!this.touchDown) return;
20958
+
20948
20959
window.removeEventListener('ontouchend' in window ? 'touchend' : 'mouseup', this.onDragEnd);
20949
- if ('onmouseout' in window) e.target.removeEventListener('mouseout', this.onDragEnd);
20950
20960
var newState = this.dragged ? this.state.buttonPosition > 0.2 : !this.state.on;
20951
20961
this.handleChange(newState);
20952
20962
this.touchDown = this.dragged = null;
@@ -20973,17 +20983,15 @@
20973
20983
20974
20984
return _react2.default.createElement(
20975
20985
'div',
20976
- {
20977
- /*onClick={this.handleChange}*/
20978
- style: { position: 'relative', height: 0.6 * this.state.width }
20979
- },
20986
+ { style: { position: 'relative', height: 0.6 * this.state.width } },
20980
20987
_react2.default.createElement('div', { style: passive }),
20981
20988
_react2.default.createElement('div', { style: active }),
20982
20989
_react2.default.createElement('div', {
20983
20990
onMouseMove: this.onDrag,
20984
20991
onTouchMove: this.onDrag,
20985
20992
onTouchStart: this.onPointerDown,
20986
20993
onMouseDown: this.onPointerDown,
20994
+ onMouseOut: this.onDragEnd,
20987
20995
style: button })
20988
20996
);
20989
20997
}
0 commit comments