michael@0: //* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /** michael@0: * SelectBookmarkDialog controls the user interface for the "Use Bookmark for michael@0: * Home Page" dialog. michael@0: * michael@0: * The caller (gMainPane.setHomePageToBookmark in main.js) invokes this dialog michael@0: * with a single argument - a reference to an object with a .urls property and michael@0: * a .names property. This dialog is responsible for updating the contents of michael@0: * the .urls property with an array of URLs to use as home pages and for michael@0: * updating the .names property with an array of names for those URLs before it michael@0: * closes. michael@0: */ michael@0: var SelectBookmarkDialog = { michael@0: init: function SBD_init() { michael@0: document.getElementById("bookmarks").place = michael@0: "place:queryType=1&folder=" + PlacesUIUtils.allBookmarksFolderId; michael@0: michael@0: // Initial update of the OK button. michael@0: this.selectionChanged(); michael@0: }, michael@0: michael@0: /** michael@0: * Update the disabled state of the OK button as the user changes the michael@0: * selection within the view. michael@0: */ michael@0: selectionChanged: function SBD_selectionChanged() { michael@0: var accept = document.documentElement.getButton("accept"); michael@0: var bookmarks = document.getElementById("bookmarks"); michael@0: var disableAcceptButton = true; michael@0: if (bookmarks.hasSelection) { michael@0: if (!PlacesUtils.nodeIsSeparator(bookmarks.selectedNode)) michael@0: disableAcceptButton = false; michael@0: } michael@0: accept.disabled = disableAcceptButton; michael@0: }, michael@0: michael@0: onItemDblClick: function SBD_onItemDblClick() { michael@0: var bookmarks = document.getElementById("bookmarks"); michael@0: var selectedNode = bookmarks.selectedNode; michael@0: if (selectedNode && PlacesUtils.nodeIsURI(selectedNode)) { michael@0: /** michael@0: * The user has double clicked on a tree row that is a link. Take this to michael@0: * mean that they want that link to be their homepage, and close the dialog. michael@0: */ michael@0: document.documentElement.getButton("accept").click(); michael@0: } michael@0: }, michael@0: michael@0: /** michael@0: * User accepts their selection. Set all the selected URLs or the contents michael@0: * of the selected folder as the list of homepages. michael@0: */ michael@0: accept: function SBD_accept() { michael@0: var bookmarks = document.getElementById("bookmarks"); michael@0: NS_ASSERT(bookmarks.hasSelection, michael@0: "Should not be able to accept dialog if there is no selected URL!"); michael@0: var urls = []; michael@0: var names = []; michael@0: var selectedNode = bookmarks.selectedNode; michael@0: if (PlacesUtils.nodeIsFolder(selectedNode)) { michael@0: var contents = PlacesUtils.getFolderContents(selectedNode.itemId).root; michael@0: var cc = contents.childCount; michael@0: for (var i = 0; i < cc; ++i) { michael@0: var node = contents.getChild(i); michael@0: if (PlacesUtils.nodeIsURI(node)) { michael@0: urls.push(node.uri); michael@0: names.push(node.title); michael@0: } michael@0: } michael@0: contents.containerOpen = false; michael@0: } michael@0: else { michael@0: urls.push(selectedNode.uri); michael@0: names.push(selectedNode.title); michael@0: } michael@0: window.arguments[0].urls = urls; michael@0: window.arguments[0].names = names; michael@0: } michael@0: };