browser/metro/base/content/helperui/FindHelperUI.js

Wed, 31 Dec 2014 06:55:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:50 +0100
changeset 2
7e26c7da4463
permissions
-rw-r--r--

Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2

michael@0 1 // -*- Mode: js2; tab-width: 2; indent-tabs-mode: nil; js2-basic-offset: 2; js2-skip-preprocessor-directives: t; -*-
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 /* We don't support zooming yet, disable Animated zoom by clamping it to the default zoom. */
michael@0 7 const kBrowserFindZoomLevelMin = 1;
michael@0 8 const kBrowserFindZoomLevelMax = 1;
michael@0 9
michael@0 10 var FindHelperUI = {
michael@0 11 type: "find",
michael@0 12 commands: {
michael@0 13 next: "cmd_findNext",
michael@0 14 previous: "cmd_findPrevious",
michael@0 15 close: "cmd_findClose"
michael@0 16 },
michael@0 17
michael@0 18 _finder: null,
michael@0 19 _open: false,
michael@0 20 _status: null,
michael@0 21 _searchString: "",
michael@0 22
michael@0 23 /*
michael@0 24 * Properties
michael@0 25 */
michael@0 26
michael@0 27 get isActive() {
michael@0 28 return this._open;
michael@0 29 },
michael@0 30
michael@0 31 get status() {
michael@0 32 return this._status;
michael@0 33 },
michael@0 34
michael@0 35 set status(val) {
michael@0 36 if (val != this._status) {
michael@0 37 this._status = val;
michael@0 38 if (!val)
michael@0 39 this._textbox.removeAttribute("status");
michael@0 40 else
michael@0 41 this._textbox.setAttribute("status", val);
michael@0 42 this.updateCommands(this._textbox.value);
michael@0 43 }
michael@0 44 },
michael@0 45
michael@0 46 init: function findHelperInit() {
michael@0 47 this._textbox = document.getElementById("findbar-textbox");
michael@0 48 this._container = Elements.findbar;
michael@0 49
michael@0 50 this._cmdPrevious = document.getElementById(this.commands.previous);
michael@0 51 this._cmdNext = document.getElementById(this.commands.next);
michael@0 52
michael@0 53 this._textbox.addEventListener("keydown", this);
michael@0 54
michael@0 55 // Listen for events where form assistant should be closed
michael@0 56 Elements.tabList.addEventListener("TabSelect", this, true);
michael@0 57 Elements.browsers.addEventListener("URLChanged", this, true);
michael@0 58 window.addEventListener("MozAppbarShowing", this);
michael@0 59 window.addEventListener("MozFlyoutPanelShowing", this, false);
michael@0 60 },
michael@0 61
michael@0 62 handleEvent: function findHelperHandleEvent(aEvent) {
michael@0 63 switch (aEvent.type) {
michael@0 64 case "TabSelect":
michael@0 65 this.hide();
michael@0 66 break;
michael@0 67
michael@0 68 case "URLChanged":
michael@0 69 if (aEvent.detail && aEvent.target == getBrowser())
michael@0 70 this.hide();
michael@0 71 break;
michael@0 72
michael@0 73 case "keydown":
michael@0 74 if (aEvent.keyCode == Ci.nsIDOMKeyEvent.DOM_VK_RETURN) {
michael@0 75 let backwardsSearch = aEvent.shiftKey;
michael@0 76 this.searchAgain(this._searchString, backwardsSearch);
michael@0 77 }
michael@0 78 break;
michael@0 79
michael@0 80 case "MozAppbarShowing":
michael@0 81 case "MozFlyoutPanelShowing":
michael@0 82 if (aEvent.target != this._container) {
michael@0 83 this.hide();
michael@0 84 }
michael@0 85 break;
michael@0 86 }
michael@0 87 },
michael@0 88
michael@0 89 show: function findHelperShow() {
michael@0 90 if (BrowserUI.isStartTabVisible) {
michael@0 91 return;
michael@0 92 }
michael@0 93 if (this._open) {
michael@0 94 setTimeout(() => {
michael@0 95 this._textbox.select();
michael@0 96 this._textbox.focus();
michael@0 97 }, 0);
michael@0 98 return;
michael@0 99 }
michael@0 100
michael@0 101 // Hide any menus
michael@0 102 ContextUI.dismiss();
michael@0 103
michael@0 104 // Shutdown selection related ui
michael@0 105 SelectionHelperUI.closeEditSession();
michael@0 106
michael@0 107 let findbar = this._container;
michael@0 108 setTimeout(() => {
michael@0 109 findbar.show();
michael@0 110 this.search(this._textbox.value);
michael@0 111 this._textbox.select();
michael@0 112 this._textbox.focus();
michael@0 113
michael@0 114 this._open = true;
michael@0 115 }, 0);
michael@0 116
michael@0 117 // Prevent the view to scroll automatically while searching
michael@0 118 Browser.selectedBrowser.scrollSync = false;
michael@0 119 },
michael@0 120
michael@0 121 hide: function findHelperHide() {
michael@0 122 if (!this._open)
michael@0 123 return;
michael@0 124
michael@0 125 ContentAreaObserver.shiftBrowserDeck(0);
michael@0 126
michael@0 127 let onTransitionEnd = () => {
michael@0 128 this._container.removeEventListener("transitionend", onTransitionEnd, true);
michael@0 129 this._textbox.value = "";
michael@0 130 this.status = null;
michael@0 131 this._open = false;
michael@0 132 if (this._finder) {
michael@0 133 this._finder.removeResultListener(this);
michael@0 134 this._finder = null
michael@0 135 }
michael@0 136 // Restore the scroll synchronisation
michael@0 137 Browser.selectedBrowser.scrollSync = true;
michael@0 138 };
michael@0 139
michael@0 140 this._textbox.blur();
michael@0 141 this._container.addEventListener("transitionend", onTransitionEnd, true);
michael@0 142 this._container.dismiss();
michael@0 143 },
michael@0 144
michael@0 145 search: function findHelperSearch(aValue) {
michael@0 146 if (!this._finder) {
michael@0 147 this._finder = Browser.selectedBrowser.finder;
michael@0 148 this._finder.addResultListener(this);
michael@0 149 }
michael@0 150 this._searchString = aValue;
michael@0 151 if (aValue != "") {
michael@0 152 this._finder.fastFind(aValue, false, false);
michael@0 153 } else {
michael@0 154 this.updateCommands();
michael@0 155 }
michael@0 156 },
michael@0 157
michael@0 158 searchAgain: function findHelperSearchAgain(aValue, aFindBackwards) {
michael@0 159 // This can happen if the user taps next/previous after re-opening the search bar
michael@0 160 if (!this._finder) {
michael@0 161 this.search(aValue);
michael@0 162 return;
michael@0 163 }
michael@0 164
michael@0 165 this._finder.findAgain(aFindBackwards, false, false);
michael@0 166 },
michael@0 167
michael@0 168 goToPrevious: function findHelperGoToPrevious() {
michael@0 169 this.searchAgain(this._searchString, true);
michael@0 170 },
michael@0 171
michael@0 172 goToNext: function findHelperGoToNext() {
michael@0 173 this.searchAgain(this._searchString, false);
michael@0 174 },
michael@0 175
michael@0 176 onFindResult: function(aData) {
michael@0 177 this._status = aData.result;
michael@0 178 if (aData.rect) {
michael@0 179 this._zoom(aData.rect, Browser.selectedBrowser.contentDocumentHeight);
michael@0 180 }
michael@0 181 this.updateCommands();
michael@0 182 },
michael@0 183
michael@0 184 updateCommands: function findHelperUpdateCommands() {
michael@0 185 let disabled = (this._status == Ci.nsITypeAheadFind.FIND_NOTFOUND) || (this._searchString == "");
michael@0 186 this._cmdPrevious.setAttribute("disabled", disabled);
michael@0 187 this._cmdNext.setAttribute("disabled", disabled);
michael@0 188 },
michael@0 189
michael@0 190 _zoom: function _findHelperZoom(aElementRect, aContentHeight) {
michael@0 191 // The rect we get here is the content rect including scroll offset
michael@0 192 // in the page.
michael@0 193
michael@0 194 // If the text falls below the find bar and keyboard shift content up.
michael@0 195 let browserShift = 0;
michael@0 196 // aElementRect.y is the top left origin of the selection rect.
michael@0 197 if ((aElementRect.y + aElementRect.height) >
michael@0 198 (aContentHeight - this._container.boxObject.height)) {
michael@0 199 browserShift += this._container.boxObject.height;
michael@0 200 }
michael@0 201 browserShift += Services.metro.keyboardHeight;
michael@0 202
michael@0 203 // If the rect top of the selection is above the view, don't shift content
michael@0 204 // (or if it's already shifted, shift it back down).
michael@0 205 if (aElementRect.y < browserShift) {
michael@0 206 browserShift = 0;
michael@0 207 }
michael@0 208
michael@0 209 // Shift the deck so that the selection is within the visible view.
michael@0 210 ContentAreaObserver.shiftBrowserDeck(browserShift);
michael@0 211
michael@0 212 // Adjust for keyboad display and position the text selection rect in
michael@0 213 // the middle of the viewable area.
michael@0 214 let xPos = aElementRect.x;
michael@0 215 let yPos = aElementRect.y;
michael@0 216 let scrollAdjust = ((ContentAreaObserver.height - Services.metro.keyboardHeight) * .5) +
michael@0 217 Services.metro.keyboardHeight;
michael@0 218 yPos -= scrollAdjust;
michael@0 219 if (yPos < 0) {
michael@0 220 yPos = 0;
michael@0 221 }
michael@0 222
michael@0 223 // TODO zoom via apzc, right now all we support is scroll
michael@0 224 // positioning.
michael@0 225 Browser.selectedBrowser.contentWindow.scrollTo(xPos, yPos);
michael@0 226 }
michael@0 227 };

mercurial