UNPKG

8.98 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', {
4 value: true
5});
6
7var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
8
9var _get = function get(_x2, _x3, _x4) { var _again = true; _function: while (_again) { var object = _x2, property = _x3, receiver = _x4; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x2 = parent; _x3 = property; _x4 = receiver; _again = true; desc = parent = undefined; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } };
10
11function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
12
13function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
14
15function _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; }
16
17var _react = require('react');
18
19var _react2 = _interopRequireDefault(_react);
20
21var _colr = require('colr');
22
23var _colr2 = _interopRequireDefault(_colr);
24
25var _Board = require('./Board');
26
27var _Board2 = _interopRequireDefault(_Board);
28
29var _Preview = require('./Preview');
30
31var _Preview2 = _interopRequireDefault(_Preview);
32
33var _Ribbon = require('./Ribbon');
34
35var _Ribbon2 = _interopRequireDefault(_Ribbon);
36
37var _Alpha = require('./Alpha');
38
39var _Alpha2 = _interopRequireDefault(_Alpha);
40
41var _Params = require('./Params');
42
43var _Params2 = _interopRequireDefault(_Params);
44
45var _utilsValidationColor = require('./utils/validationColor');
46
47var _utilsValidationColor2 = _interopRequireDefault(_utilsValidationColor);
48
49function noop() {}
50
51var colr = new _colr2['default']();
52
53var Panel = (function (_React$Component) {
54 _inherits(Panel, _React$Component);
55
56 function Panel(props) {
57 var _this = this;
58
59 _classCallCheck(this, Panel);
60
61 _get(Object.getPrototypeOf(Panel.prototype), 'constructor', this).call(this, props);
62
63 var color = props.color || props.defaultColor;
64 var hsv = colr.fromHex(color).toHsvObject();
65
66 this.state = {
67 paramsHsv: hsv,
68 hsv: hsv,
69 alpha: props.alpha || props.defaultAlpha
70 };
71
72 var events = ['onChange', 'onAlphaChange', 'onFocus', 'onBlur', 'onSystemColorPickerOpen'];
73 // bind methods
74 events.forEach(function (m) {
75 _this[m] = _this[m].bind(_this);
76 });
77 }
78
79 _createClass(Panel, [{
80 key: 'componentWillReceiveProps',
81 value: function componentWillReceiveProps(nextProps) {
82 if (nextProps.color) {
83 var hsv = colr.fromHex(nextProps.color).toHsvObject();
84 this.setState({
85 hsv: hsv
86 });
87 }
88 if (nextProps.alpha !== undefined) {
89 this.setState({
90 alpha: nextProps.alpha
91 });
92 }
93 }
94 }, {
95 key: 'onChange',
96 value: function onChange(hsvObject) {
97 var syncParams = arguments.length <= 1 || arguments[1] === undefined ? true : arguments[1];
98
99 var hsv = hsvObject;
100 var state = {
101 hsv: hsv
102 };
103 if (syncParams) {
104 state.paramsHsv = hsv;
105 }
106 this.setState(state);
107
108 var ret = {
109 color: this.getHexColor(hsv),
110 hsv: hsv,
111 alpha: this.state.alpha
112 };
113 this.props.onChange(ret);
114 }
115 }, {
116 key: 'onSystemColorPickerOpen',
117 value: function onSystemColorPickerOpen(e) {
118 // only work with broswer which support color input
119 if (e.target.type === 'color') {
120 this.systemColorPickerOpen = true;
121 }
122 }
123 }, {
124 key: 'onAlphaChange',
125 value: function onAlphaChange(alpha) {
126 if (this.props.alpha === undefined) {
127 this.setState({
128 alpha: alpha
129 });
130 }
131 this.props.onChange({
132 color: this.getHexColor(),
133 hsv: this.state.hsv,
134 alpha: alpha
135 });
136 }
137 }, {
138 key: 'onFocus',
139 value: function onFocus() {
140 if (this._blurTimer) {
141 clearTimeout(this._blurTimer);
142 this._blurTimer = null;
143 } else {
144 this.props.onFocus();
145 }
146 }
147 }, {
148 key: 'onBlur',
149 value: function onBlur() {
150 var _this2 = this;
151
152 if (this._blurTimer) {
153 clearTimeout(this._blurTimer);
154 }
155 this._blurTimer = setTimeout(function () {
156 // if is system color picker open, then stop run blur
157 if (_this2.systemColorPickerOpen) {
158 _this2.systemColorPickerOpen = false;
159 return;
160 }
161
162 _this2.props.onBlur();
163 }, 100);
164 }
165 }, {
166 key: 'getHexColor',
167 value: function getHexColor(hsv) {
168 return colr.fromHsvObject(hsv || this.state.hsv).toHex();
169 }
170 }, {
171 key: 'render',
172 value: function render() {
173 var prefixCls = this.props.prefixCls;
174 var hsv = this.state.hsv;
175 var alpha = this.state.alpha;
176 return _react2['default'].createElement(
177 'div',
178 {
179 className: prefixCls,
180 style: this.props.style,
181 onFocus: this.onFocus,
182 onBlur: this.onBlur,
183 tabIndex: '0'
184 },
185 _react2['default'].createElement(
186 'div',
187 { className: prefixCls + '-' + 'inner' },
188 _react2['default'].createElement(_Board2['default'], {
189 rootPrefixCls: prefixCls,
190 hsv: hsv,
191 onChange: this.onChange
192 }),
193 _react2['default'].createElement(
194 'div',
195 { className: prefixCls + '-' + 'wrap' },
196 _react2['default'].createElement(
197 'div',
198 { className: prefixCls + '-' + 'wrap-ribbon' },
199 _react2['default'].createElement(_Ribbon2['default'], {
200 rootPrefixCls: prefixCls,
201 hsv: hsv,
202 onChange: this.onChange
203 })
204 ),
205 _react2['default'].createElement(
206 'div',
207 { className: prefixCls + '-' + 'wrap-alpha' },
208 _react2['default'].createElement(_Alpha2['default'], {
209 rootPrefixCls: prefixCls,
210 alpha: alpha,
211 hsv: hsv,
212 onChange: this.onAlphaChange
213 })
214 ),
215 _react2['default'].createElement(
216 'div',
217 { className: prefixCls + '-' + 'wrap-preview' },
218 _react2['default'].createElement(_Preview2['default'], {
219 rootPrefixCls: prefixCls,
220 alpha: alpha,
221 onChange: this.onChange,
222 onInputClick: this.onSystemColorPickerOpen,
223 hsv: hsv
224 })
225 )
226 ),
227 _react2['default'].createElement(
228 'div',
229 { className: prefixCls + '-' + 'wrap', style: { height: 40, marginTop: 6 } },
230 _react2['default'].createElement(_Params2['default'], {
231 rootPrefixCls: prefixCls,
232 hsv: this.state.paramsHsv,
233 alpha: alpha,
234 onAlphaChange: this.onAlphaChange,
235 onChange: this.onChange,
236 mode: this.props.mode
237 })
238 )
239 )
240 );
241 }
242 }]);
243
244 return Panel;
245})(_react2['default'].Component);
246
247exports['default'] = Panel;
248
249Panel.propTypes = {
250 defaultAlpha: _react.PropTypes.number,
251 defaultColor: _utilsValidationColor2['default'],
252 // can custom
253 prefixCls: _react.PropTypes.string,
254 color: _utilsValidationColor2['default'],
255 alpha: _react.PropTypes.number,
256 style: _react.PropTypes.object,
257 onChange: _react.PropTypes.func,
258 onFocus: _react.PropTypes.func,
259 onBlur: _react.PropTypes.func,
260 mode: _react.PropTypes.oneOf(['RGB', 'HSL', 'HSB'])
261};
262
263Panel.defaultProps = {
264 prefixCls: 'rc-color-picker-panel',
265 defaultColor: '#ff0000',
266 defaultAlpha: 100,
267 style: {},
268 onChange: noop,
269 onFocus: noop,
270 onBlur: noop,
271 mode: 'RGB'
272};
273module.exports = exports['default'];
\No newline at end of file