|
| 1 | +import { ObjectDictionary } from "@opticss/util"; |
| 2 | +import { Style, Block, isBlock, isBlockClass, isState } from './index'; |
| 3 | + |
| 4 | +import { BlockPath } from "../BlockSyntax"; |
| 5 | +import { CssBlockError } from "../errors"; |
| 6 | + |
| 7 | +export abstract class Inheritable< |
| 8 | + Self extends Inheritable<Self, Parent, Child>, |
| 9 | + Parent extends Inheritable<any, any, Self> | null = null, |
| 10 | + Child extends Inheritable<any, Self, any> | null = null |
| 11 | +> { |
| 12 | + |
| 13 | + protected _name: string; |
| 14 | + protected _base: Self | undefined; |
| 15 | + public _block: Block; |
| 16 | + protected _baseName: string; |
| 17 | + protected _parent: Parent | undefined; |
| 18 | + protected _children: Map<string, Child> = new Map; |
| 19 | + |
| 20 | + /** |
| 21 | + * Given a parent that is a base class of this style, retrieve this style's |
| 22 | + * base style from it, if it exists. This method does not traverse into base styles. |
| 23 | + */ |
| 24 | + protected abstract newChild(name: string): Child; |
| 25 | + |
| 26 | + // TODO: Currently only ever returns itself if is a style. Need to get it to look other things up. |
| 27 | + public lookup(path: string | BlockPath): Style | undefined { |
| 28 | + path = new BlockPath(path); |
| 29 | + if (isBlockClass(this) || isState(this)) return this; |
| 30 | + return undefined; |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Inheritable constructor |
| 35 | + * @param name Name for this Inheritable instance. |
| 36 | + * @param parent The parent Inheritable of this node. |
| 37 | + */ |
| 38 | + constructor(name: string, parent?: Parent,) { |
| 39 | + this._name = name; |
| 40 | + this._parent = parent; |
| 41 | + } |
| 42 | + |
| 43 | + public get name(): string { return this._name; } |
| 44 | + public get baseName(): string | undefined { return this._baseName; } |
| 45 | + public get parent(): Parent | undefined { return this._parent; } |
| 46 | + |
| 47 | + /** |
| 48 | + * Get the style that this style inherits from, if any. |
| 49 | + * |
| 50 | + * This walks down the declared styles of the parent's inheritance chain, |
| 51 | + * and attempts to find a matching directly declared style on each. |
| 52 | + * |
| 53 | + * The result is cached because it never changes and is decidable as soon |
| 54 | + * as the style is instantiated. |
| 55 | + */ |
| 56 | + public get base(): Self | undefined { |
| 57 | + if (this._base !== undefined || !this.parent) { |
| 58 | + return this._base || undefined; |
| 59 | + } |
| 60 | + let baseParent: Parent | undefined = this.parent.base; |
| 61 | + while (baseParent) { |
| 62 | + let cls = baseParent ? baseParent.getChild(this.name) : undefined; |
| 63 | + if (cls) { |
| 64 | + this._base = cls; |
| 65 | + return cls; |
| 66 | + } |
| 67 | + baseParent = baseParent.base; |
| 68 | + } |
| 69 | + return this._base = undefined; |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * traverse parents and return the base block object. |
| 74 | + * @returns The base block in this container tree. |
| 75 | + */ |
| 76 | + public get block(): Block { |
| 77 | + if (isBlock(this)) { return this._block = this; } |
| 78 | + if (this._block !== undefined) { return this._block; } |
| 79 | + if (this.parent) { return this._block = this.parent.block; } |
| 80 | + throw new CssBlockError("Tried to access `block` on an orphaned `Style`"); |
| 81 | + } |
| 82 | + |
| 83 | + setBase(baseName: string, base: Self) { |
| 84 | + this._baseName = baseName; |
| 85 | + this._base = base; |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Compute all block objects that are implied by this block object through |
| 90 | + * inheritance. Does not include this object or the styles it implies through |
| 91 | + * other relationships to this object. |
| 92 | + * |
| 93 | + * If nothing is inherited, this returns an empty set. |
| 94 | + */ |
| 95 | + resolveInheritance(): Self[] { |
| 96 | + let inherited = new Array<Self>(); |
| 97 | + let base: Self | undefined = this.base; |
| 98 | + while (base) { |
| 99 | + inherited.unshift(base); |
| 100 | + base = base.base; |
| 101 | + } |
| 102 | + return inherited; |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Resolves the child with the given name from this node's inheritance |
| 107 | + * chain. Returns undefined if the child is not found. |
| 108 | + * @param name The name of the child to resolve. |
| 109 | + */ |
| 110 | + resolveChild(name: string): Child | null { |
| 111 | + let state: Child | null = this.getChild(name); |
| 112 | + let container: Self | undefined = this.base; |
| 113 | + while (!state && container) { |
| 114 | + state = container.getChild(name); |
| 115 | + container = container.base; |
| 116 | + } |
| 117 | + return state || null; |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Given a parent that is a base class of this style, retrieve this style's |
| 122 | + * base style from it, if it exists. This method does not traverse into base styles. |
| 123 | + */ |
| 124 | + protected getChild(key: string): Child | null { |
| 125 | + return this._children.get(key) || null; |
| 126 | + } |
| 127 | + |
| 128 | + protected setChild(key: string, value: Child): Child { |
| 129 | + this._children.set(key, value); |
| 130 | + return value; |
| 131 | + } |
| 132 | + |
| 133 | + protected ensureChild(name: string): Child { |
| 134 | + if (!this._children.has(name)) { |
| 135 | + this.setChild(name, this.newChild(name)); |
| 136 | + } |
| 137 | + return this._children.get(name) as Child; |
| 138 | + } |
| 139 | + |
| 140 | + protected children(): Child[]{ |
| 141 | + return [...this._children.values()]; |
| 142 | + } |
| 143 | + |
| 144 | + // TODO: Cache this maybe? Convert entire model to only use hash?... |
| 145 | + protected childrenHash(): ObjectDictionary<Child> { |
| 146 | + let out = {}; |
| 147 | + for (let [key, value] of this._children.entries() ) { |
| 148 | + out[key] = value; |
| 149 | + } |
| 150 | + return out; |
| 151 | + } |
| 152 | + |
| 153 | +} |
| 154 | + |
| 155 | +export abstract class Source< |
| 156 | + Self extends Inheritable<Self, null, Child>, |
| 157 | + Child extends Inheritable<Child, Self, any> |
| 158 | +> extends Inheritable<Self, null, Child> { |
| 159 | + public parent: null; |
| 160 | + protected _children: Map<string, Child>; |
| 161 | +} |
| 162 | + |
| 163 | +export abstract class Node< |
| 164 | + Self extends Inheritable<Self, Parent, Child>, |
| 165 | + Parent extends Inheritable<Parent, any, Self>, |
| 166 | + Child extends Inheritable<Child, Self, any> |
| 167 | +> extends Inheritable<Self, Parent, Child> { |
| 168 | + public parent: Parent; |
| 169 | + protected _children: Map<string, Child>; |
| 170 | +} |
| 171 | + |
| 172 | +export abstract class Sink< |
| 173 | + Self extends Inheritable<Self, Parent, null>, |
| 174 | + Parent extends Inheritable<any, any, Self> |
| 175 | +> extends Inheritable<Self, Parent, null> { |
| 176 | + public parent: Parent; |
| 177 | + protected _children: Map<string, null>; |
| 178 | +} |
0 commit comments