// -*- Mode: js2; tab-width: 2; indent-tabs-mode: nil; js2-basic-offset: 2; js2-skip-preprocessor-directives: t; -*- /* * ***** BEGIN LICENSE BLOCK ***** * Version: MPL 1.1/GPL 2.0/LGPL 2.1 * * The contents of this file are subject to the Mozilla Public License Version * 1.1 (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the * License. * * The Original Code is Mozilla Mobile Browser. * * The Initial Developer of the Original Code is * Mozilla Corporation. * Portions created by the Initial Developer are Copyright (C) 2010 * the Initial Developer. All Rights Reserved. * * Contributor(s): * Mark Finkle * Matt Brubeck * Vivien Nicolas * * Alternatively, the contents of this file may be used under the terms of * either the GNU General Public License Version 2 or later (the "GPL"), or * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), * in which case the provisions of the GPL or the LGPL are applicable instead * of those above. If you wish to allow use of your version of this file only * under the terms of either the GPL or the LGPL, and not to allow others to * use your version of this file under the terms of the MPL, indicate your * decision by deleting the provisions above and replace them with the notice * and other provisions required by the GPL or the LGPL. If you do not delete * the provisions above, a recipient may use your version of this file under * the terms of any one of the MPL, the GPL or the LGPL. * * ***** END LICENSE BLOCK ***** */ let Ci = Components.interfaces; let Cc = Components.classes; dump("###################################### forms.js loaded\n"); let HTMLTextAreaElement = Ci.nsIDOMHTMLTextAreaElement; let HTMLInputElement = Ci.nsIDOMHTMLInputElement; let HTMLSelectElement = Ci.nsIDOMHTMLSelectElement; let HTMLIFrameElement = Ci.nsIDOMHTMLIFrameElement; let HTMLDocument = Ci.nsIDOMHTMLDocument; let HTMLHtmlElement = Ci.nsIDOMHTMLHtmlElement; let HTMLBodyElement = Ci.nsIDOMHTMLBodyElement; let HTMLLabelElement = Ci.nsIDOMHTMLLabelElement; let HTMLButtonElement = Ci.nsIDOMHTMLButtonElement; let HTMLOptGroupElement = Ci.nsIDOMHTMLOptGroupElement; let HTMLOptionElement = Ci.nsIDOMHTMLOptionElement; let XULMenuListElement = Ci.nsIDOMXULMenuListElement; /** * Responsible of navigation between forms fields and of the opening of the assistant */ function FormAssistant() { addMessageListener("FormAssist:Closed", this); addMessageListener("FormAssist:Previous", this); addMessageListener("FormAssist:Next", this); addMessageListener("FormAssist:ChoiceSelect", this); addMessageListener("FormAssist:ChoiceChange", this); addMessageListener("FormAssist:AutoComplete", this); addMessageListener("Content:SetWindowSize", this); addEventListener("keyup", this, false); addEventListener("focus", this, true); addEventListener("pageshow", this, false); addEventListener("pagehide", this, false); this._enabled = Services.prefs.getBoolPref("formhelper.enabled"); }; FormAssistant.prototype = { _selectWrapper: null, _currentIndex: -1, _elements: [], get currentElement() { return this._elements[this._currentIndex]; }, get currentIndex() { return this._currentIndex; }, set currentIndex(aIndex) { let element = this._elements[aIndex]; if (!element) return -1; if (this._isVisibleElement(element)) { this._currentIndex = aIndex; gFocusManager.setFocus(element, Ci.nsIFocusManager.FLAG_NOSCROLL); // To ensure we get the current caret positionning of the focused // element we need to delayed a bit the event this._executeDelayed(function(self) { sendAsyncMessage("FormAssist:Show", self._getJSON()); }); } else { // Repopulate the list of elements in the page, some could have gone // because of AJAX changes for example this._elements = []; let currentIndex = this._getAllElements(gFocusManager.focusedElement) if (aIndex < this._currentIndex) this.currentIndex = currentIndex - 1; else if (aIndex > this._currentIndex) this.currentIndex = currentIndex + 1; else if (this._currentIndex != currentIndex) this.currentIndex = currentIndex; } return element; }, _open: false, open: function formHelperOpen(aElement) { // if the click is on an option element we want to check if the parent is a valid target if (aElement instanceof HTMLOptionElement && aElement.parentNode instanceof HTMLSelectElement && !aElement.disabled) { aElement = aElement.parentNode; } // bug 526045 - the form assistant will close if a click happen: // * outside of the scope of the form helper // * hover a button of type=[image|submit] // * hover a disabled element if (!this._isValidElement(aElement)) { let passiveButtons = { button: true, checkbox: true, file: true, radio: true, reset: true }; if ((aElement instanceof HTMLInputElement || aElement instanceof HTMLButtonElement) && passiveButtons[aElement.type] && !aElement.disabled) return false; return this.close(); } // Look for a top editable element if (this._isEditable(aElement)) aElement = this._getTopLevelEditable(aElement); // Checking if the element is the current focused one while the form assistant is open // allow the user to reposition the caret into an input element if (this._open && aElement == this.currentElement) { //hack bug 604351 // if the element is the same editable element and the VKB is closed, reopen it let utils = Util.getWindowUtils(content); if (utils.IMEStatus == utils.IME_STATUS_DISABLED && aElement instanceof HTMLInputElement && aElement.mozIsTextField(false)) { aElement.blur(); aElement.focus(); } // If the element is a