Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved support for single-box Widgets #1125

Merged
merged 3 commits into from
Aug 28, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/widget/HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ Widget Change History
@VERSION@
------

* No changes.
* Improved support for single-box widgets (BB === CB) by defaulting boundingBox to srcNode if CONTENT_TEMPLATE is null.

3.11.0
------

* The Widget HTML_PARSER implementation has been updated to use the new
* The Widget HTML_PARSER implementation has been updated to use the new
_preAddAttrs() hook in Base, since Base now adds all attributes across
the hierarchy in one shot. Widget HTML_PARSER requires contentBox/srcNode,
and related attributes to be set up first.

This is purely an internal implementation change at the base Widget layer,
and there is no impact to existing implementations.
and there is no impact to existing implementations.

3.10.3
------
Expand Down
21 changes: 20 additions & 1 deletion src/widget/js/Widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ ATTRS[RENDERED] = {
* @writeOnce
*/
ATTRS[BOUNDING_BOX] = {
value:null,
valueFn:"_defaultBB",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a test which subclasses Widget and sets a custom value for the boundingBox attribute to make sure that the subclass' value wins over Widget's valueFn?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated with a few additional tests. Let me know if they don't address the test case you were looking for.

setter: "_setBB",
writeOnce: TRUE
};
Expand Down Expand Up @@ -773,6 +773,25 @@ Y.extend(Widget, Y.Base, {
return (this.CONTENT_TEMPLATE === null) ? this.get(BOUNDING_BOX) : this._setBox(null, node, this.CONTENT_TEMPLATE, false);
},

/**
* Returns the default value for the boundingBox attribute.
*
* For the Widget class, this will most commonly be null (resulting in a new
* boundingBox node instance being created), unless a srcNode was provided
* and CONTENT_TEMPLATE is null, in which case it will be srcNode.
* This behavior was introduced in @VERSION@ to accomodate single-box widgets
* whose BB & CB both point to srcNode (e.g. Y.Button).
*
* @method _defaultBB
* @protected
*/
_defaultBB : function() {
var node = this.get(SRC_NODE),
nullCT = (this.CONTENT_TEMPLATE === null);

return ((node && nullCT) ? node : null);
},

/**
* Returns the default value for the contentBox attribute.
*
Expand Down
68 changes: 56 additions & 12 deletions src/widget/tests/unit/widget.html
Original file line number Diff line number Diff line change
Expand Up @@ -988,6 +988,41 @@

Y.Assert.areEqual("myWidget[foo]", w.toString());

w.destroy();
},

"testBoundingBoxWithSrcNode" : function() {
var w = this.createWidget({
boundingBox: Y.Node.create("<span id='bb'></span>"),
srcNode: Y.Node.create("<span id='cb'></span>")
});

Y.Assert.areEqual("span", w.get("boundingBox").get("tagName").toLowerCase());
Y.Assert.areEqual("span", w.get("contentBox").get("tagName").toLowerCase());
Y.Assert.areEqual("bb", w.get("boundingBox").get("id"));
Y.Assert.areEqual("cb", w.get("contentBox").get("id"));

w.destroy();
},

"testOverridenBoundingBoxATTR" : function() {
function MyOtherWidget () {
MyOtherWidget.superclass.constructor.apply(this, arguments);
}

Y.extend(MyOtherWidget, MyWidget, {}, {
NAME: 'MyOtherWidget',
ATTRS: {
boundingBox: {
value: Y.Node.create('<div id="another-widget"></div>')
}
}
});

var w = new MyOtherWidget();

Y.Assert.areEqual("another-widget", w.get("boundingBox").get("id"));

w.destroy();
}
}, true);
Expand Down Expand Up @@ -1059,8 +1094,7 @@

w.render();

// FIXME: Include after this is fixed: http://yuilibrary.com/projects/yui3/ticket/2530076, 2530311.
// Y.Assert.areSame(n, w.get("contentBox"), "srcNode not used as content box");
Y.Assert.areSame(n, w.get("contentBox"), "srcNode not used as content box");
Y.Assert.isFalse(w.get("boundingBox").hasClass("yui3-mysingleboxwidget-loading"), "yui3-mysingleboxwidget-loading should have removed");

w.destroy();
Expand Down Expand Up @@ -1146,7 +1180,7 @@
w.destroy();
},

// FIXME: Include after this is fixed: http://yuilibrary.com/projects/yui3/ticket/2530076, 2530311.
// FIXME: Include after this is fixed: http://yuilibrary.com/projects/yui3/ticket/2530076.
"testContentBox" : null,

/*
Expand Down Expand Up @@ -1190,7 +1224,7 @@
w.destroy();
},

// FIXME: Include after this is fixed: http://yuilibrary.com/projects/yui3/ticket/2530076, 2530311.
// FIXME: Include after this is fixed: http://yuilibrary.com/projects/yui3/ticket/2530076.
"testContentBoxRenderTo" : null,

/*
Expand All @@ -1214,11 +1248,7 @@
},
*/

// FIXME: Include after this is fixed: http://yuilibrary.com/projects/yui3/ticket/2530076, 2530311.
"testSrcNode" : null,

/*
function() {
"testSrcNode" : function() {

var container = Y.one("#widgetRenderContainer");
container.append("<div id='srcNode'><div id='foo'></div></div>");
Expand All @@ -1235,11 +1265,10 @@
Y.Assert.isTrue(w.get("boundingBox").compareTo(sn), "cb !== bb !== sn");
Y.Assert.isTrue(w.get("boundingBox").get("parentNode").compareTo(container), "srcNode moved from it's place in the DOM");

// Y.Assert.isNotNull(w.get("contentBox").one("#foo"), "contents of srcNode not maintained");
Y.Assert.isNotNull(w.get("contentBox").one("#foo"), "contents of srcNode not maintained");

w.destroy(true);
}
*/
},

// CONSCIOUSLY NOT TESTED - CAN'T PASS 2 BOXES TO A SINGLE BOX WIDGET
"testBoundingBoxContentBox" : null,
Expand All @@ -1252,6 +1281,21 @@

Y.Assert.areEqual("mySingleBoxWidget[foo]", w.toString());

w.destroy();
},

"testBoundingBoxWithSrcNode" : function() {
var w = this.createWidget({
boundingBox: Y.Node.create("<span id='bb'></span>"),
srcNode: Y.Node.create("<span id='cb'></span>")
});

// If CONTENT_TEMPLATE is null, CB resolves to BB (see: Widget#_setCB).
// While creating a single-box by specifying BB & srcNode probably doesn't,
// make sense, this test is here to ensure that changes to current behavior are
// considered before being made.
Y.Assert.isTrue(w.get("contentBox").compareTo(w.get("boundingBox")), "bb !== cb");

w.destroy();
}

Expand Down