Author: shane.bryzak(a)jboss.com
Date: 2009-06-18 02:01:16 -0400 (Thu, 18 Jun 2009)
New Revision: 11173
Modified:
sandbox/trunk/modules/xwidgets/examples/helloworld/index.html
sandbox/trunk/modules/xwidgets/examples/helloworld/test.xw
sandbox/trunk/modules/xwidgets/examples/helloworld/xw.Button.js
sandbox/trunk/modules/xwidgets/src/main/javascript/xw.Button.js
sandbox/trunk/modules/xwidgets/src/main/javascript/xw.js
Log:
extremely rough first draft of component events
Modified: sandbox/trunk/modules/xwidgets/examples/helloworld/index.html
===================================================================
--- sandbox/trunk/modules/xwidgets/examples/helloworld/index.html 2009-06-18 01:52:13 UTC
(rev 11172)
+++ sandbox/trunk/modules/xwidgets/examples/helloworld/index.html 2009-06-18 06:01:16 UTC
(rev 11173)
@@ -7,7 +7,7 @@
<script src="../../src/main/javascript/xw.js"></script>
<script type="text/javascript">
- // Can't set the resource base to another path
+ // Can't set the resource base to a higher path
// because of browser security restrictions
//xw.setResourceBase("../../src/main/javascript");
Modified: sandbox/trunk/modules/xwidgets/examples/helloworld/test.xw
===================================================================
--- sandbox/trunk/modules/xwidgets/examples/helloworld/test.xw 2009-06-18 01:52:13 UTC
(rev 11172)
+++ sandbox/trunk/modules/xwidgets/examples/helloworld/test.xw 2009-06-18 06:01:16 UTC
(rev 11173)
@@ -3,6 +3,12 @@
<panel>
<label value="Hello World!"/>
- <button value="Click me!"/>
+ <button value="Click me!">
+ <event type="onclick">
+ <action type="script">
+ <![CDATA[alert("Hi there!");]]>
+ </action>
+ </event>
+ </button>
</panel>
</view>
\ No newline at end of file
Modified: sandbox/trunk/modules/xwidgets/examples/helloworld/xw.Button.js
===================================================================
--- sandbox/trunk/modules/xwidgets/examples/helloworld/xw.Button.js 2009-06-18 01:52:13
UTC (rev 11172)
+++ sandbox/trunk/modules/xwidgets/examples/helloworld/xw.Button.js 2009-06-18 06:01:16
UTC (rev 11173)
@@ -19,6 +19,18 @@
this.parent.control.appendChild(this.control);
var text = document.createTextNode(this.value);
this.control.appendChild(text);
+
+ if (!xw.Sys.isUndefined(this.events))
+ {
+ if (!xw.Sys.isUndefined(this.events.onclick))
+ {
+ var events = this.events;
+ var action = function() {
+ events.onclick.invoke();
+ };
+ xw.Sys.chainEvent(this.control, "click", action);
+ }
+ }
}
}
}
Modified: sandbox/trunk/modules/xwidgets/src/main/javascript/xw.Button.js
===================================================================
--- sandbox/trunk/modules/xwidgets/src/main/javascript/xw.Button.js 2009-06-18 01:52:13
UTC (rev 11172)
+++ sandbox/trunk/modules/xwidgets/src/main/javascript/xw.Button.js 2009-06-18 06:01:16
UTC (rev 11173)
@@ -19,6 +19,18 @@
this.parent.control.appendChild(this.control);
var text = document.createTextNode(this.value);
this.control.appendChild(text);
+
+ if (!xw.Sys.isUndefined(this.events))
+ {
+ if (!xw.Sys.isUndefined(this.events.onclick))
+ {
+ var events = this.events;
+ var action = function() {
+ events.onclick.invoke();
+ };
+ xw.Sys.chainEvent(this.control, "click", action);
+ }
+ }
}
}
}
Modified: sandbox/trunk/modules/xwidgets/src/main/javascript/xw.js
===================================================================
--- sandbox/trunk/modules/xwidgets/src/main/javascript/xw.js 2009-06-18 01:52:13 UTC (rev
11172)
+++ sandbox/trunk/modules/xwidgets/src/main/javascript/xw.js 2009-06-18 06:01:16 UTC (rev
11173)
@@ -97,6 +97,11 @@
return value.substring(0, 1).toUpperCase() + value.substring(1, value.length);
}
+xw.Sys.chainEvent = function(ctl, eventName, eventFunc)
+{
+ return ctl.addEventListener(eventName, eventFunc, false);
+}
+
/**
* Control manager
*/
@@ -185,13 +190,16 @@
var element = elements.item(i);
if (element instanceof Element)
{
- var controlName = xw.Sys.capitalize(element.tagName);
- if (!xw.Sys.arrayContains(this.controls, controlName))
+ if (element.tagName != "event")
{
- this.controls.push(controlName);
- }
-
- this.parseChildNodes(element.childNodes);
+ var controlName = xw.Sys.capitalize(element.tagName);
+ if (!xw.Sys.arrayContains(this.controls, controlName))
+ {
+ this.controls.push(controlName);
+ }
+
+ this.parseChildNodes(element.childNodes);
+ }
}
}
}
@@ -218,33 +226,68 @@
xw.ViewParser.prototype.parseControl = function(element, parentControl)
{
var tag = element.tagName;
- var controlName = tag.substring(0,1).toUpperCase() +
- tag.substring(1, tag.length);
- // TODO Test for events here
-
- // TODO improve this
- var control = eval("new xw.controls." + controlName + "()");
- control.parent = parentControl;
-
- if (xw.Sys.isUndefined(parentControl.children))
+ if (tag == "event")
{
- parentControl.children = new Array();
+ if (xw.Sys.isUndefined(parentControl.events))
+ {
+ parentControl.events = {};
+ }
+ xw.EventParser.parse(element, parentControl.events);
}
- parentControl.children.push(control);
+ else
+ {
+ // TODO improve this
+ var controlName = xw.Sys.capitalize(tag);
+ var control = eval("new xw.controls." + controlName + "()");
+ control.parent = parentControl;
+
+ if (xw.Sys.isUndefined(parentControl.children))
+ {
+ parentControl.children = new Array();
+ }
+ parentControl.children.push(control);
+
+ if (element.hasAttributes())
+ {
+ // Set control properties
+ for (var i = 0; i < element.attributes.length; i++)
+ {
+ var name = element.attributes[i].name;
+ var value = element.getAttribute(name);
+ control[name] = value;
+ }
+ }
- if (element.hasAttributes())
+ this.parseChildren(element.childNodes, control);
+ }
+ }
+}
+
+xw.EventParser = {};
+xw.EventParser.parse = function(element, events)
+{
+ var eventType = element.getAttribute("type");
+ for (var i = 0; i < element.childNodes.length; i++)
+ {
+ var child = element.childNodes.item(i);
+ if (child instanceof Element && child.tagName == "action")
{
- // Set control properties
- for (var i = 0; i < element.attributes.length; i++)
+ if (child.getAttribute("type") == "script")
{
- var name = element.attributes[i].name;
- var value = element.getAttribute(name);
- control[name] = value;
+ for (var j = 0; j < child.childNodes.length; j++)
+ {
+ if (child.childNodes[j].nodeType == 4) // CDATA_SECTION_NODE
+ {
+ var actionScript = child.childNodes[j].nodeValue;
+ var action = new xw.Action();
+ action.script = actionScript;
+ events[eventType] = action;
+ }
+ }
}
+ break;
}
-
- this.parseChildren(element.childNodes, control);
}
}
@@ -327,6 +370,19 @@
}
}
+xw.Action = function()
+{
+ this.script = null;
+
+ xw.Action.prototype.invoke = function()
+ {
+ if (!xw.Sys.isUndefined(this.script))
+ {
+ return eval(this.script);
+ }
+ }
+}
+
/**
* A single instance of a view
*/
Show replies by date