Commit ea753106 by niskac

Added plugin registration api and removed automatic CSS registration.

Changed BootInput to abstract and implmented BootInputHorizontal. Improved BootTabbed.
parent 87f6d939
......@@ -27,8 +27,6 @@ return array(
'components'=>array(
'bootstrap'=>array(
'class'=>'bootstrap.components.Bootstrap',
'coreCss'=>false,
'responsiveCss'=>false,
),
'errorHandler'=>array(
'errorAction'=>'site/error',
......
......@@ -28,6 +28,7 @@
<?php $this->widget('BootNavbar',array(
'brand'=>CHtml::encode(Yii::app()->name),
'collapse'=>true,
'items'=>array(
array(
'class'=>'bootstrap.widgets.BootMenu',
......
......@@ -76,6 +76,7 @@ Yii::app()->user->setFlash('error',
'fixed'=>false,
'brand'=>'Project name',
'brandUrl'=>'#',
'collapse'=>true, // requires bootstrap-responsive.css
'items'=>array(
array(
'class'=>'bootstrap.widgets.BootMenu',
......@@ -119,6 +120,7 @@ Yii::app()->user->setFlash('error',
'fixed'=>false,
'brand'=>'Project name',
'brandUrl'=>'#',
'collapse'=>true, // requires bootstrap-responsive.css
'items'=>array(
array(
'class'=>'bootstrap.widgets.BootMenu',
......
......@@ -16,9 +16,12 @@ class BootActiveForm extends CActiveForm
// The different form types.
const TYPE_VERTICAL = 'form-vertical';
const TYPE_INLINE = 'form-inline';
const TYPE_SEARCH = 'form-search';
const TYPE_HORIZONTAL = 'form-horizontal';
const INPUT_HORIZONTAL = 'bootstrap.widgets.BootInputHorizontal';
const INPUT_INLINE = 'bootstrap.widgets.BootInputInline';
const INPUT_VERTICAL = 'bootstrap.widgets.BootInputVertical';
/**
* @var string the form type. See class constants.
*/
......@@ -59,8 +62,31 @@ class BootActiveForm extends CActiveForm
*/
public function inputRow($type, $model, $attribute, $data = null, $htmlOptions = array())
{
// Determine the input widget class name.
switch ($this->type)
{
case self::TYPE_INLINE:
case self::TYPE_HORIZONTAL:
case self::TYPE_VERTICAL:
default:
$className = self::INPUT_HORIZONTAL;
break;
/*
case self::TYPE_INLINE:
$className = self::INPUT_INLINE;
break;
case self::TYPE_VERTICAL:
default:
$className = self::INPUT_VERTICAL;
break;
*/
}
ob_start();
Yii::app()->controller->widget('BootInput',array(
Yii::app()->controller->widget($className, array(
'type'=>$type,
'form'=>$this,
'model'=>$model,
......
......@@ -21,7 +21,7 @@ class BootAlert extends BootWidget
/**
* @var string the template to use for displaying flash messages.
*/
public $template = '<div class="alert alert-block alert-{key} fade in"><a class="close" data-dismiss="alert">&times;</a>{message}</div>';
public $template = '<div class="alert alert-block alert-{key}{class}"><a class="close" data-dismiss="alert">&times;</a>{message}</div>';
/**
* @var array the html options.
*/
......@@ -33,8 +33,11 @@ class BootAlert extends BootWidget
public function init()
{
parent::init();
$this->registerScriptFile('bootstrap-alert.js');
$this->htmlOptions['id'] = $this->getId();
Yii::app()->bootstrap->registerAlert();
if (!isset($this->htmlOptions['id']))
$this->htmlOptions['id'] = $this->getId();
}
/**
......@@ -47,10 +50,18 @@ class BootAlert extends BootWidget
echo CHtml::openTag('div', $this->htmlOptions);
$transitions = Yii::app()->bootstrap->isPluginRegistered(Bootstrap::PLUGIN_TRANSITION);
foreach ($this->keys as $key)
{
if (Yii::app()->user->hasFlash($key))
echo strtr($this->template, array('{key}'=>$key, '{message}'=>Yii::app()->user->getFlash($key)));
{
echo strtr($this->template, array(
'{class}'=>$transitions ? ' fade in' : '',
'{key}'=>$key,
'{message}'=>Yii::app()->user->getFlash($key),
));
}
}
echo '</div>';
......
<?php
/**
* BootInputBlock class file.
* BootInput class file.
* @author Christoffer Niska <ChristofferNiska@gmail.com>
* @copyright Copyright &copy; Christoffer Niska 2011-
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
......@@ -8,9 +8,10 @@
/**
* Bootstrap input widget.
* Used for rendering inputs with boostrap standards.
* Used for rendering inputs according to Bootstrap standards.
* @todo Implement BootInputInline and BootInputVertical. http://twitter.github.com/bootstrap/base-css.html#forms
*/
class BootInput extends CInputWidget
abstract class BootInput extends CInputWidget
{
// The different input types.
const TYPE_CHECKBOX = 'checkbox';
......@@ -133,196 +134,112 @@ class BootInput extends CInputWidget
/**
* Renders a checkbox.
* @return string the rendered content
* @abstract
*/
protected function checkBox()
{
echo '<div class="controls">';
echo '<label class="checkbox" for="'.CHtml::getIdByName(CHtml::resolveName($this->model, $this->attribute)).'">';
echo $this->form->checkBox($this->model, $this->attribute, $this->htmlOptions).PHP_EOL;
echo $this->model->getAttributeLabel($this->attribute);
echo $this->getError().$this->getHint();
echo '</label></div>';
}
abstract protected function checkBox();
/**
* Renders a list of checkboxes.
* @return string the rendered content
* @abstract
*/
protected function checkBoxList()
{
echo $this->getLabel().'<div class="controls">';
echo $this->form->checkBoxList($this->model, $this->attribute, $this->data, $this->htmlOptions);
echo $this->getError().$this->getHint();
echo '</div>';
}
abstract protected function checkBoxList();
/**
* Renders a list of inline checkboxes.
* @return string the rendered content
* @abstract
*/
protected function checkBoxListInline()
{
$this->htmlOptions['inline'] = true;
$this->checkBoxList();
}
abstract protected function checkBoxListInline();
/**
* Renders a drop down list (select).
* @return string the rendered content
* @abstract
*/
protected function dropDownList()
{
echo $this->getLabel().'<div class="controls">';
echo $this->form->dropDownList($this->model, $this->attribute, $this->data, $this->htmlOptions);
echo $this->getError().$this->getHint();
echo '</div>';
}
abstract protected function dropDownList();
/**
* Renders a file field.
* @return string the rendered content
* @abstract
*/
protected function fileField()
{
echo $this->getLabel().'<div class="controls">';
echo $this->form->fileField($this->model, $this->attribute, $this->htmlOptions);
echo $this->getError().$this->getHint();
echo '</div>';
}
abstract protected function fileField();
/**
* Renders a password field.
* @return string the rendered content
* @abstract
*/
protected function passwordField()
{
echo $this->getLabel().'<div class="controls">';
echo $this->form->passwordField($this->model, $this->attribute, $this->htmlOptions);
echo $this->getError().$this->getHint();
echo '</div>';
}
abstract protected function passwordField();
/**
* Renders a radio button.
* @return string the rendered content
* @abstract
*/
protected function radioButton()
{
echo '<div class="controls">';
echo '<label class="radio" for="'.CHtml::getIdByName(CHtml::resolveName($this->model, $this->attribute)).'">';
echo $this->form->radioButton($this->model, $this->attribute, $this->htmlOptions).PHP_EOL;
echo $this->model->getAttributeLabel($this->attribute);
echo $this->getError().$this->getHint();
echo '</label></div>';
}
abstract protected function radioButton();
/**
* Renders a list of radio buttons.
* @return string the rendered content
* @abstract
*/
protected function radioButtonList()
{
echo $this->getLabel().'<div class="controls">';
echo $this->form->radioButtonList($this->model, $this->attribute, $this->data, $this->htmlOptions);
echo $this->getError().$this->getHint();
echo '</div>';
}
abstract protected function radioButtonList();
/**
* Renders a list of inline radio buttons.
* @return string the rendered content
* @abstract
*/
protected function radioButtonListInline()
{
$this->htmlOptions['inline'] = true;
$this->radioButtonList();
}
abstract protected function radioButtonListInline();
/**
* Renders a textarea.
* @return string the rendered content
* @abstract
*/
protected function textArea()
{
echo $this->getLabel().'<div class="controls">';
echo $this->form->textArea($this->model, $this->attribute, $this->htmlOptions);
echo $this->getError().$this->getHint();
echo '</div>';
}
abstract protected function textArea();
/**
* Renders a text field.
* @return string the rendered content
* @abstract
*/
protected function textField()
{
echo $this->getLabel().'<div class="controls">';
echo $this->form->textField($this->model, $this->attribute, $this->htmlOptions);
echo $this->getError().$this->getHint();
echo '</div>';
}
abstract protected function textField();
/**
* Renders a CAPTCHA.
* @return string the rendered content
* @abstract
*/
protected function captcha()
{
echo $this->getLabel().'<div class="controls"><div class="captcha">';
echo '<div class="widget">'.$this->widget('CCaptcha', array('showRefreshButton'=>false), true).'</div>';
echo $this->form->textField($this->model, $this->attribute, $this->htmlOptions);
echo $this->getError().$this->getHint();
echo '</div></div>';
}
abstract protected function captcha();
/**
* Renders an uneditable field.
* @return string the rendered content
* @abstract
*/
protected function uneditableField()
{
echo $this->getLabel().'<div class="controls">';
echo '<span class="uneditable-input">'.$this->model->{$this->attribute}.'</span>';
echo $this->getError().$this->getHint();
echo '</div>';
}
abstract protected function uneditableField();
/**
* Returns the label for this block.
* @return string the label
* @abstract
*/
protected function getLabel()
{
if ($this->label !== false && !in_array($this->type, array('checkbox', 'radio')) && $this->hasModel())
return $this->form->labelEx($this->model, $this->attribute);
else if ($this->label !== null)
return $this->label;
else
return '';
}
abstract protected function getLabel();
/**
* Returns the hint text for this block.
* @return string the hint text
* @abstract
*/
protected function getHint()
{
if (isset($this->htmlOptions['hint']))
{
$hint = $this->htmlOptions['hint'];
unset($this->htmlOptions['hint']);
return '<p class="help-block">'.$hint.'</p>';
}
else
return '';
}
abstract protected function getHint();
/**
* Returns the error text for this block.
* @return string the error text
* @abstract
*/
protected function getError()
{
return $this->form->error($this->model, $this->attribute);
}
abstract protected function getError();
}
<?php
/**
* BootInputHorizontal class file.
* @author Christoffer Niska <ChristofferNiska@gmail.com>
* @copyright Copyright &copy; Christoffer Niska 2011-
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
*/
Yii::import('bootstrap.widgets.BootInput');
/**
* Bootstrap horizontal form input widget.
* @since 0.9.8
*/
class BootInputHorizontal extends BootInput
{
/**
* Renders a checkbox.
* @return string the rendered content
*/
protected function checkBox()
{
echo '<div class="controls">';
echo '<label class="checkbox" for="'.CHtml::getIdByName(CHtml::resolveName($this->model, $this->attribute)).'">';
echo $this->form->checkBox($this->model, $this->attribute, $this->htmlOptions).PHP_EOL;
echo $this->model->getAttributeLabel($this->attribute);
echo $this->getError().$this->getHint();
echo '</label></div>';
}
/**
* Renders a list of checkboxes.
* @return string the rendered content
*/
protected function checkBoxList()
{
echo $this->getLabel().'<div class="controls">';
echo $this->form->checkBoxList($this->model, $this->attribute, $this->data, $this->htmlOptions);
echo $this->getError().$this->getHint();
echo '</div>';
}
/**
* Renders a list of inline checkboxes.
* @return string the rendered content
*/
protected function checkBoxListInline()
{
$this->htmlOptions['inline'] = true;
$this->checkBoxList();
}
/**
* Renders a drop down list (select).
* @return string the rendered content
*/
protected function dropDownList()
{
echo $this->getLabel().'<div class="controls">';
echo $this->form->dropDownList($this->model, $this->attribute, $this->data, $this->htmlOptions);
echo $this->getError().$this->getHint();
echo '</div>';
}
/**
* Renders a file field.
* @return string the rendered content
*/
protected function fileField()
{
echo $this->getLabel().'<div class="controls">';
echo $this->form->fileField($this->model, $this->attribute, $this->htmlOptions);
echo $this->getError().$this->getHint();
echo '</div>';
}
/**
* Renders a password field.
* @return string the rendered content
*/
protected function passwordField()
{
echo $this->getLabel().'<div class="controls">';
echo $this->form->passwordField($this->model, $this->attribute, $this->htmlOptions);
echo $this->getError().$this->getHint();
echo '</div>';
}
/**
* Renders a radio button.
* @return string the rendered content
*/
protected function radioButton()
{
echo '<div class="controls">';
echo '<label class="radio" for="'.CHtml::getIdByName(CHtml::resolveName($this->model, $this->attribute)).'">';
echo $this->form->radioButton($this->model, $this->attribute, $this->htmlOptions).PHP_EOL;
echo $this->model->getAttributeLabel($this->attribute);
echo $this->getError().$this->getHint();
echo '</label></div>';
}
/**
* Renders a list of radio buttons.
* @return string the rendered content
*/
protected function radioButtonList()
{
echo $this->getLabel().'<div class="controls">';
echo $this->form->radioButtonList($this->model, $this->attribute, $this->data, $this->htmlOptions);
echo $this->getError().$this->getHint();
echo '</div>';
}
/**
* Renders a list of inline radio buttons.
* @return string the rendered content
*/
protected function radioButtonListInline()
{
$this->htmlOptions['inline'] = true;
$this->radioButtonList();
}
/**
* Renders a textarea.
* @return string the rendered content
*/
protected function textArea()
{
echo $this->getLabel().'<div class="controls">';
echo $this->form->textArea($this->model, $this->attribute, $this->htmlOptions);
echo $this->getError().$this->getHint();
echo '</div>';
}
/**
* Renders a text field.
* @return string the rendered content
*/
protected function textField()
{
echo $this->getLabel().'<div class="controls">';
echo $this->form->textField($this->model, $this->attribute, $this->htmlOptions);
echo $this->getError().$this->getHint();
echo '</div>';
}
/**
* Renders a CAPTCHA.
* @return string the rendered content
*/
protected function captcha()
{
echo $this->getLabel().'<div class="controls"><div class="captcha">';
echo '<div class="widget">'.$this->widget('CCaptcha', array('showRefreshButton'=>false), true).'</div>';
echo $this->form->textField($this->model, $this->attribute, $this->htmlOptions);
echo $this->getError().$this->getHint();
echo '</div></div>';
}
/**
* Renders an uneditable field.
* @return string the rendered content
*/
protected function uneditableField()
{
echo $this->getLabel().'<div class="controls">';
echo '<span class="uneditable-input">'.$this->model->{$this->attribute}.'</span>';
echo $this->getError().$this->getHint();
echo '</div>';
}
/**
* Returns the label for this block.
* @return string the label
*/
protected function getLabel()
{
if ($this->label !== false && !in_array($this->type, array('checkbox', 'radio')) && $this->hasModel())
return $this->form->labelEx($this->model, $this->attribute);
else if ($this->label !== null)
return $this->label;
else
return '';
}
/**
* Returns the hint text for this block.
* @return string the hint text
*/
protected function getHint()
{
if (isset($this->htmlOptions['hint']))
{
$hint = $this->htmlOptions['hint'];
unset($this->htmlOptions['hint']);
return '<p class="help-block">'.$hint.'</p>';
}
else
return '';
}
/**
* Returns the error text for this block.
* @return string the error text
*/
protected function getError()
{
return $this->form->error($this->model, $this->attribute);
}
}
......@@ -56,17 +56,11 @@ class BootMenu extends BootWidget
*/
public function init()
{
$id = $this->getId();
if (isset($this->htmlOptions['id']))
$id = $this->htmlOptions['id'];
else
$this->htmlOptions['id'] = $id;
if (!isset($this->htmlOptions['id']))
$this->htmlOptions['id'] = $this->getId();
$route = $this->controller->getRoute();
$this->items = $this->normalizeItems($this->items, $route);
if ($this->scrollspy)
Yii::app()->clientScript->registerScript(__CLASS__.'#'.$this->id, "jQuery('#{$id}').scrollspy();");
}
/**
......@@ -92,6 +86,11 @@ class BootMenu extends BootWidget
echo CHtml::openTag('ul', $this->htmlOptions).PHP_EOL;
$this->renderItems($this->items);
echo '</ul>';
Yii::app()->bootstrap->registerDropdown();
if ($this->scrollspy)
Yii::app()->bootstrap->registerScrollSpy('#'.$this->id);
}
}
......
......@@ -20,8 +20,19 @@ class BootModal extends BootWidget
public function init()
{
parent::init();
$this->registerScriptFile('bootstrap-modal.js');
$this->htmlOptions['id'] = $this->getId();
Yii::app()->bootstrap->registerModal();
if (!isset($this->htmlOptions['id']))
$this->htmlOptions['id'] = $this->getId();
if (isset($this->htmlOptions['class']))
$this->htmlOptions['class'] .= ' modal';
else
$this->htmlOptions['class'] = 'modal';
if (Yii::app()->bootstrap->isPluginRegistered(Bootstrap::PLUGIN_TRANSITION))
$this->htmlOptions['class'] .= ' fade';
echo CHtml::openTag('div', $this->htmlOptions).PHP_EOL;
}
......@@ -37,28 +48,32 @@ class BootModal extends BootWidget
if (isset($this->events['show']))
{
$fn = CJavaScript::encode($this->events['show']);
Yii::app()->clientScript->registerScript(__CLASS__.'#'.$this->id.'.show', "jQuery('#{$this->id}').on('show', {$fn});");
Yii::app()->clientScript->registerScript(__CLASS__.'#'.$this->id.'.show',
"jQuery('#{$this->id}').on('show', {$fn});");
}
// Register the "shown" event-handler.
if (isset($this->events['shown']))
{
$fn = CJavaScript::encode($this->events['shown']);
Yii::app()->clientScript->registerScript(__CLASS__.'#'.$this->id.'.shown', "jQuery('#{$this->id}').on('shown', {$fn});");
Yii::app()->clientScript->registerScript(__CLASS__.'#'.$this->id.'.shown',
"jQuery('#{$this->id}').on('shown', {$fn});");
}
// Register the "hide" event-handler.
if (isset($this->events['hide']))
{
$fn = CJavaScript::encode($this->events['hide']);
Yii::app()->clientScript->registerScript(__CLASS__.'#'.$this->id.'.hide', "jQuery('#{$this->id}').on('hide', {$fn});");
Yii::app()->clientScript->registerScript(__CLASS__.'#'.$this->id.'.hide',
"jQuery('#{$this->id}').on('hide', {$fn});");
}
// Register the "hidden" event-handler.
if (isset($this->events['hidden']))
{
$fn = CJavaScript::encode($this->events['hidden']);
Yii::app()->clientScript->registerScript(__CLASS__.'#'.$this->id.'.hidden', "jQuery('#{$this->id}').on('hidden', {$fn});");
Yii::app()->clientScript->registerScript(__CLASS__.'#'.$this->id.'.hidden',
"jQuery('#{$this->id}').on('hidden', {$fn});");
}
}
}
......@@ -42,10 +42,9 @@ class BootNavbar extends BootWidget
*/
public $fixed = true;
/**
* @var boolean whether to enable collapsing on narrow screens.
* Enabled by default if the collapse plugin is registered.
* @var boolean whether to enable collapsing on narrow screens. Default to false.
*/
public $collapse = null;
public $collapse = false;
/**
* Initializes the widget.
......@@ -60,8 +59,8 @@ class BootNavbar extends BootWidget
$this->brandUrl = Yii::app()->homeUrl;
}
if (!isset($this->collapse))
$this->collapse = Yii::app()->bootstrap->isPluginRegistered('collapse');
if ($this->collapse)
Yii::app()->bootstrap->registerCollapse();
}
/**
......
......@@ -25,7 +25,9 @@ class BootTabbed extends BootWidget
* @var array the tab configuration.
*/
public $tabs = array();
/**
* @var boolean whether to encode item labels.
*/
public $encodeLabel = true;
/**
......@@ -34,7 +36,11 @@ class BootTabbed extends BootWidget
public function init()
{
parent::init();
$this->registerScriptFile('bootstrap-tab.js');
Yii::app()->bootstrap->registerTabs();
if (!isset($this->htmlOptions['id']))
$this->htmlOptions['id'] = $this->getId();
}
/**
......@@ -42,12 +48,6 @@ class BootTabbed extends BootWidget
*/
public function run()
{
$id = $this->getId();
if (isset($this->htmlOptions['id']))
$id = $this->htmlOptions['id'];
else
$this->htmlOptions['id'] = $id;
$panes = array();
$items = $this->normalizeTabs($this->tabs, $panes);
......@@ -55,6 +55,7 @@ class BootTabbed extends BootWidget
$this->controller->widget('bootstrap.widgets.BootMenu', array(
'type'=>$this->type,
'encodeLabel'=>$this->encodeLabel,
'items'=>$items,
));
......@@ -62,7 +63,7 @@ class BootTabbed extends BootWidget
echo implode('', $panes);
echo '</div></div>';
$this->registerScript(__CLASS__.'#'.$id, "jQuery('{$id}').tab('show');");
$this->registerScript(__CLASS__.'#'.$this->id, "jQuery('{$this->id}').tab('show');");
/*
// Register the "show" event-handler.
......@@ -93,24 +94,25 @@ class BootTabbed extends BootWidget
protected function normalizeTabs($tabs, &$panes, &$i = 0)
{
$id = $this->getId();
$transitions = Yii::app()->bootstrap->transitions;
$transitions = Yii::app()->bootstrap->isPluginRegistered(Bootstrap::PLUGIN_TRANSITION);
$items = array();
foreach ($tabs as &$tab)
foreach ($tabs as $tab)
{
$i++;
$item = array();
$item = $tab;
$itemId = isset($tab['id']) ? $tab['id'] : $id.'_tab_'.$i;
if (!isset($item['id']))
$item['id'] = $id.'_tab_'.$i;
$item['label'] = isset($tab['label']) ? $tab['label'] : '';
if (!isset($item['label']))
$item['label'] = '';
if (!isset($tab['items']))
$item['url'] = '#'.$itemId;
$item['url'] = '#'.$item['id'];
if (isset($tab['itemOptions']))
$item['itemOptions'] = $tab['itemOptions'];
if (!isset($item['itemOptions']))
$item['itemOptions'] = array();
if ($i === 1)
{
......@@ -120,37 +122,46 @@ class BootTabbed extends BootWidget
$item['itemOptions']['class'] = 'active';
}
$item['linkOptions']['data-toggle'] = 'tab';
if (isset($tab['items']))
$item['items'] = $this->normalizeTabs($tab['items'], $panes, $i);
{
$item['items'] = $this->normalizeTabs($item['items'], $panes, $i);
unset($item['url']);
}
if (!isset($item['content']))
$item['content'] = '';
$item['linkOptions']['data-toggle'] = 'tab';
$content = $item['content'];
unset($item['content']);
if (!isset($tab['content']))
$tab['content'] = '';
if (!isset($item['paneOptions']))
$item['paneOptions'] = array();
if (!isset($tab['paneOptions']))
$tab['paneOptions'] = array();
$paneOptions = $item['paneOptions'];
unset($item['paneOptions']);
$tab['paneOptions']['id'] = $itemId;
$paneOptions['id'] = $item['id'];
if (isset($tab['paneOptions']['class']))
$tab['paneOptions']['class'] .= ' tab-pane';
$paneOptions['class'] .= ' tab-pane';
else
$tab['paneOptions']['class'] = 'tab-pane';
$paneOptions['class'] = 'tab-pane';
if ($transitions)
$tab['paneOptions']['class'] .= ' fade';
$paneOptions['class'] .= ' fade';
if ($i === 1)
{
if ($transitions)
$tab['paneOptions']['class'] .= ' in';
$paneOptions['class'] .= ' in';
$tab['paneOptions']['class'] .= ' active';
$paneOptions['class'] .= ' active';
}
$items[] = $item;
$panes[] = CHtml::tag('div', $tab['paneOptions'], $tab['content']);
$panes[] = CHtml::tag('div', $paneOptions, $content);
$items[] = $item;
}
return $items;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment