Bug 1100801 - create a JS module for functions shared between addressbook and autocomplete. r=mkmelin, r=Neil

This commit is contained in:
aceman 2015-08-21 13:05:00 +02:00
Родитель 5d024ec5b0
Коммит c595e376a5
9 изменённых файлов: 96 добавлений и 176 удалений

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

@ -644,78 +644,6 @@ function GetSelectedDirectory()
}
}
/**
* There is an exact replica of this method in mailnews/.
* We need to remove this duplication with the help of a jsm in mailnews/
*
* Parse the multiword search string to extract individual search terms
* (separated on the basis of spaces) or quoted exact phrases to search
* against multiple fields of the addressbook cards.
*
* @param aSearchString The full search string entered by the user.
*
* @return an array of separated search terms from the full search string.
*/
function getSearchTokens(aSearchString)
{
let searchString = aSearchString.trim();
if (searchString == "")
return [];
let quotedTerms = [];
// Split up multiple search words to create a *foo* and *bar* search against
// search fields, using the OR-search template from modelQuery for each word.
// If the search query has quoted terms as "foo bar", extract them as is.
let startIndex;
while ((startIndex = searchString.indexOf('"')) != -1) {
let endIndex = searchString.indexOf('"', startIndex + 1);
if (endIndex == -1)
endIndex = searchString.length;
quotedTerms.push(searchString.substring(startIndex + 1, endIndex));
let query = searchString.substring(0, startIndex);
if (endIndex < searchString.length)
query += searchString.substr(endIndex + 1);
searchString = query.trim();
}
let searchWords = [];
if (searchString.length != 0) {
searchWords = quotedTerms.concat(searchString.split(/\s+/));
} else {
searchWords = quotedTerms;
}
return searchWords;
}
/*
* Given a database model query and a list of search tokens,
* return query URI.
*
* @param aModelQuery database model query
* @param aSearchWords an array of search tokens.
*
* @return query URI.
*/
function generateQueryURI(aModelQuery, aSearchWords)
{
// If there are no search tokens, we simply return an empty string.
if (!aSearchWords || aSearchWords.length == 0)
return "";
let queryURI = "";
aSearchWords.forEach(searchWord =>
queryURI += aModelQuery.replace(/@V/g, encodeABTermValue(searchWord)));
// queryURI has all the (or(...)) searches, link them up with (and(...)).
queryURI = "?(and" + queryURI + ")";
return queryURI;
}
function onAbClearSearch()
{
var searchInput = document.getElementById("peopleSearchInput");
@ -851,16 +779,6 @@ function makePhotoFile(aDir, aExtension) {
return newFile;
}
/**
* Encode the string passed as value into an addressbook search term.
* The '(' and ')' characters are special for the addressbook
* search query language, but are not escaped in encodeURIComponent()
* so must be done manually on top of it.
*/
function encodeABTermValue(aString) {
return encodeURIComponent(aString).replace(/\(/g, "%28").replace(/\)/g, "%29");
}
/**
* Validates the given year and returns it, if it looks sane.
* Returns kDefaultYear (a leap year), if no valid date is given.

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

@ -4,13 +4,14 @@
* 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/. */
Components.utils.import("resource:///modules/ABQueryUtils.jsm");
function GetAbViewListener()
{
// the ab panel doesn't care if the total changes, or if the selection changes
return null;
}
function contactsListOnClick(event)
{
CommandUpdate_AddressBook();

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

@ -8,6 +8,7 @@
Components.utils.import("resource://gre/modules/Services.jsm");
// Ensure the activity modules are loaded for this window.
Components.utils.import("resource:///modules/activity/activityModules.js");
Components.utils.import("resource:///modules/ABQueryUtils.jsm");
Components.utils.import("resource:///modules/mailServices.js");
const nsIAbListener = Components.interfaces.nsIAbListener;

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

@ -4,6 +4,7 @@
Components.utils.import("resource://gre/modules/Services.jsm");
Components.utils.import("resource:///modules/mailServices.js");
Components.utils.import("resource:///modules/ABQueryUtils.jsm");
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
const ACR = Components.interfaces.nsIAutoCompleteResult;
@ -439,89 +440,6 @@ nsAbAutoCompleteSearch.prototype = {
.nsIAutoCompleteSearch])
};
/**
* Encode the string passed as value into an addressbook search term.
* The '(' and ')' characters are special for the addressbook
* search query language, but are not escaped in encodeURIComponent()
* so it must be done manually on top of it.
*/
function encodeABTermValue(aString) {
return encodeURIComponent(aString).replace(/\(/g, "%28").replace(/\)/g, "%29");
}
/**
* This is an exact replica of the method in abCommon.js and needs to
* be merged to remove this duplication.
*
* Parse the multiword search string to extract individual search terms
* (separated on the basis of spaces) or quoted exact phrases to search
* against multiple fields of the addressbook cards.
*
* @param aSearchString The full search string entered by the user.
*
* @return an array of separated search terms from the full search string.
*/
function getSearchTokens(aSearchString)
{
let searchString = aSearchString.trim();
if (searchString == "")
return [];
let quotedTerms = [];
// Split up multiple search words to create a *foo* and *bar* search against
// search fields, using the OR-search template from modelQuery for each word.
// If the search query has quoted terms as "foo bar", extract them as is.
let startIndex;
while ((startIndex = searchString.indexOf('"')) != -1) {
let endIndex = searchString.indexOf('"', startIndex + 1);
if (endIndex == -1)
endIndex = searchString.length;
quotedTerms.push(searchString.substring(startIndex + 1, endIndex));
let query = searchString.substring(0, startIndex);
if (endIndex < searchString.length)
query += searchString.substr(endIndex + 1);
searchString = query.trim();
}
let searchWords = [];
if (searchString.length != 0) {
searchWords = quotedTerms.concat(searchString.split(/\s+/));
} else {
searchWords = quotedTerms;
}
return searchWords;
}
/*
* Given a database model query and a list of search tokens,
* return query URI.
*
* @param aModelQuery database model query
* @param aSearchWords an array of search tokens.
*
* @return query URI.
*/
function generateQueryURI(aModelQuery, aSearchWords)
{
// If there are no search tokens, we simply return an empty string.
if (!aSearchWords || aSearchWords.length == 0)
return "";
let queryURI = "";
aSearchWords.forEach(searchWord =>
queryURI += aModelQuery.replace(/@V/g, encodeABTermValue(searchWord)));
// queryURI has all the (or(...)) searches, link them up with (and(...)).
queryURI = "?(and" + queryURI + ")";
return queryURI;
}
// Module
var components = [nsAbAutoCompleteSearch];

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

@ -0,0 +1,87 @@
/* 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/. */
/**
* This file contains helper methods for dealing with addressbook search URIs.
*/
const EXPORTED_SYMBOLS = ["getSearchTokens", "generateQueryURI", "encodeABTermValue"];
/**
* Parse the multiword search string to extract individual search terms
* (separated on the basis of spaces) or quoted exact phrases to search
* against multiple fields of the addressbook cards.
*
* @param aSearchString The full search string entered by the user.
*
* @return an array of separated search terms from the full search string.
*/
function getSearchTokens(aSearchString) {
let searchString = aSearchString.trim();
if (searchString == "")
return [];
let quotedTerms = [];
// Split up multiple search words to create a *foo* and *bar* search against
// search fields, using the OR-search template from modelQuery for each word.
// If the search query has quoted terms as "foo bar", extract them as is.
let startIndex;
while ((startIndex = searchString.indexOf('"')) != -1) {
let endIndex = searchString.indexOf('"', startIndex + 1);
if (endIndex == -1)
endIndex = searchString.length;
quotedTerms.push(searchString.substring(startIndex + 1, endIndex));
let query = searchString.substring(0, startIndex);
if (endIndex < searchString.length)
query += searchString.substr(endIndex + 1);
searchString = query.trim();
}
let searchWords = [];
if (searchString.length != 0) {
searchWords = quotedTerms.concat(searchString.split(/\s+/));
} else {
searchWords = quotedTerms;
}
return searchWords;
}
/*
* Given a database model query and a list of search tokens,
* return query URI.
*
* @param aModelQuery database model query
* @param aSearchWords an array of search tokens.
*
* @return query URI.
*/
function generateQueryURI(aModelQuery, aSearchWords) {
// If there are no search tokens, we simply return an empty string.
if (!aSearchWords || aSearchWords.length == 0)
return "";
let queryURI = "";
aSearchWords.forEach(searchWord =>
queryURI += aModelQuery.replace(/@V/g, encodeABTermValue(searchWord)));
// queryURI has all the (or(...)) searches, link them up with (and(...)).
queryURI = "?(and" + queryURI + ")";
return queryURI;
}
/**
* Encode the string passed as value into an addressbook search term.
* The '(' and ')' characters are special for the addressbook
* search query language, but are not escaped in encodeURIComponent()
* so must be done manually on top of it.
*/
function encodeABTermValue(aString) {
return encodeURIComponent(aString).replace(/\(/g, "%28").replace(/\)/g, "%29");
}

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

@ -49,6 +49,7 @@ SOURCES += [
]
EXTRA_JS_MODULES += [
'ABQueryUtils.jsm',
'errUtils.js',
'folderUtils.jsm',
'hostnameUtils.jsm',

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

@ -729,16 +729,6 @@ function makePhotoFile(aDir, aExtension) {
return newFile;
}
/**
* Encode the string passed as value into an addressbook search term.
* The '(' and ')' characters are special for the addressbook
* search query language, but are not escaped in encodeURIComponent()
* so must be done manually on top of it.
*/
function encodeABTermValue(aString) {
return encodeURIComponent(aString).replace(/\(/g, "%28").replace(/\)/g, "%29");
}
/**
* Validates the given year and returns it, if it looks sane.
* Returns kDefaultYear (a leap year), if no valid date is given.

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

@ -3,6 +3,8 @@
* 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/. */
Components.utils.import("resource:///modules/ABQueryUtils.jsm");
var addressbook = 0;
var composeWindow = 0;
var msgCompFields = 0;

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

@ -2,6 +2,8 @@
* 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/. */
Components.utils.import("resource:///modules/ABQueryUtils.jsm");
const nsIAbListener = Components.interfaces.nsIAbListener;
const kPrefMailAddrBookLastNameFirst = "mail.addr_book.lastnamefirst";
const kPersistCollapseMapStorage = "directoryTree.json";