selectBookmark.js (4430B)
1 //* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ 2 /* This Source Code Form is subject to the terms of the Mozilla Public 3 * License, v. 2.0. If a copy of the MPL was not distributed with this 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 5 6 /* Shared Places Import - change other consumers if you change this: */ 7 var { XPCOMUtils } = ChromeUtils.importESModule( 8 "resource://gre/modules/XPCOMUtils.sys.mjs" 9 ); 10 11 ChromeUtils.defineESModuleGetters(this, { 12 PlacesTransactions: "resource://gre/modules/PlacesTransactions.sys.mjs", 13 PlacesUIUtils: "moz-src:///browser/components/places/PlacesUIUtils.sys.mjs", 14 PlacesUtils: "resource://gre/modules/PlacesUtils.sys.mjs", 15 PrivateBrowsingUtils: "resource://gre/modules/PrivateBrowsingUtils.sys.mjs", 16 }); 17 18 XPCOMUtils.defineLazyScriptGetter( 19 this, 20 "PlacesTreeView", 21 "chrome://browser/content/places/treeView.js" 22 ); 23 XPCOMUtils.defineLazyScriptGetter( 24 this, 25 ["PlacesInsertionPoint", "PlacesController", "PlacesControllerDragHelper"], 26 "chrome://browser/content/places/controller.js" 27 ); 28 /* End Shared Places Import */ 29 30 /** 31 * SelectBookmarkDialog controls the user interface for the "Use Bookmark for 32 * Home Page" dialog. 33 * 34 * The caller (gMainPane.setHomePageToBookmark in main.js) invokes this dialog 35 * with a single argument - a reference to an object with a .urls property and 36 * a .names property. This dialog is responsible for updating the contents of 37 * the .urls property with an array of URLs to use as home pages and for 38 * updating the .names property with an array of names for those URLs before it 39 * closes. 40 */ 41 var SelectBookmarkDialog = { 42 init: function SBD_init() { 43 let bookmarks = document.getElementById("bookmarks"); 44 bookmarks.place = 45 "place:type=" + Ci.nsINavHistoryQueryOptions.RESULTS_AS_ROOTS_QUERY; 46 bookmarks.addEventListener("dblclick", () => this.onItemDblClick()); 47 bookmarks.addEventListener("select", () => this.selectionChanged()); 48 49 // Initial update of the OK button. 50 this.selectionChanged(); 51 document.addEventListener("dialogaccept", function () { 52 SelectBookmarkDialog.accept(); 53 }); 54 }, 55 56 /** 57 * Update the disabled state of the OK button as the user changes the 58 * selection within the view. 59 */ 60 selectionChanged: function SBD_selectionChanged() { 61 var accept = document 62 .getElementById("selectBookmarkDialog") 63 .getButton("accept"); 64 var bookmarks = document.getElementById("bookmarks"); 65 var disableAcceptButton = true; 66 if (bookmarks.hasSelection) { 67 if (!PlacesUtils.nodeIsSeparator(bookmarks.selectedNode)) { 68 disableAcceptButton = false; 69 } 70 } 71 accept.disabled = disableAcceptButton; 72 }, 73 74 onItemDblClick: function SBD_onItemDblClick() { 75 var bookmarks = document.getElementById("bookmarks"); 76 var selectedNode = bookmarks.selectedNode; 77 if (selectedNode && PlacesUtils.nodeIsURI(selectedNode)) { 78 /** 79 * The user has double clicked on a tree row that is a link. Take this to 80 * mean that they want that link to be their homepage, and close the dialog. 81 */ 82 document 83 .getElementById("selectBookmarkDialog") 84 .getButton("accept") 85 .click(); 86 } 87 }, 88 89 /** 90 * User accepts their selection. Set all the selected URLs or the contents 91 * of the selected folder as the list of homepages. 92 */ 93 accept: function SBD_accept() { 94 var bookmarks = document.getElementById("bookmarks"); 95 if (!bookmarks.hasSelection) { 96 throw new Error( 97 "Should not be able to accept dialog if there is no selected URL!" 98 ); 99 } 100 var urls = []; 101 var names = []; 102 var selectedNode = bookmarks.selectedNode; 103 if (PlacesUtils.nodeIsFolderOrShortcut(selectedNode)) { 104 let concreteGuid = PlacesUtils.getConcreteItemGuid(selectedNode); 105 var contents = PlacesUtils.getFolderContents(concreteGuid).root; 106 var cc = contents.childCount; 107 for (var i = 0; i < cc; ++i) { 108 var node = contents.getChild(i); 109 if (PlacesUtils.nodeIsURI(node)) { 110 urls.push(node.uri); 111 names.push(node.title); 112 } 113 } 114 contents.containerOpen = false; 115 } else { 116 urls.push(selectedNode.uri); 117 names.push(selectedNode.title); 118 } 119 window.arguments[0].urls = urls; 120 window.arguments[0].names = names; 121 }, 122 }; 123 124 window.addEventListener("load", () => SelectBookmarkDialog.init());