class View extends Listenable { /** * The constructor accepts a DOM element as a parameter. * If it is omitted, a new element is created using the tag name that the class defines. * If no class name is defined, the constructor name in kebab case is used instead. */ constructor(element = null) { super(); this.element = element; if (!this.element) { var tagName = this.getTagName(); if (!tagName) { tagName = this.constructor.name.toKebabCase(); } this.element = document.createElement(tagName); this.element.setAttribute("view-class", this.constructor.name); this.build(this.element); } } /** * The build function is called in the constructor when no element was passed as the base. * Override it to create custom subviews or manipulate the root element. * Don't forget to return the element in the end! */ build(element) { return element; } /** * Returns the tag name of this element. * Override this if you want to have a custom tag name for your custom view class. * If this function is not overridden, the class name is used for the tag name for newly created elements (converted to kebab-case). * * This functionality is used for view classes that are inherited by subclasses. * That is because the CSS rules of the superclass view should generally also apply to the subclass view. * Therefore, the subclass view can get the tag name of the superclass by not overriding this function. * Only the view-class attribute will indicate the true nature of the element in this case, which will be useful for specific CSS styling. */ getTagName() { return null; } /** * Called once the view has been processed by UIKit during the view controller initialization. */ viewDidLoad() { } /** * Called everytime before the view is shown. * The layout is already done at this step, so it's safe to use scrollWidth and scrollHeight and what not. */ viewWillAppear() { } /** * Called everytime after the view is shown. */ viewDidAppear() { } /** * Called everytime before the view is hidden. */ viewWillDisappear() { } /** * Called everytime after the view is hidden. */ viewDidDisappear() { } } UIKit.registerViewType(View);