Bug 1637133 - Refactor recurrence functions for summary dialog. r=darktrojan
authorPaul Morris <paul@thunderbird.net>
Wed, 20 May 2020 14:10:59 +0000
changeset 29627 120372d6f96bba97e59b455d7872a34900596fbd
parent 29626 3c657c39ada593b905bc11f53b4a6755018aab02
child 29628 cddacd682d3be5e327eed527cf36695ad7b1c375
push id17463
push usergeoff@darktrojan.net
push dateThu, 21 May 2020 03:30:07 +0000
treeherdercomm-central@c0c2cc7c1b7d [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersdarktrojan
bugs1637133
Bug 1637133 - Refactor recurrence functions for summary dialog. r=darktrojan Differential Revision: https://phabricator.services.mozilla.com/D74795
calendar/base/content/dialogs/calendar-summary-dialog.js
--- a/calendar/base/content/dialogs/calendar-summary-dialog.js
+++ b/calendar/base/content/dialogs/calendar-summary-dialog.js
@@ -136,17 +136,17 @@ function onLoad() {
   let supportsReminders =
     argCalendar.getProperty("capabilities.alarms.oninvitations.supported") !== false;
   if (!window.readOnly && supportsReminders) {
     document.getElementById("reminder-row").removeAttribute("hidden");
     loadReminders(window.calendarItem.getAlarms());
     updateReminder();
   }
 
-  updateRepeatDetails();
+  updateRecurrenceDetails(getRecurrenceString(item));
   updateAttendees();
   updateLink();
 
   let location = item.getProperty("LOCATION");
 
   if (location) {
     document.getElementById("location-row").removeAttribute("hidden");
     let urlMatch = location.match(/(https?:\/\/[^ ]*)/);
@@ -422,73 +422,83 @@ function updateToolbar() {
     );
   } else {
     gNotification.notificationbox.removeAllNotifications();
   }
 }
 
 /**
  * Updates the dialog w.r.t recurrence, i.e shows a text describing the item's
- * recurrence)
+ * recurrence.
+ *
+ * @param {string} details - Recurrence details as a string.
  */
-function updateRepeatDetails() {
-  let args = window.arguments[0];
-  let item = args.calendarEvent;
+function updateRecurrenceDetails(details) {
+  let repeatRow = document.getElementById("repeat-row");
+  let repeatDetails = document.getElementById("repeat-details");
 
-  // step to the parent (in order to show the
-  // recurrence info which is stored at the parent).
-  item = item.parentItem;
+  if (!details) {
+    repeatRow.setAttribute("hidden", "true");
+    repeatDetails.setAttribute("collapsed", "true");
 
-  // retrieve a valid recurrence rule from the currently
-  // set recurrence info. bail out if there's more
-  // than a single rule or something other than a rule.
-  let recurrenceInfo = item.recurrenceInfo;
-  if (!recurrenceInfo) {
+    while (repeatDetails.children.length) {
+      repeatDetails.lastChild.remove();
+    }
     return;
   }
 
-  document.getElementById("repeat-row").removeAttribute("hidden");
-
-  // First of all collapse the details text. If we fail to
-  // create a details string, we simply don't show anything.
-  // this could happen if the repeat rule is something exotic
-  // we don't have any strings prepared for.
-  let repeatDetails = document.getElementById("repeat-details");
-  repeatDetails.setAttribute("collapsed", "true");
+  repeatRow.removeAttribute("hidden");
+  repeatDetails.removeAttribute("collapsed");
 
-  // Try to create a descriptive string from the rule(s).
-  let kDefaultTimezone = cal.dtz.defaultTimezone;
-  let startDate = item.startDate || item.entryDate;
-  let endDate = item.endDate || item.dueDate;
-  startDate = startDate ? startDate.getInTimezone(kDefaultTimezone) : null;
-  endDate = endDate ? endDate.getInTimezone(kDefaultTimezone) : null;
-  let detailsString = recurrenceRule2String(recurrenceInfo, startDate, endDate, startDate.isDate);
+  let lines = details.split("\n");
 
-  if (!detailsString) {
-    detailsString = cal.l10n.getString("calendar-event-dialog", "ruleTooComplexSummary");
-  }
-
-  // Now display the string...
-  let lines = detailsString.split("\n");
-  repeatDetails.removeAttribute("collapsed");
   while (repeatDetails.children.length > lines.length) {
     repeatDetails.lastChild.remove();
   }
-  let numChilds = repeatDetails.children.length;
+  while (repeatDetails.children.length < lines.length) {
+    repeatDetails.appendChild(repeatDetails.firstElementChild.cloneNode(true));
+  }
   for (let i = 0; i < lines.length; i++) {
-    if (i >= numChilds) {
-      let newNode = repeatDetails.firstElementChild.cloneNode(true);
-      repeatDetails.appendChild(newNode);
-    }
     repeatDetails.children[i].value = lines[i];
-    repeatDetails.children[i].setAttribute("tooltiptext", detailsString);
+    repeatDetails.children[i].setAttribute("tooltiptext", details);
   }
 }
 
 /**
+ * Given a calendar event or task, return a string that describes the item's
+ * recurrence pattern, or null if there is no recurrence info.
+ *
+ * @param {calIItem} item - A calendar item.
+ * @return {string | null} A string describing the item's recurrence pattern or null.
+ */
+function getRecurrenceString(item) {
+  // Recurrence info is stored on the parent item.
+  let parent = item.parentItem;
+
+  let recurrenceInfo = parent.recurrenceInfo;
+  if (!recurrenceInfo) {
+    return null;
+  }
+
+  let kDefaultTimezone = cal.dtz.defaultTimezone;
+
+  let rawStartDate = parent.startDate || parent.entryDate;
+  let rawEndDate = parent.endDate || parent.dueDate;
+
+  let startDate = rawStartDate ? rawStartDate.getInTimezone(kDefaultTimezone) : null;
+  let endDate = rawEndDate ? rawEndDate.getInTimezone(kDefaultTimezone) : null;
+
+  let details =
+    recurrenceRule2String(recurrenceInfo, startDate, endDate, startDate.isDate) ||
+    cal.l10n.getString("calendar-event-dialog", "ruleTooComplexSummary");
+
+  return details;
+}
+
+/**
  * Updates the attendee listbox, displaying all attendees invited to the
  * window's item.
  */
 function updateAttendees() {
   if (window.attendees && window.attendees.length) {
     document.getElementById("item-attendees").removeAttribute("hidden");
     setupAttendees();
   }