home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World 2003 May
/
PCWorld_2003-05_cd.bin
/
Komunik
/
phoenix
/
chrome
/
browser.jar
/
content
/
browser
/
bookmarks
/
addBookmark2.js
< prev
next >
Wrap
Text File
|
2002-10-13
|
6KB
|
173 lines
/**
* Add Bookmark Dialog.
* ====================
*
* This is a generic bookmark dialog that allows for bookmark addition
* and folder selection. It can be opened with various parameters that
* result in appearance/purpose differences and initial state.
*
* Use: Open with 'openDialog', with the flags
* 'centerscreen,chrome,dialog=no,resizable=yes'
*
* Parameters:
* Apart from the standard openDialog parameters, this dialog can
* be passed additional information, which gets mapped to the
* window.arguments array:
*
* window.arguments[0]: Bookmark Name. The value to be prefilled
* into the "Name: " field (if visible).
* window.arguments[1]: Bookmark URL: The location of the bookmark.
* The value to be filled in the "Location: "
* field (if visible).
* window.arguments[2]: Bookmark Folder. The RDF Resource URI of the
* folder that this bookmark should be created in.
* window.arguments[3]: Bookmark Charset. The charset that should be
* used when adding a bookmark to the specified
* URL. (Usually the charset of the current
* document when launching this window).
* window.arguments[4]: The mode of operation. See notes for details.
* window.arguments[5]: If the mode is "addGroup", this is an array
* of objects with name, URL and charset
* properties, one for each group member.
*/
var gSelectedFolder;
var gName;
var gGroup;
var gList;
var gIndentation; // temporary hack to indent the folders
var gNameArc;
function Startup()
{
initServices();
initBMService();
gNameArc = RDF.GetResource(NC_NS+"Name");
gName = document.getElementById("name");
gGroup = document.getElementById("addgroup");
gList = document.getElementById("select-menu");
gName.value = window.arguments[0];
gName.select();
gName.focus();
onFieldInput();
setTimeout(fillSelectFolderMenupopup, 0);
gIndentation = Array(16);
gIndentation[0] = "";
for (var i=1; i<16; ++i)
gIndentation[i]=gIndentation[i-1]+" ";
}
function onFieldInput()
{
const ok = document.documentElement.getButton("accept");
ok.disabled = gName.value == "";
}
function onOK()
{
document.getElementById("addBookmarkDialog").setAttribute("selectedFolder",gSelectedFolder);
var rFolder = RDF.GetResource(gSelectedFolder);
RDFC.Init(BMDS, rFolder);
var url, rSource;
if (gGroup && gGroup.checked) {
rSource = BMDS.createFolder(gName.value);
const groups = window.arguments[5];
for (var i = 0; i < groups.length; ++i) {
url = getNormalizedURL(groups[i].url);
BMDS.createBookmarkInContainer(groups[i].name, url,
groups[i].charset, rSource, -1);
}
} else {
url = getNormalizedURL(window.arguments[1]);
rSource = BMDS.createBookmark(gName.value, url, window.arguments[3]);
}
if (!gBMtxmgr)
gBMtxmgr= BookmarksUtils.getTransactionManager();
var selection, target;
selection = BookmarksUtils.getSelectionFromResource(rSource);
target = BookmarksUtils.getSelectionFromResource(rFolder);
target = BookmarksUtils.getTargetFromSelection(target);
BookmarksUtils.insertSelection("newbookmark", selection, target);
// in insertSelection, the ds flush is delayed. It will never be performed,
// since this dialog is destroyed before.
// We have to flush manually
var remoteDS = BMDS.QueryInterface(Components.interfaces.nsIRDFRemoteDataSource);
remoteDS.Flush();
}
function getNormalizedURL(url)
{
// Check to see if the item is a local directory path, and if so, convert
// to a file URL so that aggregation with rdf:files works
try {
const kLF = Components.classes["@mozilla.org/file/local;1"]
.createInstance(Components.interfaces.nsILocalFile);
kLF.initWithPath(url);
if (kLF.exists()) {
var ioService = Components.classes["@mozilla.org/network/io-service;1"]
.getService(Components.interfaces.nsIIOService);
var fileHandler = ioService.getProtocolHandler("file")
.QueryInterface(Components.interfaces.nsIFileProtocolHandler);
url = fileHandler.getURLSpecFromFile(kLF);
}
}
catch (e) {
}
return url;
}
function fillFolder(aPopup, aFolder, aDepth)
{
RDFC.Init(BMDS, aFolder);
var children = RDFC.GetElements();
while (children.hasMoreElements()) {
var curr = children.getNext();
if (RDFCU.IsContainer(BMDS, curr)) {
curr = curr.QueryInterface(Components.interfaces.nsIRDFResource);
var element = document.createElementNS(XUL_NS, "menuitem");
var name = BMDS.GetTarget(curr, gNameArc, true).QueryInterface(kRDFLITIID).Value;
element.setAttribute("label", gIndentation[aDepth]+name);
element.setAttribute("id", curr.Value);
aPopup.appendChild(element);
if (curr.Value == gSelectedFolder)
gList.selectedItem = element;
fillFolder(aPopup, curr, ++aDepth);
--aDepth;
}
}
}
function fillSelectFolderMenupopup ()
{
gSelectedFolder = document.getElementById("addBookmarkDialog").getAttribute("selectedFolder");
var popup = document.getElementById("select-folder");
// clearing the old menupopup
while (popup.hasChildNodes())
popup.removeChild(popup.firstChild);
// to be removed once I checkin the top folder
var element = document.createElementNS(XUL_NS, "menuitem");
element.setAttribute("label", "Bookmarks");
element.setAttribute("id", "NC:BookmarksRoot");
popup.appendChild(element);
var folder = RDF.GetResource("NC:BookmarksRoot");
fillFolder(popup, folder, 1);
if (gList.selectedIndex == -1) {
gList.selectedIndex = 0;
gSelectedFolder = "NC:BookmarksRoot";
}
}
function selectFolder(aEvent)
{
gSelectedFolder = aEvent.target.id;
}