2017-03-08 21:49:41 +00:00
|
|
|
import { BaseXmlComponent } from "./base";
|
2017-07-08 20:45:19 +01:00
|
|
|
import { IXmlableObject } from "./xmlable-object";
|
2016-05-20 00:40:31 +01:00
|
|
|
|
2018-01-23 01:33:12 +00:00
|
|
|
export type AttributeMap<T> = { [P in keyof T]: string };
|
2016-05-20 00:40:31 +01:00
|
|
|
|
2017-03-10 10:42:24 +01:00
|
|
|
export abstract class XmlAttributeComponent<T> extends BaseXmlComponent {
|
|
|
|
protected root: T;
|
|
|
|
protected xmlKeys: AttributeMap<T>;
|
2016-05-20 00:40:31 +01:00
|
|
|
|
2017-03-10 10:42:24 +01:00
|
|
|
constructor(properties: T) {
|
|
|
|
super("_attr");
|
2016-05-26 15:08:34 +01:00
|
|
|
this.root = properties;
|
2016-05-20 00:40:31 +01:00
|
|
|
}
|
|
|
|
|
2017-07-08 20:45:19 +01:00
|
|
|
public prepForXml(): IXmlableObject {
|
2017-03-09 20:49:14 +01:00
|
|
|
const attrs = {};
|
2017-03-10 10:42:24 +01:00
|
|
|
Object.keys(this.root).forEach((key) => {
|
|
|
|
const value = this.root[key];
|
|
|
|
if (value !== undefined) {
|
|
|
|
const newKey = this.xmlKeys[key];
|
|
|
|
attrs[newKey] = value;
|
|
|
|
}
|
|
|
|
});
|
2018-01-23 01:33:12 +00:00
|
|
|
return { _attr: attrs };
|
2016-05-20 00:40:31 +01:00
|
|
|
}
|
2017-03-08 21:49:41 +00:00
|
|
|
}
|