organism-react-i13n
Version:
190 lines (149 loc) • 5.67 kB
JavaScript
;
var _interopRequireDefault = require("reshow-runtime/helpers/interopRequireDefault");
var _interopRequireWildcard = require("reshow-runtime/helpers/interopRequireWildcard");
exports.__esModule = true;
exports["default"] = void 0;
var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("reshow-runtime/helpers/objectWithoutPropertiesLoose"));
var _react = _interopRequireWildcard(require("react"));
var _reshow = require("reshow");
var _reactAtomicMolecule = require("react-atomic-molecule");
var _organismReactIframe = _interopRequireDefault(require("organism-react-iframe"));
var _organismReactAjax = require("organism-react-ajax");
var _getObjectValue = _interopRequireDefault(require("get-object-value"));
var _winDoc = require("win-doc");
var _windowOnload2 = _interopRequireDefault(require("window-onload"));
var _getRandomId = require("get-random-id");
var _reshowHooks = require("reshow-hooks");
var _i13nStore = _interopRequireDefault(require("../../src/stores/i13nStore.js"));
var _index = require("../../src/index.js");
var _jsxRuntime = require("react/jsx-runtime");
var _MonitorPvidContainer, _MonitorBrowserBFCont;
var _excluded = ["I18N"];
var _windowOnload = (0, _windowOnload2["default"])(),
process = _windowOnload[0],
close = _windowOnload[1];
var urlDecode = decodeURIComponent;
var MonitorPvidContainer = function MonitorPvidContainer(props) {
var pvid = (0, _react.useRef)();
return /*#__PURE__*/(0, _jsxRuntime.jsx)(_reshow.Return, {
initStates: ["data", "pvid"],
children: function children(_ref) {
var nextPvid = _ref.pvid,
I13N = _ref.I13N;
if (pvid.current !== nextPvid) {
pvid.current = nextPvid;
(0, _index.i13nDispatch)("config/set", {
pvid: nextPvid,
I13N: I13N
});
}
return null;
}
});
};
var MonitorBrowserBFContainer = function MonitorBrowserBFContainer(_ref2) {
var nextBfApplyUrl = _ref2.bfApplyUrl,
nextToggleBfChange = _ref2.toggleBfChange;
var prevToggleBfChange = (0, _reshowHooks.usePrevious)(nextToggleBfChange);
var bfApplyUrl = (0, _react.useRef)();
if (prevToggleBfChange !== nextToggleBfChange) {
(0, _index.i13nDispatch)("action", {
I13N: {
action: "bfChange",
before: urlDecode((0, _getObjectValue["default"])(_i13nStore["default"].getState().get("lastUrl"), null, "")),
after: urlDecode(nextBfApplyUrl),
last: urlDecode((0, _getObjectValue["default"])(bfApplyUrl.current, null, ""))
}
});
bfApplyUrl.current = nextBfApplyUrl;
}
return null;
};
var handleIframe = function handleIframe(iframe) {
(0, _index.i13nDispatch)("config/set", {
iframe: iframe
});
};
var I13nElement = function I13nElement(props) {
var _Unsafe;
var _useState = (0, _react.useState)(),
iframe = _useState[0],
setIframe = _useState[1];
var _useState2 = (0, _react.useState)(),
isLoad = _useState2[0],
setIsLoad = _useState2[1];
var pvid = (0, _react.useRef)(true);
var oWin = (0, _winDoc.win)();
(0, _react.useEffect)(function () {
if (!oWin.__null) {
var I18N = props.I18N,
otherProps = (0, _objectWithoutPropertiesLoose2["default"])(props, _excluded);
oWin.i13nDispatch = _index.i13nDispatch;
(0, _index.i13nDispatch)("config/set", otherProps);
setIsLoad(true);
}
return function () {
close();
};
}, []);
return (0, _react.useMemo)(function () {
if (!isLoad) {
return null;
}
var dIframe;
if (iframe) {
dIframe = /*#__PURE__*/(0, _jsxRuntime.jsx)(_organismReactIframe["default"], {
style: Styles.iframe,
ref: handleIframe,
children: _Unsafe || (_Unsafe = /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactAtomicMolecule.Unsafe, {
children: iframe
}))
});
}
return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactAtomicMolecule.SemanticUI, {
children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_reshow.Return, {
initStates: ["pvid", "I13N"],
store: _i13nStore["default"],
children: function children(_ref3) {
var nextPvid = _ref3.pvid,
I13N = _ref3.I13N;
if (pvid.current !== nextPvid) {
pvid.current = nextPvid;
var query = {};
if (oWin.startUpTime) {
query.sp = Math.round((0, _getRandomId.getTimestamp)() - oWin.startUpTime);
oWin.startUpTime = false; //only log in page refresh
}
setTimeout(function () {
return process(function () {
return (0, _index.i13nDispatch)("impression", {
query: query,
I13N: (0, _getObjectValue["default"])(I13N),
callback: function callback(json, text) {
return setIframe(text);
}
});
});
});
}
return null;
}
}), _MonitorPvidContainer || (_MonitorPvidContainer = /*#__PURE__*/(0, _jsxRuntime.jsx)(MonitorPvidContainer, {})), /*#__PURE__*/(0, _jsxRuntime.jsx)(_reshow.Return, {
initStates: ["bfApplyUrl", "toggleBfChange"],
store: _organismReactAjax.ajaxStore,
children: _MonitorBrowserBFCont || (_MonitorBrowserBFCont = /*#__PURE__*/(0, _jsxRuntime.jsx)(MonitorBrowserBFContainer, {}))
}), dIframe]
});
}, [iframe, isLoad]);
};
var _default = I13nElement;
exports["default"] = _default;
var Styles = {
iframe: {
width: 1,
height: 1,
position: "absolute",
top: -99999
}
};
module.exports = exports.default;