Skip to content

Commit

Permalink
Refresh: periods: DRY.
Browse files Browse the repository at this point in the history
  • Loading branch information
BrRoman committed Jan 9, 2024
1 parent f721fa6 commit 8c5c9d2
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 202 deletions.
32 changes: 32 additions & 0 deletions static/js/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,38 @@ function get_winner(ref_tempo, ref_sancto) {
return winner;
}

function period(duration, start, prefix_tempo, week_start, day_start) {
var html = "";
var year = start.getFullYear();
var month = start.getMonth();
for (var i = 0; i < duration; i++) {
var date = new Date(start.getTime() + (i * 24 * 3600 * 1000));
var day = date.getDate();
var weekday = date.getDay();
var month_usual_number = date.getMonth() + 1;
var ref_tempo = prefix_tempo + (week_start + Math.ceil((i + 1) / 7)) + '_' + (day_start + (i % 7));
console.log(ref_tempo);
var ref_sancto = add_zero(month_usual_number) + month_usual_number + '_' + add_zero(day) + day;
var winner = get_winner(ref_tempo, ref_sancto);
html = html.concat(component(
date,
year,
month,
day,
weekday,
winner['before'],
winner['color'],
winner['header'],
winner['body'],
winner['after'],
));
year = date.getFullYear();
month = date.getMonth();
}

return html;
}

function component(date, year, month, day, weekday, before, color, header, body, after) {
// New year, new month:
var block_new_year, block_new_month;
Expand Down
236 changes: 34 additions & 202 deletions static/js/refresh.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,226 +35,58 @@ function refresh_ordo(year) {

// Advent:
var advent_duration = 21 + christmas_weekday;
for (var i = 0; i < advent_duration; i++) {
var date = new Date(first_sunday_of_advent.getTime() + (i * 24 * 3600 * 1000));
var day = date.getDate();
var weekday = date.getDay();
var month_usual_number = date.getMonth() + 1;
var ref_tempo = 'adv_' + Math.ceil((i + 1) / 7) + '_' + (i % 7);
var ref_sancto = add_zero(month_usual_number) + month_usual_number + '_' + add_zero(day) + day;
var winner = get_winner(ref_tempo, ref_sancto);
content = content.concat(component(
date,
year,
month,
day,
weekday,
winner['before'],
winner['color'],
winner['header'],
winner['body'],
winner['after'],
));
month = date.getMonth();
}
content = content.concat(period(advent_duration, first_sunday_of_advent, 'adv_', 0, 0));

// Christmas time:
var christmas_time_duration = 19 - ((christmas_weekday + 5) % 7);
for (var i = 0; i < christmas_time_duration; i++) {
var date = new Date(christmas.getTime() + (i * 24 * 3600 * 1000));
var day = date.getDate();
var weekday = date.getDay();
var month_usual_number = date.getMonth() + 1;
var ref_tempo = 'christmas_' + Math.ceil((i + 1) / 7) + '_' + (i % 7);
var ref_sancto = add_zero(month_usual_number) + month_usual_number + '_' + add_zero(day) + day;
var winner = get_winner(ref_tempo, ref_sancto);
content = content.concat(component(
date,
year,
month,
day,
weekday,
winner['before'],
winner['color'],
winner['header'],
winner['body'],
winner['after'],
));
year = date.getFullYear();
month = date.getMonth();
}
var baptism = new Date(date.getTime() + (24 * 3600 * 1000));
content = content.concat(period(christmas_time_duration, christmas, 'christmas_', 0, 0));

var baptism = new Date(christmas.getTime() + (christmas_time_duration * day_in_milliseconds));

// Ash Wednesday:
var easter = get_easter_date(year);
var ash_wednesday = new Date(easter.getTime() - (46 * 24 * 3600 * 1000));
var easter = get_easter_date(year + 1);
var ash_wednesday = new Date(easter.getTime() - (46 * day_in_milliseconds));
ash_wednesday = new Date(ash_wednesday.getTime() - (ash_wednesday.getTimezoneOffset() * 60 * 1000));

// Tempus per Annum until Ash Wednesday:
var tempus_per_annum_until_ash_duration = (ash_wednesday - baptism) / (1000 * 3600 * 24) - 1;

for (var i = 0; i < tempus_per_annum_until_ash_duration; i++) {
var date = new Date(baptism.getTime() + (i * 24 * 3600 * 1000));
var day = date.getDate();
var weekday = date.getDay();
var month_usual_number = date.getMonth() + 1;
var ref_tempo = 'pa_' + Math.ceil((i + 1) / 7) + '_' + (i % 7);
var ref_sancto = add_zero(month_usual_number) + month_usual_number + '_' + add_zero(day) + day;
var winner = get_winner(ref_tempo, ref_sancto);
content = content.concat(component(
date,
year,
month,
day,
weekday,
winner['before'],
winner['color'],
winner['header'],
winner['body'],
winner['after'],
));
month = date.getMonth();
}
content = content.concat(period(tempus_per_annum_until_ash_duration, baptism, 'pa_', 0, 0));

// Lent:
// Days after Ash wednesday:
for (var i = 0; i < 4; i++) {
var date = new Date(ash_wednesday.getTime() + (i * 24 * 3600 * 1000));
var day = date.getDate();
var weekday = date.getDay();
var month_usual_number = date.getMonth() + 1;
var ref_tempo = 'lent_0_' + (i + 3);
var ref_sancto = add_zero(month_usual_number) + month_usual_number + '_' + add_zero(day) + day;
var winner = get_winner(ref_tempo, ref_sancto);
content = content.concat(component(
date,
year,
month,
day,
weekday,
winner['before'],
winner['color'],
winner['header'],
winner['body'],
winner['after'],
));
month = date.getMonth();
}
content = content.concat(period(4, ash_wednesday, 'ash_', 0, 3));
// Rest of Lent:
for (var i = 0; i < 42; i++) {
var date = new Date(ash_wednesday.getTime() + ((i + 4) * 24 * 3600 * 1000));
var day = date.getDate();
var weekday = date.getDay();
var month_usual_number = date.getMonth() + 1;
var ref_tempo = 'lent_' + Math.ceil((i + 1) / 7) + '_' + (i % 7);
var ref_sancto = add_zero(month_usual_number) + month_usual_number + '_' + add_zero(day) + day;
var winner = get_winner(ref_tempo, ref_sancto);
content = content.concat(component(
date,
year,
month,
day,
weekday,
winner['before'],
winner['color'],
winner['header'],
winner['body'],
winner['after'],
));
month = date.getMonth();
}
content = content.concat(period(42, new Date(ash_wednesday.getTime() + (4 * day_in_milliseconds)), 'lent_', 0, 0));

// Paschaltide:
for (var i = 0; i < 50; i++) {
var date = new Date(ash_wednesday.getTime() + ((i + 46) * 24 * 3600 * 1000));
var day = date.getDate();
var weekday = date.getDay();
var month_usual_number = date.getMonth() + 1;
var ref_tempo = 'tp_' + Math.ceil((i + 1) / 7) + '_' + (i % 7);
var ref_sancto = add_zero(month_usual_number) + month_usual_number + '_' + add_zero(day) + day;
var winner = get_winner(ref_tempo, ref_sancto);
content = content.concat(component(
date,
year,
month,
day,
weekday,
winner['before'],
winner['color'],
winner['header'],
winner['body'],
winner['after'],
));
month = date.getMonth();
}
var pentecost = new Date(date.getTime() + (24 * 3600 * 1000));
content = content.concat(period(50, easter, 'tp_', 0, 0));

var pentecost = new Date(easter.getTime() + (49 * day_in_milliseconds));

// Tempus per Annum after Pentecost:
christmas = get_christmas_date(year);
var advent = new Date(christmas.getTime() - ((christmas.getDay() + 21) * 24 * 3600 * 1000));
var tempus_per_annum_after_pentecost_duration = ((advent - pentecost) / (1000 * 3600 * 24)) - 6;
christmas = get_christmas_date(year + 1);
var advent = new Date(christmas.getTime() - ((christmas.getDay() + 21) * day_in_milliseconds));
var tempus_per_annum_after_pentecost_duration = ((advent - pentecost) / day_in_milliseconds);
var num_per_annum_of_pentecost = Math.floor(34 - (tempus_per_annum_after_pentecost_duration / 7));
// Days after Pentecost:
for (var i = 0; i < 6; i++) {
var date = new Date(pentecost.getTime() + (i * 24 * 3600 * 1000));
var day = date.getDate();
var weekday = date.getDay();
var month_usual_number = date.getMonth() + 1;
var ref_tempo = 'pa_' + num_per_annum_of_pentecost + '_' + ((i + 1) % 7);
var ref_sancto = add_zero(month_usual_number) + month_usual_number + '_' + add_zero(day) + day;
var winner = get_winner(ref_tempo, ref_sancto);
content = content.concat(component(
date,
year,
month,
day,
weekday,
winner['before'],
winner['color'],
winner['header'],
winner['body'],
winner['after'],
));
month = date.getMonth();
}
// Rest of Tempus per Annum:
for (var i = 0; i < tempus_per_annum_after_pentecost_duration; i++) {
var date = new Date(pentecost.getTime() + ((i + 6) * 24 * 3600 * 1000));
var day = date.getDate();
var weekday = date.getDay();
var month_usual_number = date.getMonth() + 1;
var ref_tempo = 'pa_' + (num_per_annum_of_pentecost + Math.ceil((i + 1) / 7)) + '_' + (i % 7);
var ref_sancto = add_zero(month_usual_number) + month_usual_number + '_' + add_zero(day) + day;
switch (i) {
case 0:
winner = days_tempo['trinity'];
break;
case 4:
winner = days_tempo['corpus_domini'];
break;
case 12:
winner = days_tempo['cor_jesu'];
break;
case 13:
winner = days_tempo['cor_mariæ_immaculatum'];
break;
default:
winner = get_winner(ref_tempo, ref_sancto);
}
content = content.concat(component(
date,
year,
month,
day,
weekday,
winner['before'],
winner['color'],
winner['header'],
winner['body'],
winner['after'],
));
month = date.getMonth();
}
// // 1. Days after Pentecost:
content = content.concat(period(6, new Date(pentecost.getTime() + day_in_milliseconds), 'pa_', num_per_annum_of_pentecost, 1));
// // 2. Rest of Tempus per Annum:
content = content.concat(period(tempus_per_annum_after_pentecost_duration - 7, new Date(pentecost.getTime() + 7 * day_in_milliseconds), 'pa_', num_per_annum_of_pentecost + 1, 0));

// TODO:
// switch (i) {
// case 0:
// winner = days_tempo['trinity'];
// break;
// case 4:
// winner = days_tempo['corpus_domini'];
// break;
// case 12:
// winner = days_tempo['cor_jesu'];
// break;
// case 13:
// winner = days_tempo['cor_mariæ_immaculatum'];
// break;

$('#ordo').html(content);

Expand Down

0 comments on commit 8c5c9d2

Please sign in to comment.