UNPKG

fractal-core

Version:

A minimalist and well crafted app, content or component is our conviction

83 lines 3.02 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const core_1 = require("../core"); // set of helpers for building components // send a message to an input of a component from outside a Module /* istanbul ignore next */ function sendMsg(mod, id, inputName, msg) { return __awaiter(this, void 0, void 0, function* () { let ctx = mod.rootCtx; yield core_1.toIn(ctx.components[id])(inputName, msg); }); } exports.sendMsg = sendMsg; function setGroup(name, group) { return function (comp) { comp.groups[name] = group; return comp; }; } exports.setGroup = setGroup; function spaceOf(ctx) { return ctx.components[ctx.id]; } exports.spaceOf = spaceOf; // make a new component from another merging her state function props(state) { return function (comp) { let newComp = Object.assign({}, comp); // shallow clone newComp.state = core_1.clone(Object.assign(comp.state, state)); return newComp; }; } exports.props = props; function styles(style) { return function (comp) { let newComp = Object.assign({}, comp); // shallow clone newComp.groups.style = core_1.clone(core_1.mergeStyles(comp.groups.style, style)); return newComp; }; } exports.styles = styles; exports.compGroup = (groupName, arr, fn) => arr.reduce((comps, c) => { comps[groupName + '_' + c[0]] = fn(c[1]); return comps; }, {}); /** * Get the component descendants ids * @param ctx Any Context * @param id The component id */ exports.getDescendantIds = (ctx, id) => { let searchStr = id + '$'; return Object.keys(ctx.components).filter(compId => compId.includes(searchStr)); }; /** * Get the context of the parent of a component * @param ctx The component context */ exports.getParentCtx = (ctx) => ctx.components[(ctx.id + '').split('$').slice(0, -1).join('$')]; /** * Function for concateniting properties to a component, makes a copy fisrt * @param component The target component * @param options Options to concatenate */ exports.comp = (component, options) => { const newComp = core_1.clone(component); if (options.state) { newComp.state = Object.assign(newComp.state, options.state); } if (options.style) { newComp.groups.style = core_1.mergeStyles(newComp.groups.style, options.style); } return newComp; }; //# sourceMappingURL=component.js.map