| 1 |
/*--------------------------------------------------------------------------------------------- |
| 2 |
* Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 |
* Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 |
*--------------------------------------------------------------------------------------------*/ |
| 5 |
|
| 6 |
export class FastDomNode<T extends HTMLElement> { |
| 7 |
|
| 8 |
private _maxWidth: string = ''; |
| 9 |
private _width: string = ''; |
| 10 |
private _height: string = ''; |
| 11 |
private _top: string = ''; |
| 12 |
private _left: string = ''; |
| 13 |
private _bottom: string = ''; |
| 14 |
private _right: string = ''; |
| 15 |
private _paddingTop: string = ''; |
| 16 |
private _paddingLeft: string = ''; |
| 17 |
private _paddingBottom: string = ''; |
| 18 |
private _paddingRight: string = ''; |
| 19 |
private _fontFamily: string = ''; |
| 20 |
private _fontWeight: string = ''; |
| 21 |
private _fontSize: string = ''; |
| 22 |
private _fontStyle: string = ''; |
| 23 |
private _fontFeatureSettings: string = ''; |
| 24 |
private _fontVariationSettings: string = ''; |
| 25 |
private _textDecoration: string = ''; |
| 26 |
private _lineHeight: string = ''; |
| 27 |
private _letterSpacing: string = ''; |
| 28 |
private _className: string = ''; |
| 29 |
private _display: string = ''; |
| 30 |
private _position: string = ''; |
| 31 |
private _visibility: string = ''; |
| 32 |
private _color: string = ''; |
| 33 |
private _backgroundColor: string = ''; |
| 34 |
private _layerHint: boolean = false; |
| 35 |
private _contain: 'none' | 'strict' | 'content' | 'size' | 'layout' | 'style' | 'paint' = 'none'; |
| 36 |
private _boxShadow: string = ''; |
| 37 |
|
| 38 |
constructor( |
| 39 |
public readonly domNode: T |
| 40 |
) { } |
| 41 |
|
| 42 |
public setMaxWidth(_maxWidth: number | string): void { |
| 43 |
const maxWidth = numberAsPixels(_maxWidth); |
| 44 |
if (this._maxWidth === maxWidth) { |
| 45 |
return; |
| 46 |
} |
| 47 |
this._maxWidth = maxWidth; |
| 48 |
this.domNode.style.maxWidth = this._maxWidth; |
| 49 |
} |
| 50 |
|
| 51 |
public setWidth(_width: number | string): void { |
| 52 |
const width = numberAsPixels(_width); |
| 53 |
if (this._width === width) { |
| 54 |
return; |
| 55 |
} |
| 56 |
this._width = width; |
| 57 |
this.domNode.style.width = this._width; |
| 58 |
} |
| 59 |
|
| 60 |
public setHeight(_height: number | string): void { |
| 61 |
const height = numberAsPixels(_height); |
| 62 |
if (this._height === height) { |
| 63 |
return; |
| 64 |
} |
| 65 |
this._height = height; |
| 66 |
this.domNode.style.height = this._height; |
| 67 |
} |
| 68 |
|
| 69 |
public setTop(_top: number | string): void { |
| 70 |
const top = numberAsPixels(_top); |
| 71 |
if (this._top === top) { |
| 72 |
return; |
| 73 |
} |
| 74 |
this._top = top; |
| 75 |
this.domNode.style.top = this._top; |
| 76 |
} |
| 77 |
|
| 78 |
public setLeft(_left: number | string): void { |
| 79 |
const left = numberAsPixels(_left); |
| 80 |
if (this._left === left) { |
| 81 |
return; |
| 82 |
} |
| 83 |
this._left = left; |
| 84 |
this.domNode.style.left = this._left; |
| 85 |
} |
| 86 |
|
| 87 |
public setBottom(_bottom: number | string): void { |
| 88 |
const bottom = numberAsPixels(_bottom); |
| 89 |
if (this._bottom === bottom) { |
| 90 |
return; |
| 91 |
} |
| 92 |
this._bottom = bottom; |
| 93 |
this.domNode.style.bottom = this._bottom; |
| 94 |
} |
| 95 |
|
| 96 |
public setRight(_right: number | string): void { |
| 97 |
const right = numberAsPixels(_right); |
| 98 |
if (this._right === right) { |
| 99 |
return; |
| 100 |
} |
| 101 |
this._right = right; |
| 102 |
this.domNode.style.right = this._right; |
| 103 |
} |
| 104 |
|
| 105 |
public setPaddingTop(_paddingTop: number | string): void { |
| 106 |
const paddingTop = numberAsPixels(_paddingTop); |
| 107 |
if (this._paddingTop === paddingTop) { |
| 108 |
return; |
| 109 |
} |
| 110 |
this._paddingTop = paddingTop; |
| 111 |
this.domNode.style.paddingTop = this._paddingTop; |
| 112 |
} |
| 113 |
|
| 114 |
public setPaddingLeft(_paddingLeft: number | string): void { |
| 115 |
const paddingLeft = numberAsPixels(_paddingLeft); |
| 116 |
if (this._paddingLeft === paddingLeft) { |
| 117 |
return; |
| 118 |
} |
| 119 |
this._paddingLeft = paddingLeft; |
| 120 |
this.domNode.style.paddingLeft = this._paddingLeft; |
| 121 |
} |
| 122 |
|
| 123 |
public setPaddingBottom(_paddingBottom: number | string): void { |
| 124 |
const paddingBottom = numberAsPixels(_paddingBottom); |
| 125 |
if (this._paddingBottom === paddingBottom) { |
| 126 |
return; |
| 127 |
} |
| 128 |
this._paddingBottom = paddingBottom; |
| 129 |
this.domNode.style.paddingBottom = this._paddingBottom; |
| 130 |
} |
| 131 |
|
| 132 |
public setPaddingRight(_paddingRight: number | string): void { |
| 133 |
const paddingRight = numberAsPixels(_paddingRight); |
| 134 |
if (this._paddingRight === paddingRight) { |
| 135 |
return; |
| 136 |
} |
| 137 |
this._paddingRight = paddingRight; |
| 138 |
this.domNode.style.paddingRight = this._paddingRight; |
| 139 |
} |
| 140 |
|
| 141 |
public setFontFamily(fontFamily: string): void { |
| 142 |
if (this._fontFamily === fontFamily) { |
| 143 |
return; |
| 144 |
} |
| 145 |
this._fontFamily = fontFamily; |
| 146 |
this.domNode.style.fontFamily = this._fontFamily; |
| 147 |
} |
| 148 |
|
| 149 |
public setFontWeight(fontWeight: string): void { |
| 150 |
if (this._fontWeight === fontWeight) { |
| 151 |
return; |
| 152 |
} |
| 153 |
this._fontWeight = fontWeight; |
| 154 |
this.domNode.style.fontWeight = this._fontWeight; |
| 155 |
} |
| 156 |
|
| 157 |
public setFontSize(_fontSize: number | string): void { |
| 158 |
const fontSize = numberAsPixels(_fontSize); |
| 159 |
if (this._fontSize === fontSize) { |
| 160 |
return; |
| 161 |
} |
| 162 |
this._fontSize = fontSize; |
| 163 |
this.domNode.style.fontSize = this._fontSize; |
| 164 |
} |
| 165 |
|
| 166 |
public setFontStyle(fontStyle: string): void { |
| 167 |
if (this._fontStyle === fontStyle) { |
| 168 |
return; |
| 169 |
} |
| 170 |
this._fontStyle = fontStyle; |
| 171 |
this.domNode.style.fontStyle = this._fontStyle; |
| 172 |
} |
| 173 |
|
| 174 |
public setFontFeatureSettings(fontFeatureSettings: string): void { |
| 175 |
if (this._fontFeatureSettings === fontFeatureSettings) { |
| 176 |
return; |
| 177 |
} |
| 178 |
this._fontFeatureSettings = fontFeatureSettings; |
| 179 |
this.domNode.style.fontFeatureSettings = this._fontFeatureSettings; |
| 180 |
} |
| 181 |
|
| 182 |
public setFontVariationSettings(fontVariationSettings: string): void { |
| 183 |
if (this._fontVariationSettings === fontVariationSettings) { |
| 184 |
return; |
| 185 |
} |
| 186 |
this._fontVariationSettings = fontVariationSettings; |
| 187 |
this.domNode.style.fontVariationSettings = this._fontVariationSettings; |
| 188 |
} |
| 189 |
|
| 190 |
public setTextDecoration(textDecoration: string): void { |
| 191 |
if (this._textDecoration === textDecoration) { |
| 192 |
return; |
| 193 |
} |
| 194 |
this._textDecoration = textDecoration; |
| 195 |
this.domNode.style.textDecoration = this._textDecoration; |
| 196 |
} |
| 197 |
|
| 198 |
public setLineHeight(_lineHeight: number | string): void { |
| 199 |
const lineHeight = numberAsPixels(_lineHeight); |
| 200 |
if (this._lineHeight === lineHeight) { |
| 201 |
return; |
| 202 |
} |
| 203 |
this._lineHeight = lineHeight; |
| 204 |
this.domNode.style.lineHeight = this._lineHeight; |
| 205 |
} |
| 206 |
|
| 207 |
public setLetterSpacing(_letterSpacing: number | string): void { |
| 208 |
const letterSpacing = numberAsPixels(_letterSpacing); |
| 209 |
if (this._letterSpacing === letterSpacing) { |
| 210 |
return; |
| 211 |
} |
| 212 |
this._letterSpacing = letterSpacing; |
| 213 |
this.domNode.style.letterSpacing = this._letterSpacing; |
| 214 |
} |
| 215 |
|
| 216 |
public setClassName(className: string): void { |
| 217 |
if (this._className === className) { |
| 218 |
return; |
| 219 |
} |
| 220 |
this._className = className; |
| 221 |
this.domNode.className = this._className; |
| 222 |
} |
| 223 |
|
| 224 |
public toggleClassName(className: string, shouldHaveIt?: boolean): void { |
| 225 |
this.domNode.classList.toggle(className, shouldHaveIt); |
| 226 |
this._className = this.domNode.className; |
| 227 |
} |
| 228 |
|
| 229 |
public setDisplay(display: string): void { |
| 230 |
if (this._display === display) { |
| 231 |
return; |
| 232 |
} |
| 233 |
this._display = display; |
| 234 |
this.domNode.style.display = this._display; |
| 235 |
} |
| 236 |
|
| 237 |
public setPosition(position: string): void { |
| 238 |
if (this._position === position) { |
| 239 |
return; |
| 240 |
} |
| 241 |
this._position = position; |
| 242 |
this.domNode.style.position = this._position; |
| 243 |
} |
| 244 |
|
| 245 |
public setVisibility(visibility: string): void { |
| 246 |
if (this._visibility === visibility) { |
| 247 |
return; |
| 248 |
} |
| 249 |
this._visibility = visibility; |
| 250 |
this.domNode.style.visibility = this._visibility; |
| 251 |
} |
| 252 |
|
| 253 |
public setColor(color: string): void { |
| 254 |
if (this._color === color) { |
| 255 |
return; |
| 256 |
} |
| 257 |
this._color = color; |
| 258 |
this.domNode.style.color = this._color; |
| 259 |
} |
| 260 |
|
| 261 |
public setBackgroundColor(backgroundColor: string): void { |
| 262 |
if (this._backgroundColor === backgroundColor) { |
| 263 |
return; |
| 264 |
} |
| 265 |
this._backgroundColor = backgroundColor; |
| 266 |
this.domNode.style.backgroundColor = this._backgroundColor; |
| 267 |
} |
| 268 |
|
| 269 |
public setLayerHinting(layerHint: boolean): void { |
| 270 |
if (this._layerHint === layerHint) { |
| 271 |
return; |
| 272 |
} |
| 273 |
this._layerHint = layerHint; |
| 274 |
this.domNode.style.transform = this._layerHint ? 'translate3d(0px, 0px, 0px)' : ''; |
| 275 |
} |
| 276 |
|
| 277 |
public setBoxShadow(boxShadow: string): void { |
| 278 |
if (this._boxShadow === boxShadow) { |
| 279 |
return; |
| 280 |
} |
| 281 |
this._boxShadow = boxShadow; |
| 282 |
this.domNode.style.boxShadow = boxShadow; |
| 283 |
} |
| 284 |
|
| 285 |
public setContain(contain: 'none' | 'strict' | 'content' | 'size' | 'layout' | 'style' | 'paint'): void { |
| 286 |
if (this._contain === contain) { |
| 287 |
return; |
| 288 |
} |
| 289 |
this._contain = contain; |
| 290 |
(<any>this.domNode.style).contain = this._contain; |
| 291 |
} |
| 292 |
|
| 293 |
public setAttribute(name: string, value: string): void { |
| 294 |
this.domNode.setAttribute(name, value); |
| 295 |
} |
| 296 |
|
| 297 |
public removeAttribute(name: string): void { |
| 298 |
this.domNode.removeAttribute(name); |
| 299 |
} |
| 300 |
|
| 301 |
public appendChild(child: FastDomNode<T>): void { |
| 302 |
this.domNode.appendChild(child.domNode); |
| 303 |
} |
| 304 |
|
| 305 |
public removeChild(child: FastDomNode<T>): void { |
| 306 |
this.domNode.removeChild(child.domNode); |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
function numberAsPixels(value: number | string): string { |
| 311 |
return (typeof value === 'number' ? `${value}px` : value); |
| 312 |
} |
| 313 |
|
| 314 |
export function createFastDomNode<T extends HTMLElement>(domNode: T): FastDomNode<T> { |
| 315 |
return new FastDomNode(domNode); |
| 316 |
} |