2011-01-16 22:51:39 +01:00
|
|
|
<?php
|
|
|
|
|
Provide hasChildren() to replace isEmptyContent()
Summary:
Fixes T3698. Sometimes views need to render differently depending on whether they contain content or not. The existing approach for this is `isEmptyContent()`, which doesn't work well and is sort of hacky (it implies double-rendering content, which is not always free or side-effect free).
Instead, provide a test for an element without children. This test is powerful enough to catch the easy cases of `null`, etc., and just do the expected thing, but will not catch a View which is reduced upon rendering. Since this is rare and we have no actual need for it today, just accept that as a limitation.
Test Plan:
Viewed Timeline and Feed UI examples. Viewed Feed (feed), Pholio (timelineview), and Differential (old transactionview).
{F53915}
Reviewers: chad, btrahan
Reviewed By: chad
CC: aran
Maniphest Tasks: T3698
Differential Revision: https://secure.phabricator.com/D6718
2013-08-12 16:51:01 +02:00
|
|
|
/**
|
|
|
|
* @task children Managing Children
|
|
|
|
*/
|
2013-03-09 22:52:21 +01:00
|
|
|
abstract class AphrontView extends Phobject
|
|
|
|
implements PhutilSafeHTMLProducerInterface {
|
2011-01-16 22:51:39 +01:00
|
|
|
|
2012-12-20 23:27:12 +01:00
|
|
|
protected $user;
|
2011-01-16 22:51:39 +01:00
|
|
|
protected $children = array();
|
|
|
|
|
Provide hasChildren() to replace isEmptyContent()
Summary:
Fixes T3698. Sometimes views need to render differently depending on whether they contain content or not. The existing approach for this is `isEmptyContent()`, which doesn't work well and is sort of hacky (it implies double-rendering content, which is not always free or side-effect free).
Instead, provide a test for an element without children. This test is powerful enough to catch the easy cases of `null`, etc., and just do the expected thing, but will not catch a View which is reduced upon rendering. Since this is rare and we have no actual need for it today, just accept that as a limitation.
Test Plan:
Viewed Timeline and Feed UI examples. Viewed Feed (feed), Pholio (timelineview), and Differential (old transactionview).
{F53915}
Reviewers: chad, btrahan
Reviewed By: chad
CC: aran
Maniphest Tasks: T3698
Differential Revision: https://secure.phabricator.com/D6718
2013-08-12 16:51:01 +02:00
|
|
|
|
|
|
|
/* -( Configuration )------------------------------------------------------ */
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @task config
|
|
|
|
*/
|
2012-12-20 23:27:12 +01:00
|
|
|
public function setUser(PhabricatorUser $user) {
|
|
|
|
$this->user = $user;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
Provide hasChildren() to replace isEmptyContent()
Summary:
Fixes T3698. Sometimes views need to render differently depending on whether they contain content or not. The existing approach for this is `isEmptyContent()`, which doesn't work well and is sort of hacky (it implies double-rendering content, which is not always free or side-effect free).
Instead, provide a test for an element without children. This test is powerful enough to catch the easy cases of `null`, etc., and just do the expected thing, but will not catch a View which is reduced upon rendering. Since this is rare and we have no actual need for it today, just accept that as a limitation.
Test Plan:
Viewed Timeline and Feed UI examples. Viewed Feed (feed), Pholio (timelineview), and Differential (old transactionview).
{F53915}
Reviewers: chad, btrahan
Reviewed By: chad
CC: aran
Maniphest Tasks: T3698
Differential Revision: https://secure.phabricator.com/D6718
2013-08-12 16:51:01 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @task config
|
|
|
|
*/
|
2012-12-20 23:27:12 +01:00
|
|
|
protected function getUser() {
|
|
|
|
return $this->user;
|
|
|
|
}
|
|
|
|
|
Provide hasChildren() to replace isEmptyContent()
Summary:
Fixes T3698. Sometimes views need to render differently depending on whether they contain content or not. The existing approach for this is `isEmptyContent()`, which doesn't work well and is sort of hacky (it implies double-rendering content, which is not always free or side-effect free).
Instead, provide a test for an element without children. This test is powerful enough to catch the easy cases of `null`, etc., and just do the expected thing, but will not catch a View which is reduced upon rendering. Since this is rare and we have no actual need for it today, just accept that as a limitation.
Test Plan:
Viewed Timeline and Feed UI examples. Viewed Feed (feed), Pholio (timelineview), and Differential (old transactionview).
{F53915}
Reviewers: chad, btrahan
Reviewed By: chad
CC: aran
Maniphest Tasks: T3698
Differential Revision: https://secure.phabricator.com/D6718
2013-08-12 16:51:01 +02:00
|
|
|
|
|
|
|
/* -( Managing Children )-------------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test if this View accepts children.
|
|
|
|
*
|
|
|
|
* By default, views accept children, but subclases may override this method
|
|
|
|
* to prevent children from being appended. Doing so will cause
|
|
|
|
* @{method:appendChild} to throw exceptions instead of appending children.
|
|
|
|
*
|
|
|
|
* @return bool True if the View should accept children.
|
|
|
|
* @task children
|
|
|
|
*/
|
2012-11-21 03:01:18 +01:00
|
|
|
protected function canAppendChild() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
Provide hasChildren() to replace isEmptyContent()
Summary:
Fixes T3698. Sometimes views need to render differently depending on whether they contain content or not. The existing approach for this is `isEmptyContent()`, which doesn't work well and is sort of hacky (it implies double-rendering content, which is not always free or side-effect free).
Instead, provide a test for an element without children. This test is powerful enough to catch the easy cases of `null`, etc., and just do the expected thing, but will not catch a View which is reduced upon rendering. Since this is rare and we have no actual need for it today, just accept that as a limitation.
Test Plan:
Viewed Timeline and Feed UI examples. Viewed Feed (feed), Pholio (timelineview), and Differential (old transactionview).
{F53915}
Reviewers: chad, btrahan
Reviewed By: chad
CC: aran
Maniphest Tasks: T3698
Differential Revision: https://secure.phabricator.com/D6718
2013-08-12 16:51:01 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Append a child to the list of children.
|
|
|
|
*
|
|
|
|
* This method will only work if the view supports children, which is
|
|
|
|
* determined by @{method:canAppendChild}.
|
|
|
|
*
|
|
|
|
* @param wild Something renderable.
|
|
|
|
* @return this
|
|
|
|
*/
|
2011-01-16 22:51:39 +01:00
|
|
|
final public function appendChild($child) {
|
2012-11-21 03:01:18 +01:00
|
|
|
if (!$this->canAppendChild()) {
|
|
|
|
$class = get_class($this);
|
|
|
|
throw new Exception(
|
2013-03-02 00:37:32 +01:00
|
|
|
pht("View '%s' does not support children.", $class));
|
2012-11-21 03:01:18 +01:00
|
|
|
}
|
Provide hasChildren() to replace isEmptyContent()
Summary:
Fixes T3698. Sometimes views need to render differently depending on whether they contain content or not. The existing approach for this is `isEmptyContent()`, which doesn't work well and is sort of hacky (it implies double-rendering content, which is not always free or side-effect free).
Instead, provide a test for an element without children. This test is powerful enough to catch the easy cases of `null`, etc., and just do the expected thing, but will not catch a View which is reduced upon rendering. Since this is rare and we have no actual need for it today, just accept that as a limitation.
Test Plan:
Viewed Timeline and Feed UI examples. Viewed Feed (feed), Pholio (timelineview), and Differential (old transactionview).
{F53915}
Reviewers: chad, btrahan
Reviewed By: chad
CC: aran
Maniphest Tasks: T3698
Differential Revision: https://secure.phabricator.com/D6718
2013-08-12 16:51:01 +02:00
|
|
|
|
2011-01-16 22:51:39 +01:00
|
|
|
$this->children[] = $child;
|
Provide hasChildren() to replace isEmptyContent()
Summary:
Fixes T3698. Sometimes views need to render differently depending on whether they contain content or not. The existing approach for this is `isEmptyContent()`, which doesn't work well and is sort of hacky (it implies double-rendering content, which is not always free or side-effect free).
Instead, provide a test for an element without children. This test is powerful enough to catch the easy cases of `null`, etc., and just do the expected thing, but will not catch a View which is reduced upon rendering. Since this is rare and we have no actual need for it today, just accept that as a limitation.
Test Plan:
Viewed Timeline and Feed UI examples. Viewed Feed (feed), Pholio (timelineview), and Differential (old transactionview).
{F53915}
Reviewers: chad, btrahan
Reviewed By: chad
CC: aran
Maniphest Tasks: T3698
Differential Revision: https://secure.phabricator.com/D6718
2013-08-12 16:51:01 +02:00
|
|
|
|
2011-01-16 22:51:39 +01:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
Provide hasChildren() to replace isEmptyContent()
Summary:
Fixes T3698. Sometimes views need to render differently depending on whether they contain content or not. The existing approach for this is `isEmptyContent()`, which doesn't work well and is sort of hacky (it implies double-rendering content, which is not always free or side-effect free).
Instead, provide a test for an element without children. This test is powerful enough to catch the easy cases of `null`, etc., and just do the expected thing, but will not catch a View which is reduced upon rendering. Since this is rare and we have no actual need for it today, just accept that as a limitation.
Test Plan:
Viewed Timeline and Feed UI examples. Viewed Feed (feed), Pholio (timelineview), and Differential (old transactionview).
{F53915}
Reviewers: chad, btrahan
Reviewed By: chad
CC: aran
Maniphest Tasks: T3698
Differential Revision: https://secure.phabricator.com/D6718
2013-08-12 16:51:01 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Produce children for rendering.
|
|
|
|
*
|
|
|
|
* Historically, this method reduced children to a string representation,
|
|
|
|
* but it no longer does.
|
|
|
|
*
|
|
|
|
* @return wild Renderable children.
|
|
|
|
* @task
|
|
|
|
*/
|
2011-01-16 22:51:39 +01:00
|
|
|
final protected function renderChildren() {
|
2013-03-09 22:52:41 +01:00
|
|
|
return $this->children;
|
2013-01-29 03:09:00 +01:00
|
|
|
}
|
|
|
|
|
Provide hasChildren() to replace isEmptyContent()
Summary:
Fixes T3698. Sometimes views need to render differently depending on whether they contain content or not. The existing approach for this is `isEmptyContent()`, which doesn't work well and is sort of hacky (it implies double-rendering content, which is not always free or side-effect free).
Instead, provide a test for an element without children. This test is powerful enough to catch the easy cases of `null`, etc., and just do the expected thing, but will not catch a View which is reduced upon rendering. Since this is rare and we have no actual need for it today, just accept that as a limitation.
Test Plan:
Viewed Timeline and Feed UI examples. Viewed Feed (feed), Pholio (timelineview), and Differential (old transactionview).
{F53915}
Reviewers: chad, btrahan
Reviewed By: chad
CC: aran
Maniphest Tasks: T3698
Differential Revision: https://secure.phabricator.com/D6718
2013-08-12 16:51:01 +02:00
|
|
|
|
2013-03-09 22:52:41 +01:00
|
|
|
/**
|
Provide hasChildren() to replace isEmptyContent()
Summary:
Fixes T3698. Sometimes views need to render differently depending on whether they contain content or not. The existing approach for this is `isEmptyContent()`, which doesn't work well and is sort of hacky (it implies double-rendering content, which is not always free or side-effect free).
Instead, provide a test for an element without children. This test is powerful enough to catch the easy cases of `null`, etc., and just do the expected thing, but will not catch a View which is reduced upon rendering. Since this is rare and we have no actual need for it today, just accept that as a limitation.
Test Plan:
Viewed Timeline and Feed UI examples. Viewed Feed (feed), Pholio (timelineview), and Differential (old transactionview).
{F53915}
Reviewers: chad, btrahan
Reviewed By: chad
CC: aran
Maniphest Tasks: T3698
Differential Revision: https://secure.phabricator.com/D6718
2013-08-12 16:51:01 +02:00
|
|
|
* Test if an element has no children.
|
|
|
|
*
|
|
|
|
* @return bool True if this element has children.
|
|
|
|
* @task children
|
2013-03-09 22:52:41 +01:00
|
|
|
*/
|
Provide hasChildren() to replace isEmptyContent()
Summary:
Fixes T3698. Sometimes views need to render differently depending on whether they contain content or not. The existing approach for this is `isEmptyContent()`, which doesn't work well and is sort of hacky (it implies double-rendering content, which is not always free or side-effect free).
Instead, provide a test for an element without children. This test is powerful enough to catch the easy cases of `null`, etc., and just do the expected thing, but will not catch a View which is reduced upon rendering. Since this is rare and we have no actual need for it today, just accept that as a limitation.
Test Plan:
Viewed Timeline and Feed UI examples. Viewed Feed (feed), Pholio (timelineview), and Differential (old transactionview).
{F53915}
Reviewers: chad, btrahan
Reviewed By: chad
CC: aran
Maniphest Tasks: T3698
Differential Revision: https://secure.phabricator.com/D6718
2013-08-12 16:51:01 +02:00
|
|
|
final public function hasChildren() {
|
|
|
|
if ($this->children) {
|
|
|
|
$this->children = $this->reduceChildren($this->children);
|
|
|
|
}
|
|
|
|
return (bool)$this->children;
|
2011-01-16 22:51:39 +01:00
|
|
|
}
|
|
|
|
|
Provide hasChildren() to replace isEmptyContent()
Summary:
Fixes T3698. Sometimes views need to render differently depending on whether they contain content or not. The existing approach for this is `isEmptyContent()`, which doesn't work well and is sort of hacky (it implies double-rendering content, which is not always free or side-effect free).
Instead, provide a test for an element without children. This test is powerful enough to catch the easy cases of `null`, etc., and just do the expected thing, but will not catch a View which is reduced upon rendering. Since this is rare and we have no actual need for it today, just accept that as a limitation.
Test Plan:
Viewed Timeline and Feed UI examples. Viewed Feed (feed), Pholio (timelineview), and Differential (old transactionview).
{F53915}
Reviewers: chad, btrahan
Reviewed By: chad
CC: aran
Maniphest Tasks: T3698
Differential Revision: https://secure.phabricator.com/D6718
2013-08-12 16:51:01 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Reduce effectively-empty lists of children to be actually empty. This
|
|
|
|
* recursively removes `null`, `''`, and `array()` from the list of children
|
|
|
|
* so that @{method:hasChildren} can more effectively align with expectations.
|
|
|
|
*
|
|
|
|
* NOTE: Because View children are not rendered, a View which renders down
|
|
|
|
* to nothing will not be reduced by this method.
|
|
|
|
*
|
|
|
|
* @param list<wild> Renderable children.
|
|
|
|
* @return list<wild> Reduced list of children.
|
|
|
|
* @task children
|
|
|
|
*/
|
|
|
|
private function reduceChildren(array $children) {
|
|
|
|
foreach ($children as $key => $child) {
|
|
|
|
if ($child === null) {
|
|
|
|
unset($children[$key]);
|
|
|
|
} else if ($child === '') {
|
|
|
|
unset($children[$key]);
|
|
|
|
} else if (is_array($child)) {
|
|
|
|
$child = $this->reduceChildren($child);
|
|
|
|
if ($child) {
|
|
|
|
$children[$key] = $child;
|
|
|
|
} else {
|
|
|
|
unset($children[$key]);
|
2013-01-29 03:09:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
Provide hasChildren() to replace isEmptyContent()
Summary:
Fixes T3698. Sometimes views need to render differently depending on whether they contain content or not. The existing approach for this is `isEmptyContent()`, which doesn't work well and is sort of hacky (it implies double-rendering content, which is not always free or side-effect free).
Instead, provide a test for an element without children. This test is powerful enough to catch the easy cases of `null`, etc., and just do the expected thing, but will not catch a View which is reduced upon rendering. Since this is rare and we have no actual need for it today, just accept that as a limitation.
Test Plan:
Viewed Timeline and Feed UI examples. Viewed Feed (feed), Pholio (timelineview), and Differential (old transactionview).
{F53915}
Reviewers: chad, btrahan
Reviewed By: chad
CC: aran
Maniphest Tasks: T3698
Differential Revision: https://secure.phabricator.com/D6718
2013-08-12 16:51:01 +02:00
|
|
|
return $children;
|
2013-01-29 03:09:33 +01:00
|
|
|
}
|
|
|
|
|
Provide hasChildren() to replace isEmptyContent()
Summary:
Fixes T3698. Sometimes views need to render differently depending on whether they contain content or not. The existing approach for this is `isEmptyContent()`, which doesn't work well and is sort of hacky (it implies double-rendering content, which is not always free or side-effect free).
Instead, provide a test for an element without children. This test is powerful enough to catch the easy cases of `null`, etc., and just do the expected thing, but will not catch a View which is reduced upon rendering. Since this is rare and we have no actual need for it today, just accept that as a limitation.
Test Plan:
Viewed Timeline and Feed UI examples. Viewed Feed (feed), Pholio (timelineview), and Differential (old transactionview).
{F53915}
Reviewers: chad, btrahan
Reviewed By: chad
CC: aran
Maniphest Tasks: T3698
Differential Revision: https://secure.phabricator.com/D6718
2013-08-12 16:51:01 +02:00
|
|
|
|
|
|
|
/* -( Rendering )---------------------------------------------------------- */
|
|
|
|
|
|
|
|
|
2011-01-16 22:51:39 +01:00
|
|
|
abstract public function render();
|
|
|
|
|
Provide hasChildren() to replace isEmptyContent()
Summary:
Fixes T3698. Sometimes views need to render differently depending on whether they contain content or not. The existing approach for this is `isEmptyContent()`, which doesn't work well and is sort of hacky (it implies double-rendering content, which is not always free or side-effect free).
Instead, provide a test for an element without children. This test is powerful enough to catch the easy cases of `null`, etc., and just do the expected thing, but will not catch a View which is reduced upon rendering. Since this is rare and we have no actual need for it today, just accept that as a limitation.
Test Plan:
Viewed Timeline and Feed UI examples. Viewed Feed (feed), Pholio (timelineview), and Differential (old transactionview).
{F53915}
Reviewers: chad, btrahan
Reviewed By: chad
CC: aran
Maniphest Tasks: T3698
Differential Revision: https://secure.phabricator.com/D6718
2013-08-12 16:51:01 +02:00
|
|
|
|
|
|
|
/* -( PhutilSafeHTMLProducerInterface )------------------------------------ */
|
|
|
|
|
|
|
|
|
2013-03-09 22:52:21 +01:00
|
|
|
public function producePhutilSafeHTML() {
|
|
|
|
return $this->render();
|
|
|
|
}
|
|
|
|
|
2011-01-16 22:51:39 +01:00
|
|
|
}
|