This repository has been archived on 2025-01-01. You can view files and clone it, but cannot push or open issues or pull requests.
moodle-local_treestudyplan/amd/src/util/string-helper.js

120 lines
3.6 KiB
JavaScript
Raw Normal View History

import {get_strings} from 'core/str';
/**
* Load the translation of strings from a strings object
* @param {Object} strings The map of strings
* @returns {Object} The map with strings loaded in
*/
export function load_strings(strings){
for(let idx in strings){
let stringkeys = [];
2023-06-16 23:11:43 +02:00
for(const handle in strings[idx]){
const key = strings[idx][handle];
let parts = key.split("$");
let identifier = parts[0];
let component = (parts.length > 1)?parts[1]:'local_treestudyplan';
stringkeys.push({ key: identifier, component: component});
}
get_strings(stringkeys).then(function(str){
let i = 0;
for(const key in strings[idx]){
strings[idx][key] = str[i];
i++;
}
});
}
return strings;
}
/**
* Load the translation of strings from a strings object based on keys
* Used for loading values for a drop down menu or the like
* @param {Object} string_keys The map of stringkeys
* @returns {Object} The map with strings loaded in
*/
export function load_stringkeys(string_keys){
for(let idx in string_keys){
// Get text strings for condition settings
let stringkeys = [];
for(const i in string_keys[idx]){
const key = string_keys[idx][i].textkey;
let parts = key.split("$");
let identifier = parts[0];
let component = (parts.length > 1)?parts[1]:'local_treestudyplan';
stringkeys.push({ key: identifier, component: component});
}
get_strings(stringkeys).then(function(strings){
for(const i in strings) {
const s = strings[i];
const l = string_keys[idx][i];
l.text = s;
}
});
}
return string_keys;
}
/**
* Format a date according to localized custom
* @param {Date|string} d The date to convert
* @param {boolean} short Short format (default false)
* @returns {string}
*/
export function format_date(d,short){
if(!(d instanceof Date)){
d = new Date(d);
}
let monthformat = "short";
if(short){
monthformat = "numeric";
}
return d.toLocaleDateString(document.documentElement.lang,{
year: 'numeric', month: monthformat, day: 'numeric'
});
}
/**
* Provides standardized information about the period between two dates
* As
* @param {Date|string} first First day of the span
* @param {Date|string} last Last day of the span
* @returns {Object} Object containing formatted start and end dates and span information
*/
export function datespaninfo(first,last){
if(!(first instanceof Date)){ first = new Date(first);}
if(!(last instanceof Date)){ last = new Date(last);}
// Make sure the end date is at the very end of the day and the start date at the very beginning
first.setHours(0);
first.setMinutes(0);
first.setSeconds(0);
first.setMilliseconds(0);
last.setHours(23);
last.setMinutes(59);
last.setSeconds(59);
last.setMilliseconds(999);
const dayspan = Math.round(((last - first)+1)/(24*60*60*1000)); // Add one millisecond to offset the 999 ms
const years = Math.floor(dayspan/365); // Yes, we ignore leap years/leap days
const ydaysleft = dayspan % 365;
const weeks = Math.floor(ydaysleft/7);
const wdaysleft = ydaysleft % 7;
return {
first: first,
last: last,
totaldays: dayspan,
years: years,
weeks: weeks,
days: wdaysleft,
formatted: {
first: format_date(first),
last: format_date(last),
}
};
}