Author: shane.bryzak(a)jboss.com
Date: 2009-06-17 21:52:13 -0400 (Wed, 17 Jun 2009)
New Revision: 11172
Added:
sandbox/trunk/modules/xwidgets/examples/helloworld/xw.Button.js
sandbox/trunk/modules/xwidgets/src/main/javascript/xw.Button.js
Modified:
sandbox/trunk/modules/xwidgets/examples/helloworld/index.html
sandbox/trunk/modules/xwidgets/examples/helloworld/test.xw
sandbox/trunk/modules/xwidgets/examples/helloworld/xw.Panel.js
sandbox/trunk/modules/xwidgets/src/main/javascript/xw.Panel.js
sandbox/trunk/modules/xwidgets/src/main/javascript/xw.js
Log:
refactored to clean up component mode, two phase render
Modified: sandbox/trunk/modules/xwidgets/examples/helloworld/index.html
===================================================================
--- sandbox/trunk/modules/xwidgets/examples/helloworld/index.html 2009-06-17 15:04:13 UTC
(rev 11171)
+++ sandbox/trunk/modules/xwidgets/examples/helloworld/index.html 2009-06-18 01:52:13 UTC
(rev 11172)
@@ -1,5 +1,5 @@
<html>
- <body>
+ <body style="background-color:#eeeeee">
<h1>Hello World Example</h1>
<div id="container"></div>
Modified: sandbox/trunk/modules/xwidgets/examples/helloworld/test.xw
===================================================================
--- sandbox/trunk/modules/xwidgets/examples/helloworld/test.xw 2009-06-17 15:04:13 UTC
(rev 11171)
+++ sandbox/trunk/modules/xwidgets/examples/helloworld/test.xw 2009-06-18 01:52:13 UTC
(rev 11172)
@@ -2,5 +2,7 @@
<view
xmlns="http://jboss.com/products/xwidgets">
<panel>
<label value="Hello World!"/>
+
+ <button value="Click me!"/>
</panel>
</view>
\ No newline at end of file
Added: sandbox/trunk/modules/xwidgets/examples/helloworld/xw.Button.js
===================================================================
--- sandbox/trunk/modules/xwidgets/examples/helloworld/xw.Button.js
(rev 0)
+++ sandbox/trunk/modules/xwidgets/examples/helloworld/xw.Button.js 2009-06-18 01:52:13
UTC (rev 11172)
@@ -0,0 +1,24 @@
+Package("xw.controls");
+
+xw.controls.Button = function()
+{
+ this.control = null;
+ this.value = null;
+
+ xw.controls.Button.prototype.setParent = function(parent)
+ {
+ this.parent = parent;
+ }
+
+ xw.controls.Button.prototype.paint = function()
+ {
+ if (this.control == null)
+ {
+ this.control = document.createElement("button");
+ this.control.widget = this;
+ this.parent.control.appendChild(this.control);
+ var text = document.createTextNode(this.value);
+ this.control.appendChild(text);
+ }
+ }
+}
Modified: sandbox/trunk/modules/xwidgets/examples/helloworld/xw.Panel.js
===================================================================
--- sandbox/trunk/modules/xwidgets/examples/helloworld/xw.Panel.js 2009-06-17 15:04:13 UTC
(rev 11171)
+++ sandbox/trunk/modules/xwidgets/examples/helloworld/xw.Panel.js 2009-06-18 01:52:13 UTC
(rev 11172)
@@ -6,7 +6,7 @@
this.height = 100;
this.parent = null;
this.control = null;
-
+
xw.controls.Panel.prototype.setParent = function(parent)
{
this.parent = parent;
@@ -23,7 +23,16 @@
this.control.style.width = "400";
this.control.style.height = "200";
- this.control.style.border = "1px solid black";
+ this.control.style.backgroundColor = "#ece9d6";
+ this.control.style.borderTop = "1px solid white";
+ this.control.style.borderLeft = "1px solid white";
+ this.control.style.borderBottom = "1px solid #555555";
+ this.control.style.borderRight = "1px solid #555555";
}
+
+ for (var i = 0; i < this.children.length; i++)
+ {
+ this.children[i].paint();
+ }
}
}
Added: sandbox/trunk/modules/xwidgets/src/main/javascript/xw.Button.js
===================================================================
--- sandbox/trunk/modules/xwidgets/src/main/javascript/xw.Button.js
(rev 0)
+++ sandbox/trunk/modules/xwidgets/src/main/javascript/xw.Button.js 2009-06-18 01:52:13
UTC (rev 11172)
@@ -0,0 +1,24 @@
+Package("xw.controls");
+
+xw.controls.Button = function()
+{
+ this.control = null;
+ this.value = null;
+
+ xw.controls.Button.prototype.setParent = function(parent)
+ {
+ this.parent = parent;
+ }
+
+ xw.controls.Button.prototype.paint = function()
+ {
+ if (this.control == null)
+ {
+ this.control = document.createElement("button");
+ this.control.widget = this;
+ this.parent.control.appendChild(this.control);
+ var text = document.createTextNode(this.value);
+ this.control.appendChild(text);
+ }
+ }
+}
Modified: sandbox/trunk/modules/xwidgets/src/main/javascript/xw.Panel.js
===================================================================
--- sandbox/trunk/modules/xwidgets/src/main/javascript/xw.Panel.js 2009-06-17 15:04:13 UTC
(rev 11171)
+++ sandbox/trunk/modules/xwidgets/src/main/javascript/xw.Panel.js 2009-06-18 01:52:13 UTC
(rev 11172)
@@ -6,7 +6,7 @@
this.height = 100;
this.parent = null;
this.control = null;
-
+
xw.controls.Panel.prototype.setParent = function(parent)
{
this.parent = parent;
@@ -23,7 +23,16 @@
this.control.style.width = "400";
this.control.style.height = "200";
- this.control.style.border = "1px solid black";
+ this.control.style.backgroundColor = "#ece9d6";
+ this.control.style.borderTop = "1px solid white";
+ this.control.style.borderLeft = "1px solid white";
+ this.control.style.borderBottom = "1px solid #555555";
+ this.control.style.borderRight = "1px solid #555555";
}
+
+ for (var i = 0; i < this.children.length; i++)
+ {
+ this.children[i].paint();
+ }
}
}
Modified: sandbox/trunk/modules/xwidgets/src/main/javascript/xw.js
===================================================================
--- sandbox/trunk/modules/xwidgets/src/main/javascript/xw.js 2009-06-17 15:04:13 UTC (rev
11171)
+++ sandbox/trunk/modules/xwidgets/src/main/javascript/xw.js 2009-06-18 01:52:13 UTC (rev
11172)
@@ -162,11 +162,99 @@
}
/**
+ * Parses the XML view definition and creates a View instance
+ */
+xw.ViewParser = function(viewRoot, container)
+{
+ this.viewRoot = viewRoot;
+ this.container = container;
+ this.controls = new Array();
+
+ /**
+ * Parses the XML and builds a list of controls
+ */
+ xw.ViewParser.prototype.parse = function()
+ {
+ this.parseChildNodes(this.viewRoot.childNodes);
+ }
+
+ xw.ViewParser.prototype.parseChildNodes = function(elements)
+ {
+ for (var i = 0; i < elements.length; i++)
+ {
+ var element = elements.item(i);
+ if (element instanceof Element)
+ {
+ var controlName = xw.Sys.capitalize(element.tagName);
+ if (!xw.Sys.arrayContains(this.controls, controlName))
+ {
+ this.controls.push(controlName);
+ }
+
+ this.parseChildNodes(element.childNodes);
+ }
+ }
+ }
+
+ xw.ViewParser.prototype.createView = function()
+ {
+ var view = new xw.View();
+ view.container = this.container;
+ this.parseChildren(this.viewRoot.childNodes, view);
+ return view;
+ }
+
+ xw.ViewParser.prototype.parseChildren = function(children, parentControl)
+ {
+ for (var i = 0; i < children.length; i++)
+ {
+ if (children.item(i) instanceof Element)
+ {
+ this.parseControl(children.item(i), parentControl);
+ }
+ }
+ }
+
+ 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))
+ {
+ 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;
+ }
+ }
+
+ this.parseChildren(element.childNodes, control);
+ }
+}
+
+/**
* View manager - responsible for caching views
*/
xw.ViewManager = {};
+xw.ViewManager.viewCache = {};
xw.ViewManager.pendingViews = new Array();
-xw.ViewManager.loadViewCallback = function(req, container)
+xw.ViewManager.loadViewCallback = function(req, viewName, container)
{
if (req.readyState == 4)
{
@@ -175,12 +263,7 @@
var viewRoot = req.responseXML.documentElement;
if (viewRoot.tagName == "view")
{
- // create the view and push it into the pendingViews array
- var view = new xw.View(viewRoot, container);
- xw.ViewManager.pendingViews.push(view);
- // next we want to load all of the controls
- var controls = view.getDependencies();
- xw.ControlManager.loadControls(controls);
+ xw.ViewManager.createView(viewRoot, container);
}
else
{
@@ -194,30 +277,50 @@
}
}
+xw.ViewManager.createView = function(viewRoot, container)
+{
+ var parser = new xw.ViewParser(viewRoot, container);
+ parser.parse();
+
+ xw.ViewManager.pendingViews.push(parser);
+
+ // next we want to load all of the controls
+ xw.ControlManager.loadControls(parser.controls);
+}
+
xw.ViewManager.signalControlsLoaded = function()
{
if (xw.ViewManager.pendingViews.length > 0)
{
- var view = xw.ViewManager.pendingViews.shift();
- view.render();
- }
-
+ var parser = xw.ViewManager.pendingViews.shift();
+ parser.createView().paint();
+ }
}
xw.ViewManager.openView = function(viewName, container)
{
- var callback = function(req) {
- xw.ViewManager.loadViewCallback(req, container);
- };
- xw.ViewLoader.load(viewName, callback);
+ // If we haven't previously loaded the view, do it now
+ if (xw.Sys.isUndefined(xw.ViewManager.viewCache[viewName]))
+ {
+ var callback = function(req) {
+ xw.ViewManager.loadViewCallback(req, viewName, container);
+ };
+ xw.ViewLoader.load(viewName, callback);
+ }
+ else
+ {
+ xw.ViewLoader.createView(xw.ViewManager.viewCache[viewName], container);
+ }
}
/**
- * Base class for controls
+ * Base class for controls - TODO implement control inheritance
*/
xw.Control = function()
{
this.parent = null;
+ this.children = new Array();
+
xw.Control.prototype.setParent = function(parent)
{
this.parent = parent;
@@ -227,85 +330,20 @@
/**
* A single instance of a view
*/
-xw.View = function(viewRoot, container)
+xw.View = function()
{
- this.viewRoot = viewRoot;
- this.container = container;
- this.control = xw.Sys.getObject(container);
+ this.container = null;
+ this.children = new Array();
- xw.View.prototype.render = function()
+ xw.View.prototype.paint = function()
{
- this.renderChildren(this.viewRoot.childNodes, this);
- }
+ this.control = xw.Sys.getObject(this.container);
- xw.View.prototype.getDependencies = function()
- {
- var controls = new Array();
- this.parseControls(this.viewRoot.childNodes, controls);
- return controls;
- }
-
- xw.View.prototype.parseControls = function(elements, controls)
- {
- for (var i = 0; i < elements.length; i++)
+ for (var i = 0; i < this.children.length; i++)
{
- var element = elements.item(i);
- if (element instanceof Element)
- {
- if (!xw.Sys.arrayContains(controls, element.tagName))
- {
- controls[controls.length] = element.tagName;
- }
- var children = element.childNodes;
- if (children.length > 0)
- {
- this.parseControls(children, controls);
- }
- }
+ this.children[i].paint();
}
- }
-
- xw.View.prototype.renderChildren = function(children, parentControl)
- {
- for (var i = 0; i < children.length; i++)
- {
- if (children.item(i) instanceof Element)
- {
- this.renderControl(children.item(i), parentControl);
- }
- }
- }
-
- xw.View.prototype.renderControl = function(element, parent)
- {
- var tag = element.tagName;
- var controlName = tag.substring(0,1).toUpperCase() +
- tag.substring(1, tag.length);
-
- // TODO fix
- var control = eval("new xw.controls." + controlName + "()");
-
- control.setParent(parent);
-
- 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;
- }
- }
-
- control.paint();
-
- var children = element.childNodes;
- if (children.length > 0)
- {
- this.renderChildren(children, control);
- }
- }
+ }
}
/**