1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, '__esModule', { value: true });
|
4 |
|
5 | var _extends = require('@babel/runtime/helpers/extends');
|
6 | var _objectWithoutPropertiesLoose = require('@babel/runtime/helpers/objectWithoutPropertiesLoose');
|
7 | var _inheritsLoose = require('@babel/runtime/helpers/inheritsLoose');
|
8 | var svgInjector = require('@tanem/svg-injector');
|
9 | var PropTypes = require('prop-types');
|
10 | var React = require('react');
|
11 |
|
12 | function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
13 |
|
14 | function _interopNamespace(e) {
|
15 | if (e && e.__esModule) return e;
|
16 | var n = Object.create(null);
|
17 | if (e) {
|
18 | Object.keys(e).forEach(function (k) {
|
19 | if (k !== 'default') {
|
20 | var d = Object.getOwnPropertyDescriptor(e, k);
|
21 | Object.defineProperty(n, k, d.get ? d : {
|
22 | enumerable: true,
|
23 | get: function () { return e[k]; }
|
24 | });
|
25 | }
|
26 | });
|
27 | }
|
28 | n["default"] = e;
|
29 | return Object.freeze(n);
|
30 | }
|
31 |
|
32 | var _extends__default = _interopDefaultLegacy(_extends);
|
33 | var _objectWithoutPropertiesLoose__default = _interopDefaultLegacy(_objectWithoutPropertiesLoose);
|
34 | var _inheritsLoose__default = _interopDefaultLegacy(_inheritsLoose);
|
35 | var PropTypes__namespace = _interopNamespace(PropTypes);
|
36 | var React__namespace = _interopNamespace(React);
|
37 |
|
38 |
|
39 | var shallowDiffers = function shallowDiffers(a, b) {
|
40 | for (var i in a) {
|
41 | if (!(i in b)) {
|
42 | return true;
|
43 | }
|
44 | }
|
45 |
|
46 | for (var _i in b) {
|
47 | if (a[_i] !== b[_i]) {
|
48 | return true;
|
49 | }
|
50 | }
|
51 |
|
52 | return false;
|
53 | };
|
54 |
|
55 | var _excluded = ["afterInjection", "beforeInjection", "evalScripts", "fallback", "httpRequestWithCredentials", "loading", "renumerateIRIElements", "src", "useRequestCache", "wrapper"];
|
56 | var svgNamespace = 'http://www.w3.org/2000/svg';
|
57 | var xlinkNamespace = 'http://www.w3.org/1999/xlink';
|
58 | var ReactSVG = function (_React$Component) {
|
59 | _inheritsLoose__default["default"](ReactSVG, _React$Component);
|
60 |
|
61 | function ReactSVG() {
|
62 | var _this;
|
63 |
|
64 | for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
65 | args[_key] = arguments[_key];
|
66 | }
|
67 |
|
68 | _this = _React$Component.call.apply(_React$Component, [this].concat(args)) || this;
|
69 | _this.initialState = {
|
70 | hasError: false,
|
71 | isLoading: true
|
72 | };
|
73 | _this.state = _this.initialState;
|
74 | _this._isMounted = false;
|
75 | _this.reactWrapper = void 0;
|
76 | _this.nonReactWrapper = void 0;
|
77 |
|
78 | _this.refCallback = function (reactWrapper) {
|
79 | _this.reactWrapper = reactWrapper;
|
80 | };
|
81 |
|
82 | return _this;
|
83 | }
|
84 |
|
85 | var _proto = ReactSVG.prototype;
|
86 |
|
87 | _proto.renderSVG = function renderSVG() {
|
88 | var _this2 = this;
|
89 |
|
90 |
|
91 | if (this.reactWrapper instanceof Node) {
|
92 | var _this$props = this.props,
|
93 | beforeInjection = _this$props.beforeInjection,
|
94 | evalScripts = _this$props.evalScripts,
|
95 | httpRequestWithCredentials = _this$props.httpRequestWithCredentials,
|
96 | renumerateIRIElements = _this$props.renumerateIRIElements,
|
97 | src = _this$props.src,
|
98 | useRequestCache = _this$props.useRequestCache;
|
99 |
|
100 |
|
101 | var afterInjection = this.props.afterInjection;
|
102 | var wrapper = this.props.wrapper;
|
103 |
|
104 |
|
105 | var nonReactWrapper;
|
106 | var nonReactTarget;
|
107 |
|
108 | if (wrapper === 'svg') {
|
109 | nonReactWrapper = document.createElementNS(svgNamespace, wrapper);
|
110 | nonReactWrapper.setAttribute('xmlns', svgNamespace);
|
111 | nonReactWrapper.setAttribute('xmlns:xlink', xlinkNamespace);
|
112 | nonReactTarget = document.createElementNS(svgNamespace, wrapper);
|
113 | } else {
|
114 | nonReactWrapper = document.createElement(wrapper);
|
115 | nonReactTarget = document.createElement(wrapper);
|
116 | }
|
117 |
|
118 | nonReactWrapper.appendChild(nonReactTarget);
|
119 | nonReactTarget.dataset.src = src;
|
120 | this.nonReactWrapper = this.reactWrapper.appendChild(nonReactWrapper);
|
121 |
|
122 | var afterEach = function afterEach(error, svg) {
|
123 | if (error) {
|
124 | _this2.removeSVG();
|
125 |
|
126 | if (!_this2._isMounted) {
|
127 | afterInjection(error);
|
128 | return;
|
129 | }
|
130 | }
|
131 |
|
132 |
|
133 |
|
134 | if (_this2._isMounted) {
|
135 | _this2.setState(function () {
|
136 | return {
|
137 | hasError: !!error,
|
138 | isLoading: false
|
139 | };
|
140 | }, function () {
|
141 | afterInjection(error, svg);
|
142 | });
|
143 | }
|
144 | };
|
145 |
|
146 | svgInjector.SVGInjector(nonReactTarget, {
|
147 | afterEach: afterEach,
|
148 | beforeEach: beforeInjection,
|
149 | cacheRequests: useRequestCache,
|
150 | evalScripts: evalScripts,
|
151 | httpRequestWithCredentials: httpRequestWithCredentials,
|
152 | renumerateIRIElements: renumerateIRIElements
|
153 | });
|
154 | }
|
155 | };
|
156 |
|
157 | _proto.removeSVG = function removeSVG() {
|
158 | var _this$nonReactWrapper;
|
159 |
|
160 | if ((_this$nonReactWrapper = this.nonReactWrapper) != null && _this$nonReactWrapper.parentNode) {
|
161 | this.nonReactWrapper.parentNode.removeChild(this.nonReactWrapper);
|
162 | this.nonReactWrapper = null;
|
163 | }
|
164 | };
|
165 |
|
166 | _proto.componentDidMount = function componentDidMount() {
|
167 | this._isMounted = true;
|
168 | this.renderSVG();
|
169 | };
|
170 |
|
171 | _proto.componentDidUpdate = function componentDidUpdate(prevProps) {
|
172 | var _this3 = this;
|
173 |
|
174 | if (shallowDiffers(prevProps, this.props)) {
|
175 | this.setState(function () {
|
176 | return _this3.initialState;
|
177 | }, function () {
|
178 | _this3.removeSVG();
|
179 |
|
180 | _this3.renderSVG();
|
181 | });
|
182 | }
|
183 | };
|
184 |
|
185 | _proto.componentWillUnmount = function componentWillUnmount() {
|
186 | this._isMounted = false;
|
187 | this.removeSVG();
|
188 | };
|
189 |
|
190 | _proto.render = function render() {
|
191 |
|
192 | var _this$props2 = this.props;
|
193 | _this$props2.afterInjection;
|
194 | _this$props2.beforeInjection;
|
195 | _this$props2.evalScripts;
|
196 | var Fallback = _this$props2.fallback;
|
197 | _this$props2.httpRequestWithCredentials;
|
198 | var Loading = _this$props2.loading;
|
199 | _this$props2.renumerateIRIElements;
|
200 | _this$props2.src;
|
201 | _this$props2.useRequestCache;
|
202 | var wrapper = _this$props2.wrapper,
|
203 | rest = _objectWithoutPropertiesLoose__default["default"](_this$props2, _excluded);
|
204 |
|
205 |
|
206 |
|
207 |
|
208 | var Wrapper = wrapper;
|
209 | return React__namespace.createElement(Wrapper, _extends__default["default"]({}, rest, {
|
210 | ref: this.refCallback
|
211 | }, wrapper === 'svg' ? {
|
212 | xmlns: svgNamespace,
|
213 | xmlnsXlink: xlinkNamespace
|
214 | } : {}), this.state.isLoading && Loading && React__namespace.createElement(Loading, null), this.state.hasError && Fallback && React__namespace.createElement(Fallback, null));
|
215 | };
|
216 |
|
217 | return ReactSVG;
|
218 | }(React__namespace.Component);
|
219 | ReactSVG.defaultProps = {
|
220 | afterInjection: function afterInjection() {
|
221 | return undefined;
|
222 | },
|
223 | beforeInjection: function beforeInjection() {
|
224 | return undefined;
|
225 | },
|
226 | evalScripts: 'never',
|
227 | fallback: null,
|
228 | httpRequestWithCredentials: false,
|
229 | loading: null,
|
230 | renumerateIRIElements: true,
|
231 | useRequestCache: true,
|
232 | wrapper: 'div'
|
233 | };
|
234 | ReactSVG.propTypes = {
|
235 | afterInjection: PropTypes__namespace.func,
|
236 | beforeInjection: PropTypes__namespace.func,
|
237 | evalScripts: PropTypes__namespace.oneOf(['always', 'once', 'never']),
|
238 | fallback: PropTypes__namespace.oneOfType([PropTypes__namespace.func, PropTypes__namespace.object, PropTypes__namespace.string]),
|
239 | httpRequestWithCredentials: PropTypes__namespace.bool,
|
240 | loading: PropTypes__namespace.oneOfType([PropTypes__namespace.func, PropTypes__namespace.object, PropTypes__namespace.string]),
|
241 | renumerateIRIElements: PropTypes__namespace.bool,
|
242 | src: PropTypes__namespace.string.isRequired,
|
243 | useRequestCache: PropTypes__namespace.bool,
|
244 | wrapper: PropTypes__namespace.oneOf(['div', 'span', 'svg'])
|
245 | };
|
246 |
|
247 | exports.ReactSVG = ReactSVG;
|
248 |
|