1 | "use strict";
|
2 |
|
3 | var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
4 |
|
5 | var _typeof = require("@babel/runtime/helpers/typeof");
|
6 |
|
7 | Object.defineProperty(exports, "__esModule", {
|
8 | value: true
|
9 | });
|
10 | exports["default"] = void 0;
|
11 |
|
12 | var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
|
13 |
|
14 | var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
|
15 |
|
16 | var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
17 |
|
18 | var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
|
19 |
|
20 | var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
|
21 |
|
22 | var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
|
23 |
|
24 | var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
|
25 |
|
26 | var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
|
27 |
|
28 | var _react = _interopRequireWildcard(require("react"));
|
29 |
|
30 | var _propTypes = _interopRequireDefault(require("prop-types"));
|
31 |
|
32 | var _isNull = _interopRequireDefault(require("lodash/isNull"));
|
33 |
|
34 | var _isString = _interopRequireDefault(require("lodash/isString"));
|
35 |
|
36 | var _reactImageFallback = _interopRequireDefault(require("react-image-fallback"));
|
37 |
|
38 | var _link = _interopRequireDefault(require("@lskjs/link"));
|
39 |
|
40 | function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
41 |
|
42 | function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
43 |
|
44 | function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
|
45 |
|
46 | function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
|
47 |
|
48 | function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = (0, _getPrototypeOf2["default"])(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2["default"])(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2["default"])(this, result); }; }
|
49 |
|
50 | function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
|
51 |
|
52 |
|
53 | var textSizeRatio = 3;
|
54 | var textSizePercent = 30;
|
55 |
|
56 | var hashCode = function hashCode(str) {
|
57 | if ((0, _isNull["default"])(str)) return null;
|
58 | if (!(0, _isString["default"])(str)) str += '';
|
59 | var hash = 0;
|
60 | var i;
|
61 | var chr;
|
62 | if (str.length === 0) return hash;
|
63 |
|
64 | for (i = 0; i < str.length; i++) {
|
65 | chr = str.charCodeAt(i);
|
66 | hash = (hash << 5) - hash + chr;
|
67 | hash |= 0;
|
68 | }
|
69 |
|
70 | return Math.abs(hash);
|
71 | };
|
72 |
|
73 | var Avatar = function (_PureComponent) {
|
74 | (0, _inherits2["default"])(Avatar, _PureComponent);
|
75 |
|
76 | var _super = _createSuper(Avatar);
|
77 |
|
78 | function Avatar() {
|
79 | (0, _classCallCheck2["default"])(this, Avatar);
|
80 | return _super.apply(this, arguments);
|
81 | }
|
82 |
|
83 | (0, _createClass2["default"])(Avatar, [{
|
84 | key: "getColorByHash",
|
85 | value: function getColorByHash(num) {
|
86 | if ((0, _isNull["default"])(num)) {
|
87 | var index = Math.floor(Math.random() * Avatar.defaultColors.length);
|
88 | return Avatar.defaultColors[index];
|
89 | }
|
90 |
|
91 | num %= Avatar.defaultColors.length;
|
92 | return Avatar.defaultColors[num - 1] || Avatar.defaultColors[0];
|
93 | }
|
94 | }, {
|
95 | key: "getInnerStyle",
|
96 | value: function getInnerStyle() {
|
97 | var _this$props = this.props,
|
98 | size = _this$props.size,
|
99 | textScale = _this$props.textScale,
|
100 | shape = _this$props.shape,
|
101 | backgroundColor = _this$props.backgroundColor,
|
102 | innerStyle = _this$props.innerStyle,
|
103 | textColor = _this$props.textColor,
|
104 | title = _this$props.title,
|
105 | id = _this$props.id,
|
106 | src = _this$props.src;
|
107 | var str = id || title || null;
|
108 | var color = backgroundColor || this.getColorByHash(hashCode(str));
|
109 | var width = this.props.width || size;
|
110 | var height = this.props.height || size;
|
111 | var lineHeight = size;
|
112 | var fontSize = Math.min(
|
113 | Math.floor(size * textSizePercent / 100), lineHeight);
|
114 | var borderRadius = 'none';
|
115 |
|
116 | if (shape === 'circle') {
|
117 | borderRadius = '50%';
|
118 | } else if (shape === 'rounded') {
|
119 | borderRadius = 6;
|
120 | }
|
121 |
|
122 | return _objectSpread({
|
123 | boxSizing: 'border-box',
|
124 | maxWidth: '100%',
|
125 | objectFit: 'cover',
|
126 | display: 'flex',
|
127 | justifyContent: 'center',
|
128 | alignItems: 'center',
|
129 |
|
130 | width: width,
|
131 | height: height,
|
132 | textAlign: 'center',
|
133 | fontFamily: 'Helvetica, Arial, sans-serif',
|
134 | borderRadius: borderRadius,
|
135 | fontSize: "".concat(fontSize, "px"),
|
136 | lineHeight: "".concat(lineHeight, "px"),
|
137 | backgroundColor: src ? 'rgba(0, 0, 0, 0)' : color,
|
138 | color: textColor
|
139 | }, innerStyle);
|
140 | }
|
141 | }, {
|
142 | key: "renderAsImage",
|
143 | value: function renderAsImage() {
|
144 | var title = this.props.title || this.props.name;
|
145 | var src = this.props.src || this.props.avatar;
|
146 | return _react["default"].createElement(_reactImageFallback["default"], {
|
147 | src: src,
|
148 | fallbackImage: this.props.defaultAvatar || this.constructor.defaultAvatar,
|
149 | initialImage: this.props.defaultAvatar || this.constructor.defaultAvatar,
|
150 | style: this.getInnerStyle(),
|
151 | alt: title,
|
152 | title: title
|
153 | });
|
154 | }
|
155 | }, {
|
156 | key: "renderAsText",
|
157 | value: function renderAsText() {
|
158 | var _this$props2 = this.props,
|
159 | placeholder = _this$props2.placeholder,
|
160 | placeholderLimit = _this$props2.placeholderLimit;
|
161 | var title = this.props.title || this.props.name || placeholder || '';
|
162 |
|
163 | if (!placeholder) {
|
164 | placeholder = title.split(' ').map(function (s) {
|
165 | return s.charAt(0);
|
166 | }).join('').substr(0, placeholderLimit);
|
167 | }
|
168 |
|
169 | return _react["default"].createElement("div", {
|
170 | style: this.getInnerStyle()
|
171 | }, placeholder);
|
172 | }
|
173 | }, {
|
174 | key: "render",
|
175 | value: function render() {
|
176 | var _this$props3 = this.props,
|
177 | size = _this$props3.size,
|
178 | style = _this$props3.style,
|
179 | className = _this$props3.className,
|
180 | children = _this$props3.children,
|
181 | componentClass = _this$props3.componentClass;
|
182 | var src = this.props.src || this.props.avatar;
|
183 | var width = this.props.width || size;
|
184 | var height = this.props.height || size;
|
185 |
|
186 | var wrapperStyle = _objectSpread({
|
187 | display: 'block',
|
188 | position: 'relative',
|
189 | width: width,
|
190 | height: height
|
191 | }, style);
|
192 |
|
193 | var link = this.props.link || this.props.href;
|
194 | var Component = componentClass || (link ? _link["default"] : 'div');
|
195 | return _react["default"].createElement(Component, {
|
196 | href: link,
|
197 | className: className,
|
198 | style: wrapperStyle
|
199 | }, src != null ? this.renderAsImage() : this.renderAsText(), children);
|
200 | }
|
201 | }]);
|
202 | return Avatar;
|
203 | }(_react.PureComponent);
|
204 |
|
205 | Avatar.Badge = function (props) {
|
206 | var Wrap = props.componentClass,
|
207 | left = props.left,
|
208 | top = props.top,
|
209 | right = props.right,
|
210 | bottom = props.bottom,
|
211 | props2 = (0, _objectWithoutProperties2["default"])(props, ["componentClass", "left", "top", "right", "bottom"]);
|
212 | var offset = '7%';
|
213 | var style = {
|
214 | position: 'absolute'
|
215 | };
|
216 | ['left', 'top', 'right', 'bottom'].forEach(function (dir) {
|
217 | if (!props[dir]) return;
|
218 |
|
219 | if (typeof props[dir] === 'string') {
|
220 | style[dir] = props[dir];
|
221 | } else if (typeof props[dir] === 'number') {
|
222 | style[dir] = "".concat(props[dir], "%");
|
223 | } else {
|
224 | style[dir] = offset;
|
225 | }
|
226 | });
|
227 | return _react["default"].createElement("div", (0, _extends2["default"])({
|
228 | style: style
|
229 | }, props2), props.children);
|
230 | };
|
231 |
|
232 | Avatar.defaultColors = ['#F8B195', '#F67280', '#C06C84', '#6C5B7B', '#355C7D'];
|
233 | Avatar.defaultAvatar = '//cdn.mgbeta.ru/lsk/no-avatar.png';
|
234 | Avatar.defaultProps = {
|
235 | title: '',
|
236 | name: '',
|
237 | src: null,
|
238 | size: 64,
|
239 | width: null,
|
240 | height: null,
|
241 |
|
242 | textColor: '#d9d9d9',
|
243 | textScale: 1,
|
244 | placeholder: null,
|
245 | placeholderLimit: 3,
|
246 | shape: 'circle',
|
247 | style: {},
|
248 | innerStyle: {}
|
249 | };
|
250 | Avatar.propTypes = {
|
251 | title: _propTypes["default"].string,
|
252 | src: _propTypes["default"].string,
|
253 | size: _propTypes["default"].number,
|
254 | width: _propTypes["default"].any,
|
255 |
|
256 | height: _propTypes["default"].any,
|
257 |
|
258 | color: _propTypes["default"].string,
|
259 | textColor: _propTypes["default"].string,
|
260 | textScale: _propTypes["default"].number,
|
261 | shape: _propTypes["default"].string,
|
262 | placeholder: _propTypes["default"].string,
|
263 | placeholderLimit: _propTypes["default"].number,
|
264 |
|
265 | style: _propTypes["default"].object,
|
266 | innerStyle: _propTypes["default"].object
|
267 | };
|
268 | var _default = Avatar;
|
269 | exports["default"] = _default;
|
270 |
|
\ | No newline at end of file |