UNPKG

mframejs

Version:
140 lines (106 loc) 3.95 kB
import { ViewController } from './viewController'; import { ContainerClasses } from '../container/exported'; import { BindingEngine } from '../binding/exported'; import { IAttribute, IBindingContext } from '../interface/exported'; import { Logger } from '../utils/exported'; /** * Creates and controlls custom attributes */ export class AttributeController { public classInstance: IAttribute; private logger: Logger; private viewController: ViewController; /** * Creates instance of AttributeController * @param bindingContext - bindingContext to use * @param htmlNode - html node * @param attributeNode - attibute node * @param attributeName - name of registered attribute to use * @param viewController - viewController to use */ constructor( private bindingContext: IBindingContext, private htmlNode: Node, private attributeNode: Attr, attributeName: string, viewController: ViewController) { this.logger = Logger.getLogger(attributeNode.name, 'attribute'); this.classInstance = ContainerClasses.get(attributeName); this.htmlNode = htmlNode; if (attributeNode.name === 'if.bind' || attributeNode.name === 'repeat.for') { // TODO: needs to be more dynamic, either as a param when creating or look and check if its regeistered as a controller this.viewController = new ViewController(htmlNode, viewController); this.viewController.addAttribute(this); } else { viewController.addAttribute(this); this.viewController = viewController; } } /** * Start element life cycle * */ public init(): void { this.create(); } /** * Get class instance from parent * @param _customElement custom element class you want to try and search for */ public searchForInstance<T>(_customElement: T): T | null { return this.viewController.searchForInstance(_customElement); } /** * Get viewController * */ public getView(): ViewController { return this.viewController; } /** * Sets vars and calls create on attribute * @internal * */ public create(): void { this.logger.log('created', this.attributeNode.name, this.attributeNode.value); this.classInstance.$element = this.htmlNode; this.classInstance.$bindingContext = this.bindingContext; this.classInstance.$attribute = this.attributeNode; this.classInstance.$controller = this; BindingEngine.subscribeClassMetaBinding(this.classInstance); if (this.classInstance.created) { this.classInstance.created(); } } /** * calls elements attached() if it exist * */ public attached(): void { this.logger.log('attached', this.attributeNode.name, this.attributeNode.value); if (this.classInstance.attached) { this.classInstance.attached(); } } /** * calls elements detached() if it exist and clears it * */ public detached(): void { this.logger.log('detached', this.attributeNode.name, this.attributeNode.value); if (this.classInstance.detached) { this.classInstance.detached(); } BindingEngine.unSubscribeClassMetaBinding(this.classInstance); this.bindingContext = null; this.htmlNode = null; this.attributeNode = null; this.classInstance.$element = null; this.classInstance.$bindingContext = null; this.classInstance.$attribute = null; this.classInstance.$controller = null; this.classInstance = null; this.viewController = null; } }