UNPKG

dash-renderer

Version:

render dash components in react

162 lines (159 loc) 7.74 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.crawlLayout = exports.EventEmitter = void 0; exports.urlBase = urlBase; var _ramda = require("ramda"); function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure 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 _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 _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } } function _arrayWithHoles(r) { if (Array.isArray(r)) return r; } /* * requests_pathname_prefix is the new config parameter introduced in * dash==0.18.0. The previous versions just had url_base_pathname */ function urlBase(config) { var hasUrlBase = (0, _ramda.has)('url_base_pathname', config); var hasReqPrefix = (0, _ramda.has)('requests_pathname_prefix', config); if ((0, _ramda.type)(config) !== 'Object' || !hasUrlBase && !hasReqPrefix) { throw new Error("\n Trying to make an API request but neither\n \"url_base_pathname\" nor \"requests_pathname_prefix\"\n is in `config`. `config` is: ", config); } var base = hasReqPrefix ? config.requests_pathname_prefix : config.url_base_pathname; return base.charAt(base.length - 1) === '/' ? base : base + '/'; } var propsChildren = ['props', 'children']; // crawl a layout object or children array, apply a function on every object var _crawlLayout = exports.crawlLayout = function crawlLayout(object, func) { var currentPath = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : []; var extraPath = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : undefined; if (Array.isArray(object)) { // children array object.forEach((child, i) => { if (extraPath) { var objOf = (0, _ramda.findIndex)(p => (0, _ramda.includes)('{}', p), extraPath); if (objOf !== -1) { var front = (0, _ramda.slice)(0, objOf, extraPath); var back = (0, _ramda.slice)(objOf, extraPath.length, extraPath); if (front.length) { _crawlLayout((0, _ramda.path)(front, child), func, (0, _ramda.concat)(currentPath, (0, _ramda.concat)([i], front)), back); } else { var backPath = back.map(p => p.replace('{}', '')).filter(e => e); var childObj, childPath = (0, _ramda.concat)([i], backPath); if (backPath.length) { childObj = (0, _ramda.path)(backPath, child); } else { childObj = child; } for (var key in childObj) { var value = childObj[key]; _crawlLayout(value, func, (0, _ramda.concat)(currentPath, childPath.concat([key]))); } } } else { _crawlLayout((0, _ramda.path)(extraPath, child), func, (0, _ramda.concat)(currentPath, (0, _ramda.concat)([i], extraPath))); } } else { _crawlLayout(child, func, (0, _ramda.append)(i, currentPath)); } }); } else if ((0, _ramda.type)(object) === 'Object') { func(object, currentPath); var children = (0, _ramda.path)(propsChildren, object); if (children) { var newPath = (0, _ramda.concat)(currentPath, propsChildren); _crawlLayout(children, func, newPath); } var childrenProps = (0, _ramda.pathOr)([], [object.namespace, object.type], window.__dashprivate_childrenProps); childrenProps.forEach(childrenProp => { if (childrenProp.includes('[]')) { var _childrenProp$split$m = childrenProp.split('[]').map(p => p.split('.').filter(e => e)), _childrenProp$split$m2 = _slicedToArray(_childrenProp$split$m, 2), frontPath = _childrenProp$split$m2[0], backPath = _childrenProp$split$m2[1]; var front = (0, _ramda.concat)(['props'], frontPath); var basePath = (0, _ramda.concat)(currentPath, front); _crawlLayout((0, _ramda.path)(front, object), func, basePath, backPath); } else { if (childrenProp.includes('{}')) { var opath = childrenProp.split('.'); var _frontPath = []; var _backPath = []; var found = false; for (var i = 0; i < opath.length; i++) { var curPath = opath[i]; if (!found && curPath.includes('{}')) { found = true; _frontPath.push(curPath.replace('{}', '')); } else { if (found) { _backPath.push(curPath); } else { _frontPath.push(curPath); } } } var _newPath = (0, _ramda.concat)(currentPath, ['props', ..._frontPath]); var oValue = (0, _ramda.path)(['props', ..._frontPath], object); if (oValue !== undefined) { for (var key in oValue) { var value = oValue[key]; if (_backPath.length) { _crawlLayout((0, _ramda.path)(_backPath, value), func, (0, _ramda.concat)(_newPath, [key, ..._backPath])); } else { _crawlLayout(value, func, [..._newPath, key]); } } } } else { var _newPath2 = (0, _ramda.concat)(currentPath, ['props', ...childrenProp.split('.')]); _crawlLayout((0, _ramda.path)(['props', ...childrenProp.split('.')], object), func, _newPath2); } } }); } }; // There are packages for this but it's simple enough, I just // adapted it from https://gist.github.com/mudge/5830382 class EventEmitter { constructor() { this._ev = {}; } on(event, listener) { var events = this._ev[event] = this._ev[event] || []; events.push(listener); return () => this.removeListener(event, listener); } removeListener(event, listener) { var events = this._ev[event]; if (events) { var idx = events.indexOf(listener); if (idx > -1) { events.splice(idx, 1); } } } emit(event) { for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { args[_key - 1] = arguments[_key]; } var events = this._ev[event]; if (events) { events.forEach(listener => listener.apply(this, args)); } } once(event, listener) { var _this = this; var remove = this.on(event, function () { remove(); for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { args[_key2] = arguments[_key2]; } listener.apply(_this, args); }); } } exports.EventEmitter = EventEmitter;