Widgets should inherit common base class

* Get rid of the pass-view-object-around approach
* Remove <i> tags
* Remove PHP_EOL where it doesn't make sense
* Fix small HTML issues
This commit is contained in:
Thomas Gelf 2014-03-17 17:04:09 +01:00
parent e964da9579
commit 4466053679
10 changed files with 56 additions and 82 deletions

View File

@ -6,9 +6,7 @@
namespace Icinga\Web\Widget; namespace Icinga\Web\Widget;
use Icinga\Exception\ProgrammingError; use Icinga\Exception\ProgrammingError;
use Zend_Controller_Action_HelperBroker as ZfActionHelper; use Icinga\Application\Icinga;
use Zend_View_Abstract;
use Icinga\Web\Widget\Widget;
use Exception; use Exception;
/** /**
@ -26,7 +24,7 @@ use Exception;
* @author Icinga-Web Team <info@icinga.org> * @author Icinga-Web Team <info@icinga.org>
* @license http://www.gnu.org/copyleft/gpl.html GNU General Public License * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License
*/ */
abstract class AbstractWidget implements Widget abstract class AbstractWidget
{ {
/** /**
* If you are going to access the current view with the view() function, * If you are going to access the current view with the view() function,
@ -89,6 +87,8 @@ abstract class AbstractWidget implements Widget
); );
} }
abstract public function render();
/** /**
* Access the current view * Access the current view
* *
@ -100,16 +100,7 @@ abstract class AbstractWidget implements Widget
protected function view() protected function view()
{ {
if (self::$view === null) { if (self::$view === null) {
self::$view = Icinga::app()->getViewRenderer()->view;
$renderer = ZfActionHelper::getStaticHelper(
'viewRenderer'
);
if (null === $renderer->view) {
$renderer->initView();
}
self::$view = $renderer->view;
} }
return self::$view; return self::$view;

View File

@ -329,11 +329,9 @@ class HistoryColorGrid extends AbstractWidget {
} }
/** /**
* @param Zend_View_Abstract $view
*
* @return string * @return string
*/ */
public function render(Zend_View_Abstract $view) public function render()
{ {
if (empty($this->data)) { if (empty($this->data)) {
return '<div>No entries</div>'; return '<div>No entries</div>';

View File

@ -29,12 +29,10 @@
namespace Icinga\Web\Widget\Chart; namespace Icinga\Web\Widget\Chart;
use Icinga\Web\Widget\Widget; use Icinga\Web\Widget\AbstractWidget;
use Icinga\Web\Url; use Icinga\Web\Url;
use \Zend_View_Abstract; class PieChart extends AbstractWidget
class PieChart implements Widget
{ {
/** /**
* The template string used for rendering this widget * The template string used for rendering this widget
@ -49,7 +47,7 @@ class PieChart implements Widget
EOD; EOD;
/** /**
* @var \Icinga\Web\Url * @var Url
*/ */
private $url; private $url;
@ -112,13 +110,12 @@ EOD;
* Renders this widget via the given view and returns the * Renders this widget via the given view and returns the
* HTML as a string * HTML as a string
* *
* @param \Zend_View_Abstract $view
* @return string * @return string
*/ */
public function render(Zend_View_Abstract $view) public function render()
{ {
$template = $this->template; $template = $this->template;
$template = preg_replace('{{url}}', $this->url->getAbsoluteUrl(), $template); $template = preg_replace('{{url}}', $this->url, $template);
$template = preg_replace('{{width}}', $this->width, $template); $template = preg_replace('{{width}}', $this->width, $template);
$template = preg_replace('{{height}}', $this->height, $template); $template = preg_replace('{{height}}', $this->height, $template);
return $template; return $template;

View File

@ -300,12 +300,12 @@ class Dashboard extends AbstractWidget
/** /**
* @see Icinga\Web\Widget::render * @see Icinga\Web\Widget::render
*/ */
public function render(Zend_View_Abstract $view) public function render()
{ {
if (empty($this->panes)) { if (empty($this->panes)) {
return ''; return '';
} }
return $this->determineActivePane()->render($view); return $this->determineActivePane()->render();
} }
/** /**

View File

@ -32,8 +32,8 @@ namespace Icinga\Web\Widget\Dashboard;
use Icinga\Util\Dimension; use Icinga\Util\Dimension;
use Icinga\Web\Form; use Icinga\Web\Form;
use Icinga\Web\Url; use Icinga\Web\Url;
use Icinga\Web\Widget\Widget; use Icinga\Web\Widget\AbstractWidget;
use Zend_View_Abstract; use Icinga\Web\View;
use Zend_Config; use Zend_Config;
use Zend_Form_Element_Submit; use Zend_Form_Element_Submit;
use Zend_Form_Element_Button; use Zend_Form_Element_Button;
@ -45,7 +45,7 @@ use Exception;
* This is the element displaying a specific view in icinga2web * This is the element displaying a specific view in icinga2web
* *
*/ */
class Component implements Widget class Component extends AbstractWidget
{ {
/** /**
* The url of this Component * The url of this Component
@ -95,7 +95,7 @@ class Component implements Widget
private $template =<<<'EOD' private $template =<<<'EOD'
<div class="container" data-icinga-url="{URL}"> <div class="container" data-icinga-url="{URL}">
<h1><a href="{FULL_URL}" data-base-target="col1">{TITLE}</a></h1> <h1>{REMOVE}<a href="{FULL_URL}" data-base-target="col1">{TITLE}</a></h1>
<noscript> <noscript>
<iframe src="{URL}" style="height:100%; width:99%" frameborder="no"></iframe> <iframe src="{URL}" style="height:100%; width:99%" frameborder="no"></iframe>
</noscript> </noscript>
@ -207,27 +207,28 @@ EOD;
/** /**
* @see Widget::render() * @see Widget::render()
*/ */
public function render(Zend_View_Abstract $view) public function render()
{ {
$view = $this->view();
$url = clone($this->url); $url = clone($this->url);
$url->addParams(array('view' => 'compact')); $url->addParams(array('view' => 'compact'));
$html = str_replace('{URL}', $url->getAbsoluteUrl(), $this->template); $html = str_replace('{URL}', $url, $this->template);
$html = str_replace('{FULL_URL}', $url->getUrlWithout('view')->getAbsoluteUrl(), $html); $html = str_replace('{FULL_URL}', $url->getUrlWithout('view'), $html);
$html = str_replace('{REMOVE_BTN}', $this->getRemoveForm($view), $html); $html = str_replace('{REMOVE_BTN}', $this->getRemoveForm($view), $html);
$html = str_replace('{DIMENSION}', $this->getBoxSizeAsCSS(), $html); $html = str_replace('{DIMENSION}', $this->getBoxSizeAsCSS(), $html);
$html = str_replace('{{IS_FULL}}', $this->fullsize ? 'row' : '', $html); $html = str_replace('{{IS_FULL}}', $this->fullsize ? 'row' : '', $html);
$html = str_replace('{TITLE}', htmlentities($this->getTitle()), $html); $html = str_replace('{TITLE}', $view->escape($this->getTitle()), $html);
$html = str_replace('{REMOVE}', $this->getRemoveForm(), $html);
return $html; return $html;
} }
/** /**
* Render the form for removing a dashboard elemetn * Render the form for removing a dashboard elemetn
* *
* @param Zend_View_Abstract $view The view to use for rendering
* @return string The html representation of the form * @return string The html representation of the form
*/ */
public function getRemoveForm(Zend_View_Abstract $view) protected function getRemoveForm()
{ {
$removeUrl = Url::fromPath( $removeUrl = Url::fromPath(
'/dashboard/removecomponent', '/dashboard/removecomponent',
@ -238,18 +239,19 @@ EOD;
); );
$form = new Form(); $form = new Form();
$form->setMethod('POST'); $form->setMethod('POST');
$form->setAttrib('class', 'inline');
$form->setAction($removeUrl); $form->setAction($removeUrl);
$form->addElement( $form->addElement(
new Zend_Form_Element_Button( new Zend_Form_Element_Button(
'remove_pane_btn', 'remove_pane_btn',
array( array(
'class'=> 'btn btn-danger pull-right', 'class'=> 'link-like pull-right',
'type' => 'submit', 'type' => 'submit',
'label' => 'Remove' 'label' => 'x'
) )
) )
); );
return $form->render($view); return $form;
} }
public function setFullsize($bool) public function setFullsize($bool)

View File

@ -31,7 +31,7 @@ namespace Icinga\Web\Widget\Dashboard;
use Icinga\Exception\ConfigurationError; use Icinga\Exception\ConfigurationError;
use Icinga\Exception\ProgrammingError; use Icinga\Exception\ProgrammingError;
use Icinga\Web\Widget\Widget; use Icinga\Web\Widget\AbstractWidget;
use Zend_Config; use Zend_Config;
use Zend_View_Abstract; use Zend_View_Abstract;
@ -39,7 +39,7 @@ use Zend_View_Abstract;
* A pane, displaying different Dashboard components * A pane, displaying different Dashboard components
* *
*/ */
class Pane implements Widget class Pane extends AbstractWidget
{ {
/** /**
* The name of this pane, as defined in the ini file * The name of this pane, as defined in the ini file
@ -162,13 +162,9 @@ class Pane implements Widget
/** /**
* @see Widget::render * @see Widget::render
*/ */
public function render(Zend_View_Abstract $view) public function render()
{ {
$html = PHP_EOL; return implode("\n", $this->components) . "\n";
foreach ($this->components as $component) {
$html .= PHP_EOL . $component->render($view);
}
return $html;
} }
/** /**

View File

@ -88,12 +88,11 @@ EOT;
/** /**
* Render this widget * Render this widget
* *
* @param Zend_View_Abstract $view The view to use for rendering the widget
* @return string The HTML of the widget as a string * @return string The HTML of the widget as a string
*/ */
public function render(Zend_View_Abstract $view) public function render()
{ {
$view = $this->view();
$form = new Form(); $form = new Form();
$form->setAttrib('class', 'inline'); $form->setAttrib('class', 'inline');
$form->setMethod('GET'); $form->setMethod('GET');

View File

@ -132,11 +132,9 @@ class SortBox extends AbstractWidget
* Renders this widget via the given view and returns the * Renders this widget via the given view and returns the
* HTML as a string * HTML as a string
* *
* @param Zend_View_Abstract $view
*
* @return string * @return string
*/ */
public function render(Zend_View_Abstract $view) public function render()
{ {
$form = new Form(); $form = new Form();
$form->setAttrib('class', 'inline'); $form->setAttrib('class', 'inline');

View File

@ -29,8 +29,7 @@
namespace Icinga\Web\Widget; namespace Icinga\Web\Widget;
use \Icinga\Web\Url; use Icinga\Web\Url;
use \Zend_View_Abstract;
/** /**
* A single tab, usually used through the tabs widget * A single tab, usually used through the tabs widget
@ -46,7 +45,7 @@ use \Zend_View_Abstract;
* @property string $urlParams Action URL Parameters * @property string $urlParams Action URL Parameters
* *
*/ */
class Tab implements Widget class Tab extends AbstractWidget
{ {
/** /**
* Whether this tab is currently active * Whether this tab is currently active
@ -220,31 +219,30 @@ class Tab implements Widget
/** /**
* @see Widget::render() * @see Widget::render()
*/ */
public function render(Zend_View_Abstract $view) public function render()
{ {
$view = $this->view();
$class = $this->active ? ' class="active" ' : ''; $class = $this->active ? ' class="active" ' : '';
$caption = $view->escape($this->title); $caption = $view->escape($this->title);
if ($this->icon !== null) { if ($this->icon !== null) {
$caption = '<img src="' . $this->icon->getAbsoluteUrl() $caption = $view->img($this->icon, array('class' => 'icon')) . ' ' . $caption;
. '" style="width:16px;height:16px"/> ' . $caption;
} elseif ($this->iconCls !== null) {
$caption = '<i class="' . $this->iconCls . '"></i> ' . $caption;
} }
if ($this->url !== null) { if ($this->url !== null) {
$this->url->overwriteParams($this->urlParams); $this->url->overwriteParams($this->urlParams);
$tagParams = ''; $tagParams = '';
if ($this->tagParams !== null) { if ($this->tagParams !== null) {
foreach ($this->tagParams as $key => $value) { $tagParams = $view->propertiesToString($this->tagParams);
$tagParams .= ' ' . $key . '="' . $value . '"';
}
} }
$tab = '<a' . $tagParams .' href="' . $this->url->getAbsoluteUrl() $tab = sprintf(
. '">' . $caption . '</a>'; '<a href="%s"%s>%s</a>',
$this->url,
$tagParams,
$caption
);
} else { } else {
$tab = $caption; $tab = $caption;
} }
return '<li ' . $class . '>' . $tab . "</li>\n";
return '<li ' . $class . '>' . $tab . '</li>' . PHP_EOL;
} }
} }

View File

@ -32,13 +32,12 @@ namespace Icinga\Web\Widget;
use Icinga\Exception\ProgrammingError; use Icinga\Exception\ProgrammingError;
use Icinga\Web\Widget\Tabextension\Tabextension; use Icinga\Web\Widget\Tabextension\Tabextension;
use Icinga\Application\Icinga; use Icinga\Application\Icinga;
use Zend_View_Abstract;
use Countable; use Countable;
/** /**
* Navigation tab widget * Navigation tab widget
*/ */
class Tabs implements Countable, Widget class Tabs extends AbstractWidget implements Countable
{ {
/** /**
* Template used for the base tabs * Template used for the base tabs
@ -227,11 +226,9 @@ EOT;
/** /**
* Render the dropdown area with it's tabs and return the resulting HTML * Render the dropdown area with it's tabs and return the resulting HTML
* *
* @param Zend_View_Abstract $view The view used for rendering
*
* @return mixed|string * @return mixed|string
*/ */
private function renderDropdownTabs(Zend_View_Abstract $view) private function renderDropdownTabs()
{ {
if (empty($this->dropdownTabs)) { if (empty($this->dropdownTabs)) {
return ''; return '';
@ -242,7 +239,7 @@ EOT;
if ($tab === null) { if ($tab === null) {
continue; continue;
} }
$tabs .= $tab->render($view); $tabs .= $tab;
} }
return str_replace('{TABS}', $tabs, $this->dropdownTpl); return str_replace('{TABS}', $tabs, $this->dropdownTpl);
} }
@ -250,11 +247,9 @@ EOT;
/** /**
* Render all tabs, except the ones in dropdown area and return the resulting HTML * Render all tabs, except the ones in dropdown area and return the resulting HTML
* *
* @param Zend_View_Abstract $view The view used for rendering
*
* @return string * @return string
*/ */
private function renderTabs(Zend_View_Abstract $view) private function renderTabs()
{ {
$tabs = ''; $tabs = '';
foreach ($this->tabs as $name => $tab) { foreach ($this->tabs as $name => $tab) {
@ -262,7 +257,7 @@ EOT;
if (in_array($name, $this->dropdownTabs)) { if (in_array($name, $this->dropdownTabs)) {
continue; continue;
} }
$tabs .= $tab->render($view); $tabs .= $tab;
} }
return $tabs; return $tabs;
} }
@ -272,15 +267,15 @@ EOT;
* *
* @see Widget::render * @see Widget::render
*/ */
public function render(Zend_View_Abstract $view) public function render()
{ {
if (empty($this->tabs)) { if (empty($this->tabs)) {
return ''; return '';
} }
$html = $this->baseTpl; $html = $this->baseTpl;
$html = str_replace('{TABS}', $this->renderTabs($view), $html); $html = str_replace('{TABS}', $this->renderTabs(), $html);
$html = str_replace('{DROPDOWN}', $this->renderDropdownTabs($view), $html); $html = str_replace('{DROPDOWN}', $this->renderDropdownTabs(), $html);
return $html; return $html;
} }