Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 | 1x 1x 25x 1x 2x 1x 1x 1x 1x 1x 1x 11x 7x 3x 8x 8x 6x 2x 7x 4x 1x 2x 1x 14x 6x 5x 4x 1x 1x 8x 7x 4x 3x 3x 3x 2x 1x 1x 8x 1x 8x 4x 3x 1x 4x 3x 2x 1x 1x 1x 4x 1x 25x 25x 25x 6x 25x 25x 25x 25x 1x 1x 1x 1x | import { InvalidArgumentError } from "./errors"
/*
TODO
- [ ] Add observable support
- [ ] Add collection support (ObservableList)
- [ ] Add animation helpers (for css animations)
*/
interface ClassOptions {
[s: string]: boolean
}
interface AttributeOptions {
[s: string]: string | number | boolean | undefined
}
interface PropertyOptions {
[s: string]: any
}
interface ElementOptions {
type?: string
class?: string | ClassOptions | undefined
attr?: AttributeOptions | undefined
prop?: PropertyOptions | undefined
children?: Array<Selection> | undefined
}
class Selection {
/** Creates a new selection with the element provided */
constructor(public element: HTMLElement) {
}
/** Returns true if the value passed in is a Selection */
public static isSelection (value: any): value is Selection {
return value instanceof Selection
}
/** Gets the text of the selected element */
public text (): string
/** Sets the text of ths selected element */
public text (text: string): this
public text (text?: string): this | string {
if (arguments.length === 0) {
return this.element.innerText
} else {
this.element.innerText = text
return this
}
}
/** Add a selected element, or array of selected elements to this selection */
public add (selection: Selection | Array<Selection>): this {
if (Array.isArray(selection)) {
for (const s of selection) {
this.element.appendChild(s.element)
}
} else {
this.element.appendChild(selection.element)
}
return this
}
/** Removes children from this selection */
public remove (selection: Selection | Array<Selection>): this {
if (Array.isArray(selection)) {
for (const s of selection) {
this.element.removeChild(s.element)
}
} else {
this.element.removeChild(selection.element)
}
return this
}
/** Sets the child elements of this selection */
public set (selection: Selection | Array<Selection>): this {
this.removeAll()
if (Array.isArray(selection)) {
for (const s of selection) {
this.element.appendChild(s.element)
}
} else {
this.element.appendChild(selection.element)
}
return this
}
/** Removes all children elements from this element */
public removeAll (): this {
while (this.element.firstChild) {
this.element.removeChild(this.element.firstChild)
}
return this
}
/** Sets the class for the element, or gets it */
public class (): string
public class (cls: string | ClassOptions): this
public class (cls?: string | ClassOptions): this | string {
if (arguments.length > 0) {
if (typeof cls === 'string') {
this.element.className = cls
} else {
for (const key of Object.keys(cls)) {
if (cls[key]) {
this.element.classList.add(key)
} else {
this.element.classList.remove(key)
}
}
}
return this
} else {
return this.element.className
}
}
/** Returns true if the selected element has the class */
public hasClass (cls: string): boolean {
return this.element.classList.contains(cls)
}
public attr (key: string): string | undefined
public attr (options: AttributeOptions): this
public attr (key: string, value: string | number | boolean | undefined): this
public attr (keyOrOptions: string | AttributeOptions, value?: string | number | boolean | undefined): this | string | undefined {
if (arguments.length === 2) {
if (typeof keyOrOptions === 'string') {
if (value !== undefined) {
this.element.setAttribute(keyOrOptions, value.toString())
} else {
this.element.removeAttribute(keyOrOptions)
}
} else {
throw new InvalidArgumentError("key should be a string")
}
} else if (arguments.length === 1) {
if (typeof keyOrOptions === 'string') {
return this.element.getAttribute(keyOrOptions) || undefined
} else {
for (const key of Object.keys(keyOrOptions)) {
const value = keyOrOptions[key]
if (value !== undefined) {
this.element.setAttribute(key, value.toString())
} else {
this.element.removeAttribute(key)
}
}
}
} else {
throw new InvalidArgumentError("expected 1 or 2 arguments")
}
return this
}
public prop (key: string): any
public prop (options: PropertyOptions): this
public prop (key: string, value: any | undefined): this
public prop (keyOrOptions: string | PropertyOptions, value?: any | undefined): this | any {
if (arguments.length === 2) {
if (typeof keyOrOptions === 'string') {
(this.element as any)[keyOrOptions] = value
} else {
throw new InvalidArgumentError("key should be a string")
}
} else if (arguments.length === 1) {
if (typeof keyOrOptions === 'string') {
return (this.element as any)[keyOrOptions]
} else {
for (const key of Object.keys(keyOrOptions)) {
(this.element as any)[key] = keyOrOptions[key]
}
}
} else {
throw new InvalidArgumentError("expected 1 or 2 arguments")
}
return this
}
}
function createSelectionFromElement (element: Element): Selection {
if (element instanceof HTMLElement) {
return new Selection(element)
} else {
throw new Error(typeof (element) + ' elements are not supported yet in the Selection api')
}
}
/** Selects an element in the document (using document.querySelector under the hood)
* An element can be converted into a selection using this method
*/
function select (selector: string | HTMLElement | Selection): Selection {
if (typeof selector === 'string') {
const element = document.querySelector(selector)
return createSelectionFromElement(element)
} else if (selector instanceof HTMLElement) {
return createSelectionFromElement(selector)
} else if (selector instanceof Selection) {
return selector
} else {
throw new Error('Invalid value passed into select(): ' + selector)
}
}
/** Selects all elements from the document (using document.querySelectorAll under the hood) */
function selectAll (selector: string): Array<Selection> {
if (typeof selector === 'string') {
const elements = document.querySelectorAll(selector)
return Array.from(elements).map(createSelectionFromElement)
} else {
throw new Error('Invalid value passed into selectAll(): ' + selector)
}
}
/** Creates a new detached element */
function element (options?: ElementOptions): Selection {
const element = document.createElement(options ? options.type || 'div' : 'div')
const selection = new Selection(element)
if (options && options.class) {
selection.class(options.class)
}
Iif (options && options.attr) {
selection.attr(options.attr)
}
Iif (options && options.prop) {
selection.prop(options.prop)
}
Iif (options && options.children) {
selection.set(options.children)
}
return selection
}
export {
Selection,
select,
selectAll,
element
} |