UNPKG

dash-renderer

Version:

render dash components in react

197 lines (194 loc) 10.4 kB
"use strict"; 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); } Object.defineProperty(exports, "__esModule", { value: true }); exports.crawlLayout = exports.EventEmitter = void 0; exports.urlBase = urlBase; var _ramda = require("ramda"); function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); } function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } } function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), 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); } 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 _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 _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(function (child, i) { if (extraPath) { var objOf = (0, _ramda.findIndex)(function (p) { return (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(function (p) { return p.replace('{}', ''); }).filter(function (e) { return 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(function (childrenProp) { if (childrenProp.includes('[]')) { var _childrenProp$split$m = childrenProp.split('[]').map(function (p) { return p.split('.').filter(function (e) { return 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'].concat(_frontPath)); var oValue = (0, _ramda.path)(['props'].concat(_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].concat(_backPath))); } else { _crawlLayout(value, func, [].concat(_toConsumableArray(_newPath), [key])); } } } } else { var _newPath2 = (0, _ramda.concat)(currentPath, ['props'].concat(_toConsumableArray(childrenProp.split('.')))); _crawlLayout((0, _ramda.path)(['props'].concat(_toConsumableArray(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 var EventEmitter = exports.EventEmitter = /*#__PURE__*/function () { function EventEmitter() { _classCallCheck(this, EventEmitter); this._ev = {}; } return _createClass(EventEmitter, [{ key: "on", value: function on(event, listener) { var _this = this; var events = this._ev[event] = this._ev[event] || []; events.push(listener); return function () { return _this.removeListener(event, listener); }; } }, { key: "removeListener", value: function removeListener(event, listener) { var events = this._ev[event]; if (events) { var idx = events.indexOf(listener); if (idx > -1) { events.splice(idx, 1); } } } }, { key: "emit", value: function emit(event) { var _this2 = this; 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(function (listener) { return listener.apply(_this2, args); }); } } }, { key: "once", value: function once(event, listener) { var _this3 = 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(_this3, args); }); } }]); }();