-
Notifications
You must be signed in to change notification settings - Fork 0
/
svgelement.ts
executable file
·44 lines (41 loc) · 1.46 KB
/
svgelement.ts
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
/**
* a little wrapper for creating SVG elements and getting/setting their attributes
* and observing their events.
* inspired by d3.js (http://d3js.org)
*/
class Elem {
elem: Element;
/**
* @param svg is the parent SVG object that will host the new element
* @param tag could be "rect", "line", "ellipse", etc.
*/
constructor(svg: HTMLElement, tag: string, parent: Element = svg) {
this.elem = document.createElementNS(svg.namespaceURI, tag);
parent.appendChild(this.elem);
}
/**
* all purpose attribute getter/setter
* @param name attribute name
* @param value value to assign to the attribute
* @returns called with just the name of the attribute it returns the attribute's current value as a string, called with the name and a new value it sets the attribute and returns this object
* so subsequent calls can be chained
*/
attr(name: string): string
attr(name: string, value: string | number): this
attr(name: string, value?: string | number): this | string {
if (typeof value === 'undefined') {
return this.elem.getAttribute(name)!;
}
this.elem.setAttribute(name, value.toString());
return this;
}
text(content: string) {
this.elem.textContent = content
}
/**
* @returns an Observable for the specified event on this element
*/
observe<T extends Event>(event: string): Observable<T> {
return Observable.fromEvent<T>(this.elem, event);
}
}