* fix: multi lines and spaces issues * fix: eslint operator-linebreak issue * fix: eslint quotes issue * fix: remaining quotes issues * fix: eslint object curly newline issue * fix: eslint object curly spacing issue * fix: eslint brace-style issues * fix: react jsx indent and props issues * fix: eslint trailing spaces issues * fix: eslint linbreak style issue * fix: eslint space unary operator issue * fix: eslint line around directives issue * fix: void and typeof space unary ops issue
108 lines
3.8 KiB
JavaScript
108 lines
3.8 KiB
JavaScript
/**
|
|
*
|
|
* A helper function to utilize DateUtils quickly in display templates.
|
|
*
|
|
* @param: {string} data-datetime A pre-localized datetime string, assumed to be in UTC.
|
|
* @param: {string} lang The user's preferred language.
|
|
* @param: {string} data-timezone (optional) A user-set timezone preference.
|
|
* @param: {object} data-format (optional) a format constant as defined in DataUtil.dateFormatEnum.
|
|
* @param: {string} data-string (optional) a string for parsing through StringUtils after localizing
|
|
* datetime
|
|
*
|
|
* @return: {string} a user-time, localized, formatted datetime string
|
|
*
|
|
*/
|
|
|
|
(function(define) {
|
|
'use strict';
|
|
|
|
define([
|
|
'jquery',
|
|
'edx-ui-toolkit/js/utils/date-utils',
|
|
'edx-ui-toolkit/js/utils/string-utils'
|
|
], function($, DateUtils, StringUtils) {
|
|
var DateUtilFactory;
|
|
var localizedTime;
|
|
var stringHandler;
|
|
var displayDatetime;
|
|
var isValid;
|
|
var transform;
|
|
var dueDateFormat;
|
|
var dateFormat;
|
|
|
|
dueDateFormat = Object.freeze({
|
|
'%Y-%d-%m': 'YYYY, D MMM HH[:]mm z', // example: 2018, 01 Jan 15:30 UTC
|
|
'%m-%d-%Y': 'MMM D, YYYY HH[:]mm z', // example: Jan 01, 2018 15:30 UTC
|
|
'%d-%m-%Y': 'D MMM YYYY HH[:]mm z', // example: 01 Jan, 2018 15:30 UTC
|
|
'%Y-%m-%d': 'YYYY, MMM D HH[:]mm z' // example: 2018, Jan 01 15:30 UTC
|
|
});
|
|
|
|
transform = function(iterationKey) {
|
|
var context;
|
|
$(iterationKey).each(function() {
|
|
if (isValid($(this).data('datetime'))) {
|
|
dateFormat = DateUtils.dateFormatEnum[$(this).data('format')];
|
|
if (typeof dateFormat === 'undefined') {
|
|
dateFormat = dueDateFormat[$(this).data('format')];
|
|
}
|
|
context = {
|
|
datetime: $(this).data('datetime'),
|
|
timezone: $(this).data('timezone'),
|
|
language: $(this).data('language'),
|
|
format: dateFormat
|
|
};
|
|
displayDatetime = stringHandler(
|
|
localizedTime(context),
|
|
$(this).data('string'),
|
|
$(this).data('datetoken')
|
|
);
|
|
$(this).text(displayDatetime);
|
|
} else {
|
|
displayDatetime = stringHandler(
|
|
$(this).data('string')
|
|
);
|
|
$(this).text(displayDatetime);
|
|
}
|
|
});
|
|
};
|
|
|
|
localizedTime = function(context) {
|
|
return DateUtils.localize(context);
|
|
};
|
|
|
|
stringHandler = function(localTimeString, containerString, token) {
|
|
var returnString;
|
|
var interpolateDict = {};
|
|
var dateToken;
|
|
if (isValid(token)) {
|
|
dateToken = token;
|
|
} else {
|
|
dateToken = 'date';
|
|
}
|
|
interpolateDict[dateToken] = localTimeString;
|
|
|
|
if (isValid(containerString)) {
|
|
returnString = StringUtils.interpolate(
|
|
containerString,
|
|
interpolateDict
|
|
);
|
|
} else {
|
|
returnString = localTimeString;
|
|
}
|
|
return returnString;
|
|
};
|
|
|
|
isValid = function(candidateVariable) {
|
|
return candidateVariable !== undefined
|
|
&& candidateVariable !== ''
|
|
&& candidateVariable !== 'Invalid date'
|
|
&& candidateVariable !== 'None';
|
|
};
|
|
DateUtilFactory = {
|
|
transform: transform,
|
|
stringHandler: stringHandler
|
|
};
|
|
return DateUtilFactory;
|
|
});
|
|
}).call(this, define || RequireJS.define);
|