@mos-connection/helper
Version:
Helper functions for the MOS-connection library
297 lines • 15.7 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.XMLMosItem = exports.XMLMosItems = exports.XMLROStory = exports.XMLROStoryBase = exports.XMLROStories = exports.XMLRunningOrder = exports.XMLRunningOrderBase = exports.XMLMosROAck = void 0;
const lib_1 = require("../lib");
const ensureMethods_1 = require("../../utils/ensureMethods");
const ROAck_1 = require("./ROAck");
const xmlConversion_1 = require("../profile1/xmlConversion");
const Utils_1 = require("../../utils/Utils");
const parseMosTypes_1 = require("../parseMosTypes");
const ParseError_1 = require("../ParseError");
/* eslint-disable @typescript-eslint/no-namespace */
var XMLMosROAck;
(function (XMLMosROAck) {
function fromXML(path, xml, strict) {
try {
xml = (0, ensureMethods_1.ensureXMLObject)(xml, strict);
const mosTypes = (0, parseMosTypes_1.getParseMosTypes)(strict);
const roAck = new ROAck_1.ROAck({
ID: mosTypes.mosString128.createRequired(xml.roID, 'roID'),
Status: mosTypes.mosString128.createRequired(xml.roStatus, 'roStatus'),
Stories: [],
}, strict);
const xmlStoryIDs = (0, ensureMethods_1.ensureArray)(xml.storyID);
const xmlItemIDs = (0, ensureMethods_1.ensureArray)(xml.itemID);
const xmlObjIDs = (0, ensureMethods_1.ensureArray)(xml.objID);
const xmlStatuses = (0, ensureMethods_1.ensureArray)(xml.status);
roAck.Stories = [];
const iMax = Math.max(xmlStoryIDs.length, xmlItemIDs.length, xmlObjIDs.length, xmlStatuses.length);
let story = null;
let item = null;
for (let i = 0; i < iMax; i++) {
if (xmlStoryIDs[i]) {
story = {
ID: mosTypes.mosString128.createRequired(xmlStoryIDs[i], `storyID.${i}`),
Items: [],
};
roAck.Stories.push(story);
}
if (xmlItemIDs[i]) {
item = {
ID: mosTypes.mosString128.createRequired(xmlItemIDs[i], `itemID.${i}`),
Channel: mosTypes.mosString128.createRequired('', 'Channel'),
Objects: [],
};
if (story)
story.Items.push(item);
}
if (xmlObjIDs[i] && xmlStatuses[i]) {
const roAckObj = {
Status: mosTypes.string.createRequired(xmlStatuses[i], `status.${i}`),
ID: mosTypes.mosString128.createRequired(xmlObjIDs[i], `objID.${i}`),
};
if (item)
item.Objects.push(roAckObj);
}
}
(0, lib_1.omitUndefined)(roAck);
return roAck;
}
catch (e) {
throw ParseError_1.ParseError.handleCaughtError(path, e);
}
}
XMLMosROAck.fromXML = fromXML;
})(XMLMosROAck = exports.XMLMosROAck || (exports.XMLMosROAck = {}));
var XMLRunningOrderBase;
(function (XMLRunningOrderBase) {
function fromXML(path, xml, strict) {
try {
xml = (0, ensureMethods_1.ensureXMLObject)(xml, strict);
const mosTypes = (0, parseMosTypes_1.getParseMosTypes)(strict);
const ro = {
ID: mosTypes.mosString128.createRequired(xml.roID, 'roID'),
Slug: mosTypes.mosString128.createRequired(xml.roSlug, 'roSlug'),
EditorialStart: mosTypes.mosTime.createOptional(xml.roEdStart, 'roEdStart'),
EditorialDuration: mosTypes.mosDuration.createOptional(xml.roEdDur, 'roEdDur'),
DefaultChannel: mosTypes.mosString128.createOptional(xml.roChannel, 'roChannel'),
Trigger: mosTypes.mosString128.createOptional(xml.roTrigger, 'roTrigger'),
MacroIn: mosTypes.mosString128.createOptional(xml.macroIn, 'macroIn'),
MacroOut: mosTypes.mosString128.createOptional(xml.macroOut, 'macroOut'),
MosExternalMetaData: (0, lib_1.has)(xml, 'mosExternalMetadata')
? xmlConversion_1.XMLMosExternalMetaData.fromXML('mosExternalMetadata', xml.mosExternalMetadata, strict)
: undefined,
};
(0, lib_1.omitUndefined)(ro);
return ro;
}
catch (e) {
throw ParseError_1.ParseError.handleCaughtError(path, e);
}
}
XMLRunningOrderBase.fromXML = fromXML;
function toXML(xmlRo, ro, strict) {
(0, Utils_1.addTextElementInternal)(xmlRo, 'roID', ro.ID, undefined, strict);
(0, Utils_1.addTextElementInternal)(xmlRo, 'roSlug', ro.Slug, undefined, strict);
if (ro.DefaultChannel)
(0, Utils_1.addTextElementInternal)(xmlRo, 'roChannel', ro.DefaultChannel, undefined, strict);
if (ro.EditorialStart)
(0, Utils_1.addTextElementInternal)(xmlRo, 'roEdStart', ro.EditorialStart, undefined, strict);
if (ro.EditorialDuration)
(0, Utils_1.addTextElementInternal)(xmlRo, 'roEdDur', ro.EditorialDuration, undefined, strict);
if (ro.Trigger)
(0, Utils_1.addTextElementInternal)(xmlRo, 'roTrigger', ro.Trigger, undefined, strict);
if (ro.MacroIn)
(0, Utils_1.addTextElementInternal)(xmlRo, 'macroIn', ro.MacroIn, undefined, strict);
if (ro.MacroOut)
(0, Utils_1.addTextElementInternal)(xmlRo, 'macroOut', ro.MacroOut, undefined, strict);
if (ro.MosExternalMetaData)
xmlConversion_1.XMLMosExternalMetaData.toXML(xmlRo, ro.MosExternalMetaData);
}
XMLRunningOrderBase.toXML = toXML;
})(XMLRunningOrderBase = exports.XMLRunningOrderBase || (exports.XMLRunningOrderBase = {}));
var XMLRunningOrder;
(function (XMLRunningOrder) {
function fromXML(path, xml, strict) {
try {
xml = (0, ensureMethods_1.ensureXMLObject)(xml, strict);
const stories = XMLROStories.fromXML('story', xml.story, strict);
const ro = {
...XMLRunningOrderBase.fromXML('', xml, strict),
Stories: stories,
};
(0, lib_1.omitUndefined)(ro);
return ro;
}
catch (e) {
throw ParseError_1.ParseError.handleCaughtError(path, e);
}
}
XMLRunningOrder.fromXML = fromXML;
})(XMLRunningOrder = exports.XMLRunningOrder || (exports.XMLRunningOrder = {}));
var XMLROStories;
(function (XMLROStories) {
function fromXML(path, xmlStories, strict) {
try {
return (0, ensureMethods_1.ensureArray)(xmlStories).map((xmlStory, index) => {
return XMLROStory.fromXML(`[${index}]`, (0, ensureMethods_1.ensureXMLObject)(xmlStory, strict), strict);
});
}
catch (e) {
throw ParseError_1.ParseError.handleCaughtError(path, e);
}
}
XMLROStories.fromXML = fromXML;
})(XMLROStories = exports.XMLROStories || (exports.XMLROStories = {}));
var XMLROStoryBase;
(function (XMLROStoryBase) {
function toXML(xmlStory, story, strict) {
(0, Utils_1.addTextElementInternal)(xmlStory, 'storyID', story.ID, undefined, strict);
if (story.Slug)
(0, Utils_1.addTextElementInternal)(xmlStory, 'storySlug', story.Slug, undefined, strict);
if (story.Number)
(0, Utils_1.addTextElementInternal)(xmlStory, 'storyNum', story.Number, undefined, strict);
if (story.MosExternalMetaData)
xmlConversion_1.XMLMosExternalMetaData.toXML(xmlStory, story.MosExternalMetaData);
}
XMLROStoryBase.toXML = toXML;
})(XMLROStoryBase = exports.XMLROStoryBase || (exports.XMLROStoryBase = {}));
var XMLROStory;
(function (XMLROStory) {
function fromXML(path, xml, strict) {
try {
xml = (0, ensureMethods_1.ensureXMLObject)(xml, strict);
const mosTypes = (0, parseMosTypes_1.getParseMosTypes)(strict);
const story = {
ID: mosTypes.mosString128.createRequired(xml.storyID, 'storyID'),
Slug: mosTypes.mosString128.createOptional(xml.storySlug, 'storySlug'),
Items: [],
// TODO: Add & test Number, ObjectID, MOSID, mosAbstract, Paths
// Channel, EditorialStart, EditorialDuration, UserTimingDuration, Trigger, MacroIn, MacroOut, MosExternalMetaData
// MosExternalMetaData: handleError(MOSExternalMetaData.fromXML(xml.mosExternalMetadata], 'mosExternalMetadata').
Number: mosTypes.mosString128.createOptional(xml.storyNum, 'storyNum'),
MosExternalMetaData: (0, lib_1.has)(xml, 'mosExternalMetadata')
? xmlConversion_1.XMLMosExternalMetaData.fromXML('mosExternalMetadata', xml.mosExternalMetadata, strict)
: undefined,
};
if ((0, lib_1.has)(xml, 'item'))
story.Items = story.Items.concat(XMLMosItems.fromXML('item', xml.item, strict));
if ((0, lib_1.has)(xml, 'storyBody')) {
try {
const xmlStoryBody = (0, ensureMethods_1.ensureXMLObject)(xml.storyBody, strict);
// Note: the <storyBody> is sent in roStorySend
if ((0, lib_1.has)(xmlStoryBody, 'storyItem')) {
story.Items = story.Items.concat(XMLMosItems.fromXML('storyItem', xmlStoryBody.storyItem, strict));
}
}
catch (e) {
throw ParseError_1.ParseError.handleCaughtError('storyBody', e);
}
}
(0, lib_1.omitUndefined)(story);
return story;
}
catch (e) {
throw ParseError_1.ParseError.handleCaughtError(path, e);
}
}
XMLROStory.fromXML = fromXML;
function toXML(xmlRoot, story, strict) {
const xmlStory = (0, Utils_1.addTextElementInternal)(xmlRoot, 'story', undefined, undefined, strict);
XMLROStoryBase.toXML(xmlStory, story, strict);
story.Items.forEach((item) => {
XMLMosItem.toXML(xmlStory, item, strict);
});
}
XMLROStory.toXML = toXML;
})(XMLROStory = exports.XMLROStory || (exports.XMLROStory = {}));
var XMLMosItems;
(function (XMLMosItems) {
function fromXML(path, xmlItems, strict) {
try {
return (0, ensureMethods_1.ensureArray)(xmlItems).map((xmlItem, index) => {
return XMLMosItem.fromXML(`[${index}]`, xmlItem, strict);
});
}
catch (e) {
throw ParseError_1.ParseError.handleCaughtError(path, e);
}
}
XMLMosItems.fromXML = fromXML;
})(XMLMosItems = exports.XMLMosItems || (exports.XMLMosItems = {}));
var XMLMosItem;
(function (XMLMosItem) {
function fromXML(path, xml, strict) {
try {
xml = (0, ensureMethods_1.ensureXMLObject)(xml, strict);
const mosTypes = (0, parseMosTypes_1.getParseMosTypes)(strict);
const item = {
ID: mosTypes.mosString128.createRequired(xml.itemID, 'itemID'),
ObjectID: mosTypes.mosString128.createRequired(xml.objID, 'objID'),
MOSID: mosTypes.string.createRequired(xml.mosID, 'mosID'),
Slug: mosTypes.mosString128.createOptional(xml.itemSlug, 'itemSlug'),
Paths: xml.objPaths ? xmlConversion_1.XMLObjectPaths.fromXML('objPaths', xml.objPaths, strict) : undefined,
EditorialStart: mosTypes.number.createOptional(xml.itemEdStart, 'itemEdStart'),
EditorialDuration: mosTypes.number.createOptional(xml.itemEdDur, 'itemEdDur'),
UserTimingDuration: mosTypes.number.createOptional(xml.itemUserTimingDur, 'itemUserTimingDur'),
Trigger: xml.itemTrigger,
MosExternalMetaData: (0, lib_1.has)(xml, 'mosExternalMetadata')
? xmlConversion_1.XMLMosExternalMetaData.fromXML('mosExternalMetadata', xml.mosExternalMetadata, strict)
: undefined,
mosAbstract: mosTypes.string.createOptional(xml.mosAbstract, 'mosAbstract'),
ObjectSlug: mosTypes.mosString128.createOptional(xml.objSlug, 'objSlug'),
Channel: mosTypes.mosString128.createOptional(xml.itemChannel, 'itemChannel'),
Duration: mosTypes.number.createOptional(xml.objDur, 'objDur'),
TimeBase: mosTypes.number.createOptional(xml.objTB, 'objTB'),
MacroIn: mosTypes.mosString128.createOptional(xml.macroIn, 'macroIn'),
MacroOut: mosTypes.mosString128.createOptional(xml.macroOut, 'macroOut'),
};
if ((0, lib_1.has)(xml, 'mosObj')) {
// Note: the <mosObj> is sent in roStorySend
item.MosObjects = xmlConversion_1.XMLMosObjects.fromXML('mosObj', xml.mosObj, strict);
}
(0, lib_1.omitUndefined)(item);
return item;
}
catch (e) {
throw ParseError_1.ParseError.handleCaughtError(path, e);
}
}
XMLMosItem.fromXML = fromXML;
function toXML(root, item, strict) {
const xmlItem = (0, Utils_1.addTextElementInternal)(root, 'item', undefined, undefined, strict);
(0, Utils_1.addTextElementInternal)(xmlItem, 'itemID', item.ID, undefined, strict);
(0, Utils_1.addTextElementInternal)(xmlItem, 'objID', item.ObjectID, undefined, strict);
(0, Utils_1.addTextElementInternal)(xmlItem, 'mosID', item.MOSID, undefined, strict);
if (item.Slug)
(0, Utils_1.addTextElementInternal)(xmlItem, 'itemSlug', item.Slug, undefined, strict);
xmlConversion_1.XMLObjectPaths.toXML(xmlItem, item.Paths, strict);
if (item.EditorialStart !== undefined)
(0, Utils_1.addTextElementInternal)(xmlItem, 'itemEdStart', item.EditorialStart, undefined, strict);
if (item.EditorialDuration !== undefined)
(0, Utils_1.addTextElementInternal)(xmlItem, 'itemEdDur', item.EditorialDuration, undefined, strict);
if (item.UserTimingDuration !== undefined)
(0, Utils_1.addTextElementInternal)(xmlItem, 'itemUserTimingDur', item.UserTimingDuration, undefined, strict);
if (item.Trigger)
(0, Utils_1.addTextElementInternal)(xmlItem, 'itemTrigger', item.Trigger, undefined, strict);
if (item.MosExternalMetaData)
xmlConversion_1.XMLMosExternalMetaData.toXML(xmlItem, item.MosExternalMetaData);
if (item.mosAbstract)
(0, Utils_1.addTextElementInternal)(xmlItem, 'mosAbstract', item.mosAbstract, undefined, strict);
if (item.ObjectSlug)
(0, Utils_1.addTextElementInternal)(xmlItem, 'objSlug', item.ObjectSlug, undefined, strict);
if (item.Channel)
(0, Utils_1.addTextElementInternal)(xmlItem, 'itemChannel', item.Channel, undefined, strict);
if (item.Duration)
(0, Utils_1.addTextElementInternal)(xmlItem, 'objDur', item.Duration, undefined, strict);
if (item.TimeBase)
(0, Utils_1.addTextElementInternal)(xmlItem, 'objTB', item.TimeBase, undefined, strict);
if (item.MacroIn)
(0, Utils_1.addTextElementInternal)(xmlItem, 'macroIn', item.MacroIn, undefined, strict);
if (item.MacroOut)
(0, Utils_1.addTextElementInternal)(xmlItem, 'macroOut', item.MacroOut, undefined, strict);
// TODO: MosObjects
}
XMLMosItem.toXML = toXML;
})(XMLMosItem = exports.XMLMosItem || (exports.XMLMosItem = {}));
//# sourceMappingURL=xmlConversion.js.map