define(function () {
"use strict";
var oPage;
function datePromptPresets() {
};
datePromptPresets.prototype.draw = function (oControlHost) {
oPage = oControlHost.page;
var elDate = oControlHost.container;
elDate.innerHTML =
'' +
'' +
'' +
'' +
'';
elDate.querySelector(".btnYesterday").onclick = setDatePreset.bind(null, "Yesterday");
elDate.querySelector(".btnCurrentYTD").onclick = setDatePreset.bind(null, "YTD");
elDate.querySelector(".btnPreviousMonth").onclick = setDatePreset.bind(null, "PreviousMonth");
elDate.querySelector(".btnPreviousYear").onclick = setDatePreset.bind(null, "PreviousYear");
};
function setDatePreset(timeFrame) {
// Calculate the date range start date
var dateRangeStart = calculateRangeStart(timeFrame);
// Calculate the date range end date
var dateRangeEnd = calculateEndDate(dateRangeStart, timeFrame);
// Encode the date range as JSON
var JSONEncodedDates = EncodeDateRangeAsJSON(dateRangeStart, dateRangeEnd);
// Set the Date prompt using the JSON encoded date range
setDatePrompt(JSONEncodedDates);
};
/*
*
* This is the only function that actually uses the prompt JavaScript API using it to perform three operations
* Creates a report object
* Create a prompt object
* Set the prompt values
*
* Function arguments:
* @JSONEncodedDates is a date range with use values range start and end dates
*
*/
function setDatePrompt(JSONEncodedDates) {
// Create a prompt object using the prompt name set in Report Studio
var datePrompt = oPage.getControlByName("DatePrompt");
// Set the prompt values using the JSON encoded dates
datePrompt.addValues([JSONEncodedDates]);
};
/*
*
* This function encodes the range start and end use values into as JSON. In other words, into
* JavaScript Literal Notation such as
* { 'start': {'use': '2012-01-08'}, 'end': {'use': '2012-02-08'} }
*
* Function arguments:
* @dateRangeStart - a date object for the range start date
* @dateRangeEnd - a date object for the range end date
*
*/
function EncodeDateRangeAsJSON(dateRangeStart, dateRangeEnd) {
// Encode the date range as JSON (JavaScript Literal Notation)
// The following steps will create string representation of the date range start and end dates
// in the format yyyy-mm-dd such as 2012-01-12 for to encode into JSON
// First, get the year, month and day parts of the date range start
var yearPart = dateRangeStart.getFullYear();
var monthPart = dateRangeStart.getMonth() + 1; // Add 1 month for base zero month (Jan = month 0)
var dayPart = dateRangeStart.getDate();
// Second, create the string representation of the date range start
var startDateString = yearPart + "-" + monthPart + "-" + dayPart;
// Third, get the year, month and day parts of the date range end
var yearPart = dateRangeEnd.getFullYear();
var monthPart = dateRangeEnd.getMonth() + 1; // Add 1 month for base zero month (Jan = month 0)
var dayPart = dateRangeEnd.getDate();
// Lastly, create the string representation of the date range end
var endDateString = yearPart + "-" + monthPart + "-" + dayPart;
// Now that we have our dates as strings, encode the date range as JSON (JavaScript Literal Notation)
var JSONEncodedDateRange = {
'start': {
'use': startDateString
}, 'end': {
'use': endDateString
}
};
return JSONEncodedDateRange;
};
/*
*
* This function calculates the date range start date using today's date as the baseline.
*
* Function arguments:
* @timeFrame is a string passed to this function to determine the date range. Valid values are:
* - Yesterday (set range yesterday and today)
* - YTD
* - PreviousMonth
* - PreviousQuarter
* - PreviousYear
*/
function calculateRangeStart(timeFrame) {
// Get the system year and month
var currentDate = new Date();
// Extract year, month and day components as numbers from current date
var currentYear = currentDate.getFullYear();
var currentMonth = currentDate.getMonth(); // zero based, January is month 0
var currentDay = currentDate.getDate(); // 1 based
if (timeFrame == "Yesterday") {
// Calculate yesterdays date including determining if yesterday was in a previous Month and/or Year
// If today is not the first day of the month, subtract one day and keep the same month and year
if (currentDay != 1) {
var rangeStartYear = currentYear;
var rangeStartMonth = currentMonth;
var rangeStartDay = currentDay - 1;
} else { // Today is the first day of the month so set month to previous month
if (currentMonth != 0) { // First day of Feb through December
// The Date.setFullYear will return the last day of the previous month if you pass zero as the day
var workingDate = new Date;
workingDate.setFullYear(currentYear, currentMonth, 0);
var rangeStartYear = workingDate.getYear();
var rangeStartMonth = workingDate.getMonth();
var rangeStartDay = workingDate.getDate();
} else { // First day of January
// The Date.setFullYear will return the last day of the previous month if you pass zero as the day
var workingDate = new Date;
workingDate.setFullYear(currentYear, currentMonth, 0);
var rangeStartYear = workingDate.getYear();
var rangeStartMonth = workingDate.getMonth();
var rangeStartDay = workingDate.getDate();
}
}
} else if (timeFrame == "PreviousMonth") {
// Calculate the first day of the previous month based on today's date
// Set Current Month to previous Month (and Current Year as required)
if (currentMonth == 0) { //set month and year to previous december and year
var rangeStartMonth = 11;
var rangeStartYear = currentYear - 1;
} else { // set to previous month in same year
rangeStartMonth = currentMonth - 1;
var rangeStartYear = currentYear;
}
var rangeStartDay = 1; // Always first day of month
} else if (timeFrame == "YTD") {
// Calculate the first day of the year based on today's date
var rangeStartYear = currentYear;
var rangeStartMonth = 0; // Always January
var rangeStartDay = 1; // Always first day of month
} else if (timeFrame == "PreviousQuarter") {
// Calculate the first day of the previous calendar quarter month based on today's date
// Quarters Conversion is:
// Q1 = January to March (Month 0 to 2) >>> October (9) Previous year
// Q2 = April to June (Month 3 to 5) >>> January (Zero) Same year
// Q3 = July to September (Month 6 to 8) >>> April (3) Same year
// Q4 = October to December (Month 9 to 11) >>> July (6) Same year
// Set Quarter and Year
if (currentMonth <= 2) {
var rangeStartMonth = 9; // October
var rangeStartYear = currentYear - 1; // Previous year
} else if (currentMonth <= 5) {
var rangeStartMonth = 0; // January
var rangeStartYear = currentYear; // Same year
} else if (currentMonth <= 8) {
var rangeStartMonth = 3; // April
var rangeStartYear = currentYear; // Same year
} else {
rangeStartMonth = 6; // July
var rangeStartYear = currentYear; // Same year
}
var rangeStartDay = 1; // Always first day of month
} else if (timeFrame == "PreviousYear") {
var rangeStartYear = currentYear - 1;
var rangeStartMonth = 0; // Always January
var rangeStartDay = 1; // Always first day of month
} else {
alert("Invalid Time Frame");
}
var rangeStart = new Date(rangeStartYear, rangeStartMonth, rangeStartDay);
return rangeStart;
};
/*
*
* This function calculates the date range end date based on the passed time frame and start date
*
*/
function calculateEndDate(dateRangeStart, timeFrame) {
// Calculate the end of the date range based on the passed time frame
// Extract Year, Month and Day from passed in dateRangeStart
var workingYear = dateRangeStart.getFullYear();
var workingMonth = dateRangeStart.getMonth();
var workingDay = dateRangeStart.getDate();
if (timeFrame == "Yesterday") {
// use today's date
dateRangeEnd = new Date();
} else if (timeFrame == "PreviousMonth") {
// Calculate the last day of the passed month
// The Date.setFullYear will return the last day of the previous month if you pass zero as the day
var dateRangeEnd = new Date;
dateRangeEnd.setFullYear(workingYear, workingMonth + 1, 0);
} else if (timeFrame == "YTD") {
// Calculate the current date
dateRangeEnd = new Date();
} else if (timeFrame == "PreviousQuarter") {
var endMonth = 0;
var endDay = 0;
// Calculate the end of the passed quarter
if (workingMonth == 0) {
endMonth = 2; // March
endDay = 31; // Last day of March = 31
} else if (workingMonth == 3) {
EndMonth = 5; // June
EndDay = 30; // Last day of March = 30
} else if (workingMonth == 6) {
endMonth = 8; // September
endDay = 30; // Last day of March = 30
} else {
endMonth = 11; // December
endDay = 31; // Last day of March = 31
}
dateRangeEnd = new Date(workingYear, endMonth, endDay);
} else if (timeFrame == "PreviousYear") {
var rangeEndYear = workingYear; // Same year as start range
var rangeEndMonth = 11; // Always December
var rangeEndDay = 31; // Always last day of December
dateRangeEnd = new Date(rangeEndYear, rangeEndMonth, rangeEndDay);
} else {
alert("calculateEndDate: Invalid Time Frame");
}
return dateRangeEnd;
};
return datePromptPresets;
});