2016-05-23 22:23:05 +01:00
|
|
|
import * as _ from "lodash";
|
2017-03-08 21:49:41 +00:00
|
|
|
import { BaseXmlComponent } from "./base";
|
2016-05-20 00:40:31 +01:00
|
|
|
|
|
|
|
export abstract class XmlAttributeComponent extends BaseXmlComponent {
|
2017-03-09 19:50:33 +01:00
|
|
|
protected root: object;
|
|
|
|
private xmlKeys: object;
|
2016-05-20 00:40:31 +01:00
|
|
|
|
2017-03-09 19:50:33 +01:00
|
|
|
constructor(xmlKeys: object, properties: object) {
|
2016-05-20 00:40:31 +01:00
|
|
|
super("_attr");
|
|
|
|
this.xmlKeys = xmlKeys;
|
|
|
|
|
2016-05-26 15:08:34 +01:00
|
|
|
this.root = properties;
|
2016-05-20 00:40:31 +01:00
|
|
|
|
|
|
|
if (!properties) {
|
|
|
|
this.root = {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-10 08:58:29 +01:00
|
|
|
public prepForXml(): {_attr: {[key: string]: (string | number | boolean)}} {
|
2017-03-09 20:49:14 +01:00
|
|
|
const attrs = {};
|
2017-03-09 22:56:08 +00:00
|
|
|
if (this.root !== undefined) {
|
2016-05-20 00:40:31 +01:00
|
|
|
_.forOwn(this.root, (value, key) => {
|
2017-03-09 22:56:08 +00:00
|
|
|
if (value !== undefined) {
|
2017-03-09 20:49:14 +01:00
|
|
|
const newKey = this.xmlKeys[key];
|
|
|
|
attrs[newKey] = value;
|
|
|
|
}
|
2016-05-20 00:40:31 +01:00
|
|
|
});
|
|
|
|
}
|
2017-03-10 08:58:29 +01:00
|
|
|
return {_attr: attrs};
|
2016-05-20 00:40:31 +01:00
|
|
|
}
|
2017-03-08 21:49:41 +00:00
|
|
|
}
|