var Checkbox = Class.create(JSControl, { initialize: function($super, element){ $super(element); this.values = [ Element.readAttribute(this.element, "off-value") || "0", Element.readAttribute(this.element, "on-value") || "1" ]; this.labels = [ Element.readAttribute(this.element, "off-label") || this.values[0], Element.readAttribute(this.element, "on-label") || this.values[1] ]; this.icons = [ Element.readAttribute(this.element, "off-icon"), Element.readAttribute(this.element, "on-icon") ]; this.setEventListeners(); new AEvent("WM_INIT", {}, this); }, focused: false, setEventListeners: function(){ Event.observe(this.element, "click", this.toggleValue.bind(this)); Event.observe(this.element, "focus", this.setFocused.curry(true).bind(this)); Event.observe(this.element, "blur", this.setFocused.curry(false).bind(this)); Event.observe(document, "keypress", this.onkeypress.bindAsEventListener(this)); var wnd = AControl.GetParentWindow(this.element); wnd.Subscribe("REGISTERCHILDCONTROL", this.RegisterChildControl, this); }, OnChange: function(){ new AEvent("CHANGE", {}, this); }, toggleValue: function(){ this.setValue(!this.getValue()); this.OnChange(); }, setFocused: function(isFocused){ Element[isFocused ? "addClassName" : "removeClassName"](this.element, "Focus"); this.focused = isFocused; }, onkeypress: function(event){ if (!this.focused || this.disabled) return; if (Event.getKeyCode(event) == 32) this.toggleValue(); }, RegisterChildControl: function(evt) { var p = evt.Data(); if (p.controlId != this.id) return; Event.stopObserving(this.element, "focus"); this.element.observe("focus", function(evt) { evt.target.blur(); evt.stop(); return false; }); Event.stopObserving(this.element, "click"); p.subscribeOnClick(this.toggleValue.bind(this)); }, getValue: function(){ return Number(Element.hasClassName(this.element, "On")); }, Value: function(){ return this.values[this.getValue()]; }, setValue: function(val){ Element[val ? "addClassName" : "removeClassName"](this.element, "On"); }, setValueAux: function(){ if (!this.auxSpan) return; var iii = this.getValue(); var icon = this.icons[iii]; var label = this.labels[iii]; this.auxSpan.innerHTML=label; if (icon) Element.insert(this.auxSpan, {"top": new Element("img", {"src": icon})}); }, SetValue: function(data){ if (!data) return; if (data instanceof Optional){ if (data.IsNull()) return; this.setValue(data.Value() != this.values[0]); } else this.setValue(data != this.values[0]); this.setValueAux(); }, disabled: false, SetAttribute: function(attrName, attrValue){ switch(attrName){ case "class": case "title": case "disabled": break; case "visibility": if (!["yes", "ro", "vo", "link", "no"].include(attrValue)) break; var disabled = attrValue != "yes"; Element.setStyle(this.element, {"display": disabled ? "none": ""}); if (!this.auxSpan && disabled && attrValue != "no"){ this.auxSpan = new Element("span", {"class": "Checkbox SuperclassInput", "style": Element.readAttribute(this.element, "style")}); Element.insert(this.element, {"before": this.auxSpan}); } if (this.auxSpan){ Element.setStyle(this.auxSpan, {"display": disabled && attrValue != "no" ? "" : "none"}); Element[attrValue == "link" ? "addClassName" : "removeClassName"](this.auxSpan, "Link"); this.setValueAux(); } this.disabled = disabled; break; default: Element.writeAttribute(this.element, attrName, attrValue); if (this.auxSpan) Element.writeAttribute(this.auxSpan, attrName, attrValue); } } });