2017-12-18 22:04:17 +01:00
|
|
|
namespace pxsim.visuals {
|
|
|
|
|
2017-12-22 23:00:23 +01:00
|
|
|
export class ModuleView extends View implements LayoutElement {
|
2017-12-18 22:04:17 +01:00
|
|
|
protected content: SVGSVGElement;
|
|
|
|
|
|
|
|
protected controlShown: boolean;
|
2017-12-28 20:17:18 +01:00
|
|
|
|
|
|
|
protected opacity: number;
|
2017-12-18 22:04:17 +01:00
|
|
|
|
|
|
|
constructor(protected xml: string, protected prefix: string, protected id: NodeType, protected port: NodeType) {
|
|
|
|
super();
|
|
|
|
this.xml = this.normalizeXml(xml);
|
|
|
|
}
|
|
|
|
|
|
|
|
private normalizeXml(xml: string) {
|
|
|
|
const prefix = this.prefix;
|
|
|
|
xml = xml.replace(/id=\"(.*?)\"/g, (m: string, id: string) => {
|
|
|
|
return `id="${this.normalizeId(id)}"`;
|
|
|
|
});
|
|
|
|
xml = xml.replace(/url\(#(.*?)\)/g, (m: string, id: string) => {
|
2018-02-01 23:08:28 +01:00
|
|
|
return `url(#${this.normalizeId(id)})`;
|
2017-12-18 22:04:17 +01:00
|
|
|
});
|
|
|
|
xml = xml.replace(/xlink:href=\"#(.*?)\"/g, (m: string, id: string) => {
|
|
|
|
return `xlink:href="#${this.normalizeId(id)}"`;
|
|
|
|
});
|
|
|
|
return xml;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected normalizeId(svgId: string) {
|
|
|
|
return `${this.prefix}-${svgId}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
public getId() {
|
|
|
|
return this.id;
|
|
|
|
}
|
|
|
|
|
|
|
|
public getPort() {
|
|
|
|
return this.port;
|
|
|
|
}
|
|
|
|
|
|
|
|
public getPaddingRatio() {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public getWiringRatio() {
|
|
|
|
return 0.5;
|
|
|
|
}
|
|
|
|
|
2017-12-22 23:00:23 +01:00
|
|
|
protected buildDom(): SVGElement {
|
2017-12-18 22:04:17 +01:00
|
|
|
this.content = svg.parseString(this.xml);
|
|
|
|
this.buildDomCore();
|
|
|
|
this.attachEvents();
|
|
|
|
if (this.hasClick())
|
|
|
|
this.content.style.cursor = "pointer";
|
|
|
|
return this.content;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected buildDomCore() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public getInnerHeight() {
|
|
|
|
if (!this.content) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (!this.content.hasAttribute("viewBox")) {
|
|
|
|
return parseFloat(this.content.getAttribute("height"));
|
|
|
|
}
|
|
|
|
return parseFloat(this.content.getAttribute("viewBox").split(" ")[3]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public getInnerWidth() {
|
|
|
|
if (!this.content) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (!this.content.hasAttribute("viewBox")) {
|
|
|
|
return parseFloat(this.content.getAttribute("width"));
|
|
|
|
}
|
|
|
|
return parseFloat(this.content.getAttribute("viewBox").split(" ")[2]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public attachEvents() {
|
|
|
|
}
|
|
|
|
|
2017-12-22 23:00:23 +01:00
|
|
|
public resize(width: number, height: number) {
|
|
|
|
super.resize(width, height);
|
|
|
|
this.updateDimensions(width, height);
|
2017-12-18 22:04:17 +01:00
|
|
|
}
|
|
|
|
|
2017-12-22 23:00:23 +01:00
|
|
|
private updateDimensions(width: number, height: number) {
|
2017-12-18 22:04:17 +01:00
|
|
|
if (this.content) {
|
|
|
|
const currentWidth = this.getInnerWidth();
|
|
|
|
const currentHeight = this.getInnerHeight();
|
|
|
|
const newHeight = currentHeight / currentWidth * width;
|
2017-12-22 23:00:23 +01:00
|
|
|
const newWidth = currentWidth / currentHeight * height;
|
2017-12-18 22:04:17 +01:00
|
|
|
this.content.setAttribute('width', `${width}`);
|
|
|
|
this.content.setAttribute('height', `${newHeight}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public hasClick() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public setSelected(selected: boolean) {
|
2017-12-22 23:00:23 +01:00
|
|
|
super.setSelected(selected);
|
2017-12-18 22:04:17 +01:00
|
|
|
this.updateOpacity();
|
|
|
|
}
|
|
|
|
|
2017-12-28 20:17:18 +01:00
|
|
|
public updateState() {
|
|
|
|
this.updateOpacity();
|
|
|
|
}
|
|
|
|
|
2017-12-18 22:04:17 +01:00
|
|
|
protected updateOpacity() {
|
|
|
|
if (this.rendered) {
|
2017-12-28 20:17:18 +01:00
|
|
|
const opacity = this.selected ? 0.2 : 1;
|
|
|
|
if (this.hasClick() && this.opacity != opacity) {
|
|
|
|
this.opacity = opacity;
|
|
|
|
this.setOpacity(this.opacity);
|
2017-12-18 22:04:17 +01:00
|
|
|
if (this.selected) this.content.style.cursor = "";
|
|
|
|
else this.content.style.cursor = "pointer";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-28 20:17:18 +01:00
|
|
|
protected setOpacity(opacity: number) {
|
|
|
|
this.element.setAttribute("opacity", `${opacity}`);
|
2017-12-18 22:04:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|