define('application/models/production/uibuilder/page', [
'rofl/models/api/abstract',
'application/models/configuration',
'rofl/lib/utils',
'application/utils',
'application/models/uibuilder/hero',
'application/models/uibuilder/carousel',
'application/models/uibuilder/heroslider',
'rofl/lib/promise'
], function (
Abstract,
ApiConfig,
Utils,
AppUtils,
Hero,
Carousel,
HeroSlider,
Promise
) {
'use strict';
var LAYOUTS = {
HERO_LARGE_VERT: 'VOD_HERO_LARGE_VERTICAL',
HERO_LARGE_HORI: 'VOD_HERO_LARGE_HORIZONTAL',
HERO_SMALL_HORI: 'VOD_HERO_SMALL_HORIZONTAL',
HERO_SMALL_VERT: 'VOD_HERO_SMALL_VERTICAL',
VOD_HERO_LARGE: 'VOD_HERO_LARGE',
EPG_HERO_LARGE: 'EPG_HERO_LARGE',
CAROUSEL_LARGE_HORI: 'VOD_LARGE_HORIZONTAL',
CAROUSEL_LARGE_VERT: 'VOD_LARGE_VERTICAL',
CAROUSEL_SMALL_HORI: 'VOD_SMALL_HORIZONTAL',
CAROUSEL_SMALL_VERT: 'VOD_SMALL_VERTICAL',
VOD_LARGE: 'VOD_LARGE',
VOD_SMALL: 'VOD_SMALL',
EPG_LARGE: 'EPG_LARGE',
EPG_SPORT: 'EPG_SPORT',
BOOKMARK_LARGE_HORIZONTAL: 'BOOKMARK_LARGE_HORIZONTAL'
};
return Abstract.extend({
init: function () {
this._sections = [];
this._hero = [];
},
/**
* Resolves the endpoint.
*
* @param {Object} params - The parameters.
*
* @returns {string} - The endpoint.
*/
resolveEndpoint: function (params) {
return Utils.formatTemplate('101/1.2.0/A/nld/smarttv/kpn/PAGE/{{page}}', params);
},
/**
* Validates the response.
*
* @param {Object} response - The response.
* @returns {boolean} Response.
*/
validateResponse: function (response) {
return response.resultCode === ApiConfig.RESPONSE_CODES.OK;
},
/**
* Transforms the response.
*
* @param {Object} response - The API response data.
* @returns {Object} - The Page model.
*/
transformFromRead: function (response) {
var containers = Utils.getNested(response, 'resultObj', 'containers');
if (!containers.length) {
return this;
}
Utils.each(containers, function (container) {
switch (container.layout) {
case LAYOUTS.CAROUSEL_LARGE_HORI:
case LAYOUTS.CAROUSEL_LARGE_VERT:
case LAYOUTS.CAROUSEL_SMALL_HORI:
case LAYOUTS.CAROUSEL_SMALL_VERT:
case LAYOUTS.VOD_LARGE:
case LAYOUTS.VOD_SMALL:
case LAYOUTS.EPG_LARGE:
case LAYOUTS.EPG_SPORT:
case LAYOUTS.BOOKMARK_LARGE_HORIZONTAL:
this._sections.push(new Carousel(container));
break;
case LAYOUTS.HERO_LARGE_HORI:
case LAYOUTS.HERO_LARGE_VERT:
case LAYOUTS.HERO_SMALL_HORI:
case LAYOUTS.HERO_SMALL_VERT:
case LAYOUTS.VOD_HERO_LARGE:
case LAYOUTS.EPG_HERO_LARGE:
// There can be multiple Hero responses coming back from the API.
this._hero.push(new Hero(container));
break;
}
}, this);
return this;
},
/**
* Returns the Hero model of the page.
*
* @returns {Array} - The Hero model.
*/
getHero: function () {
return AppUtils.randomiseArray(this._hero);
},
/**
* Returns all the individual section models.
*
* @returns {Array} - The section models.
*/
getSections: function () {
var hero = this.getHero();
if (hero) {
return hero.concat(this._sections);
}
return this._sections;
},
/**
* Prepares the widget and loads all required data.
*
* @returns {Promise} - Promise resolving with the slider hero model.
*/
prepareHero: function () {
var hero = this._hero,
preparePromises = [];
if (hero.length) {
// Prepare each hero item.
Utils.each(hero, function (heroItem) {
preparePromises.push(heroItem.prepare());
});
return Promise
.all(preparePromises)
.then(Utils.bind(function (heroItems) {
heroItems = Utils.filter(heroItems, function (heroItem) {
// Filter out locked items and slider items with no data item.
return heroItem.getItem() && !heroItem.isLocked();
});
return new HeroSlider(heroItems);
}, this));
}
// Resolve if no hero items available. (empty Array).
return Promise.resolve(hero);
},
/**
* Prepares the widget and loads all required data.
*
* @returns {Promise} - Promise resolving with the slider item model.
*/
prepareCarousels: function () {
var carousels = this._sections,
preparePromises = [];
if (carousels.length) {
// Prepare carousels.
Utils.each(carousels, function (carousel) {
preparePromises.push(carousel.prepare());
});
return Promise
.all(preparePromises)
.then(Utils.bind(function (sectionItems) {
return sectionItems;
}, this));
}
// Resolve if no hero items available. (empty Array).
return Promise.resolve(carousels);
}
});
});