4 * Copyright 2016 RIFT.IO Inc
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 var React
= require('react');
23 * Event listener Mixins. A vast majority of components are going to expose these events to the user so we're making
24 * a central location to house all of them.
28 onClick
: React
.PropTypes
.func
,
29 onMouseUp
: React
.PropTypes
.func
,
30 onMouseDown
: React
.PropTypes
.func
,
31 onMouseOver
: React
.PropTypes
.func
,
32 onMouseEnter
: React
.PropTypes
.func
,
33 onMouseLeave
: React
.PropTypes
.func
,
34 onMouseOut
: React
.PropTypes
.func
,
36 onTouchCancel
: React
.PropTypes
.func
,
37 onTouchEnd
: React
.PropTypes
.func
,
38 onTouchMove
: React
.PropTypes
.func
,
39 onTouchStart
: React
.PropTypes
.func
,
41 onKeyDown
: React
.PropTypes
.func
,
42 onKeyPress
: React
.PropTypes
.func
,
43 onKeyUp
: React
.PropTypes
.func
,
45 onFocus
: React
.PropTypes
.func
,
46 onBlur
: React
.PropTypes
.func
50 * A vast majority of these functions just check to see if the event function is defined and then passes the function
51 * both the event and the local props.
54 onClick: function(e
) {
55 if (Boolean(this.props
.onClick
) && !this.state
.isDisabled
&& !this.state
.isReadOnly
) {
56 //this.props.isActive = true;
57 this.props
.onClick(e
, this);
62 onMouseUp: function(e
) {
63 if (!this.state
.isDisabled
&& !this.state
.isReadOnly
) {
64 this.setState({isActive
:false});
65 if (Boolean(this.props
.onMouseUp
)) {
66 this.props
.onMouseUp(e
, this);
72 onMouseDown: function(e
) {
73 if (!this.state
.isDisabled
&& !this.state
.isReadOnly
) {
74 this.setState({isActive
:true});
75 if (Boolean(this.props
.onMouseDown
)) {
76 this.props
.onMouseDown(e
, this);
80 onMouseOver: function(e
) {
81 if (Boolean(this.props
.onMouseOver
) && !this.state
.isDisabled
&& !this.state
.isReadOnly
) {
82 this.props
.onMouseOver(e
, this);
87 onMouseEnter: function(e
) {
88 if (!this.state
.isDisabled
&& !this.state
.isReadOnly
) {
89 this.setState({isHovered
:true});
90 if (this.props
.onMouseEnter
) {
91 this.props
.onMouseEnter(e
, this);
97 onMouseLeave: function(e
) {
98 if (!this.state
.isDisabled
&& !this.state
.isReadOnly
) {
99 this.setState({isHovered
:false, isActive
:false});
100 if (Boolean(this.props
.onMouseLeave
)) {
101 this.props
.onMouseLeave(e
, this);
105 onMouseOut: function(e
) {
106 if (!this.state
.isDisabled
&& !this.state
.isReadOnly
) {
107 if (Boolean(this.props
.onMouseOut
)) {
108 this.props
.onMouseOut(e
, this);
114 onTouchCancel: function(e
) {
115 if (!this.state
.isDisabled
&& !this.state
.isReadOnly
) {
116 this.setState({isActive
: false});
117 if (Boolean(this.props
.onTouchCancel
)) {
118 this.props
.onTouchCancel(e
, this);
124 onTouchEnd: function(e
) {
125 if (!this.state
.isDisabled
&& !this.state
.isReadOnly
) {
126 this.setState({isActive
: false});
127 if (Boolean(this.props
.onTouchEnd
)) {
128 this.props
.onTouchEnd(e
, this);
134 onTouchMove: function(e
) {
135 if (Boolean(this.props
.onTouchMove
) && !this.state
.isDisabled
&& !this.state
.isReadOnly
) {
136 this.props
.onTouchMove(e
, this);
141 onTouchStart: function(e
) {
142 if (!this.state
.isDisabled
&& !this.state
.isReadOnly
) {
143 this.setState({isActive
: true});
144 if (Boolean(this.props
.onTouchStart
)) {
145 this.props
.onTouchStart(e
, this);
151 onKeyDown: function(e
) {
152 if (Boolean(this.props
.onKeyDown
) && !this.state
.isDisabled
&& !this.state
.isReadOnly
) {
153 this.props
.onKeyDown(e
, this);
158 onKeyPress: function(e
) {
159 if (Boolean(this.props
.onKeyPress
) && !this.state
.isDisabled
&& !this.state
.isReadOnly
) {
160 this.props
.onKeyPress(e
, this);
165 onKeyUp: function(e
) {
166 if (Boolean(this.props
.onKeyUp
) && !this.state
.isDisabled
&& !this.state
.isReadOnly
) {
167 this.props
.onKeyUp(e
, this);
172 onFocus: function(e
) {
173 if (!this.state
.isDisabled
&& !this.state
.isReadOnly
) {
174 this.setState({isFocused
: true});
175 if (Boolean(this.props
.onFocus
)) {
176 this.props
.onFocus(e
, this);
182 onBlur: function(e
) {
183 if (!this.state
.isDisabled
&& !this.state
.isReadOnly
) {
184 this.setState({isFocused
: false});
185 if (Boolean(this.props
.onBlur
)) {
186 this.props
.onBlur(e
, this);
194 * Generic clone function that takes an object and returns an independent clone of it.
195 * Needed to give the user a clone of the props instead of the props themselves to prevent direct access to the props.
199 clone: function(obj
) {
200 if (null == obj
|| "object" != typeof obj
) return obj
;
201 var copy
= obj
.constructor();
202 for (var attr
in obj
) {
203 if (obj
.hasOwnProperty(attr
)) copy
[attr
] = obj
[attr
];