diff --git a/dist/js/react-bootstrap-switch.js b/dist/js/react-bootstrap-switch.js index 6bd69988..8461d432 100644 --- a/dist/js/react-bootstrap-switch.js +++ b/dist/js/react-bootstrap-switch.js @@ -47,6 +47,9 @@ indeterminate: this._prop('indeterminate') }; }, + componentWillReceiveProps: function(nextProps) { + return this.value(nextProps.state); + }, _prop: function(key) { if (typeof this.props[key] === 'undefined') { return this.defaults[key]; diff --git a/dist/js/react-bootstrap-switch.min.js b/dist/js/react-bootstrap-switch.min.js index 44e76889..e569da3c 100644 --- a/dist/js/react-bootstrap-switch.min.js +++ b/dist/js/react-bootstrap-switch.min.js @@ -8,4 +8,4 @@ * ======================================================================== */ -(function(){var t,e;e=require("react"),t=require("jquery"),module.exports=e.createClass({defaults:{state:!0,size:null,animate:!0,disabled:!1,readonly:!1,indeterminate:!1,inverse:!1,onColor:"primary",offColor:"default",onText:"ON",offText:"OFF",labelText:" ",handleWidth:"auto",labelWidth:"auto",baseClass:"bootstrap-switch",wrapperClass:"wrapper"},getInitialState:function(){return{state:this._prop("state"),handleWidth:this._prop("handleWidth"),labelWidth:this._prop("labelWidth"),offset:null,dragStart:!1,focus:!1,disabled:this._prop("disabled"),readonly:this._prop("readonly"),indeterminate:this._prop("indeterminate")}},_prop:function(t){return"undefined"==typeof this.props[t]?this.defaults[t]:this.props[t]},value:function(t){return"undefined"==typeof t?this.state.state:this.state.disabled||this.state.readonly?this:this.state.state===t?this:(this._changeState(!!t),this)},valueState:function(t){return this.value(t)},toggleState:function(){return this.toggleValue()},toggleValue:function(){return this.state.disabled||this.state.readonly?this:this._changeState(this.state.indeterminate?!0:!this.state.state)},disabled:function(t){return"undefined"==typeof t?this.state.disabled:(t=!!t,t===this.state.disabled?this:this.toggleDisabled())},toggleDisabled:function(){return this.setState({disabled:!this.state.disabled}),this},readonly:function(t){return"undefined"==typeof t?this.state.readonly:(t=!!t,t===this.state.readonly?this:this.toggleReadonly())},toggleReadonly:function(){return this.setState({readonly:!this.state.readonly}),this},handleWidth:function(t){return"undefined"==typeof t?this.state.handleWidth:(this.setState({handleWidth:t},function(t){return function(){return t._width(),t._containerPosition()}}(this)),this)},labelWidth:function(t){return"undefined"==typeof t?this.state.labelWidth:(this.setState({labelWidth:t},function(t){return function(){return t._width(),t._containerPosition()}}(this)),this)},_fireStateChange:function(){return"undefined"!=typeof this.props.onChange?this.props.onChange.length>=2?this.props.onChange(this,this.state.state):this.props.onChange(this.state.state):void 0},_changeState:function(t){return this.setState({indeterminate:!1,state:t},function(t){return function(){return t._containerPosition(),t._fireStateChange()}}(this))},_elmTrigger:function(e){var s;return s=t(this.refs.element),s.trigger(e)},_handleHandlers:function(){return t(this.refs.on).on("click.bootstrapSwitch",function(t){return function(e){return e.preventDefault(),e.stopPropagation(),t.state.disabled||t.state.readonly?void 0:(t._changeState(!1),t._elmTrigger("focus.bootstrapSwitch"))}}(this)),t(this.refs.off).on("click.bootstrapSwitch",function(t){return function(e){return e.preventDefault(),e.stopPropagation(),t.state.disabled||t.state.readonly?void 0:(t._changeState(!0),t._elmTrigger("focus.bootstrapSwitch"))}}(this))},componentDidMount:function(){var e,s;return e=function(t){return function(){return t._width(function(){return t._containerPosition(null)})}}(this),t(this.refs.wrapper).is(":visible")?e():s=window.setInterval(function(a){return function(){return t(a.refs.wrapper).is(":visible")?(e(),window.clearInterval(s)):void 0}}(this),50),this._handleHandlers(),this._labelHandlers(),this._elementHandlers()},_width:function(e){var s,a,n,i,r;return i=t(this.refs.on),n=t(this.refs.off),a=t(this.refs.label),s=i.add(n),s.add(a).css("width",""),r="auto"===this.state.handleWidth?Math.max(i.width(),n.width()):this.state.handleWidth,s.width(r),a.width(function(t){return function(e,s){return"auto"!==t.state.labelWidth?t.state.labelWidth:Math.max(r,s)}}(this)),this.setState({handleWidth:i.outerWidth(),labelWidth:a.outerWidth()},e)},_containerPosition:function(t){var e;return null==t&&(t=this.state.state),e=[0,"-"+this.state.handleWidth+"px"],this.setState(this.state.indeterminate?{offset:"-"+this.state.handleWidth/2+"px"}:t?{offset:this._prop("inverse")?e[1]:e[0]}:{offset:this._prop("inverse")?e[0]:e[1]})},_elementHandlers:function(){var e;return e=t(this.refs.element),e.on({"change.bootstrapSwitch":function(t){return function(e,s){return e.preventDefault(),e.stopImmediatePropagation(),t._changeState(!t.state.state)}}(this),"focus.bootstrapSwitch":function(t){return function(e){return e.preventDefault(),t.setState({focus:!0})}}(this),"blur.bootstrapSwitch":function(t){return function(e){return e.preventDefault(),t.setState({focus:!1})}}(this),"keydown.bootstrapSwitch":function(t){return function(e){if(e.which&&!t.state.disabled&&!t.state.readonly)switch(e.which){case 37:return e.preventDefault(),e.stopImmediatePropagation(),t._changeState(!1);case 39:return e.preventDefault(),e.stopImmediatePropagation(),t._changeState(!0)}}}(this)})},_labelHandlers:function(){var e;return e=t(this.refs.label),e.on({click:function(t){return t.stopPropagation()},"mousedown.bootstrapSwitch touchstart.bootstrapSwitch":function(t){return function(e){return t.state.dragStart||t.state.disabled||t.state.readonly?void 0:(e.preventDefault(),e.stopPropagation(),t.setState({indeterminate:!1,dragStart:(e.pageX||e.originalEvent.touches[0].pageX)-parseInt(t.state.offset,10)}),t._elmTrigger("focus.bootstrapSwitch"))}}(this),"mousemove.bootstrapSwitch touchmove.bootstrapSwitch":function(t){return function(e){var s;if(null!=t.state.dragStart&&(e.preventDefault(),s=(e.pageX||e.originalEvent.touches[0].pageX)-t.state.dragStart,!(s<-t.state.handleWidth||s>0)))return t.setState({offset:s+"px",dragged:!0})}}(this),"mouseup.bootstrapSwitch touchend.bootstrapSwitch":function(t){return function(e){var s,a;if(t.state.dragStart)return e.preventDefault(),a=!t.state.state,t.state.dragged&&(s=parseInt(t.state.offset),a=s>-(t.state.handleWidth/2),a=t._prop("inverse")?!a:a),t.setState({dragStart:!1,dragged:!1,state:a},function(){return t._containerPosition(),t._fireStateChange()})}}(this),"mouseleave.bootstrapSwitch":function(t){return e.trigger("mouseup.bootstrapSwitch")}})},render:function(){var t,s,a,n,i;return n=function(t){return function(){var e;return e=[""+t._prop("baseClass")].concat(t._prop("wrapperClass")),e.push(t.state.state?t._prop("baseClass")+"-on":t._prop("baseClass")+"-off"),null!=t._prop("size")&&e.push(t._prop("baseClass")+"-"+t._prop("size")),t.state.disabled&&e.push(t._prop("baseClass")+"-disabled"),t.state.readonly&&e.push(t._prop("baseClass")+"-readonly"),t.state.indeterminate&&e.push(t._prop("baseClass")+"-indeterminate"),t._prop("inverse")&&e.push(t._prop("baseClass")+"-inverse"),t._prop("id")&&e.push(t._prop("baseClass")+"-id-"+t._prop("id")),t._prop("animate")&&!t.state.dragStart&&e.push(t._prop("baseClass")+"-animate"),t.state.focus&&e.push(t._prop("baseClass")+"-focused"),e.join(" ")}}(this)(),a=e.createElement("span",{ref:"on",style:{width:this.state.handleWidth},className:this._prop("baseClass")+"-handle-on "+this._prop("baseClass")+"-"+this._prop("onColor")},this._prop("onText")),s=e.createElement("span",{ref:"off",style:{width:this.state.handleWidth},className:this._prop("baseClass")+"-handle-off "+this._prop("baseClass")+"-"+this._prop("offColor")},this._prop("offText")),t=this.state.labelWidth+2*this.state.handleWidth,i=this.state.labelWidth+this.state.handleWidth,t===i&&(t=i="auto"),e.createElement("div",{className:n,ref:"wrapper",style:{width:i}},e.createElement("div",{className:this._prop("baseClass")+"-container",ref:"container",style:{width:t,marginLeft:this.state.offset}},this._prop("inverse")?s:a,e.createElement("span",{className:this._prop("baseClass")+"-label",style:{width:this.state.labelWidth},ref:"label"},this._prop("labelText")),this._prop("inverse")?a:s,e.createElement("input",{type:"checkbox",ref:"element"})))}})}).call(this); \ No newline at end of file +(function(){var t,e;e=require("react"),t=require("jquery"),module.exports=e.createClass({defaults:{state:!0,size:null,animate:!0,disabled:!1,readonly:!1,indeterminate:!1,inverse:!1,onColor:"primary",offColor:"default",onText:"ON",offText:"OFF",labelText:" ",handleWidth:"auto",labelWidth:"auto",baseClass:"bootstrap-switch",wrapperClass:"wrapper"},getInitialState:function(){return{state:this._prop("state"),handleWidth:this._prop("handleWidth"),labelWidth:this._prop("labelWidth"),offset:null,dragStart:!1,focus:!1,disabled:this._prop("disabled"),readonly:this._prop("readonly"),indeterminate:this._prop("indeterminate")}},componentWillReceiveProps:function(t){return this.value(t.state)},_prop:function(t){return"undefined"==typeof this.props[t]?this.defaults[t]:this.props[t]},value:function(t){return"undefined"==typeof t?this.state.state:this.state.disabled||this.state.readonly?this:this.state.state===t?this:(this._changeState(!!t),this)},valueState:function(t){return this.value(t)},toggleState:function(){return this.toggleValue()},toggleValue:function(){return this.state.disabled||this.state.readonly?this:this._changeState(this.state.indeterminate?!0:!this.state.state)},disabled:function(t){return"undefined"==typeof t?this.state.disabled:(t=!!t,t===this.state.disabled?this:this.toggleDisabled())},toggleDisabled:function(){return this.setState({disabled:!this.state.disabled}),this},readonly:function(t){return"undefined"==typeof t?this.state.readonly:(t=!!t,t===this.state.readonly?this:this.toggleReadonly())},toggleReadonly:function(){return this.setState({readonly:!this.state.readonly}),this},handleWidth:function(t){return"undefined"==typeof t?this.state.handleWidth:(this.setState({handleWidth:t},function(t){return function(){return t._width(),t._containerPosition()}}(this)),this)},labelWidth:function(t){return"undefined"==typeof t?this.state.labelWidth:(this.setState({labelWidth:t},function(t){return function(){return t._width(),t._containerPosition()}}(this)),this)},_fireStateChange:function(){return"undefined"!=typeof this.props.onChange?this.props.onChange.length>=2?this.props.onChange(this,this.state.state):this.props.onChange(this.state.state):void 0},_changeState:function(t){return this.setState({indeterminate:!1,state:t},function(t){return function(){return t._containerPosition(),t._fireStateChange()}}(this))},_elmTrigger:function(e){var s;return s=t(this.refs.element),s.trigger(e)},_handleHandlers:function(){return t(this.refs.on).on("click.bootstrapSwitch",function(t){return function(e){return e.preventDefault(),e.stopPropagation(),t.state.disabled||t.state.readonly?void 0:(t._changeState(!1),t._elmTrigger("focus.bootstrapSwitch"))}}(this)),t(this.refs.off).on("click.bootstrapSwitch",function(t){return function(e){return e.preventDefault(),e.stopPropagation(),t.state.disabled||t.state.readonly?void 0:(t._changeState(!0),t._elmTrigger("focus.bootstrapSwitch"))}}(this))},componentDidMount:function(){var e,s;return e=function(t){return function(){return t._width(function(){return t._containerPosition(null)})}}(this),t(this.refs.wrapper).is(":visible")?e():s=window.setInterval(function(a){return function(){return t(a.refs.wrapper).is(":visible")?(e(),window.clearInterval(s)):void 0}}(this),50),this._handleHandlers(),this._labelHandlers(),this._elementHandlers()},_width:function(e){var s,a,n,i,r;return i=t(this.refs.on),n=t(this.refs.off),a=t(this.refs.label),s=i.add(n),s.add(a).css("width",""),r="auto"===this.state.handleWidth?Math.max(i.width(),n.width()):this.state.handleWidth,s.width(r),a.width(function(t){return function(e,s){return"auto"!==t.state.labelWidth?t.state.labelWidth:Math.max(r,s)}}(this)),this.setState({handleWidth:i.outerWidth(),labelWidth:a.outerWidth()},e)},_containerPosition:function(t){var e;return null==t&&(t=this.state.state),e=[0,"-"+this.state.handleWidth+"px"],this.setState(this.state.indeterminate?{offset:"-"+this.state.handleWidth/2+"px"}:t?{offset:this._prop("inverse")?e[1]:e[0]}:{offset:this._prop("inverse")?e[0]:e[1]})},_elementHandlers:function(){var e;return e=t(this.refs.element),e.on({"change.bootstrapSwitch":function(t){return function(e,s){return e.preventDefault(),e.stopImmediatePropagation(),t._changeState(!t.state.state)}}(this),"focus.bootstrapSwitch":function(t){return function(e){return e.preventDefault(),t.setState({focus:!0})}}(this),"blur.bootstrapSwitch":function(t){return function(e){return e.preventDefault(),t.setState({focus:!1})}}(this),"keydown.bootstrapSwitch":function(t){return function(e){if(e.which&&!t.state.disabled&&!t.state.readonly)switch(e.which){case 37:return e.preventDefault(),e.stopImmediatePropagation(),t._changeState(!1);case 39:return e.preventDefault(),e.stopImmediatePropagation(),t._changeState(!0)}}}(this)})},_labelHandlers:function(){var e;return e=t(this.refs.label),e.on({click:function(t){return t.stopPropagation()},"mousedown.bootstrapSwitch touchstart.bootstrapSwitch":function(t){return function(e){return t.state.dragStart||t.state.disabled||t.state.readonly?void 0:(e.preventDefault(),e.stopPropagation(),t.setState({indeterminate:!1,dragStart:(e.pageX||e.originalEvent.touches[0].pageX)-parseInt(t.state.offset,10)}),t._elmTrigger("focus.bootstrapSwitch"))}}(this),"mousemove.bootstrapSwitch touchmove.bootstrapSwitch":function(t){return function(e){var s;if(null!=t.state.dragStart&&(e.preventDefault(),s=(e.pageX||e.originalEvent.touches[0].pageX)-t.state.dragStart,!(s<-t.state.handleWidth||s>0)))return t.setState({offset:s+"px",dragged:!0})}}(this),"mouseup.bootstrapSwitch touchend.bootstrapSwitch":function(t){return function(e){var s,a;if(t.state.dragStart)return e.preventDefault(),a=!t.state.state,t.state.dragged&&(s=parseInt(t.state.offset),a=s>-(t.state.handleWidth/2),a=t._prop("inverse")?!a:a),t.setState({dragStart:!1,dragged:!1,state:a},function(){return t._containerPosition(),t._fireStateChange()})}}(this),"mouseleave.bootstrapSwitch":function(t){return e.trigger("mouseup.bootstrapSwitch")}})},render:function(){var t,s,a,n,i;return n=function(t){return function(){var e;return e=[""+t._prop("baseClass")].concat(t._prop("wrapperClass")),e.push(t.state.state?t._prop("baseClass")+"-on":t._prop("baseClass")+"-off"),null!=t._prop("size")&&e.push(t._prop("baseClass")+"-"+t._prop("size")),t.state.disabled&&e.push(t._prop("baseClass")+"-disabled"),t.state.readonly&&e.push(t._prop("baseClass")+"-readonly"),t.state.indeterminate&&e.push(t._prop("baseClass")+"-indeterminate"),t._prop("inverse")&&e.push(t._prop("baseClass")+"-inverse"),t._prop("id")&&e.push(t._prop("baseClass")+"-id-"+t._prop("id")),t._prop("animate")&&!t.state.dragStart&&e.push(t._prop("baseClass")+"-animate"),t.state.focus&&e.push(t._prop("baseClass")+"-focused"),e.join(" ")}}(this)(),a=e.createElement("span",{ref:"on",style:{width:this.state.handleWidth},className:this._prop("baseClass")+"-handle-on "+this._prop("baseClass")+"-"+this._prop("onColor")},this._prop("onText")),s=e.createElement("span",{ref:"off",style:{width:this.state.handleWidth},className:this._prop("baseClass")+"-handle-off "+this._prop("baseClass")+"-"+this._prop("offColor")},this._prop("offText")),t=this.state.labelWidth+2*this.state.handleWidth,i=this.state.labelWidth+this.state.handleWidth,t===i&&(t=i="auto"),e.createElement("div",{className:n,ref:"wrapper",style:{width:i}},e.createElement("div",{className:this._prop("baseClass")+"-container",ref:"container",style:{width:t,marginLeft:this.state.offset}},this._prop("inverse")?s:a,e.createElement("span",{className:this._prop("baseClass")+"-label",style:{width:this.state.labelWidth},ref:"label"},this._prop("labelText")),this._prop("inverse")?a:s,e.createElement("input",{type:"checkbox",ref:"element"})))}})}).call(this); \ No newline at end of file