From d348deeb16ee00fa36de0a145b8c5b520ef2c19c Mon Sep 17 00:00:00 2001 From: Jennifer Richards Date: Wed, 23 Mar 2022 13:27:04 -0300 Subject: [PATCH 01/13] refactor: move agenda_timezone.js code out of global scope --- ietf/static/js/agenda_timezone.js | 482 +++++++++++++++--------------- 1 file changed, 242 insertions(+), 240 deletions(-) diff --git a/ietf/static/js/agenda_timezone.js b/ietf/static/js/agenda_timezone.js index ac15db8b43..a020c0e65a 100644 --- a/ietf/static/js/agenda_timezone.js +++ b/ietf/static/js/agenda_timezone.js @@ -1,5 +1,4 @@ // Copyright The IETF Trust 2021, All Rights Reserved - /* Timezone support specific to the agenda page @@ -8,272 +7,275 @@ returning the current timezone to the set_current_tz_cb() method. This should be done before calling anything else in the file. */ +(function() { + 'use strict'; -var local_timezone = moment.tz.guess(); + var local_timezone = moment.tz.guess(); -// get_current_tz_cb must be overwritten using set_current_tz_cb -function get_current_tz_cb() { - throw new Error('Tried to get current timezone before callback registered. Use set_current_tz_cb().'); -} + // get_current_tz_cb must be overwritten using set_current_tz_cb + function get_current_tz_cb() { + throw new Error('Tried to get current timezone before callback registered. Use set_current_tz_cb().'); + } -// Initialize moments -window.initialize_moments = function () { - var times = $('.time'); - $.each(times, function (i, item) { - item.start_ts = moment.unix(this.getAttribute("data-start-time")) - .utc(); - item.end_ts = moment.unix(this.getAttribute("data-end-time")) - .utc(); - if (this.hasAttribute("weekday")) { - item.format = 2; - } else { - item.format = 1; - } - if (this.hasAttribute("format")) { - item.format = +this.getAttribute("format"); - } - }); - times = $('[data-slot-start-ts]'); - $.each(times, function (i, item) { - item.slot_start_ts = moment.unix(this.getAttribute("data-slot-start-ts")) - .utc(); - item.slot_end_ts = moment.unix(this.getAttribute("data-slot-end-ts")) - .utc(); - }); -}; + // Initialize moments + window.initialize_moments = function () { + var times = $('.time'); + $.each(times, function (i, item) { + item.start_ts = moment.unix(this.getAttribute("data-start-time")) + .utc(); + item.end_ts = moment.unix(this.getAttribute("data-end-time")) + .utc(); + if (this.hasAttribute("weekday")) { + item.format = 2; + } else { + item.format = 1; + } + if (this.hasAttribute("format")) { + item.format = +this.getAttribute("format"); + } + }); + times = $('[data-slot-start-ts]'); + $.each(times, function (i, item) { + item.slot_start_ts = moment.unix(this.getAttribute("data-slot-start-ts")) + .utc(); + item.slot_end_ts = moment.unix(this.getAttribute("data-slot-end-ts")) + .utc(); + }); + }; -function format_time(t, tz, fmt) { - var out; - var mtz = window.meeting_timezone; - if (mtz == "") { - mtz = "UTC"; - } - switch (fmt) { - case 0: - out = t.tz(tz) - .format('dddd, ') + '' + - t.tz(tz) - .format('MMMM Do YYYY, ') + '' + - t.tz(tz) - .format('HH:mm') + '' + - t.tz(tz) - .format(' Z z') + ''; - break; - case 1: - // Note, this code does not work if the meeting crosses the - // year boundary. - out = t.tz(tz) - .format("HH:mm"); - if (+t.tz(tz) - .dayOfYear() < +t.tz(mtz) - .dayOfYear()) { - out = out + " (-1)"; - } else if (+t.tz(tz) - .dayOfYear() > +t.tz(mtz) - .dayOfYear()) { - out = out + " (+1)"; + function format_time(t, tz, fmt) { + var out; + var mtz = window.meeting_timezone; + if (mtz == "") { + mtz = "UTC"; } - break; - case 2: - out = t.tz(mtz) - .format("dddd, ") - .toUpperCase() + - t.tz(tz) - .format("HH:mm"); - if (+t.tz(tz) - .dayOfYear() < +t.tz(mtz) - .dayOfYear()) { - out = out + " (-1)"; - } else if (+t.tz(tz) - .dayOfYear() > +t.tz(mtz) - .dayOfYear()) { - out = out + " (+1)"; + switch (fmt) { + case 0: + out = t.tz(tz) + .format('dddd, ') + '' + + t.tz(tz) + .format('MMMM Do YYYY, ') + '' + + t.tz(tz) + .format('HH:mm') + '' + + t.tz(tz) + .format(' Z z') + ''; + break; + case 1: + // Note, this code does not work if the meeting crosses the + // year boundary. + out = t.tz(tz) + .format("HH:mm"); + if (+t.tz(tz) + .dayOfYear() < +t.tz(mtz) + .dayOfYear()) { + out = out + " (-1)"; + } else if (+t.tz(tz) + .dayOfYear() > +t.tz(mtz) + .dayOfYear()) { + out = out + " (+1)"; + } + break; + case 2: + out = t.tz(mtz) + .format("dddd, ") + .toUpperCase() + + t.tz(tz) + .format("HH:mm"); + if (+t.tz(tz) + .dayOfYear() < +t.tz(mtz) + .dayOfYear()) { + out = out + " (-1)"; + } else if (+t.tz(tz) + .dayOfYear() > +t.tz(mtz) + .dayOfYear()) { + out = out + " (+1)"; + } + break; + case 3: + out = t.utc() + .format("YYYY-MM-DD"); + break; + case 4: + out = t.tz(tz) + .format("YYYY-MM-DD HH:mm"); + break; + case 5: + out = t.tz(tz) + .format("HH:mm"); + break; } - break; - case 3: - out = t.utc() - .format("YYYY-MM-DD"); - break; - case 4: - out = t.tz(tz) - .format("YYYY-MM-DD HH:mm"); - break; - case 5: - out = t.tz(tz) - .format("HH:mm"); - break; + return out; } - return out; -} -// Format tooltip notice -function format_tooltip_notice(start, end) { - var notice = ""; + // Format tooltip notice + function format_tooltip_notice(start, end) { + var notice = ""; - if (end.isBefore()) { - notice = "Event ended " + end.fromNow(); - } else if (start.isAfter()) { - notice = "Event will start " + start.fromNow(); - } else { - notice = "Event started " + start.fromNow() + " and will end " + - end.fromNow(); + if (end.isBefore()) { + notice = "Event ended " + end.fromNow(); + } else if (start.isAfter()) { + notice = "Event will start " + start.fromNow(); + } else { + notice = "Event started " + start.fromNow() + " and will end " + + end.fromNow(); + } + return '' + notice + ''; } - return '' + notice + ''; -} -// Format tooltip table -function format_tooltip_table(start, end) { - var current_timezone = get_current_tz_cb(); - var out = '
'; - if (window.meeting_timezone !== "") { - out += ''; - } - out += ''; - if (current_timezone !== 'UTC') { - out += ''; + // Format tooltip table + function format_tooltip_table(start, end) { + var current_timezone = get_current_tz_cb(); + var out = '
Session startSession end
Meeting timezone' + - format_time(start, window.meeting_timezone, 0) + '' + - format_time(end, window.meeting_timezone, 0) + '
Local timezone' + - format_time(start, local_timezone, 0) + '' + - format_time(end, local_timezone, 0) + '
Selected Timezone' + - format_time(start, current_timezone, 0) + '' + - format_time(end, current_timezone, 0) + '
'; + if (window.meeting_timezone !== "") { + out += ''; + } + out += ''; + if (current_timezone !== 'UTC') { + out += ''; + } + out += ''; + out += '
Session startSession end
Meeting timezone' + + format_time(start, window.meeting_timezone, 0) + '' + + format_time(end, window.meeting_timezone, 0) + '
Local timezone' + + format_time(start, local_timezone, 0) + '' + + format_time(end, local_timezone, 0) + '
Selected Timezone' + + format_time(start, current_timezone, 0) + '' + + format_time(end, current_timezone, 0) + '
UTC' + + format_time(start, 'UTC', 0) + '' + + format_time(end, 'UTC', 0) + '
' + format_tooltip_notice(start, end) + '
'; + return out; } - out += 'UTC' + - format_time(start, 'UTC', 0) + '' + - format_time(end, 'UTC', 0) + ''; - out += '' + format_tooltip_notice(start, end) + ''; - return out; -} -// Format tooltip for item -function format_tooltip(start, end) { - return '
' + - format_tooltip_table(start, end) + - '
'; -} + // Format tooltip for item + function format_tooltip(start, end) { + return '
' + + format_tooltip_table(start, end) + + '
'; + } -// Add tooltips -window.add_tooltips = function () { - $('.time') - .each(function () { - var tooltip = $(format_tooltip(this.start_ts, this.end_ts)); - tooltip[0].start_ts = this.start_ts; - tooltip[0].end_ts = this.end_ts; - tooltip[0].ustart_ts = moment(this.start_ts) - .add(-2, 'hours'); - tooltip[0].uend_ts = moment(this.end_ts) - .add(2, 'hours'); - $(this) - .closest("th, td") - .attr("data-bs-toggle", "popover") - .attr("data-bs-content", $(tooltip) - .html()) - .popover({ - html: true, - sanitize: false, - trigger: "hover" - }); - }); -}; + // Add tooltips + window.add_tooltips = function () { + $('.time') + .each(function () { + var tooltip = $(format_tooltip(this.start_ts, this.end_ts)); + tooltip[0].start_ts = this.start_ts; + tooltip[0].end_ts = this.end_ts; + tooltip[0].ustart_ts = moment(this.start_ts) + .add(-2, 'hours'); + tooltip[0].uend_ts = moment(this.end_ts) + .add(2, 'hours'); + $(this) + .closest("th, td") + .attr("data-bs-toggle", "popover") + .attr("data-bs-content", $(tooltip) + .html()) + .popover({ + html: true, + sanitize: false, + trigger: "hover" + }); + }); + }; -// Update times on the agenda based on the selected timezone -window.update_times = function (newtz) { - $('.current-tz') - .html(newtz); - $('.time') - .each(function () { - if (this.format == 4) { - var tz = this.start_ts.tz(newtz) - .format(" z"); - if (this.start_ts.tz(newtz) - .dayOfYear() == - this.end_ts.tz(newtz) - .dayOfYear()) { - $(this) - .html(format_time(this.start_ts, newtz, this.format) + - '
-' + format_time(this.end_ts, newtz, 5) + tz); + // Update times on the agenda based on the selected timezone + window.update_times = function (newtz) { + $('.current-tz') + .html(newtz); + $('.time') + .each(function () { + if (this.format == 4) { + var tz = this.start_ts.tz(newtz) + .format(" z"); + if (this.start_ts.tz(newtz) + .dayOfYear() == + this.end_ts.tz(newtz) + .dayOfYear()) { + $(this) + .html(format_time(this.start_ts, newtz, this.format) + + '
-' + format_time(this.end_ts, newtz, 5) + tz); + } else { + $(this) + .html(format_time(this.start_ts, newtz, this.format) + + '
-' + + format_time(this.end_ts, newtz, this.format) + tz); + } } else { $(this) - .html(format_time(this.start_ts, newtz, this.format) + - '
-' + - format_time(this.end_ts, newtz, this.format) + tz); + .html(format_time(this.start_ts, newtz, this.format) + '
-' + + format_time(this.end_ts, newtz, this.format)); } - } else { - $(this) - .html(format_time(this.start_ts, newtz, this.format) + '
-' + - format_time(this.end_ts, newtz, this.format)); - } - }); - update_tooltips_all(); - update_clock(); -}; + }); + update_tooltips_all(); + update_clock(); + }; -// Highlight ongoing based on the current time -window.highlight_ongoing = function () { - $("div#now") - .remove("#now"); - $('.table-warning') - .removeClass("table-warning"); - var agenda_rows = $('[data-slot-start-ts]'); - agenda_rows = agenda_rows.filter(function () { - return moment() - .isBetween(this.slot_start_ts, this.slot_end_ts); - }); - agenda_rows.addClass("table-warning"); - agenda_rows.first() - .children("th, td") - .prepend($('
')); -}; + // Highlight ongoing based on the current time + window.highlight_ongoing = function () { + $("div#now") + .remove("#now"); + $('.table-warning') + .removeClass("table-warning"); + var agenda_rows = $('[data-slot-start-ts]'); + agenda_rows = agenda_rows.filter(function () { + return moment() + .isBetween(this.slot_start_ts, this.slot_end_ts); + }); + agenda_rows.addClass("table-warning"); + agenda_rows.first() + .children("th, td") + .prepend($('
')); + }; -// Update tooltips -window.update_tooltips = function () { - var tooltips = $('.timetooltiptext'); - tooltips.filter(function () { + // Update tooltips + window.update_tooltips = function () { + var tooltips = $('.timetooltiptext'); + tooltips.filter(function () { return moment() .isBetween(this.ustart_ts, this.uend_ts); }) - .each(function () { + .each(function () { + $(this) + .html(format_tooltip_table(this.start_ts, this.end_ts)); + }); + }; + + // Update all tooltips + window.update_tooltips_all = function () { + var tooltips = $('.timetooltiptext'); + tooltips.each(function () { $(this) .html(format_tooltip_table(this.start_ts, this.end_ts)); }); -}; + }; -// Update all tooltips -window.update_tooltips_all = function () { - var tooltips = $('.timetooltiptext'); - tooltips.each(function () { - $(this) - .html(format_tooltip_table(this.start_ts, this.end_ts)); - }); -}; + // Update clock + window.update_clock = function () { + $('#current-time') + .html(format_time(moment(), get_current_tz_cb(), 0)); + }; -// Update clock -window.update_clock = function () { - $('#current-time') - .html(format_time(moment(), get_current_tz_cb(), 0)); -}; - -$.urlParam = function (name) { - var results = new RegExp('[\?&]' + name + '=([^&#]*)') - .exec(window.location.href); - if (results == null) { - return null; - } else { - return results[1] || 0; - } -}; + $.urlParam = function (name) { + var results = new RegExp('[\?&]' + name + '=([^&#]*)') + .exec(window.location.href); + if (results == null) { + return null; + } else { + return results[1] || 0; + } + }; -window.init_timers = function () { - var fast_timer = 60000 / (speedup > 600 ? 600 : speedup); - update_clock(); - highlight_ongoing(); - setInterval(function () { update_clock(); }, fast_timer); - setInterval(function () { highlight_ongoing(); }, fast_timer); - setInterval(function () { update_tooltips(); }, fast_timer); - setInterval(function () { update_tooltips_all(); }, 3600000 / speedup); -}; + window.init_timers = function () { + var fast_timer = 60000 / (speedup > 600 ? 600 : speedup); + update_clock(); + highlight_ongoing(); + setInterval(function () { update_clock(); }, fast_timer); + setInterval(function () { highlight_ongoing(); }, fast_timer); + setInterval(function () { update_tooltips(); }, fast_timer); + setInterval(function () { update_tooltips_all(); }, 3600000 / speedup); + }; -// set method used to find current time zone -window.set_current_tz_cb = function (fn) { - get_current_tz_cb = fn; -}; \ No newline at end of file + // set method used to find current time zone + window.set_current_tz_cb = function (fn) { + get_current_tz_cb = fn; + }; +})(); \ No newline at end of file From f1dbcd6a9a99cf5b24c65b058183f244c03fd4ab Mon Sep 17 00:00:00 2001 From: Jennifer Richards Date: Wed, 23 Mar 2022 13:50:50 -0300 Subject: [PATCH 02/13] refactor: separate function defs from adding to 'window' --- ietf/static/js/agenda_timezone.js | 49 +++++++++++++++++------------- ietf/templates/meeting/agenda.html | 6 ++-- 2 files changed, 31 insertions(+), 24 deletions(-) diff --git a/ietf/static/js/agenda_timezone.js b/ietf/static/js/agenda_timezone.js index a020c0e65a..4b3ff46caf 100644 --- a/ietf/static/js/agenda_timezone.js +++ b/ietf/static/js/agenda_timezone.js @@ -10,15 +10,15 @@ (function() { 'use strict'; - var local_timezone = moment.tz.guess(); + const local_timezone = moment.tz.guess(); // get_current_tz_cb must be overwritten using set_current_tz_cb - function get_current_tz_cb() { + let get_current_tz_cb = function() { throw new Error('Tried to get current timezone before callback registered. Use set_current_tz_cb().'); - } + }; // Initialize moments - window.initialize_moments = function () { + function initialize_moments() { var times = $('.time'); $.each(times, function (i, item) { item.start_ts = moment.unix(this.getAttribute("data-start-time")) @@ -41,7 +41,7 @@ item.slot_end_ts = moment.unix(this.getAttribute("data-slot-end-ts")) .utc(); }); - }; + } function format_time(t, tz, fmt) { var out; @@ -154,7 +154,7 @@ } // Add tooltips - window.add_tooltips = function () { + function add_tooltips() { $('.time') .each(function () { var tooltip = $(format_tooltip(this.start_ts, this.end_ts)); @@ -175,10 +175,10 @@ trigger: "hover" }); }); - }; + } // Update times on the agenda based on the selected timezone - window.update_times = function (newtz) { + function update_times(newtz) { $('.current-tz') .html(newtz); $('.time') @@ -207,10 +207,10 @@ }); update_tooltips_all(); update_clock(); - }; + } // Highlight ongoing based on the current time - window.highlight_ongoing = function () { + function highlight_ongoing() { $("div#now") .remove("#now"); $('.table-warning') @@ -224,10 +224,10 @@ agenda_rows.first() .children("th, td") .prepend($('
')); - }; + } // Update tooltips - window.update_tooltips = function () { + function update_tooltips() { var tooltips = $('.timetooltiptext'); tooltips.filter(function () { return moment() @@ -237,24 +237,24 @@ $(this) .html(format_tooltip_table(this.start_ts, this.end_ts)); }); - }; + } // Update all tooltips - window.update_tooltips_all = function () { + function update_tooltips_all() { var tooltips = $('.timetooltiptext'); tooltips.each(function () { $(this) .html(format_tooltip_table(this.start_ts, this.end_ts)); }); - }; + } // Update clock - window.update_clock = function () { + function update_clock() { $('#current-time') .html(format_time(moment(), get_current_tz_cb(), 0)); - }; + } - $.urlParam = function (name) { + function urlParam(name) { var results = new RegExp('[\?&]' + name + '=([^&#]*)') .exec(window.location.href); if (results == null) { @@ -262,9 +262,9 @@ } else { return results[1] || 0; } - }; + } - window.init_timers = function () { + function init_timers() { var fast_timer = 60000 / (speedup > 600 ? 600 : speedup); update_clock(); highlight_ongoing(); @@ -272,7 +272,14 @@ setInterval(function () { highlight_ongoing(); }, fast_timer); setInterval(function () { update_tooltips(); }, fast_timer); setInterval(function () { update_tooltips_all(); }, 3600000 / speedup); - }; + } + + /***** make public interface available on window *****/ + window.initialize_moments = initialize_moments; + window.add_tooltips = add_tooltips; + window.update_times = update_times; + window.urlParam = urlParam; + window.init_timers = init_timers; // set method used to find current time zone window.set_current_tz_cb = function (fn) { diff --git a/ietf/templates/meeting/agenda.html b/ietf/templates/meeting/agenda.html index 9fd2722405..a5b3f4c668 100644 --- a/ietf/templates/meeting/agenda.html +++ b/ietf/templates/meeting/agenda.html @@ -413,13 +413,13 @@

{% endif %}