gecko-dev/toolkit/content/widgets/numberbox.xml

152 строки
4.2 KiB
XML

<?xml version="1.0"?>
<!-- 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/. -->
<bindings id="numberboxBindings"
xmlns="http://www.mozilla.org/xbl"
xmlns:html="http://www.w3.org/1999/xhtml"
xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
xmlns:xbl="http://www.mozilla.org/xbl">
<binding id="numberbox"
extends="chrome://global/content/bindings/textbox.xml#textbox">
<resources>
<stylesheet src="chrome://global/skin/numberbox.css"/>
</resources>
<content>
<xul:moz-input-box anonid="moz-input-box" class="numberbox-input-box" flex="1" xbl:inherits="context,disabled,focused">
<html:input class="numberbox-input textbox-input" type="number" anonid="input"
xbl:inherits="value,min,max,maxlength,disabled,size,readonly,placeholder,tabindex,accesskey"/>
</xul:moz-input-box>
</content>
<implementation>
<field name="_valueEntered">false</field>
<field name="_value">0</field>
<property name="value" onget="return String(this.valueNumber)"
onset="return this.valueNumber = val;"/>
<property name="valueNumber">
<getter>
if (this._valueEntered) {
var newval = this.inputField.value;
this._validateValue(newval);
}
return this._value;
</getter>
<setter>
this._validateValue(val);
return val;
</setter>
</property>
<property name="min">
<getter>
var min = this.getAttribute("min");
return min ? Number(min) : 0;
</getter>
<setter>
<![CDATA[
if (typeof val == "number") {
this.setAttribute("min", val);
if (this.valueNumber < val)
this._validateValue(val);
}
return val;
]]>
</setter>
</property>
<property name="max">
<getter>
var max = this.getAttribute("max");
return max ? Number(max) : Infinity;
</getter>
<setter>
<![CDATA[
if (typeof val != "number")
return val;
var min = this.min;
if (val < min)
val = min;
this.setAttribute("max", val);
if (this.valueNumber > val)
this._validateValue(val);
return val;
]]>
</setter>
</property>
<method name="_validateValue">
<parameter name="aValue"/>
<body>
<![CDATA[
aValue = Number(aValue) || 0;
aValue = Math.round(aValue);
var min = this.min;
var max = this.max;
if (aValue < min)
aValue = min;
else if (aValue > max)
aValue = max;
this._valueEntered = false;
this._value = Number(aValue);
this.inputField.value = aValue;
return aValue;
]]>
</body>
</method>
<method name="_fireChange">
<body>
var evt = document.createEvent("Events");
evt.initEvent("change", true, true);
this.dispatchEvent(evt);
</body>
</method>
<constructor><![CDATA[
if (this.max < this.min)
this.max = this.min;
var value = this.inputField.value || 0;
this._validateValue(value);
]]></constructor>
</implementation>
<handlers>
<handler event="input" phase="capturing">
this._valueEntered = true;
</handler>
<handler event="keypress">
<![CDATA[
if (!event.ctrlKey && !event.metaKey && !event.altKey && event.charCode) {
if (event.charCode == 45 && this.min < 0)
return;
if (event.charCode < 48 || event.charCode > 57)
event.preventDefault();
}
]]>
</handler>
<handler event="change">
if (event.originalTarget == this.inputField) {
this._validateValue(this.inputField.value);
}
</handler>
</handlers>
</binding>
</bindings>