define('application/widgets/guide/filters/day', [
'application/widgets/filters/button',
'rofl/lib/l10n',
'rofl/lib/utils',
'antie/runtimecontext'
], function (
FilterButton,
L10N,
Utils,
RuntimeContext
) {
'use strict';
var application = RuntimeContext.getCurrentApplication(),
config = application.getConfiguration(),
DEFAULT_FILTER_BACK_DAYS = Utils.getNested(config, 'guide', 'backDays') || 6,
DEFAULT_FILTER_FORWARD_DAYS = Utils.getNested(config, 'guide', 'forwardDays') || 5,
l10n = L10N.getInstance();
return FilterButton.extend({
init: function init (data) {
init.base.call(this, data);
this._dates = this._getFilterDays();
},
/**
* Get a generated array of days in string form.
*
* @param {Integer} [num] - Number of generated days.
* @returns {Array} Days in string.
* @private
*/
_getFilterDays: function (num) {
var dates = [],
display,
i,
dateArray,
dateString,
currentDay = Math.floor(application.getDate().getTime() / 1000 / 60 / 60 / 24);
num = num || DEFAULT_FILTER_FORWARD_DAYS + DEFAULT_FILTER_BACK_DAYS + 1;
dateArray = this._generateDateArray();
for (i = 0; i < num; i++) {
display = 1;
switch (Math.floor(dateArray[i].getTime() / 1000 / 60 / 60 / 24)) {
case currentDay:
dateString = [
l10n.get('asset.today')
].join(' ');
break;
case (currentDay + 1):
dateString = [
l10n.get('asset.tomorrow')
].join(' ');
break;
case (currentDay - 1):
dateString = [
l10n.get('asset.yesterday')
].join(' ');
break;
default:
dateString = [
l10n.get('asset.days.' + dateArray[i].getDay()),
dateArray[i].getDate(),
l10n.get('asset.monthsLong.' + dateArray[i].getMonth())
].join(' ');
display = 2;
}
dates.push({
value: dateString,
display: display
});
}
return dates;
},
/**
* Generate array of dates.
*
* @param {number} [back] - Number of back days.
* @param {number} [forward] - Number of forward days.
* @returns {Array} Days.
* @private
*/
_generateDateArray: function (back, forward) {
var i,
date,
backDays = back || DEFAULT_FILTER_BACK_DAYS,
forwardDays = forward || DEFAULT_FILTER_FORWARD_DAYS,
days = [];
for (i = -backDays; i <= -1; i++) { // Generates back days.
date = this._generateDate(i);
days.push(date);
}
days.push(this._generateDate(0)); // Generates today.
for (i = 1; i <= forwardDays; i++) { // Generates forward days.
date = this._generateDate(i);
days.push(date);
}
return days;
},
/**
* Generate a day.
*
* @param {Integer} i - Position due to this day.
* @returns {Date} Generated date.
* @private
*/
_generateDate: function (i) {
var day = application.getDate(),
today = application.getDate();
day.setDate(today.getDate() + i);
return day;
},
/**
* Sets the text.
*
* @param {string} text - The text.
*/
setText: function setText (text) {
var filtered = Utils.filter(this._dates, function (date) {
return date.value === text;
})[0] || this.getDefaultText(text),
split = filtered.value.split(' ');
if (filtered.display === 1) {
split = split.slice(0, 1).join(' ');
} else if (filtered.display === 2) {
split = split.slice(1, 3).join(' ');
}
this._textValue = filtered.value;
setText.base.call(this, split);
},
/**
* Returns default text for date that was not found.
*
* @param {string} text - Text value.
* @returns {{display: number, value: (*|string)}} - Returns default text.
*/
getDefaultText: function (text) {
return {
value: text || '',
display: 1
};
},
/**
* Returns the text.
*
* @returns {string} - The text.
*/
getText: function () {
return this._textValue;
}
});
});