You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flex.apache.org by ah...@apache.org on 2013/06/14 18:52:57 UTC
[3/5] git commit: [flex-asjs] [refs/heads/develop] - initial JS
version of MXML view states
initial JS version of MXML view states
Project: http://git-wip-us.apache.org/repos/asf/flex-asjs/repo
Commit: http://git-wip-us.apache.org/repos/asf/flex-asjs/commit/ff99ba71
Tree: http://git-wip-us.apache.org/repos/asf/flex-asjs/tree/ff99ba71
Diff: http://git-wip-us.apache.org/repos/asf/flex-asjs/diff/ff99ba71
Branch: refs/heads/develop
Commit: ff99ba71878fe710a6a0cf151457e88e2bfa5c11
Parents: ca805a0
Author: Alex Harui <ah...@apache.org>
Authored: Tue Jun 11 14:34:38 2013 -0700
Committer: Alex Harui <ah...@apache.org>
Committed: Thu Jun 13 23:23:26 2013 -0700
----------------------------------------------------------------------
frameworks/js/FlexJS/src/mx/states/AddItems.js | 78 ++++++++++
.../js/FlexJS/src/mx/states/SetProperty.js | 66 ++++++++
frameworks/js/FlexJS/src/mx/states/State.js | 34 +++++
.../org/apache/flex/core/SimpleStatesImpl.js | 149 +++++++++++++++++++
.../FlexJS/src/org/apache/flex/core/UIBase.js | 61 +++++++-
.../FlexJS/src/org/apache/flex/core/ViewBase.js | 101 ++++++++++++-
.../apache/flex/utils/MXMLDataInterpreter.js | 2 +-
7 files changed, 488 insertions(+), 3 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/ff99ba71/frameworks/js/FlexJS/src/mx/states/AddItems.js
----------------------------------------------------------------------
diff --git a/frameworks/js/FlexJS/src/mx/states/AddItems.js b/frameworks/js/FlexJS/src/mx/states/AddItems.js
new file mode 100644
index 0000000..6dc5a6d
--- /dev/null
+++ b/frameworks/js/FlexJS/src/mx/states/AddItems.js
@@ -0,0 +1,78 @@
+/**
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+goog.provide('mx.states.AddItems');
+
+
+/**
+ * @constructor
+ */
+mx.states.AddItems = function() {
+};
+
+/**
+ * @this {mx.states.AddItems}
+ * @param {Object} document The MXML object.
+ */
+mx.states.AddItems.prototype.setDocument = function(document) {
+ this.document = document;
+};
+
+/**
+ * @type {string} document The type of override.
+ */
+mx.states.AddItems.prototype.type = 'AddItems';
+
+/**
+ * @expose
+ * @type {Object} document The MXML object.
+ */
+mx.states.AddItems.prototype.document = null;
+
+/**
+ * @expose
+ * @type {Array} items The array of items to add.
+ */
+mx.states.AddItems.prototype.items = null;
+
+/**
+ * @expose
+ * @type {Array} itemsDescriptor The descriptors for items.
+ */
+mx.states.AddItems.prototype.itemsDescriptor = null;
+
+/**
+ * @expose
+ * @type {string} destination The id of the parent.
+ */
+mx.states.AddItems.prototype.destination = null;
+
+/**
+ * @expose
+ * @type {string} propertyName The child property name (e.g. mxmlContent).
+ */
+mx.states.AddItems.prototype.propertyName = null;
+
+/**
+ * @expose
+ * @type {string} position Where the item goes relative to relativeTo.
+ */
+mx.states.AddItems.prototype.position = null;
+
+/**
+ * @expose
+ * @type {string} relativeTo The id of the child where the item goes.
+ */
+mx.states.AddItems.prototype.relativeTo = null;
+
http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/ff99ba71/frameworks/js/FlexJS/src/mx/states/SetProperty.js
----------------------------------------------------------------------
diff --git a/frameworks/js/FlexJS/src/mx/states/SetProperty.js b/frameworks/js/FlexJS/src/mx/states/SetProperty.js
new file mode 100644
index 0000000..a6e5e56
--- /dev/null
+++ b/frameworks/js/FlexJS/src/mx/states/SetProperty.js
@@ -0,0 +1,66 @@
+/**
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+goog.provide('mx.states.SetProperty');
+
+
+/**
+ * @constructor
+ */
+mx.states.SetProperty = function() {
+};
+
+/**
+ * @this {mx.states.SetProperty}
+ * @param {Object} document The MXML object.
+ */
+mx.states.SetProperty.prototype.setDocument = function(document) {
+ this.document = document;
+};
+
+/**
+ * @type {string} document The type of override.
+ */
+mx.states.SetProperty.prototype.type = 'SetProperty';
+
+/**
+ * @expose
+ * @type {Object} document The MXML object.
+ */
+mx.states.SetProperty.prototype.document = null;
+
+/**
+ * @expose
+ * @type {string} name The target property name.
+ */
+mx.states.SetProperty.prototype.name = null;
+
+/**
+ * @expose
+ * @type {string} target The id of the object.
+ */
+mx.states.SetProperty.prototype.target = null;
+
+/**
+ * @expose
+ * @type {Object} previousValue The value to revert to.
+ */
+mx.states.SetProperty.prototype.previousValue = null;
+
+/**
+ * @expose
+ * @type {Object} value The value to set.
+ */
+mx.states.SetProperty.prototype.value = null;
+
http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/ff99ba71/frameworks/js/FlexJS/src/mx/states/State.js
----------------------------------------------------------------------
diff --git a/frameworks/js/FlexJS/src/mx/states/State.js b/frameworks/js/FlexJS/src/mx/states/State.js
new file mode 100644
index 0000000..c78be6a
--- /dev/null
+++ b/frameworks/js/FlexJS/src/mx/states/State.js
@@ -0,0 +1,34 @@
+/**
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+goog.provide('mx.states.State');
+
+
+/**
+ * @constructor
+ */
+mx.states.State = function() {
+};
+
+/**
+ * @expose
+ * @type {string} name The state name.
+ */
+mx.states.State.prototype.name = null;
+
+/**
+ * @expose
+ * @type {Array} overrides The state data.
+ */
+mx.states.State.prototype.overrides = null;
http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/ff99ba71/frameworks/js/FlexJS/src/org/apache/flex/core/SimpleStatesImpl.js
----------------------------------------------------------------------
diff --git a/frameworks/js/FlexJS/src/org/apache/flex/core/SimpleStatesImpl.js b/frameworks/js/FlexJS/src/org/apache/flex/core/SimpleStatesImpl.js
new file mode 100644
index 0000000..6fbbd2f
--- /dev/null
+++ b/frameworks/js/FlexJS/src/org/apache/flex/core/SimpleStatesImpl.js
@@ -0,0 +1,149 @@
+/**
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+goog.provide('org.apache.flex.core.SimpleStatesImpl');
+
+goog.require('org.apache.flex.events.EventDispatcher');
+
+
+/**
+ * @constructor
+ */
+org.apache.flex.core.SimpleStatesImpl = function() {
+ goog.base(this);
+
+ /**
+ * @private
+ * @type {Object}
+ */
+ this.strand_ = null;
+};
+goog.inherits(org.apache.flex.core.SimpleStatesImpl,
+ org.apache.flex.events.EventDispatcher);
+
+/**
+ * @expose
+ * @this {org.apache.flex.net.dataConverters.LazyCollection}
+ * @param {Object} value The new host.
+ */
+org.apache.flex.core.SimpleStatesImpl.prototype.set_strand =
+ function(value) {
+ if (this.strand_ !== value) {
+ this.strand_ = value;
+ this.strand_.addEventListener('currentStateChanged',
+ goog.bind(this.stateChangeHandler, this));
+ }
+};
+
+/**
+ * @protected
+ * @this {org.apache.flex.core.SimpleStatesImpl}
+ */
+org.apache.flex.core.SimpleStatesImpl.prototype.stateChangeHandler =
+ function() {
+ var s, p;
+ var doc = event.target;
+ var arr = doc.states;
+ for (p in arr)
+ {
+ s = arr[p];
+ if (s.name == event.oldValue)
+ {
+ this.revert(s);
+ break;
+ }
+ }
+ for (p in arr)
+ {
+ s = arr[p];
+ if (s.name == event.newValue)
+ {
+ this.apply(s);
+ break;
+ }
+ }
+};
+
+/**
+ * @protected
+ * @this {org.apache.flex.core.SimpleStatesImpl}
+ * @param {Object} s The State to revert.
+ */
+org.apache.flex.core.SimpleStatesImpl.prototype.revert = function(s) {
+ var p, o;
+ var arr = s.overrides;
+ for (var p in arr)
+ {
+ o = arr[p];
+ if (o.type == 'AddItems')
+ {
+ for (var q in o.items)
+ {
+ var item = o.items[q];
+ var parent = o.document[o.destination];
+ item.removeFromParent(parent);
+ parent.dispatchEvent(new Event('childrenAdded'));
+ }
+ }
+ else if (o.type == 'SetProperty')
+ {
+ o.document[o.target][o.name] = o.previousValue;
+ }
+ }
+};
+
+/**
+ * @protected
+ * @this {org.apache.flex.core.SimpleStatesImpl}
+ * @param {Object} s The State to apply.
+ */
+org.apache.flex.core.SimpleStatesImpl.prototype.apply = function(s) {
+ var arr = s.overrides;
+ for (var p in arr)
+ {
+ o = arr[p];
+ if (o.type == 'AddItems')
+ {
+ if (o.items == null)
+ {
+ var di = org.apache.flex.utils.MXMLDataInterpreter;
+ o.items = di.generateMXMLArray(o.document,
+ null, o.itemsDescriptor, true);
+ }
+ for (var q in o.items)
+ {
+ var item = o.items[q];
+ var parent = o.document[o.destination];
+ if (o.relativeTo != null)
+ {
+ var child = o.document[o.relativeTo];
+ var index = child.getIndexInParent(parent);
+ if (o.position == 'after')
+ index++;
+ item.addToParentAt(parent, index);
+ }
+ else
+ {
+ item.addToParent(parent);
+ }
+ parent.dispatchEvent(new Event('childrenAdded'));
+ }
+ }
+ else if (o.type == 'SetProperty')
+ {
+ o.previousValue = o.document[o.target][o.name];
+ o.document[o.target][o.name] = o.value;
+ }
+ }
+};
http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/ff99ba71/frameworks/js/FlexJS/src/org/apache/flex/core/UIBase.js
----------------------------------------------------------------------
diff --git a/frameworks/js/FlexJS/src/org/apache/flex/core/UIBase.js b/frameworks/js/FlexJS/src/org/apache/flex/core/UIBase.js
index c52c222..ca2a6eb 100644
--- a/frameworks/js/FlexJS/src/org/apache/flex/core/UIBase.js
+++ b/frameworks/js/FlexJS/src/org/apache/flex/core/UIBase.js
@@ -51,14 +51,73 @@ org.apache.flex.core.UIBase.prototype.internalAddChild = function(child) {
/**
* @this {org.apache.flex.core.UIBase}
+ * @param {Object} child The element to be added.
+ * @param {number} index The index for the element to be added.
+ */
+org.apache.flex.core.UIBase.prototype.internalAddChildAt =
+ function(child, index) {
+ this.element.insertBefore(child, internalGetChildAt(index));
+};
+
+/**
+ * @this {org.apache.flex.core.UIBase}
+ * @param {Object} child The element to be removed.
+ */
+org.apache.flex.core.UIBase.prototype.internalRemoveChild =
+ function(child) {
+ this.element.removeChild(child);
+};
+
+/**
+ * @this {org.apache.flex.core.UIBase}
+ * @param {number} index The index.
+ * @return {Object} The child.
+ */
+org.apache.flex.core.UIBase.prototype.internalGetChildAt =
+ function(index) {
+ return this.element.childNodex[index];
+};
+
+/**
+ * @this {org.apache.flex.core.UIBase}
* @param {Object} p The parent component.
*/
org.apache.flex.core.UIBase.prototype.addToParent = function(p) {
- this.element = document.createElement('div');
+ if (this.element == null)
+ this.element = document.createElement('div');
p.internalAddChild(this.element);
};
+/**
+ * @this {org.apache.flex.core.UIBase}
+ * @param {Object} p The parent component.
+ * @param {number} index The index.
+ */
+org.apache.flex.core.UIBase.prototype.addToParentAt = function(p, index) {
+ if (this.element == null)
+ this.element = document.createElement('div');
+
+ p.internalAddChildAt(this.element);
+};
+
+/**
+ * @this {org.apache.flex.core.UIBase}
+ * @param {Object} p The parent component.
+ * @return {number} The index in parent.
+ */
+org.apache.flex.core.UIBase.prototype.getIndexInParent = function(p) {
+ return p.internalGetChildIndex(this.element);
+};
+
+/**
+ * @this {org.apache.flex.core.UIBase}
+ * @param {Object} p The parent component.
+ */
+org.apache.flex.core.UIBase.prototype.removeFromParent = function(p) {
+ p.internalRemoveChild(this.element);
+};
+
/**
* @expose
http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/ff99ba71/frameworks/js/FlexJS/src/org/apache/flex/core/ViewBase.js
----------------------------------------------------------------------
diff --git a/frameworks/js/FlexJS/src/org/apache/flex/core/ViewBase.js b/frameworks/js/FlexJS/src/org/apache/flex/core/ViewBase.js
index 82e3137..9c22630 100644
--- a/frameworks/js/FlexJS/src/org/apache/flex/core/ViewBase.js
+++ b/frameworks/js/FlexJS/src/org/apache/flex/core/ViewBase.js
@@ -14,17 +14,40 @@
goog.provide('org.apache.flex.core.ViewBase');
+// TODO: (aharui) bring this in via CSS
+goog.require('org.apache.flex.core.SimpleStatesImpl');
+
goog.require('org.apache.flex.core.UIBase');
goog.require('org.apache.flex.utils.MXMLDataInterpreter');
+
/**
* @constructor
* @extends {org.apache.flex.core.UIBase}
*/
org.apache.flex.core.ViewBase = function() {
goog.base(this);
+
+ /**
+ * @private
+ * @type {Array}
+ */
+ this.states_ = null;
+
+ /**
+ * @private
+ * @type {Array}
+ */
+ this.transitions_ = null;
+
+ /**
+ * @private
+ * @type {string}
+ */
+ this.currentState_ = null;
+
};
goog.inherits(org.apache.flex.core.ViewBase, org.apache.flex.core.UIBase);
@@ -71,7 +94,83 @@ org.apache.flex.core.ViewBase.prototype.initUI = function(model) {
* @this {org.apache.flex.core.ViewBase}
* @return {Object} The application model.
*/
-org.apache.flex.core.UIBase.prototype.get_applicationModel = function() {
+org.apache.flex.core.ViewBase.prototype.get_applicationModel = function() {
return this.applicationModel;
};
+/**
+ * @expose
+ * @this {org.apache.flex.core.ViewBase}
+ * @return {Array} The array of State objects.
+ */
+org.apache.flex.core.ViewBase.prototype.get_states = function() {
+ return this.states_;
+};
+
+/**
+ * @expose
+ * @this {org.apache.flex.core.ViewBase}
+ * @param {Array} value The array of State objects.
+ */
+org.apache.flex.core.ViewBase.prototype.set_states = function(value) {
+ this.states_ = value;
+
+ // TODO: (aharui) check if bead already exists
+ this.addBead(new org.apache.flex.core.SimpleStatesImpl());
+};
+
+/**
+ * @expose
+ * @this {org.apache.flex.core.ViewBase}
+ * @param {string} state The name of the state.
+ * @return {boolean} True if state in states array.
+ */
+org.apache.flex.core.ViewBase.prototype.hasState = function(state) {
+ for (var p in this.states_)
+ {
+ var s = states_[p];
+ if (s.name == state)
+ return true;
+ }
+ return false;
+};
+
+/**
+ * @expose
+ * @this {org.apache.flex.core.ViewBase}
+ * @return {string} The name of the current state.
+ */
+org.apache.flex.core.ViewBase.prototype.get_currentState = function() {
+ return this.states_;
+};
+
+/**
+ * @expose
+ * @this {org.apache.flex.core.ViewBase}
+ * @param {string} value The name of the current state.
+ */
+org.apache.flex.core.ViewBase.prototype.set_currentState = function(value) {
+ var event = new org.apache.flex.events.ValueChangeEvent(
+ 'currentStateChanged', false, false, this._currentState, value);
+ this._currentState = value;
+ this.dispatchEvent(event);
+};
+
+/**
+ * @expose
+ * @this {org.apache.flex.core.ViewBase}
+ * @return {Array} The array of transitions.
+ */
+org.apache.flex.core.ViewBase.prototype.get_transitions = function() {
+ return this.transitions_;
+};
+
+/**
+ * @expose
+ * @this {org.apache.flex.core.ViewBase}
+ * @param {Array} value The array of transitions.
+ */
+org.apache.flex.core.ViewBase.prototype.set_transitions = function(value) {
+ this.transitions_ = value;
+};
+
http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/ff99ba71/frameworks/js/FlexJS/src/org/apache/flex/utils/MXMLDataInterpreter.js
----------------------------------------------------------------------
diff --git a/frameworks/js/FlexJS/src/org/apache/flex/utils/MXMLDataInterpreter.js b/frameworks/js/FlexJS/src/org/apache/flex/utils/MXMLDataInterpreter.js
index 949a202..75924a9 100644
--- a/frameworks/js/FlexJS/src/org/apache/flex/utils/MXMLDataInterpreter.js
+++ b/frameworks/js/FlexJS/src/org/apache/flex/utils/MXMLDataInterpreter.js
@@ -257,7 +257,7 @@ org.apache.flex.utils.MXMLDataInterpreter.generateMXMLArray =
self = org.apache.flex.utils.MXMLDataInterpreter;
self.generateMXMLInstances(
document, comp, children, opt_recursive);
- if (typeof comp.childrenAdded === 'function')
+ if (typeof comp.childrenAdded === 'function')
comp.childrenAdded();
} else {
comp.setMXMLDescriptor(children);