Added new attribute type 'js', which cares about converting given JS code to a function

This commit is contained in:
Andreas Stöckel 2011-08-15 09:24:32 +00:00
parent 26ce82186c
commit 29c16431ad
3 changed files with 49 additions and 3 deletions

View File

@ -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)
{

View File

@ -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")
{

View File

@ -69,5 +69,5 @@ var et2_textbox = et2_inputWidget.extend({
});
et2_register_widget(et2_textbox, ["textbox"]);
et2_register_widget(et2_textbox, ["textbox", "int", "float"]);