'use strict';
const baseXml = require('xml');
class TR {
constructor(options, items) {
options = options || {};
this.title = options.title || '';
this.link = options.link || '';
this.description = options.description || '';
this.language = options.language || 'ru';
this.items = [];
if (Array.isArray(items) && items.length > 0) {
const self = this;
items.forEach(function (item) {
self.item(item);
});
}
}
xml() {
return baseXml(this.generateXML(this));
}
item(data) {
data = data || {};
this.items.push(
this.itemData(data)
);
return this;
}
/**
* Check first argument. If true - push last argument to second argument
*
* @param condition
* @param array
* @param data
*/
pushIfConditionTrue(condition, array, data) {
if (condition) {
array.push(data);
}
}
/**
* @param related
* @param itemValues
* @param relatedInfinity
*/
addRelated(related, itemValues, relatedInfinity) {
const relatedResult = related.map(function (rel) {
return {
link: [{
_attr: {
'url': rel.link,
'img': rel.image_url
}
}, rel.text]
};
});
if (relatedInfinity) {
relatedResult.push({
_attr: {'type': 'infinity'}
});
}
this.pushIfConditionTrue(related, itemValues, {'yandex:related': relatedResult});
}
/**
* @param item
* @param itemValues
*/
pushGoals(item, itemValues) {
if (item.goals.length > 0) {
item.goals.forEach(goal => itemValues.push({
'turbo:goal': {
_attr: {
type: goal.type || 'yandex',
'turbo-goal-id': goal.id,
name: goal.name,
id: goal.counter_id,
}
}
}));
}
}
getImageContent(item) {
if (item.image_url.length > 0) {
let imageCaption = '';
if (item.image_caption.length > 0) {
imageCaption = `${imageCaption}