Bug 1542717 - [de-xbl] convert map-list binding to <menupopup is='map-list'>. r=mkmelin

This commit is contained in:
Khushil Mistry 2019-04-14 00:02:55 +02:00
Родитель 0b758bf53d
Коммит b127c980bd
5 изменённых файлов: 202 добавлений и 244 удалений

Просмотреть файл

@ -79,10 +79,6 @@ menupopup[type="folder"] {
-moz-binding: url("chrome://messenger/content/addressbook/addrbookWidgets.xml#addrbooks-menupopup");
}
.map-list {
-moz-binding: url("chrome://messenger/content/addressbook/addrbookWidgets.xml#map-list");
}
.chromeclass-toolbar {
overflow-x: hidden;
}

Просмотреть файл

@ -46,6 +46,7 @@
<script type="application/javascript" src="chrome://messenger/content/mailCore.js"/>
<script type="application/javascript" src="chrome://messenger/content/mail-compacttheme.js"/>
<script type="application/javascript" src="chrome://messenger/content/addressbook/addressbook.js"/>
<script type="application/javascript" src="chrome://messenger/content/addressbook/map-list.js"/>
<script type="application/javascript" src="chrome://messenger/content/addressbook/abCommon.js"/>
<script type="application/javascript" src="chrome://communicator/content/contentAreaClick.js"/>
<script type="application/javascript" src="chrome://global/content/printUtils.js"/>
@ -817,7 +818,7 @@
type="menu-button"
oncommand="openLinkWithUrl(this.firstChild.mapURL);"
tooltiptext="&mapIt.tooltip;">
<menupopup class="map-list"/>
<menupopup is="map-list"/>
</button>
</vbox>
</hbox>
@ -886,7 +887,7 @@
type="menu-button"
oncommand="openLinkWithUrl(this.firstChild.mapURL);"
tooltiptext="&mapIt.tooltip;">
<menupopup class="map-list"/>
<menupopup is="map-list"/>
</button>
</vbox>
</hbox>

Просмотреть файл

@ -295,242 +295,4 @@
</method>
</implementation>
</binding>
<binding id="map-list"
extends="chrome://global/content/bindings/popup.xml#popup">
<implementation>
<property name="mapURL" readonly="true">
<getter><![CDATA[
return this._createMapItURL();
]]></getter>
</property>
<constructor>
<![CDATA[
this._setWidgetDisabled(true);
]]>
</constructor>
<!--
Initializes the necessary address data from an addressbook card.
@param aCard A nsIAbCard to get the address data from.
@param aAddrPrefix A prefix of the card properties to use. Use "Home" or "Work".
-->
<method name="initMapAddressFromCard">
<parameter name="aCard"/>
<parameter name="aAddrPrefix"/>
<body><![CDATA[
let mapItURLFormat = this._getMapURLPref(0);
let doNotShowMap = !mapItURLFormat || !aAddrPrefix || !aCard;
this._setWidgetDisabled(doNotShowMap);
if (doNotShowMap)
return;
this.setAttribute("map_address1", aCard.getProperty(aAddrPrefix + "Address"));
this.setAttribute("map_address2", aCard.getProperty(aAddrPrefix + "Address2"));
this.setAttribute("map_city", aCard.getProperty(aAddrPrefix + "City"));
this.setAttribute("map_state", aCard.getProperty(aAddrPrefix + "State"));
this.setAttribute("map_zip", aCard.getProperty(aAddrPrefix + "ZipCode"));
this.setAttribute("map_country", aCard.getProperty(aAddrPrefix + "Country"));
]]></body>
</method>
<!--
Initializes the necessary address data from passed in values.
-->
<method name="initMapAddress">
<parameter name="aAddr1"/>
<parameter name="aAddr2"/>
<parameter name="aCity"/>
<parameter name="aState"/>
<parameter name="aZip"/>
<parameter name="aCountry"/>
<body><![CDATA[
let mapItURLFormat = this._getMapURLPref(0);
let doNotShowMap = !mapItURLFormat || !(aAddr1 + aAddr2 + aCity + aState + aZip + aCountry);
this._setWidgetDisabled(doNotShowMap);
if (doNotShowMap)
return;
this.setAttribute("map_address1", aAddr1);
this.setAttribute("map_address2", aAddr2);
this.setAttribute("map_city", aCity);
this.setAttribute("map_state", aState);
this.setAttribute("map_zip", aZip);
this.setAttribute("map_country", aCountry);
]]></body>
</method>
<!--
Sets the disabled/enabled state of the parent widget (e.g. a button).
-->
<method name="_setWidgetDisabled">
<parameter name="aDisabled"/>
<body><![CDATA[
this.parentNode.disabled = aDisabled;
]]></body>
</method>
<!--
Returns the Map service URL from localized pref. Returns null if there
is none at the given index.
@param aIndex The index of the service to return. 0 is the default service.
-->
<method name="_getMapURLPref">
<parameter name="aIndex"/>
<body><![CDATA[
const {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");
let url = null;
if (!aIndex) {
url = Services.prefs.getComplexValue("mail.addr_book.mapit_url.format",
Ci.nsIPrefLocalizedString).data;
} else {
try {
url = Services.prefs.getComplexValue("mail.addr_book.mapit_url." + aIndex + ".format",
Ci.nsIPrefLocalizedString).data;
} catch (e) {
}
}
return url;
]]></body>
</method>
<!--
Builds menuitem elements representing map services defined in prefs
and attaches them to the specified button.
-->
<method name="_listMapServices">
<body><![CDATA[
const {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");
let index = 1;
let itemFound = true;
let defaultFound = false;
const kUserIndex = 100;
let aMapList = this;
while (aMapList.hasChildNodes()) {
aMapList.lastChild.remove();
}
let defaultUrl = this._getMapURLPref(0);
// Creates the menuitem with supplied data.
function addMapService(aUrl, aName) {
let item = document.createElement("menuitem");
item.setAttribute("url", aUrl);
item.setAttribute("label", aName);
item.setAttribute("type", "radio");
item.setAttribute("name", "mapit_service");
if (aUrl == defaultUrl)
item.setAttribute("checked", "true");
aMapList.appendChild(item);
}
// Generates a useful generic name by cutting out only the host address.
function generateName(aUrl) {
return new URL(aUrl).hostname;
}
// Add all defined map services as menuitems.
while (itemFound) {
let urlName;
let urlTemplate = this._getMapURLPref(index);
if (!urlTemplate) {
itemFound = false;
} else {
// Name is not mandatory, generate one if not found.
try {
urlName = Services.prefs.getComplexValue("mail.addr_book.mapit_url." + index + ".name",
Ci.nsIPrefLocalizedString).data;
} catch (e) {
urlName = generateName(urlTemplate);
}
}
if (itemFound) {
addMapService(urlTemplate, urlName);
index++;
if (urlTemplate == defaultUrl)
defaultFound = true;
} else if (index < kUserIndex) {
// After iterating the base region provided urls, check for user defined ones.
index = kUserIndex;
itemFound = true;
}
}
if (!defaultFound) {
// If user had put a customized map URL into mail.addr_book.mapit_url.format
// preserve it as a new map service named with the URL.
// 'index' now points to the first unused entry in prefs.
let defaultName = generateName(defaultUrl);
addMapService(defaultUrl, defaultName);
Services.prefs.setCharPref("mail.addr_book.mapit_url." + index + ".format",
defaultUrl);
Services.prefs.setCharPref("mail.addr_book.mapit_url." + index + ".name",
defaultName);
}
]]></body>
</method>
<!--
Save user selected mapping service.
@param aItem The chosen menuitem with map service.
-->
<method name="_chooseMapService">
<parameter name="aItem"/>
<body><![CDATA[
const {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");
// Save selected URL as the default.
let defaultUrl = Cc["@mozilla.org/pref-localizedstring;1"]
.createInstance(Ci.nsIPrefLocalizedString);
defaultUrl.data = aItem.getAttribute("url");
Services.prefs.setComplexValue("mail.addr_book.mapit_url.format",
Ci.nsIPrefLocalizedString, defaultUrl);
]]></body>
</method>
<!--
Generate map URL in the href attribute.
-->
<method name="_createMapItURL">
<body><![CDATA[
let urlFormat = this._getMapURLPref(0);
if (!urlFormat)
return null;
let address1 = this.getAttribute("map_address1");
let address2 = this.getAttribute("map_address2");
let city = this.getAttribute("map_city");
let state = this.getAttribute("map_state");
let zip = this.getAttribute("map_zip");
let country = this.getAttribute("map_country");
urlFormat = urlFormat.replace("@A1", encodeURIComponent(address1));
urlFormat = urlFormat.replace("@A2", encodeURIComponent(address2));
urlFormat = urlFormat.replace("@CI", encodeURIComponent(city));
urlFormat = urlFormat.replace("@ST", encodeURIComponent(state));
urlFormat = urlFormat.replace("@ZI", encodeURIComponent(zip));
urlFormat = urlFormat.replace("@CO", encodeURIComponent(country));
return urlFormat;
]]></body>
</method>
</implementation>
<handlers>
<handler event="command">
<![CDATA[
this._chooseMapService(event.target);
event.stopPropagation();
]]>
</handler>
<handler event="popupshowing">
<![CDATA[
this._listMapServices();
]]>
</handler>
</handlers>
</binding>
</bindings>

Просмотреть файл

@ -0,0 +1,198 @@
/* 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/. */
"use strict";
/* global MozElements */
{
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
/**
* The MozMapList widget behaves as a popup menu showing available map options
* for an address. It is a part of the card view in the addressbook.
*
* @extends {MozElements.MozMenuPopup}
*/
class MozMapList extends MozElements.MozMenuPopup {
connectedCallback() {
if (this.delayConnectedCallback() || this.hasConnected) {
return;
}
this.setAttribute("is", "map-list");
this.addEventListener("command", (event) => {
this._chooseMapService(event.target);
event.stopPropagation();
});
this.addEventListener("popupshowing", (event) => {
this._listMapServices();
});
this._setWidgetDisabled(true);
}
get mapURL() {
return this._createMapItURL();
}
/**
* Initializes the necessary address data from an addressbook card.
* @param {nsIAbCard} card - the card to get the addess data from
* @param {string} addPrefix - card property prefix: "Home" or "Work",
* to make the map use either HomeAddress
* or WorkAddress
*/
initMapAddressFromCard(card, addrPrefix) {
let mapItURLFormat = this._getMapURLPref();
let doNotShowMap = !mapItURLFormat || !addrPrefix || !card;
this._setWidgetDisabled(doNotShowMap);
if (doNotShowMap) {
return;
}
this.address1 = card.getProperty(addrPrefix + "Address");
this.address2 = card.getProperty(addrPrefix + "Address2");
this.city = card.getProperty(addrPrefix + "City");
this._state = card.getProperty(addrPrefix + "State");
this.zip = card.getProperty(addrPrefix + "ZipCode");
this.country = card.getProperty(addrPrefix + "Country");
}
/**
* Sets the disabled/enabled state of the parent widget (e.g. a button).
*/
_setWidgetDisabled(disabled) {
this.parentNode.disabled = disabled;
}
/**
* Returns the Map service URL from localized pref. Returns null if there
* is none at the given index.
* @param integer [index=0] - the index of the service to return. 0 is the default service.
*/
_getMapURLPref(index = 0) {
let url = null;
if (!index) {
url = Services.prefs.getComplexValue("mail.addr_book.mapit_url.format",
Ci.nsIPrefLocalizedString).data;
} else {
try {
url = Services.prefs.getComplexValue("mail.addr_book.mapit_url." + index + ".format",
Ci.nsIPrefLocalizedString).data;
} catch (e) { }
}
return url;
}
/**
* Builds menuitem elements representing map services defined in prefs
* and attaches them to the specified button.
*/
_listMapServices() {
let index = 1;
let itemFound = true;
let defaultFound = false;
const kUserIndex = 100;
let mapList = this;
while (mapList.hasChildNodes()) {
mapList.lastChild.remove();
}
let defaultUrl = this._getMapURLPref();
// Creates the menuitem with supplied data.
function addMapService(url, name) {
let item = document.createElement("menuitem");
item.setAttribute("url", url);
item.setAttribute("label", name);
item.setAttribute("type", "radio");
item.setAttribute("name", "mapit_service");
if (url == defaultUrl) {
item.setAttribute("checked", "true");
}
mapList.appendChild(item);
}
// Generates a useful generic name by cutting out only the host address.
function generateName(url) {
return new URL(url).hostname;
}
// Add all defined map services as menuitems.
while (itemFound) {
let urlName;
let urlTemplate = this._getMapURLPref(index);
if (!urlTemplate) {
itemFound = false;
} else {
// Name is not mandatory, generate one if not found.
try {
urlName = Services.prefs.getComplexValue("mail.addr_book.mapit_url." + index + ".name",
Ci.nsIPrefLocalizedString).data;
} catch (e) {
urlName = generateName(urlTemplate);
}
}
if (itemFound) {
addMapService(urlTemplate, urlName);
index++;
if (urlTemplate == defaultUrl)
defaultFound = true;
} else if (index < kUserIndex) {
// After iterating the base region provided urls, check for user defined ones.
index = kUserIndex;
itemFound = true;
}
}
if (!defaultFound) {
// If user had put a customized map URL into mail.addr_book.mapit_url.format
// preserve it as a new map service named with the URL.
// 'index' now points to the first unused entry in prefs.
let defaultName = generateName(defaultUrl);
addMapService(defaultUrl, defaultName);
Services.prefs.setCharPref("mail.addr_book.mapit_url." + index + ".format",
defaultUrl);
Services.prefs.setCharPref("mail.addr_book.mapit_url." + index + ".name",
defaultName);
}
}
/**
* Save user selected mapping service.
* @param item The chosen menuitem with map service.
*/
_chooseMapService(item) {
// Save selected URL as the default.
let defaultUrl = Cc["@mozilla.org/pref-localizedstring;1"]
.createInstance(Ci.nsIPrefLocalizedString);
defaultUrl.data = item.getAttribute("url");
Services.prefs.setComplexValue("mail.addr_book.mapit_url.format",
Ci.nsIPrefLocalizedString, defaultUrl);
}
/**
* Generate the map URL used to open the link on clicking the menulist button.
* @returns {urlFormat} - the map url generated from the address.
*/
_createMapItURL() {
let urlFormat = this._getMapURLPref();
if (!urlFormat) {
return null;
}
urlFormat = urlFormat.replace("@A1", encodeURIComponent(this.address1));
urlFormat = urlFormat.replace("@A2", encodeURIComponent(this.address2));
urlFormat = urlFormat.replace("@CI", encodeURIComponent(this.city));
urlFormat = urlFormat.replace("@ST", encodeURIComponent(this._state));
urlFormat = urlFormat.replace("@ZI", encodeURIComponent(this.zip));
urlFormat = urlFormat.replace("@CO", encodeURIComponent(this.country));
return urlFormat;
}
}
customElements.define("map-list", MozMapList, { "extends": "menupopup" });
}

Просмотреть файл

@ -13,6 +13,7 @@ messenger.jar:
content/messenger/addressbook/addrbookWidgets.xml (addrbook/content/addrbookWidgets.xml)
content/messenger/addressbook/abDragDrop.js (addrbook/content/abDragDrop.js)
content/messenger/addressbook/abMailListDialog.js (addrbook/content/abMailListDialog.js)
content/messenger/addressbook/map-list.js (addrbook/content/map-list.js)
content/messagebody/addressbook/print.css (addrbook/content/print.css)
* content/messenger/AccountManager.xul (base/prefs/content/AccountManager.xul)
content/messenger/AccountManager.js (base/prefs/content/AccountManager.js)