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,GAAG,CAAC,CAAc,UAAc,EAAd,KAAA,IAAI,CAAC,SAAS,EAAd,cAAc,EAAd,IAAc,CAAC;YAA5B,IAAI,KAAK,SAAA;YACb,EAAE,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC;gBAClC,KAAK,CAAC,QAAQ,EAAE,CAAC;YAClB,CAAC;SACD;IACF,CAAC;IAED;;OAEG;IACH,0BAAM,GAAN;QACC,IAAI,CAAC,cAAc,EAAE,CAAC;QACtB,IAAI,CAAC,OAAO,EAAE,CAAC;QACf,IAAI,CAAC,QAAQ,EAAE,CAAC;IACjB,CAAC;IA/PD;;OAEG;IACI,qBAAW,GAAa,EAAE,CAAC;IANnC;QAAC,oBAAO,CAAC,eAAe,CAAC;QACxB,qBAAQ,CAAC,cAAM,OAAA,EAAE,EAAF,CAAE,CAAC;iBAAA;IAkQnB,gBAAC;AAAD,CAjQA,AAiQC,IAAA;AAjQY,iBAAS,YAiQrB,CAAA","file":"Component.js","sourcesContent":["import UUID from \"./UUID\";\r\nimport { Template, TagName, ClassNames } from \"./Decorators\";\r\nimport ComponentStore from \"./ComponentStore\";\r\n\r\nimport assign = require(\"object-assign\");\r\n\r\nexport type ArmTemplate = (component: Component) => string;\r\n\r\n/**\r\n * A data structure representing one or more components.\r\n */\r\nexport interface DeflatedComponent {\r\n\ttype: string;\r\n\tdata: any;\r\n\tid: string;\r\n\tlabel: string;\r\n\tchildren?: DeflatedComponent[];\r\n}\r\n\r\n/**\r\n * The base for all Armature components.\r\n */\r\n@TagName(\"arm-component\")\r\n@Template(() => \"\")\r\nexport class Component {\r\n\t/**\r\n\t * A list of class names to apply to the component's root element.\r\n\t */\r\n\tstatic $classNames: string[] = [];\r\n\r\n\t/**\r\n\t * The HTML tag this component type will be using when reified.\r\n\t */\r\n\tstatic $tagName: string;\r\n\r\n\t/**\r\n\t * The template used to render this component.\r\n\t * Use @Template to specify the template for a component type.\r\n\t */\r\n\t$template: ArmTemplate;\r\n\r\n\t/**\r\n\t * The state backing this component.\r\n\t */\r\n\t$data: any;\r\n\r\n\t/**\r\n\t * The globally unique identifier for this component.\r\n\t */\r\n\t$id: string;\r\n\r\n\t/**\r\n\t * The label identifying this component.\r\n\t * This is unique among all siblings of this component.\r\n\t */\r\n\t$label: string;\r\n\r\n\t/**\r\n\t * The element representing this component, if one exists.\r\n\t */\r\n\t$element: HTMLElement;\r\n\r\n\t/**\r\n\t * The components this component contains.\r\n\t */\r\n\t$children: Component[] = [];\r\n\r\n\t/**\r\n\t * The component containing this component, if one exists.\r\n\t */\r\n\t$parent: Component;\r\n\r\n\t/**\r\n\t * Creates a new component with the given data.\r\n\t *\r\n\t * @param data The data to initialize the component with\r\n\t */\r\n\tconstructor(data: any) {\r\n\t\tthis.$data = data;\r\n\t\tthis.$id = UUID.get();\r\n\t}\r\n\r\n\t/**\r\n\t * Creates or retrieves a component that is attached to another component.\r\n\t * Using the same parent and 'label' parameters will retrieve the same object.\r\n\t * Pass null to the data attribute to throw if a new component is created.\r\n\t *\r\n\t * @param parent The component to attach to\r\n\t * @param label The label to use when creating or retrieving the component\r\n\t * @param data The data to initialize the component with if it hasn't been created\r\n\t */\r\n\tstatic $for(parent: Component, label: string, data: any) {\r\n\t\tconst identifier = this.$getIdentifier(label);\r\n\r\n\t\tconst existing = parent.$children.find(v => v.$getIdentifier() === identifier);\r\n\r\n\t\tif (existing) {\r\n\t\t\treturn existing;\r\n\t\t}\r\n\r\n\t\tif (data === null) {\r\n\t\t\tthrow new Error(\"Subcomponent was created with no data!\");\r\n\t\t}\r\n\r\n\t\tconst inst = new this(data);\r\n\t\tinst.$label = label;\r\n\t\tinst.$parent = parent;\r\n\r\n\t\tparent.$children.push(inst);\r\n\r\n\t\treturn inst;\r\n\t}\r\n\r\n\t/**\r\n\t * Serializes the component's data for potential recreation.\r\n\t */\r\n\t$serializeData() {\r\n\t\treturn assign({}, this.$data);\r\n\t}\r\n\r\n\t/**\r\n\t * Returns a way to uniquely identify this component type.\r\n\t */\r\n\tstatic $getTypeName() {\r\n\t\tconst classString = this.$classNames.map(v => \".\" + v).join(\"\");\r\n\r\n\t\treturn this.$tagName + classString;\r\n\t}\r\n\r\n\t/**\r\n\t * Creates an object that can be used to recreate this component.\r\n\t */\r\n\t$deflate(): DeflatedComponent {\r\n\t\tconst thisClass = <typeof Component>this.constructor;\r\n\r\n\t\treturn {\r\n\t\t\ttype: thisClass.$getTypeName(),\r\n\t\t\tdata: this.$serializeData(),\r\n\t\t\tid: this.$id,\r\n\t\t\tlabel: this.$label,\r\n\t\t\tchildren: this.$children.map(v => v.$deflate())\r\n\t\t};\r\n\t}\r\n\r\n\t/**\r\n\t * Creates an instance of a component from data generated by $deflate.\r\n\t *\r\n\t * @param deflated The deflated component data to use\r\n\t * @param inst An optional existing object to inflate\r\n\t */\r\n\tstatic $inflate(deflated: DeflatedComponent, inst?: Component) {\r\n\t\tif (!inst) {\r\n\t\t\tinst = new this(deflated.data);\r\n\t\t}\r\n\r\n\t\tinst.$id = deflated.id;\r\n\t\tinst.$label = deflated.label;\r\n\r\n\t\tif (deflated.children) {\r\n\t\t\tfor (let child of deflated.children) {\r\n\t\t\t\tconst thatClass = ComponentStore.get(child.type);\r\n\r\n\t\t\t\tconst childInst = thatClass.$for(inst, child.label, child.data);\r\n\t\t\t\tthatClass.$inflate(child, childInst);\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\treturn inst;\r\n\t}\r\n\r\n\t/**\r\n\t * Returns an identifier that should be unique among a component's siblings.\r\n\t *\r\n\t * @param label The label of the component to create the label for\r\n\t */\r\n\tstatic $getIdentifier(label: string) {\r\n\t\treturn this.$getTypeName() + \"__\" + label;\r\n\t}\r\n\r\n\t/**\r\n\t * Returns an identifier that should be unique among a component's siblings.\r\n\t */\r\n\t$getIdentifier() {\r\n\t\tconst thisClass = <typeof Component>this.constructor;\r\n\r\n\t\treturn thisClass.$getIdentifier(this.$label);\r\n\t}\r\n\r\n\ttoString() {\r\n\t\treturn this.$getHTML();\r\n\t}\r\n\r\n\t/**\r\n\t * Builds the HTML associated with this component.\r\n\t */\r\n\t$getHTML() {\r\n\t\tconst thisClass = <typeof Component>this.constructor;\r\n\r\n\t\treturn `<${ thisClass.$tagName }\r\n\t\t\tclass=\"${ thisClass.$classNames.join(\" \") }\"\r\n\t\t\tdata-armid=\"${ this.$id }\"\r\n\t\t\t${ this.$label ? `data-label=\"${ this.$label }\"` : \"\" }\r\n\t\t\t>${ this.$template(this) }</${ thisClass.$tagName }>`;\r\n\t}\r\n\r\n\t/**\r\n\t * Creates an HTML element for this component if one does not exist.\r\n\t */\r\n\t$ensureElement() {\r\n\t\tif (!this.$element) {\r\n\t\t\tconst thisClass = <typeof Component>this.constructor;\r\n\r\n\t\t\tconst el = document.createElement(thisClass.$tagName);\r\n\t\t\tel.classList.add(...thisClass.$classNames);\r\n\r\n\t\t\tthis.$attachTo(el);\r\n\t\t}\r\n\t}\r\n\r\n\t/**\r\n\t * Attaches this component to an existing HTML element.\r\n\t *\r\n\t * @param element The element to attach to\r\n\t */\r\n\t$attachTo(element: HTMLElement) {\r\n\t\tthis.$element = element;\r\n\t\telement.setAttribute(\"data-armid\", this.$id.toString());\r\n\r\n\t\tif (this.$label) {\r\n\t\t\telement.setAttribute(\"data-label\", this.$label);\r\n\t\t}\r\n\t}\r\n\r\n\t/**\r\n\t * Attempts to locate this component's element from a parent element.\r\n\t *\r\n\t * @param parent The element to search in\r\n\t */\r\n\t$locate(parent: HTMLElement = document.body) {\r\n\t\tconst thisClass = <typeof Component>this.constructor;\r\n\r\n\t\tconst el = <HTMLElement>parent.querySelector(`${ thisClass.$tagName }[data-armid=\"${ this.$id }\"]`);\r\n\r\n\t\tif (el) {\r\n\t\t\tthis.$element = el;\r\n\r\n\t\t\tthis.$children.forEach(child => {\r\n\t\t\t\tchild.$locate(el);\r\n\t\t\t});\r\n\r\n\t\t\treturn el;\r\n\t\t}\r\n\t}\r\n\r\n\t/**\r\n\t * Evaluates the component's template and puts the result into the component's element\r\n\t */\r\n\t$render() {\r\n\t\tthis.$element.innerHTML = this.$template(this);\r\n\t}\r\n\r\n\t/**\r\n\t * Attaches event handlers for the component and its children.\r\n\t * Often used after $render, or as part of $reify.\r\n\t */\r\n\t$hydrate() {\r\n\t\tfor (let child of this.$children) {\r\n\t\t\tif (child.$locate(this.$element)) {\r\n\t\t\t\tchild.$hydrate();\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\r\n\t/**\r\n\t * Ensures this component's element exists, then renders and hydrates it.\r\n\t */\r\n\t$reify() {\r\n\t\tthis.$ensureElement();\r\n\t\tthis.$render();\r\n\t\tthis.$hydrate();\r\n\t}\r\n}"],"sourceRoot":"/source/"}
\No newline at end of file