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;
use Icinga\Exception\ProgrammingError;
use Zend_Controller_Action_HelperBroker as ZfActionHelper;
use Zend_View_Abstract;
use Icinga\Web\Widget\Widget;
use Icinga\Application\Icinga;
use Exception;
/**
@ -26,7 +24,7 @@ use Exception;
* @author Icinga-Web Team <info@icinga.org>
* @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,
@ -89,6 +87,8 @@ abstract class AbstractWidget implements Widget
);
}
abstract public function render();
/**
* Access the current view
*
@ -100,16 +100,7 @@ abstract class AbstractWidget implements Widget
protected function view()
{
if (self::$view === null) {
$renderer = ZfActionHelper::getStaticHelper(
'viewRenderer'
);
if (null === $renderer->view) {
$renderer->initView();
}
self::$view = $renderer->view;
self::$view = Icinga::app()->getViewRenderer()->view;
}
return self::$view;

View File

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

View File

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

View File

@ -300,12 +300,12 @@ class Dashboard extends AbstractWidget
/**
* @see Icinga\Web\Widget::render
*/
public function render(Zend_View_Abstract $view)
public function render()
{
if (empty($this->panes)) {
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\Web\Form;
use Icinga\Web\Url;
use Icinga\Web\Widget\Widget;
use Zend_View_Abstract;
use Icinga\Web\Widget\AbstractWidget;
use Icinga\Web\View;
use Zend_Config;
use Zend_Form_Element_Submit;
use Zend_Form_Element_Button;
@ -45,7 +45,7 @@ use Exception;
* This is the element displaying a specific view in icinga2web
*
*/
class Component implements Widget
class Component extends AbstractWidget
{
/**
* The url of this Component
@ -95,7 +95,7 @@ class Component implements Widget
private $template =<<<'EOD'
<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>
<iframe src="{URL}" style="height:100%; width:99%" frameborder="no"></iframe>
</noscript>
@ -207,27 +207,28 @@ EOD;
/**
* @see Widget::render()
*/
public function render(Zend_View_Abstract $view)
public function render()
{
$view = $this->view();
$url = clone($this->url);
$url->addParams(array('view' => 'compact'));
$html = str_replace('{URL}', $url->getAbsoluteUrl(), $this->template);
$html = str_replace('{FULL_URL}', $url->getUrlWithout('view')->getAbsoluteUrl(), $html);
$html = str_replace('{URL}', $url, $this->template);
$html = str_replace('{FULL_URL}', $url->getUrlWithout('view'), $html);
$html = str_replace('{REMOVE_BTN}', $this->getRemoveForm($view), $html);
$html = str_replace('{DIMENSION}', $this->getBoxSizeAsCSS(), $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;
}
/**
* 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
*/
public function getRemoveForm(Zend_View_Abstract $view)
protected function getRemoveForm()
{
$removeUrl = Url::fromPath(
'/dashboard/removecomponent',
@ -238,18 +239,19 @@ EOD;
);
$form = new Form();
$form->setMethod('POST');
$form->setAttrib('class', 'inline');
$form->setAction($removeUrl);
$form->addElement(
new Zend_Form_Element_Button(
'remove_pane_btn',
array(
'class'=> 'btn btn-danger pull-right',
'class'=> 'link-like pull-right',
'type' => 'submit',
'label' => 'Remove'
'label' => 'x'
)
)
);
return $form->render($view);
return $form;
}
public function setFullsize($bool)

View File

@ -31,7 +31,7 @@ namespace Icinga\Web\Widget\Dashboard;
use Icinga\Exception\ConfigurationError;
use Icinga\Exception\ProgrammingError;
use Icinga\Web\Widget\Widget;
use Icinga\Web\Widget\AbstractWidget;
use Zend_Config;
use Zend_View_Abstract;
@ -39,7 +39,7 @@ use Zend_View_Abstract;
* 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
@ -162,13 +162,9 @@ class Pane implements Widget
/**
* @see Widget::render
*/
public function render(Zend_View_Abstract $view)
public function render()
{
$html = PHP_EOL;
foreach ($this->components as $component) {
$html .= PHP_EOL . $component->render($view);
}
return $html;
return implode("\n", $this->components) . "\n";
}
/**

View File

@ -88,12 +88,11 @@ EOT;
/**
* 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
*/
public function render(Zend_View_Abstract $view)
public function render()
{
$view = $this->view();
$form = new Form();
$form->setAttrib('class', 'inline');
$form->setMethod('GET');

View File

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

View File

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

View File

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