{"version":3,"sources":["webpack:///./src/main/web/components/ui/template/TemplateItem.ts"],"names":["props","context","state","parsedTemplate","getChildContext","baseContext","parentContext","this","templateDataContext","dataContextOverride","capturedContext","componentDidMount","compileTemplate","componentWillReceiveProps","templateEqual","template","shouldComponentUpdate","nextProps","nextState","shallowEqual","componentProps","componentMapper","render","error","createElement","ErrorNotification","errorMessage","component","root","flattenRoot","D","span","isArray","children","cloneElement","className","classNames","Maybe","fromNullable","map","cp","getOrElse","parsed","Array","length","capturer","CapturedContext","inheritAndCapture","appliedTemplateScope","compile","source","then","renderedHtml","options","ModuleRegistry","parseHtmlToReact","setState","getResult","catch","console","defaultProps","id","Math","random","undefined","Component","a","b","isEqual","Object","is","key","hasOwnProperty","TemplateItem"],"mappings":"0IAmBA,OACA,QACA,QACA,OACA,QAEA,QACA,SACA,SAEA,SAwBA,cAME,sBAAYA,EAA0BC,GAAtC,MACE,YAAMD,EAAOC,IAAQ,K,OACrB,EAAKC,MAAQ,CACXC,eAAgB,M,EAyGtB,OAlHkC,4BAahC,uBAAAC,gBAAA,WACE,IAAMC,EAAc,YAAMD,gBAAe,WACnCE,EAAgBC,KAAKN,QAAQO,oBAE7BC,EAAgD,CACpDD,oBAAqBD,KAAKL,MAAMQ,iBAAmBJ,GAErD,OAAO,EAAP,uBAAYD,GAAgBI,IAG9B,uBAAAE,kBAAA,WACEJ,KAAKK,gBAAgBL,KAAKP,QAG5B,uBAAAa,0BAAA,SAA0Bb,GACnBc,cAAcd,EAAMe,SAAUR,KAAKP,MAAMe,WAC5CR,KAAKK,gBAAgBZ,IAIzB,uBAAAgB,sBAAA,SAAsBC,EAA8BC,GAClD,QACEJ,cAAcP,KAAKP,MAAMe,SAAUE,EAAUF,WAC7CI,aAAaZ,KAAKP,MAAMoB,eAAgBH,EAAUG,iBAClDb,KAAKP,MAAMqB,kBAAoBJ,EAAUI,iBACzCF,aAAaZ,KAAKL,MAAOgB,KAI7B,uBAAAI,OAAA,WACE,GAAIf,KAAKL,MAA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pC,MAAMA,GACd,EAAKiC,SAAS,CAAEjC,MAAK,QA9GpB,aAAAqC,aAAqE,CAC1EC,GAAIC,KAAKC,SACThD,cAAUiD,GA+Gd,aAlHA,CAAkC,EAAAC,WAoHlC,SAASnD,cAAcoD,EAAaC,GAClC,OAAOD,IAAMC,GAAMD,EAAEhB,SAAWiB,EAAEjB,QAAU,EAAAkB,QAAQF,EAAEb,QAASc,EAAEd,SAGnE,SAASlC,aAAgB+C,EAAMC,GAC7B,GAAIE,OAAOC,GAAGJ,EAAGC,GACf,OAAO,EAET,GAAiB,iBAAND,GAA+B,iBAANC,EAClC,OAAO,EAET,IAAK,IAAMI,KAAOL,EAChB,GAAKA,EAAEM,eAAeD,MAGjBJ,EAAEK,eAAeD,KAASF,OAAOC,GAAGJ,EAAEK,GAAMJ,EAAEI,KACjD,OAAO,EAGX,IAAK,IAAMA,KAAOJ,EAChB,GAAKA,EAAEK,eAAeD,KAGjBL,EAAEM,eAAeD,GACpB,OAAO,EAGX,OAAO,EA/II,EAAAE,eAkJb,UAAeA","file":"default~app~diagram-search-result~field-editor~inline-template~mp-account-manager~mp-admin-config-ma~7530fdbc-1322d60f77a085de2740.js","sourcesContent":["/**\n * ResearchSpace\n * Copyright (C) 2020, © Trustees of the British Museum\n * Copyright (C) 2015-2019, metaphacts GmbH\n *\n * This program is free software: you can redistribute it and/or modify\n * it under the terms of the GNU General Public License as published by\n * the Free Software Foundation, either version 3 of the License, or\n * (at your option) any later version.\n\n * This program is distributed in the hope that it will be useful,\n * but WITHOUT ANY WARRANTY; without even the implied warranty of\n * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n * GNU General Public License for more details.\n *\n * You should have received a copy of the GNU General Public License\n * along with this program. If not, see .\n */\n\nimport { createElement, ReactElement, cloneElement, Props } from 'react';\nimport * as D from 'react-dom-factories';\nimport { isEqual, isArray } from 'lodash';\nimport * as classNames from 'classnames';\nimport * as Maybe from 'data.maybe';\n\nimport { Component, TemplateContext } from 'platform/api/components';\nimport { CapturedContext } from 'platform/api/services/template';\nimport { ModuleRegistry } from 'platform/api/module-loader';\n\nimport { ErrorNotification } from 'platform/components/ui/notification';\n\nexport interface TemplateItemProps extends Props {\n template: Template;\n componentProps?: {\n [key: string]: any;\n };\n componentMapper?: (component: JSX.Element) => JSX.Element;\n}\n\ntype Template = {\n source: string;\n options?: {};\n};\n\ninterface State {\n parsedTemplate?: ReactElement | ReactElement[];\n capturedContext?: CapturedContext;\n error?: any;\n}\n\n/**\n * Helper component that renders parametrized handlebars templates to react elements.\n */\nexport class TemplateItem extends Component {\n static defaultProps: Partial = {\n id: Math.random(),\n template: undefined,\n };\n\n constructor(props: TemplateItemProps, context: any) {\n super(props, context);\n this.state = {\n parsedTemplate: null,\n };\n }\n\n getChildContext() {\n const baseContext = super.getChildContext();\n const parentContext = this.context.templateDataContext;\n\n const dataContextOverride: Partial = {\n templateDataContext: this.state.capturedContext || parentContext,\n };\n return { ...baseContext, ...dataContextOverride };\n }\n\n componentDidMount() {\n this.compileTemplate(this.props);\n }\n\n componentWillReceiveProps(props) {\n if (!templateEqual(props.template, this.props.template)) {\n this.compileTemplate(props);\n }\n }\n\n shouldComponentUpdate(nextProps: TemplateItemProps, nextState: State) {\n return !(\n templateEqual(this.props.template, nextProps.template) &&\n shallowEqual(this.props.componentProps, nextProps.componentProps) &&\n this.props.componentMapper === nextProps.componentMapper &&\n shallowEqual(this.state, nextState)\n );\n }\n\n render() {\n if (this.state.error) {\n return createElement(ErrorNotification, { errorMessage: this.state.error });\n }\n\n const { parsedTemplate } = this.state;\n const root = this.flattenRoot(parsedTemplate);\n\n let component: JSX.Element;\n if (typeof root === 'string') {\n component = D.span({}, root);\n } else if (isArray(root)) {\n return root;\n } else if (root) {\n // propagate also props.children, we need this for react-resizable\n // to be able to add resize handle to templated items\n const children = this.props.children ? [root.props.children, this.props.children] : root.props.children;\n component = cloneElement(root, {\n ...root.props,\n ...this.props.componentProps,\n className: classNames(\n Maybe.fromNullable(this.props.componentProps)\n .map((cp) => cp.className)\n .getOrElse(''),\n root.props.className\n ),\n children,\n });\n } else {\n component = null;\n }\n\n if (component && this.props.componentMapper) {\n component = this.props.componentMapper(component);\n }\n\n return component;\n }\n\n private flattenRoot(parsed: ReactElement | ReactElement[]) {\n if (Array.isArray(parsed)) {\n if (parsed.length === 0) {\n return null;\n } else if (parsed.length > 1) {\n return parsed;\n } else {\n return parsed[0];\n }\n } else {\n return parsed;\n }\n }\n\n private compileTemplate(props) {\n const { templateDataContext } = this.context;\n\n const capturer = CapturedContext.inheritAndCapture(templateDataContext);\n this.appliedTemplateScope\n .compile(props.template.source)\n // parse to react, but do not omit whitespaces\n .then((template) => {\n const renderedHtml = template(props.template.options, { capturer, parentContext: templateDataContext });\n return ModuleRegistry.parseHtmlToReact(renderedHtml);\n })\n .then((parsedTemplate) => {\n this.setState({ parsedTemplate, capturedContext: capturer.getResult() });\n })\n .catch((error) => {\n console.error(error);\n this.setState({ error });\n });\n }\n}\n\nfunction templateEqual(a: Template, b: Template) {\n return a === b || (a.source === b.source && isEqual(a.options, b.options));\n}\n\nfunction shallowEqual(a: T, b: T) {\n if (Object.is(a, b)) {\n return true;\n }\n if (typeof a !== 'object' || typeof b !== 'object') {\n return false;\n }\n for (const key in a) {\n if (!a.hasOwnProperty(key)) {\n continue;\n }\n if (!b.hasOwnProperty(key) || !Object.is(a[key], b[key])) {\n return false;\n }\n }\n for (const key in b) {\n if (!b.hasOwnProperty(key)) {\n continue;\n }\n if (!a.hasOwnProperty(key)) {\n return false;\n }\n }\n return true;\n}\n\nexport default TemplateItem;\n"],"sourceRoot":""}