calendar/import-export/CalHtmlExport.jsm
author Mozilla Releng Treescript <release+treescript@mozilla.org>
Mon, 01 Jun 2020 21:06:52 +0000
changeset 38423 a72e4d7908e0d4da3cfb13e0db7eee2d478269d3
parent 38270 39a884a7b19acd946dedf75a441f0d14631053eb
child 39075 18f27bc33168c576676908b2a9cdb34707fb89e2
permissions -rw-r--r--
no bug - Bumping Thunderbird l10n changesets r=release a=l10n-bump CLOSED TREE af -> removed ar -> 13f4e5c63aed ast -> 06238bcd1d71 be -> 2a2c4bfa536c bg -> b066986baad7 br -> 77130fec77d1 ca -> 05263001b4fb cak -> 34cffcb6f7ca cs -> 7d6080744017 cy -> 74a71abffa27 da -> d79d59042826 de -> 23f2aa4ae3de dsb -> c7548879c855 el -> e7b6bf516f62 en-GB -> 64789d6d3264 es-AR -> 44ce7b8a21f6 es-ES -> e131e33378bd et -> af1beec7a2c5 eu -> dff4a7c14438 fa -> removed fi -> b04da68482de fr -> 7e2320d07b94 fy-NL -> f43fd875b36b ga-IE -> 56b7bb91b552 gd -> 94b469991a3b gl -> 228a11d8ba8c he -> ed93e84e5416 hr -> 037459e37cc3 hsb -> 192b76cd4451 hu -> 92669fa62f5a hy-AM -> 49a2e6124917 id -> 240f090ba92f is -> 7cda20fa902a it -> ec692ed62c10 ja -> 5eed784b3cd9 ja-JP-mac -> e37f1ed0ff6d ka -> 4ce3c0e3feb7 kab -> 6ea74b301cc8 kk -> 95ec87f8788e ko -> ed50767d5b05 lt -> d71a8aa38738 ms -> ac99de934f0c nb-NO -> 4c42b2eb457c nl -> f31b19039101 nn-NO -> e2d68d6fd0eb pa-IN -> removed pl -> d6453ed2bb87 pt-BR -> ad22cae34261 pt-PT -> 693f4dace00c rm -> 80d37050607e ro -> a5af9f2e8f1d ru -> 3a82831ae1d9 si -> 55e501a4e096 sk -> d1c8c57d517d sl -> ecae5ba0d9b7 sq -> b9ad05079398 sr -> f15c2fb5fd90 sv-SE -> 21c851897e57 th -> removed tr -> dc70d34d90d1 uk -> bbfae865d18b uz -> 3a9d7e55dbe9 vi -> 20973eb20233 zh-CN -> d9fa062cd366 zh-TW -> 62ce156b3e49

/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

var EXPORTED_SYMBOLS = ["CalHtmlExporter"];

var { cal } = ChromeUtils.import("resource:///modules/calendar/calUtils.jsm");

/**
 * HTML Export Plugin
 */
function CalHtmlExporter() {
  this.wrappedJSObject = this;
}

CalHtmlExporter.prototype = {
  QueryInterface: ChromeUtils.generateQI([Ci.calIExporter]),
  classID: Components.ID("{72d9ab35-9b1b-442a-8cd0-ae49f00b159b}"),

  getFileTypes() {
    let wildmat = "*.html; *.htm";
    let label = cal.l10n.getCalString("filterHtml", [wildmat]);
    return [
      {
        QueryInterface: ChromeUtils.generateQI([Ci.calIFileType]),
        defaultExtension: "html",
        extensionFilter: wildmat,
        description: label,
      },
    ];
  },

  exportToStream(aStream, aItems, aTitle) {
    let document = cal.xml.parseFile("chrome://calendar/content/printing/calHtmlExport.html");
    let itemContainer = document.getElementById("item-container");
    document.getElementById("title").textContent = aTitle || cal.l10n.getCalString("HTMLTitle");

    // Sort aItems
    aItems.sort((a, b) => {
      let start_a = a[cal.dtz.startDateProp(a)];
      if (!start_a) {
        return -1;
      }
      let start_b = b[cal.dtz.startDateProp(b)];
      if (!start_b) {
        return 1;
      }
      return start_a.compare(start_b);
    });

    for (let item of aItems) {
      let itemNode = document.getElementById("item-template").cloneNode(true);
      itemNode.removeAttribute("id");

      let setupTextRow = function(classKey, propValue, prefixKey) {
        if (propValue) {
          let prefix = cal.l10n.getCalString(prefixKey);
          itemNode.querySelector("." + classKey + "key").textContent = prefix;
          itemNode.querySelector("." + classKey).textContent = propValue;
        } else {
          let row = itemNode.querySelector("." + classKey + "row");
          if (
            row.nextSibling.nodeType == row.nextSibling.TEXT_NODE ||
            row.nextSibling.nodeType == row.nextSibling.CDATA_SECTION_NODE
          ) {
            row.nextSibling.remove();
          }
          row.remove();
        }
      };

      let startDate = item[cal.dtz.startDateProp(item)];
      let endDate = item[cal.dtz.endDateProp(item)];
      if (startDate || endDate) {
        // This is a task with a start or due date, format accordingly
        let prefixWhen = cal.l10n.getCalString("htmlPrefixWhen");
        itemNode.querySelector(".intervalkey").textContent = prefixWhen;

        let startNode = itemNode.querySelector(".dtstart");
        let dateString = cal.dtz.formatter.formatItemInterval(item);
        startNode.setAttribute("title", startDate ? startDate.icalString : "none");
        startNode.textContent = dateString;
      } else {
        let row = itemNode.querySelector(".intervalrow");
        row.remove();
        if (
          row.nextSibling &&
          (row.nextSibling.nodeType == row.nextSibling.TEXT_NODE ||
            row.nextSibling.nodeType == row.nextSibling.CDATA_SECTION_NODE)
        ) {
          row.nextSibling.remove();
        }
      }

      let itemTitle = item.isCompleted
        ? cal.l10n.getCalString("htmlTaskCompleted", [item.title])
        : item.title;
      setupTextRow("summary", itemTitle, "htmlPrefixTitle");

      setupTextRow("location", item.getProperty("LOCATION"), "htmlPrefixLocation");
      setupTextRow("description", item.getProperty("DESCRIPTION"), "htmlPrefixDescription");

      itemContainer.appendChild(itemNode);
    }

    let templates = document.getElementById("templates");
    templates.remove();

    // Convert the javascript string to an array of bytes, using the utf8 encoder
    let convStream = Cc["@mozilla.org/intl/converter-output-stream;1"].createInstance(
      Ci.nsIConverterOutputStream
    );
    convStream.init(aStream, "UTF-8");
    convStream.writeString(cal.xml.serializeDOM(document));
  },
};