UNPKG

dash-renderer

Version:

render dash components in react

78 lines (73 loc) 3.87 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 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) }), [...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, (_, dec) => 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;