Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing code.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | "use strict"; |
michael@0 | 6 | |
michael@0 | 7 | const Cc = Components.classes; |
michael@0 | 8 | const Ci = Components.interfaces; |
michael@0 | 9 | const Cu = Components.utils; |
michael@0 | 10 | |
michael@0 | 11 | this.EXPORTED_SYMBOLS = [ "AutoCompleteE10S" ]; |
michael@0 | 12 | |
michael@0 | 13 | Cu.import("resource://gre/modules/XPCOMUtils.jsm"); |
michael@0 | 14 | Cu.import("resource://gre/modules/Services.jsm"); |
michael@0 | 15 | |
michael@0 | 16 | // nsITreeView implementation that feeds the autocomplete popup |
michael@0 | 17 | // with the search data. |
michael@0 | 18 | let AutoCompleteE10SView = { |
michael@0 | 19 | QueryInterface: XPCOMUtils.generateQI([Ci.nsITreeView, |
michael@0 | 20 | Ci.nsIAutoCompleteController]), |
michael@0 | 21 | treeBox: null, |
michael@0 | 22 | selection: null, |
michael@0 | 23 | treeData: [], |
michael@0 | 24 | |
michael@0 | 25 | get rowCount() { return this.treeData.length; }, |
michael@0 | 26 | setTree: function(treeBox) { this.treeBox = treeBox; }, |
michael@0 | 27 | getCellText: function(idx, column) { return this.treeData[idx] }, |
michael@0 | 28 | isContainer: function(idx) { return false; }, |
michael@0 | 29 | getCellValue: function(idx, column){ return false }, |
michael@0 | 30 | isContainerOpen: function(idx) { return false; }, |
michael@0 | 31 | isContainerEmpty: function(idx) { return false; }, |
michael@0 | 32 | isSeparator: function(idx) { return false; }, |
michael@0 | 33 | isSorted: function() { return false; }, |
michael@0 | 34 | isEditable: function(idx, column) { return false; }, |
michael@0 | 35 | canDrop: function(idx, orientation, dt) { return false; }, |
michael@0 | 36 | getLevel: function(idx) { return 0; }, |
michael@0 | 37 | getParentIndex: function(idx) { return -1; }, |
michael@0 | 38 | hasNextSibling: function(idx, after) { return idx < this.treeData.length - 1 }, |
michael@0 | 39 | toggleOpenState: function(idx) { }, |
michael@0 | 40 | getCellProperties: function(idx, column) { return ""; }, |
michael@0 | 41 | getRowProperties: function(idx) { return ""; }, |
michael@0 | 42 | getImageSrc: function(idx, column) { return null; }, |
michael@0 | 43 | getProgressMode : function(idx, column) { }, |
michael@0 | 44 | cycleHeader: function(column) { }, |
michael@0 | 45 | cycleCell: function(idx, column) { }, |
michael@0 | 46 | selectionChanged: function() { }, |
michael@0 | 47 | performAction: function(action) { }, |
michael@0 | 48 | performActionOnCell: function(action, index, column) { }, |
michael@0 | 49 | getColumnProperties: function(column) { return ""; }, |
michael@0 | 50 | |
michael@0 | 51 | get matchCount() this.rowCount, |
michael@0 | 52 | |
michael@0 | 53 | |
michael@0 | 54 | clearResults: function() { |
michael@0 | 55 | this.treeData = []; |
michael@0 | 56 | }, |
michael@0 | 57 | |
michael@0 | 58 | addResult: function(result) { |
michael@0 | 59 | this.treeData.push(result); |
michael@0 | 60 | }, |
michael@0 | 61 | |
michael@0 | 62 | handleEnter: function(aIsPopupSelection) { |
michael@0 | 63 | AutoCompleteE10S.handleEnter(aIsPopupSelection); |
michael@0 | 64 | }, |
michael@0 | 65 | |
michael@0 | 66 | stopSearch: function(){} |
michael@0 | 67 | }; |
michael@0 | 68 | |
michael@0 | 69 | this.AutoCompleteE10S = { |
michael@0 | 70 | init: function() { |
michael@0 | 71 | let messageManager = Cc["@mozilla.org/globalmessagemanager;1"]. |
michael@0 | 72 | getService(Ci.nsIMessageListenerManager); |
michael@0 | 73 | messageManager.addMessageListener("FormAutoComplete:SelectBy", this); |
michael@0 | 74 | messageManager.addMessageListener("FormAutoComplete:GetSelectedIndex", this); |
michael@0 | 75 | messageManager.addMessageListener("FormAutoComplete:ClosePopup", this); |
michael@0 | 76 | }, |
michael@0 | 77 | |
michael@0 | 78 | search: function(message) { |
michael@0 | 79 | let browserWindow = message.target.ownerDocument.defaultView; |
michael@0 | 80 | this.browser = browserWindow.gBrowser.selectedBrowser; |
michael@0 | 81 | this.popup = this.browser.autoCompletePopup; |
michael@0 | 82 | this.popup.hidden = false; |
michael@0 | 83 | this.popup.setAttribute("width", message.data.width); |
michael@0 | 84 | |
michael@0 | 85 | let rect = message.data; |
michael@0 | 86 | let {x, y} = this.browser.mapScreenCoordinatesFromContent(rect.left, rect.top + rect.height); |
michael@0 | 87 | this.x = x; |
michael@0 | 88 | this.y = y; |
michael@0 | 89 | |
michael@0 | 90 | let formAutoComplete = Cc["@mozilla.org/satchel/form-autocomplete;1"] |
michael@0 | 91 | .getService(Ci.nsIFormAutoComplete); |
michael@0 | 92 | |
michael@0 | 93 | formAutoComplete.autoCompleteSearchAsync(message.data.inputName, |
michael@0 | 94 | message.data.untrimmedSearchString, |
michael@0 | 95 | null, |
michael@0 | 96 | null, |
michael@0 | 97 | this.onSearchComplete.bind(this)); |
michael@0 | 98 | }, |
michael@0 | 99 | |
michael@0 | 100 | onSearchComplete: function(results) { |
michael@0 | 101 | AutoCompleteE10SView.clearResults(); |
michael@0 | 102 | |
michael@0 | 103 | let resultsArray = []; |
michael@0 | 104 | let count = results.matchCount; |
michael@0 | 105 | for (let i = 0; i < count; i++) { |
michael@0 | 106 | let result = results.getValueAt(i); |
michael@0 | 107 | resultsArray.push(result); |
michael@0 | 108 | AutoCompleteE10SView.addResult(result); |
michael@0 | 109 | } |
michael@0 | 110 | |
michael@0 | 111 | this.popup.view = AutoCompleteE10SView; |
michael@0 | 112 | |
michael@0 | 113 | this.browser.messageManager.sendAsyncMessage( |
michael@0 | 114 | "FormAutoComplete:AutoCompleteSearchAsyncResult", |
michael@0 | 115 | {results: resultsArray} |
michael@0 | 116 | ); |
michael@0 | 117 | |
michael@0 | 118 | this.popup.selectedIndex = -1; |
michael@0 | 119 | this.popup.invalidate(); |
michael@0 | 120 | |
michael@0 | 121 | if (count > 0) { |
michael@0 | 122 | this.popup.openPopupAtScreen(this.x, this.y, true); |
michael@0 | 123 | // Bug 947503 - This openPopup call is not triggering the "popupshowing" |
michael@0 | 124 | // event, which autocomplete.xml uses to track the openness of the popup |
michael@0 | 125 | // by setting its mPopupOpen flag. This flag needs to be properly set |
michael@0 | 126 | // for closePopup to work. For now, we set it manually. |
michael@0 | 127 | this.popup.mPopupOpen = true; |
michael@0 | 128 | } else { |
michael@0 | 129 | this.popup.closePopup(); |
michael@0 | 130 | } |
michael@0 | 131 | }, |
michael@0 | 132 | |
michael@0 | 133 | receiveMessage: function(message) { |
michael@0 | 134 | switch (message.name) { |
michael@0 | 135 | case "FormAutoComplete:SelectBy": |
michael@0 | 136 | this.popup.selectBy(message.data.reverse, message.data.page); |
michael@0 | 137 | break; |
michael@0 | 138 | |
michael@0 | 139 | case "FormAutoComplete:GetSelectedIndex": |
michael@0 | 140 | return this.popup.selectedIndex; |
michael@0 | 141 | |
michael@0 | 142 | case "FormAutoComplete:ClosePopup": |
michael@0 | 143 | this.popup.closePopup(); |
michael@0 | 144 | break; |
michael@0 | 145 | } |
michael@0 | 146 | }, |
michael@0 | 147 | |
michael@0 | 148 | handleEnter: function(aIsPopupSelection) { |
michael@0 | 149 | this.browser.messageManager.sendAsyncMessage( |
michael@0 | 150 | "FormAutoComplete:HandleEnter", |
michael@0 | 151 | { selectedIndex: this.popup.selectedIndex, |
michael@0 | 152 | IsPopupSelection: aIsPopupSelection } |
michael@0 | 153 | ); |
michael@0 | 154 | }, |
michael@0 | 155 | |
michael@0 | 156 | stopSearch: function() {} |
michael@0 | 157 | } |
michael@0 | 158 | |
michael@0 | 159 | this.AutoCompleteE10S.init(); |