diff --git a/etemplate/js/et2_button.js b/etemplate/js/et2_button.js index 92310ae216..4148371810 100644 --- a/etemplate/js/et2_button.js +++ b/etemplate/js/et2_button.js @@ -27,6 +27,12 @@ var et2_button = et2_baseWidget.extend({ "name": "caption", "type": "string", "description": "Label of the button" + }, + + "onclick": { + "name": "onclick", + "type": "js", + "description": "JS code which gets executed when the button is clicked" } }, @@ -36,11 +42,27 @@ var et2_button = et2_baseWidget.extend({ this.label = ""; this.btn = $j(document.createElement("button")) - .addClass("et2_button"); + .addClass("et2_button") + .click(this, function(e) {e.data.buttonClick()}); this.setDOMNode(this.btn[0]); }, + buttonClick: function() { + // Execute the JS code connected to the event handler + if (this.onclick != null) + { + if (!this.onclick()) + return false; + } + + // Fetch the form data + var formData = this.getRoot().getValues(); + + // Submit it! + console.log(formData); + }, + set_label: function(_value) { if (_value != this.value) { diff --git a/etemplate/js/et2_common.js b/etemplate/js/et2_common.js index b85c64524b..65a5af23f5 100644 --- a/etemplate/js/et2_common.js +++ b/etemplate/js/et2_common.js @@ -64,7 +64,7 @@ function et2_debug(_level, _msg) /** * Array with all types supported by the et2_checkType function. */ -var et2_validTypes = ["boolean", "string", "float", "integer", "any"]; +var et2_validTypes = ["boolean", "string", "float", "integer", "any", "js"]; /** * Object whith default values for the above types. Do not specify array or @@ -74,6 +74,7 @@ var et2_validTypes = ["boolean", "string", "float", "integer", "any"]; var et2_typeDefaults = { "boolean": false, "string": "", + "js": null, "float": 0.0, "integer": 0, "any": null @@ -114,6 +115,29 @@ function et2_checkType(_val, _type) _err(); } + if (_type == "js") + { + // Handle the default case + if (_val === null) + { + return null; + } + + // Create a new function containing the code + if (typeof _val == "string") + { + if (_val !== "") + { + // TODO: Parse JS code properly + return new Function(_val); + } + + return null; + } + + _err(); + } + // Check whether the given value is of the type "string" if (_type == "string") { diff --git a/etemplate/js/et2_textbox.js b/etemplate/js/et2_textbox.js index 6d22417e07..fcf36ac85a 100644 --- a/etemplate/js/et2_textbox.js +++ b/etemplate/js/et2_textbox.js @@ -69,5 +69,5 @@ var et2_textbox = et2_inputWidget.extend({ }); -et2_register_widget(et2_textbox, ["textbox"]); +et2_register_widget(et2_textbox, ["textbox", "int", "float"]);