2012-06-06 05:19:33 -07:00
|
|
|
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- /
|
|
|
|
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2012-06-06 05:19:33 -07:00
|
|
|
"use strict";
|
2012-06-06 05:19:33 -07:00
|
|
|
|
2012-10-04 23:21:10 -07:00
|
|
|
dump("###################################### forms.js loaded\n");
|
|
|
|
|
2012-06-06 05:19:33 -07:00
|
|
|
let Ci = Components.interfaces;
|
|
|
|
let Cc = Components.classes;
|
|
|
|
let Cu = Components.utils;
|
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
2012-06-26 07:01:53 -07:00
|
|
|
Cu.import('resource://gre/modules/XPCOMUtils.jsm');
|
2012-06-06 05:19:33 -07:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(Services, "fm",
|
|
|
|
"@mozilla.org/focus-manager;1",
|
|
|
|
"nsIFocusManager");
|
2012-06-06 05:19:33 -07:00
|
|
|
|
2012-06-06 05:19:33 -07:00
|
|
|
let HTMLInputElement = Ci.nsIDOMHTMLInputElement;
|
|
|
|
let HTMLTextAreaElement = Ci.nsIDOMHTMLTextAreaElement;
|
2012-06-06 05:19:33 -07:00
|
|
|
let HTMLSelectElement = Ci.nsIDOMHTMLSelectElement;
|
|
|
|
let HTMLOptGroupElement = Ci.nsIDOMHTMLOptGroupElement;
|
|
|
|
let HTMLOptionElement = Ci.nsIDOMHTMLOptionElement;
|
|
|
|
|
|
|
|
let FormAssistant = {
|
|
|
|
init: function fa_init() {
|
2012-06-06 05:19:33 -07:00
|
|
|
addEventListener("focus", this, true, false);
|
2012-06-26 07:01:53 -07:00
|
|
|
addEventListener("blur", this, true, false);
|
2012-06-06 05:19:33 -07:00
|
|
|
addEventListener("keypress", this, true, false);
|
|
|
|
addEventListener("resize", this, true, false);
|
2012-06-06 05:19:33 -07:00
|
|
|
addMessageListener("Forms:Select:Choice", this);
|
2012-06-06 05:19:33 -07:00
|
|
|
addMessageListener("Forms:Input:Value", this);
|
2012-10-17 04:18:14 -07:00
|
|
|
addMessageListener("Forms:Select:Blur", this);
|
2012-06-06 05:19:33 -07:00
|
|
|
Services.obs.addObserver(this, "ime-enabled-state-changed", false);
|
2012-06-06 05:19:33 -07:00
|
|
|
Services.obs.addObserver(this, "xpcom-shutdown", false);
|
|
|
|
},
|
|
|
|
|
2012-06-06 05:19:33 -07:00
|
|
|
isKeyboardOpened: false,
|
2012-10-04 23:21:10 -07:00
|
|
|
selectionStart: 0,
|
|
|
|
selectionEnd: 0,
|
|
|
|
|
2012-10-24 11:09:12 -07:00
|
|
|
_focusedElement: null,
|
|
|
|
|
|
|
|
get focusedElement() {
|
2012-10-24 16:30:14 -07:00
|
|
|
if (this._focusedElement && Cu.isDeadWrapper(this._focusedElement))
|
2012-10-24 11:09:12 -07:00
|
|
|
this._focusedElement = null;
|
2012-10-24 16:30:14 -07:00
|
|
|
|
2012-10-24 11:09:12 -07:00
|
|
|
return this._focusedElement;
|
|
|
|
},
|
|
|
|
|
|
|
|
set focusedElement(val) {
|
|
|
|
this._focusedElement = val;
|
|
|
|
},
|
|
|
|
|
2012-10-04 23:21:10 -07:00
|
|
|
setFocusedElement: function fa_setFocusedElement(element) {
|
|
|
|
if (element === this.focusedElement)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (this.focusedElement) {
|
|
|
|
this.focusedElement.removeEventListener('mousedown', this);
|
|
|
|
this.focusedElement.removeEventListener('mouseup', this);
|
2012-10-17 04:18:14 -07:00
|
|
|
if (!element) {
|
|
|
|
this.focusedElement.blur();
|
|
|
|
}
|
2012-10-04 23:21:10 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (element) {
|
|
|
|
element.addEventListener('mousedown', this);
|
|
|
|
element.addEventListener('mouseup', this);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.focusedElement = element;
|
|
|
|
},
|
|
|
|
|
2012-06-06 05:19:33 -07:00
|
|
|
handleEvent: function fa_handleEvent(evt) {
|
2012-10-04 23:21:10 -07:00
|
|
|
let focusedElement = this.focusedElement;
|
2012-06-06 05:19:33 -07:00
|
|
|
let target = evt.target;
|
|
|
|
|
2012-06-06 05:19:33 -07:00
|
|
|
switch (evt.type) {
|
2012-06-06 05:19:33 -07:00
|
|
|
case "focus":
|
2012-06-26 07:01:53 -07:00
|
|
|
if (this.isKeyboardOpened)
|
2012-06-06 05:19:33 -07:00
|
|
|
return;
|
|
|
|
|
2012-06-25 23:42:45 -07:00
|
|
|
let ignore = {
|
|
|
|
button: true,
|
|
|
|
file: true,
|
2012-06-26 07:01:53 -07:00
|
|
|
checkbox: true,
|
2012-06-25 23:42:45 -07:00
|
|
|
radio: true,
|
|
|
|
reset: true,
|
2012-07-12 19:28:19 -07:00
|
|
|
submit: true,
|
|
|
|
image: true
|
2012-06-25 23:42:45 -07:00
|
|
|
};
|
|
|
|
|
2012-10-04 23:21:10 -07:00
|
|
|
if (target instanceof HTMLSelectElement) {
|
2012-06-26 07:01:53 -07:00
|
|
|
content.setTimeout(function showIMEForSelect() {
|
2012-10-04 23:21:10 -07:00
|
|
|
sendAsyncMessage("Forms:Input", getJSON(target));
|
2012-06-26 07:01:53 -07:00
|
|
|
});
|
2012-10-04 23:21:10 -07:00
|
|
|
this.setFocusedElement(target);
|
|
|
|
} else if (target instanceof HTMLOptionElement &&
|
|
|
|
target.parentNode instanceof HTMLSelectElement) {
|
|
|
|
target = target.parentNode;
|
2012-06-26 07:01:53 -07:00
|
|
|
content.setTimeout(function showIMEForSelect() {
|
2012-10-04 23:21:10 -07:00
|
|
|
sendAsyncMessage("Forms:Input", getJSON(target));
|
2012-06-26 07:01:53 -07:00
|
|
|
});
|
2012-10-04 23:21:10 -07:00
|
|
|
this.setFocusedElement(target);
|
2012-06-26 07:01:53 -07:00
|
|
|
} else if ((target instanceof HTMLInputElement && !ignore[target.type]) ||
|
|
|
|
target instanceof HTMLTextAreaElement) {
|
2012-10-04 23:21:10 -07:00
|
|
|
this.isKeyboardOpened = this.tryShowIme(target);
|
|
|
|
this.setFocusedElement(target);
|
2012-06-26 07:01:53 -07:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "blur":
|
2012-10-04 23:21:10 -07:00
|
|
|
if (this.focusedElement) {
|
2012-06-26 07:01:53 -07:00
|
|
|
sendAsyncMessage("Forms:Input", { "type": "blur" });
|
2012-10-04 23:21:10 -07:00
|
|
|
this.setFocusedElement(null);
|
2012-10-09 08:29:32 -07:00
|
|
|
this.isKeyboardOpened = false;
|
2012-10-04 23:21:10 -07:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'mousedown':
|
|
|
|
// We only listen for this event on the currently focused element.
|
|
|
|
// When the mouse goes down, note the cursor/selection position
|
|
|
|
this.selectionStart = this.focusedElement.selectionStart;
|
|
|
|
this.selectionEnd = this.focusedElement.selectionEnd;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'mouseup':
|
|
|
|
// We only listen for this event on the currently focused element.
|
|
|
|
// When the mouse goes up, see if the cursor has moved (or the
|
|
|
|
// selection changed) since the mouse went down. If it has, we
|
|
|
|
// need to tell the keyboard about it
|
|
|
|
if (this.focusedElement.selectionStart !== this.selectionStart ||
|
|
|
|
this.focusedElement.selectionEnd !== this.selectionEnd) {
|
|
|
|
this.tryShowIme(this.focusedElement);
|
2012-06-25 23:42:45 -07:00
|
|
|
}
|
2012-06-26 07:01:53 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case "resize":
|
|
|
|
if (!this.isKeyboardOpened)
|
|
|
|
return;
|
2012-06-06 05:19:33 -07:00
|
|
|
|
2012-10-04 23:21:10 -07:00
|
|
|
if (this.focusedElement) {
|
|
|
|
this.focusedElement.scrollIntoView(false);
|
2012-06-26 07:01:53 -07:00
|
|
|
}
|
2012-06-06 05:19:33 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case "keypress":
|
|
|
|
if (evt.keyCode != evt.DOM_VK_ESCAPE || !this.isKeyboardOpened)
|
|
|
|
return;
|
|
|
|
|
|
|
|
sendAsyncMessage("Forms:Input", { "type": "blur" });
|
|
|
|
this.isKeyboardOpened = false;
|
|
|
|
|
|
|
|
evt.preventDefault();
|
|
|
|
evt.stopPropagation();
|
|
|
|
break;
|
2012-06-06 05:19:33 -07:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
receiveMessage: function fa_receiveMessage(msg) {
|
2012-10-04 23:21:10 -07:00
|
|
|
let target = this.focusedElement;
|
2012-06-26 07:01:53 -07:00
|
|
|
if (!target) {
|
2012-06-06 05:19:33 -07:00
|
|
|
return;
|
2012-06-26 07:01:53 -07:00
|
|
|
}
|
2012-06-06 05:19:33 -07:00
|
|
|
|
2012-06-06 05:19:33 -07:00
|
|
|
let json = msg.json;
|
|
|
|
switch (msg.name) {
|
2012-09-26 04:08:50 -07:00
|
|
|
case "Forms:Input:Value": {
|
2012-06-06 05:19:33 -07:00
|
|
|
target.value = json.value;
|
2012-09-26 04:08:50 -07:00
|
|
|
|
|
|
|
let event = content.document.createEvent('HTMLEvents');
|
|
|
|
event.initEvent('input', true, false);
|
|
|
|
target.dispatchEvent(event);
|
2012-06-06 05:19:33 -07:00
|
|
|
break;
|
2012-09-26 04:08:50 -07:00
|
|
|
}
|
2012-06-06 05:19:33 -07:00
|
|
|
|
2012-06-06 05:19:33 -07:00
|
|
|
case "Forms:Select:Choice":
|
|
|
|
let options = target.options;
|
2012-09-04 23:10:55 -07:00
|
|
|
let valueChanged = false;
|
2012-06-06 05:19:33 -07:00
|
|
|
if ("index" in json) {
|
2012-09-04 23:10:55 -07:00
|
|
|
if (options.selectedIndex != json.index) {
|
|
|
|
options.selectedIndex = json.index;
|
|
|
|
valueChanged = true;
|
|
|
|
}
|
2012-06-06 05:19:33 -07:00
|
|
|
} else if ("indexes" in json) {
|
|
|
|
for (let i = 0; i < options.length; i++) {
|
2012-09-04 23:10:55 -07:00
|
|
|
let newValue = (json.indexes.indexOf(i) != -1);
|
|
|
|
if (options.item(i).selected != newValue) {
|
|
|
|
options.item(i).selected = newValue;
|
|
|
|
valueChanged = true;
|
|
|
|
}
|
2012-06-06 05:19:33 -07:00
|
|
|
}
|
|
|
|
}
|
2012-09-04 23:10:55 -07:00
|
|
|
|
|
|
|
// only fire onchange event if any selected option is changed
|
|
|
|
if (valueChanged) {
|
|
|
|
let event = content.document.createEvent('HTMLEvents');
|
|
|
|
event.initEvent('change', true, true);
|
|
|
|
target.dispatchEvent(event);
|
|
|
|
}
|
2012-06-06 05:19:33 -07:00
|
|
|
break;
|
2012-10-17 04:18:14 -07:00
|
|
|
|
|
|
|
case "Forms:Select:Blur": {
|
|
|
|
this.setFocusedElement(null);
|
|
|
|
break;
|
|
|
|
}
|
2012-06-06 05:19:33 -07:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
observe: function fa_observe(subject, topic, data) {
|
2012-06-06 05:19:33 -07:00
|
|
|
switch (topic) {
|
|
|
|
case "ime-enabled-state-changed":
|
|
|
|
let isOpen = this.isKeyboardOpened;
|
|
|
|
let shouldOpen = parseInt(data);
|
|
|
|
if (shouldOpen && !isOpen) {
|
|
|
|
let target = Services.fm.focusedElement;
|
|
|
|
|
|
|
|
if (!target || !this.tryShowIme(target)) {
|
2012-10-04 23:21:10 -07:00
|
|
|
this.setFocusedElement(null);
|
2012-06-06 05:19:33 -07:00
|
|
|
return;
|
|
|
|
} else {
|
2012-10-04 23:21:10 -07:00
|
|
|
this.setFocusedElement(target);
|
2012-06-06 05:19:33 -07:00
|
|
|
}
|
|
|
|
} else if (!shouldOpen && isOpen) {
|
|
|
|
sendAsyncMessage("Forms:Input", { "type": "blur" });
|
|
|
|
}
|
|
|
|
this.isKeyboardOpened = shouldOpen;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "xpcom-shutdown":
|
|
|
|
Services.obs.removeObserver(this, "ime-enabled-state-changed", false);
|
|
|
|
Services.obs.removeObserver(this, "xpcom-shutdown");
|
|
|
|
removeMessageListener("Forms:Select:Choice", this);
|
2012-10-04 23:21:10 -07:00
|
|
|
removeMessageListener("Forms:Input:Value", this);
|
2012-06-06 05:19:33 -07:00
|
|
|
break;
|
|
|
|
}
|
2012-06-06 05:19:33 -07:00
|
|
|
},
|
|
|
|
|
2012-06-06 05:19:33 -07:00
|
|
|
tryShowIme: function(element) {
|
2012-07-12 19:28:19 -07:00
|
|
|
if (!element) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-06-06 05:19:33 -07:00
|
|
|
// FIXME/bug 729623: work around apparent bug in the IME manager
|
|
|
|
// in gecko.
|
|
|
|
let readonly = element.getAttribute("readonly");
|
2012-06-26 07:01:53 -07:00
|
|
|
if (readonly) {
|
2012-06-06 05:19:33 -07:00
|
|
|
return false;
|
2012-06-26 07:01:53 -07:00
|
|
|
}
|
2012-06-06 05:19:33 -07:00
|
|
|
|
|
|
|
sendAsyncMessage("Forms:Input", getJSON(element));
|
|
|
|
return true;
|
2012-06-06 05:19:33 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
FormAssistant.init();
|
|
|
|
|
2012-06-06 05:19:33 -07:00
|
|
|
|
|
|
|
function getJSON(element) {
|
2012-06-26 07:01:53 -07:00
|
|
|
let type = element.type || "";
|
|
|
|
|
2012-08-08 12:46:38 -07:00
|
|
|
// Until the input type=date/datetime/time have been implemented
|
|
|
|
// let's return their real type even if the platform returns 'text'
|
2012-08-07 09:09:38 -07:00
|
|
|
// Related to Bug 769352 - Implement <input type=date>
|
2012-08-08 12:46:38 -07:00
|
|
|
// Related to Bug 777279 - Implement <input type=time>
|
2012-06-06 05:19:33 -07:00
|
|
|
let attributeType = element.getAttribute("type") || "";
|
2012-08-07 09:09:38 -07:00
|
|
|
|
|
|
|
if (attributeType) {
|
|
|
|
var typeLowerCase = attributeType.toLowerCase();
|
|
|
|
switch (typeLowerCase) {
|
|
|
|
case "date":
|
|
|
|
case "time":
|
|
|
|
case "datetime":
|
|
|
|
case "datetime-local":
|
|
|
|
type = typeLowerCase;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-06-06 05:19:33 -07:00
|
|
|
|
2012-10-12 14:51:13 -07:00
|
|
|
// Gecko supports the inputmode attribute on text fields (but not textareas).
|
|
|
|
// But it doesn't recognize "verbatim" and other modes that we're interested
|
|
|
|
// in in Gaia, and the inputmode property returns "auto" for any value
|
|
|
|
// that gecko does not support. So we must query the inputmode attribute
|
|
|
|
// with getAttribute() rather than just using the inputmode property here.
|
|
|
|
// See https://bugzilla.mozilla.org/show_bug.cgi?id=746142
|
|
|
|
let inputmode = element.getAttribute('inputmode');
|
2012-10-04 23:21:10 -07:00
|
|
|
if (inputmode) {
|
|
|
|
inputmode = inputmode.toLowerCase();
|
2012-10-12 14:51:13 -07:00
|
|
|
} else {
|
|
|
|
inputmode = '';
|
2012-10-04 23:21:10 -07:00
|
|
|
}
|
|
|
|
|
2012-06-06 05:19:33 -07:00
|
|
|
return {
|
|
|
|
"type": type.toLowerCase(),
|
2012-09-29 09:32:57 -07:00
|
|
|
"choices": getListForElement(element),
|
2012-10-04 23:21:10 -07:00
|
|
|
"value": element.value,
|
|
|
|
"inputmode": inputmode,
|
|
|
|
"selectionStart": element.selectionStart,
|
|
|
|
"selectionEnd": element.selectionEnd
|
2012-06-06 05:19:33 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2012-06-06 05:19:33 -07:00
|
|
|
function getListForElement(element) {
|
|
|
|
if (!(element instanceof HTMLSelectElement))
|
|
|
|
return null;
|
|
|
|
|
|
|
|
let optionIndex = 0;
|
|
|
|
let result = {
|
|
|
|
"multiple": element.multiple,
|
|
|
|
"choices": []
|
|
|
|
};
|
|
|
|
|
|
|
|
// Build up a flat JSON array of the choices.
|
|
|
|
// In HTML, it's possible for select element choices to be under a
|
|
|
|
// group header (but not recursively). We distinguish between headers
|
|
|
|
// and entries using the boolean "list.group".
|
|
|
|
let children = element.children;
|
|
|
|
for (let i = 0; i < children.length; i++) {
|
|
|
|
let child = children[i];
|
|
|
|
|
|
|
|
if (child instanceof HTMLOptGroupElement) {
|
|
|
|
result.choices.push({
|
|
|
|
"group": true,
|
|
|
|
"text": child.label || child.firstChild.data,
|
|
|
|
"disabled": child.disabled
|
|
|
|
});
|
|
|
|
|
|
|
|
let subchildren = child.children;
|
|
|
|
for (let j = 0; j < subchildren.length; j++) {
|
|
|
|
let subchild = subchildren[j];
|
|
|
|
result.choices.push({
|
|
|
|
"group": false,
|
|
|
|
"inGroup": true,
|
|
|
|
"text": subchild.text,
|
|
|
|
"disabled": child.disabled || subchild.disabled,
|
|
|
|
"selected": subchild.selected,
|
|
|
|
"optionIndex": optionIndex++
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else if (child instanceof HTMLOptionElement) {
|
|
|
|
result.choices.push({
|
|
|
|
"group": false,
|
|
|
|
"inGroup": false,
|
|
|
|
"text": child.text,
|
|
|
|
"disabled": child.disabled,
|
|
|
|
"selected": child.selected,
|
|
|
|
"optionIndex": optionIndex++
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|