dash-renderer
Version:
render dash components in react
92 lines (89 loc) • 4.1 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getAllPMCIds = getAllPMCIds;
exports.parsePMCId = parsePMCId;
exports.replacePMC = replacePMC;
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; }
/**
* Deserialize pattern matching ids that come in one of the form:
* - '{"type":"component","index":["MATCH"]}.children'
* - '{"type":"component","index":["MATCH"]}'
*
* @param id The raw object as a string id.
* @returns The id object.
*/
function parsePMCId(id) {
var componentId, propName;
var index = id.lastIndexOf('}');
if (index + 2 < id.length) {
propName = id.substring(index + 2);
componentId = JSON.parse(id.substring(0, index + 1));
} else {
componentId = JSON.parse(id);
}
return [componentId, propName];
}
/**
* Get all the associated ids for an id.
*
* @param id Id to get all the pmc ids from.
* @param state State of the store.
* @param triggerKey Key to remove from the equality comparison.
* @returns
*/
function getAllPMCIds(id, state, triggerKey) {
var keysOfIds = (0, _ramda.keys)(id);
var idKey = keysOfIds.join(',');
return state.paths.objs[idKey].map(obj => keysOfIds.reduce((acc, key, i) => {
acc[key] = obj.values[i];
return acc;
}, {})).filter(obj => (0, _ramda.equals)((0, _ramda.dissoc)(triggerKey, obj), (0, _ramda.dissoc)(triggerKey, id)));
}
/**
* Replace the pattern matching ids with the actual trigger value
* for MATCH, all the ids for ALL and smaller than the trigger value
* for ALLSMALLER.
*
* @param id The parsed id in dictionary format.
* @param cb Original callback info.
* @param index Index of the dependency in case there is more than one changed id.
* @param getState Function to get the state of the redux store.
* @returns List of replaced ids.
*/
function replacePMC(id, cb, index, getState) {
var extras = [];
var replaced = {};
(0, _ramda.toPairs)(id).forEach(_ref => {
var _ref2 = _slicedToArray(_ref, 2),
key = _ref2[0],
value = _ref2[1];
if (extras.length) {
// All done.
return;
}
if (Array.isArray(value)) {
var triggerValue = (cb.parsedChangedPropsIds[index] || cb.parsedChangedPropsIds[0])[key];
if (value.includes('MATCH')) {
replaced[key] = triggerValue;
} else if (value.includes('ALL')) {
extras = getAllPMCIds(id, getState(), key);
} else if (value.includes('ALLSMALLER')) {
extras = getAllPMCIds(id, getState(), key).filter(obj => obj[key] < triggerValue);
}
} else {
replaced[key] = value;
}
});
if (extras.length) {
return extras;
}
return [replaced];
}