Christoffer Niska avatar Christoffer Niska committed 58e8721

rewrote the alert widget

Comments (0)

Files changed (2)

demo/protected/views/site/index.php

     ?>
 
     <?php $this->widget('bootstrap.widgets.TbAlert', array(
-    'events'=>array(
-        'close'=>"js:function() { console.log('Alert close.'); }",
-        'closed'=>"js:function() { console.log('Alert closed.'); }",
-    ),
-)); ?>
+		'events'=>array(
+			'close'=>"js:function() { console.log('Alert close.'); }",
+			'closed'=>"js:function() { console.log('Alert closed.'); }",
+		),
+	)); ?>
 
     <h4>Source code</h4>
 
 Yii::app()->user->setFlash('warning', '<strong>Warning!</strong> Best check yo self, you\'re not looking too good.');
 Yii::app()->user->setFlash('error', '<strong>Oh snap!</strong> Change a few things up and try submitting again.');"); ?>
 
-    <?php echo $phpLighter->highlight("<?php \$this->widget('bootstrap.widgets.TbAlert'); ?>"); ?>
+	<?php echo $phpLighter->highlight("<?php \$this->widget('bootstrap.widgets.TbAlert', array(
+		'block'=>true, // display a larger alert block?
+		'fade'=>true, // use transitions?
+		'closeText'=>'&times;', // close link text - if set to false, no close link is displayed
+		'alerts'=>array( // configurations per alert type
+			'success'=>array('block'=>true, 'fade'=>true, 'closeText'=>'&times;'), // success, info, warning, error or danger
+		),
+	); ?>"); ?>
 
     <a class="top" href="#top">Back to top &uarr;</a>
 
             <h3>Animated</h3>
 
             <?php $this->widget('bootstrap.widgets.TbProgress', array(
-            'type'=>'danger', // '', 'info', 'success' or 'danger'
+            'type'=>'danger', // 'info', 'success' or 'danger'
             'percent'=>40, // the progress
             'striped'=>true,
             'animated'=>true,
     <h4>Source code</h4>
 
     <?php echo $phpLighter->highlight("<?php \$this->widget('bootstrap.widgets.TbProgress', array(
-	'type'=>'danger', // '', 'info', 'success' or 'danger'
+	'type'=>'danger', // 'info', 'success' or 'danger'
 	'percent'=>40, // the progress
 	'striped'=>true,
 	'animated'=>true,
     <h4>Source code</h4>
 
     <?php echo $phpLighter->highlight("<?php \$this->widget('bootstrap.widgets.TbLabel', array(
-	'type'=>'success', // '', 'success', 'warning', 'important', 'info' or 'inverse'
+	'type'=>'success', // 'success', 'warning', 'important', 'info' or 'inverse'
 	'label'=>'Success',
 )); ?>"); ?>
 
     <h4>Source code</h4>
 
     <?php echo $phpLighter->highlight("<?php \$this->widget('bootstrap.widgets.TbBadge', array(
-	'type'=>'success', // '', 'success', 'warning', 'important', 'info' or 'inverse'
+	'type'=>'success', // 'success', 'warning', 'important', 'info' or 'inverse'
 	'label'=>'2',
 )); ?>"); ?>
 

widgets/TbAlert.php

  */
 class TbAlert extends CWidget
 {
+	// Alert types.
+	const TYPE_SUCCESS = 'success';
+	const TYPE_INFO = 'info';
+	const TYPE_WARNING = 'warning';
+	const TYPE_ERROR = 'error';
+	const TYPE_DANGER = 'danger'; // same as error
+
 	/**
-	 * @var array the keys for which to get flash messages.
+	 * @var array the alerts configurations.
 	 */
-	public $keys = array('success', 'info', 'warning', 'error', /* or */'danger');
+	public $alerts;
 	/**
-	 * @var string the template to use for displaying flash messages.
+	 * @var string the string to use for the close link. If this is set false, no close link will be displayed.
 	 */
-	public $template = '<div class="alert alert-block alert-{key}{class}"><a class="close" data-dismiss="alert">&times;</a>{message}</div>';
+	public $closeText = '&times;';
+	/**
+	 * @var boolean indicates whether the alert should be an alert block. Defaults to 'true'.
+	 */
+	public $block = true;
+	/**
+	 * @var boolean indicates whether the modal should use transitions. Defaults to 'true'.
+	 */
+	public $fade = true;
 	/**
 	 * @var string[] the Javascript event handlers.
 	 */
 	{
 		if (!isset($this->htmlOptions['id']))
 			$this->htmlOptions['id'] = $this->getId();
+
+		if (is_string($this->alerts))
+			$this->alerts = array($this->alerts);
+
+		// Display all alert types by default.
+		if (!isset($this->alerts))
+			$this->alerts = array(self::TYPE_SUCCESS, self::TYPE_INFO, self::TYPE_WARNING, self::TYPE_ERROR, self::TYPE_DANGER);
 	}
 
 	/**
 	{
 		$id = $this->htmlOptions['id'];
 
-		if (is_string($this->keys))
-			$this->keys = array($this->keys);
-
 		echo CHtml::openTag('div', $this->htmlOptions);
 
-		foreach ($this->keys as $key)
+		foreach ($this->alerts as $type => $alert)
 		{
-			if (Yii::app()->user->hasFlash($key))
+			if (is_string($alert))
 			{
-				echo strtr($this->template, array(
-					'{class}'=>' fade in',
-					'{key}'=>$key,
-					'{message}'=>Yii::app()->user->getFlash($key),
-				));
+				$type = $alert;
+				$alert = array();
+			}
+
+			if (isset($alert['visible']) && $alert['visible'] === false)
+				continue;
+
+			if (Yii::app()->user->hasFlash($type))
+			{
+				$classes = array('alert in');
+
+				if (!isset($alert['block']))
+					$alert['block'] = $this->block;
+
+				if ($alert['block'] === true)
+					$classes[] = 'alert-block';
+
+				if (!isset($alert['fade']))
+					$alert['fade'] = $this->fade;
+
+				if ($alert['fade'] === true)
+					$classes[] = 'fade';
+
+				$validTypes = array(self::TYPE_SUCCESS, self::TYPE_INFO, self::TYPE_WARNING, self::TYPE_ERROR, self::TYPE_DANGER);
+
+				if (in_array($type, $validTypes))
+					$classes[] = 'alert-'.$type;
+
+				if (!isset($alert['htmlOptions']))
+					$alert['htmlOptions'] = array();
+
+				$classes = implode(' ', $classes);
+				if (isset($alert['htmlOptions']['class']))
+					$alert['htmlOptions']['class'] .= ' '.$classes;
+				else
+					$alert['htmlOptions']['class'] = $classes;
+
+				echo CHtml::openTag('div', $alert['htmlOptions']);
+
+				if ($this->closeText !== false && !isset($alert['closeText']))
+					$alert['closeText'] = $this->closeText;
+
+				if ($alert['closeText'] !== false)
+					echo '<a class="close" data-dismiss="alert">'.$alert['closeText'].'</a>';
+
+				echo Yii::app()->user->getFlash($type);
+
+				echo '</div>';
 			}
 		}
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.