UNPKG

dash-renderer

Version:

render dash components in react

87 lines (82 loc) 5.33 kB
"use strict"; var _index = require("../actions/index"); var _patch = require("../actions/patch"); var _paths = require("../actions/paths"); var _wrapping = require("../wrapper/wrapping"); var _stores = require("./stores"); function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); } function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } } function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); } function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); } function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; } function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } /** * Set the props of a dash component by id or path. * * @param idOrPath Path or id of the dash component to update. * @param props The props to update. */ function set_props(idOrPath, props) { var ds = (0, _stores.getStores)(); for (var y = 0; y < ds.length; y++) { var _ds$y = ds[y], dispatch = _ds$y.dispatch, getState = _ds$y.getState; var componentPath = void 0; var state = getState(); if (!Array.isArray(idOrPath)) { componentPath = (0, _paths.getPath)(state.paths, idOrPath); } else { componentPath = idOrPath; } var oldComponent = (0, _wrapping.getComponentLayout)(componentPath, state); // Handle any patch props props = (0, _patch.parsePatchProps)(props, (oldComponent === null || oldComponent === void 0 ? void 0 : oldComponent.props) || {}); // Update the props dispatch((0, _index.updateProps)({ props, itempath: componentPath, renderType: 'clientsideApi' })); dispatch((0, _index.notifyObservers)({ id: idOrPath, props })); if (!oldComponent) { return; } dispatch((0, _index.setPaths)((0, _paths.computePaths)(_objectSpread(_objectSpread({}, oldComponent), {}, { props: _objectSpread(_objectSpread({}, oldComponent.props), props) }), _toConsumableArray(componentPath), state.paths, state.paths.events))); } } // Clean url code adapted from https://github.com/braintree/sanitize-url/blob/main/src/constants.ts // to allow for data protocol. var invalidProtocols = /^([^\w]*)(javascript|vbscript)/im; var newLines = /&(tab|newline);/gi; // eslint-disable-next-line no-control-regex var ctrlChars = /[\u0000-\u001F\u007F-\u009F\u2000-\u200D\uFEFF]/gim; var htmlEntities = /&#(\w+)(^\w|;)?/g; var clean_url = function clean_url(url) { var fallback = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'about:blank'; if (url === '') { return url; } var cleaned = url.replace(newLines, '').replace(ctrlChars, '').replace(htmlEntities, function (_, dec) { return String.fromCharCode(dec); }).trim(); if (invalidProtocols.test(cleaned)) { return fallback; } return url; }; var dc = window.dash_clientside = window.dash_clientside || {}; dc['set_props'] = set_props; dc['clean_url'] = dc['clean_url'] === undefined ? clean_url : dc['clean_url']; dc['Patch'] = _patch.PatchBuilder;