1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.initLocales = initLocales;
|
7 | exports.setLanguage = setLanguage;
|
8 | exports.setLocale = setLocale;
|
9 | exports.setDirection = setDirection;
|
10 | exports.getLocale = getLocale;
|
11 | exports.getLanguage = getLanguage;
|
12 | exports.getDirection = getDirection;
|
13 | exports.config = config;
|
14 |
|
15 | var _react = _interopRequireDefault(require("react"));
|
16 |
|
17 | var _propTypes = _interopRequireDefault(require("prop-types"));
|
18 |
|
19 | var _hoistNonReactStatics = _interopRequireDefault(require("hoist-non-react-statics"));
|
20 |
|
21 | var _util = require("../util");
|
22 |
|
23 | var _getContextProps = _interopRequireDefault(require("./get-context-props"));
|
24 |
|
25 | var _errorBoundary = _interopRequireDefault(require("./error-boundary"));
|
26 |
|
27 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
28 |
|
29 | function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
|
30 |
|
31 | function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
|
32 |
|
33 | function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
|
34 |
|
35 | function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
|
36 |
|
37 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
38 |
|
39 | 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); } }
|
40 |
|
41 | function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
|
42 |
|
43 | function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
|
44 |
|
45 | function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
|
46 |
|
47 | function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function () { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
|
48 |
|
49 | function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
|
50 |
|
51 | function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
|
52 |
|
53 | function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
|
54 |
|
55 | function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
|
56 |
|
57 | 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; }
|
58 |
|
59 | 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) { _defineProperty(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; }
|
60 |
|
61 | function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
62 |
|
63 | var shallowEqual = _util.obj.shallowEqual;
|
64 |
|
65 | function getDisplayName(Component) {
|
66 | return Component.displayName || Component.name || 'Component';
|
67 | }
|
68 |
|
69 | var globalLocales;
|
70 | var currentGlobalLanguage = 'zh-cn';
|
71 | var currentGlobalLocale = {};
|
72 | var currentGlobalRtl;
|
73 |
|
74 | function initLocales(locales) {
|
75 | globalLocales = locales;
|
76 |
|
77 | if (locales) {
|
78 | currentGlobalLocale = locales[currentGlobalLanguage];
|
79 |
|
80 | if (typeof currentGlobalRtl !== 'boolean') {
|
81 | currentGlobalRtl = currentGlobalLocale && currentGlobalLocale.rtl;
|
82 | }
|
83 | }
|
84 | }
|
85 |
|
86 | function setLanguage(language) {
|
87 | if (globalLocales) {
|
88 | currentGlobalLanguage = language;
|
89 | currentGlobalLocale = globalLocales[language];
|
90 |
|
91 | if (typeof currentGlobalRtl !== 'boolean') {
|
92 | currentGlobalRtl = currentGlobalLocale && currentGlobalLocale.rtl;
|
93 | }
|
94 | }
|
95 | }
|
96 |
|
97 | function setLocale(locale) {
|
98 | currentGlobalLocale = _objectSpread(_objectSpread({}, globalLocales ? globalLocales[currentGlobalLanguage] : {}), locale);
|
99 |
|
100 | if (typeof currentGlobalRtl !== 'boolean') {
|
101 | currentGlobalRtl = currentGlobalLocale && currentGlobalLocale.rtl;
|
102 | }
|
103 | }
|
104 |
|
105 | function setDirection(dir) {
|
106 | currentGlobalRtl = dir === 'rtl';
|
107 | }
|
108 |
|
109 | function getLocale() {
|
110 | return currentGlobalLocale;
|
111 | }
|
112 |
|
113 | function getLanguage() {
|
114 | return currentGlobalLanguage;
|
115 | }
|
116 |
|
117 | function getDirection() {
|
118 | return currentGlobalRtl;
|
119 | }
|
120 |
|
121 | function config(Component) {
|
122 | var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
123 |
|
124 | if (Component.prototype.shouldComponentUpdate === undefined) {
|
125 | Component.prototype.shouldComponentUpdate = function shouldComponentUpdate(nextProps, nextState) {
|
126 | if (this.props.pure) {
|
127 | return !shallowEqual(this.props, nextProps) || !shallowEqual(this.state, nextState);
|
128 | }
|
129 |
|
130 | return true;
|
131 | };
|
132 | }
|
133 |
|
134 | var ConfigedComponent = function (_React$Component) {
|
135 | _inherits(ConfigedComponent, _React$Component);
|
136 |
|
137 | var _super = _createSuper(ConfigedComponent);
|
138 |
|
139 | function ConfigedComponent() {
|
140 | var _this;
|
141 |
|
142 | _classCallCheck(this, ConfigedComponent);
|
143 |
|
144 | for (var _len = arguments.length, _args = new Array(_len), _key = 0; _key < _len; _key++) {
|
145 | _args[_key] = arguments[_key];
|
146 | }
|
147 |
|
148 | _this = _super.call.apply(_super, [this].concat(_args));
|
149 |
|
150 | _defineProperty(_assertThisInitialized(_this), "_getInstance", function (ref) {
|
151 | _this._instance = ref;
|
152 |
|
153 | if (_this._instance && options.exportNames) {
|
154 | options.exportNames.forEach(function (name) {
|
155 | var field = _this._instance[name];
|
156 |
|
157 | if (typeof field === 'function') {
|
158 | _this[name] = field.bind(_this._instance);
|
159 | } else {
|
160 | _this[name] = field;
|
161 | }
|
162 | });
|
163 | }
|
164 | });
|
165 |
|
166 | _defineProperty(_assertThisInitialized(_this), "_deprecated", function () {
|
167 | if (_this.context.nextWarning !== false) {
|
168 | _util.log.deprecated.apply(_util.log, arguments);
|
169 | }
|
170 | });
|
171 |
|
172 | return _this;
|
173 | }
|
174 |
|
175 | _createClass(ConfigedComponent, [{
|
176 | key: "getInstance",
|
177 | value: function getInstance() {
|
178 | return this._instance;
|
179 | }
|
180 | }, {
|
181 | key: "render",
|
182 | value: function render() {
|
183 | var _this$props = this.props,
|
184 | prefix = _this$props.prefix,
|
185 | locale = _this$props.locale,
|
186 | pure = _this$props.pure,
|
187 | rtl = _this$props.rtl,
|
188 | errorBoundary = _this$props.errorBoundary,
|
189 | scriptUrl = _this$props.scriptUrl,
|
190 | others = _objectWithoutProperties(_this$props, ["prefix", "locale", "pure", "rtl", "errorBoundary", "scriptUrl"]);
|
191 |
|
192 | var _this$context = this.context,
|
193 | nextPrefix = _this$context.nextPrefix,
|
194 | _this$context$nextLoc = _this$context.nextLocale,
|
195 | nextLocale = _this$context$nextLoc === void 0 ? {} : _this$context$nextLoc,
|
196 | nextPure = _this$context.nextPure,
|
197 | nextRtl = _this$context.nextRtl,
|
198 | nextErrorBoundary = _this$context.nextErrorBoundary,
|
199 | nextScriptUrl = _this$context.nextScriptUrl;
|
200 | var displayName = options.componentName || getDisplayName(Component);
|
201 | var contextProps = (0, _getContextProps["default"])({
|
202 | prefix: prefix,
|
203 | locale: locale,
|
204 | pure: pure,
|
205 | rtl: rtl,
|
206 | errorBoundary: errorBoundary,
|
207 | scriptUrl: scriptUrl
|
208 | }, {
|
209 | nextPrefix: nextPrefix,
|
210 | nextLocale: _objectSpread(_objectSpread({}, currentGlobalLocale), nextLocale),
|
211 | nextPure: nextPure,
|
212 | nextRtl: typeof nextRtl === 'boolean' ? nextRtl : currentGlobalRtl === true ? true : undefined,
|
213 | nextErrorBoundary: nextErrorBoundary,
|
214 | nextScriptUrl: nextScriptUrl
|
215 | }, displayName);
|
216 |
|
217 | var newContextProps = ['prefix', 'locale', 'pure', 'rtl', 'scriptUrl'].reduce(function (ret, name) {
|
218 | if (typeof contextProps[name] !== 'undefined') {
|
219 | ret[name] = contextProps[name];
|
220 | }
|
221 |
|
222 | return ret;
|
223 | }, {});
|
224 | var newOthers = options.transform ? options.transform(others, this._deprecated) : others;
|
225 |
|
226 | var content = _react["default"].createElement(Component, _extends({}, newOthers, newContextProps, {
|
227 | ref: this._getInstance
|
228 | }));
|
229 |
|
230 | var _contextProps$errorBo = contextProps.errorBoundary,
|
231 | open = _contextProps$errorBo.open,
|
232 | othersBoundary = _objectWithoutProperties(_contextProps$errorBo, ["open"]);
|
233 |
|
234 | return open ? _react["default"].createElement(_errorBoundary["default"], othersBoundary, content) : content;
|
235 | }
|
236 | }]);
|
237 |
|
238 | return ConfigedComponent;
|
239 | }(_react["default"].Component);
|
240 |
|
241 | _defineProperty(ConfigedComponent, "propTypes", _objectSpread(_objectSpread({}, Component.propTypes || {}), {}, {
|
242 | prefix: _propTypes["default"].string,
|
243 | locale: _propTypes["default"].object,
|
244 | pure: _propTypes["default"].bool,
|
245 | rtl: _propTypes["default"].bool,
|
246 | errorBoundary: _propTypes["default"].oneOfType([_propTypes["default"].bool, _propTypes["default"].object]),
|
247 | scriptUrl: _propTypes["default"].string
|
248 | }));
|
249 |
|
250 | _defineProperty(ConfigedComponent, "contextTypes", _objectSpread(_objectSpread({}, Component.contextTypes || {}), {}, {
|
251 | nextPrefix: _propTypes["default"].string,
|
252 | nextLocale: _propTypes["default"].object,
|
253 | nextPure: _propTypes["default"].bool,
|
254 | nextRtl: _propTypes["default"].bool,
|
255 | nextWarning: _propTypes["default"].bool,
|
256 | nextErrorBoundary: _propTypes["default"].oneOfType([_propTypes["default"].bool, _propTypes["default"].object]),
|
257 | nextScriptUrl: _propTypes["default"].string
|
258 | }));
|
259 |
|
260 | ConfigedComponent.displayName = "Config(".concat(getDisplayName(Component), ")");
|
261 | (0, _hoistNonReactStatics["default"])(ConfigedComponent, Component);
|
262 | return ConfigedComponent;
|
263 | } |
\ | No newline at end of file |