Commits

Christoffer Niska  committed b2bd7aa

improved event handling and fixed carousel binding (fixes #78)

  • Participants
  • Parent commits 565d046

Comments (0)

Files changed (4)

File widgets/BootAlert.php

 
 		echo '</div>';
 
+		$selector = "#{$id} .alert";
+		$id .= '_'.uniqid(true, true);
+
 		/** @var CClientScript $cs */
 		$cs = Yii::app()->getClientScript();
-		$selector = "#{$id} .alert";
 		$cs->registerScript(__CLASS__.'#'.$id, "jQuery('{$selector}').alert();");
 
-		// Register the "close" event-handler.
-		if (isset($this->events['close']))
+		foreach ($this->events as $name => $handler)
 		{
-			$fn = CJavaScript::encode($this->events['close']);
-			$cs->registerScript(__CLASS__.'#'.$id.'.close', "jQuery('{$selector}').bind('close', {$fn});");
-		}
-
-		// Register the "closed" event-handler.
-		if (isset($this->events['closed']))
-		{
-			$fn = CJavaScript::encode($this->events['closed']);
-			$cs->registerScript(__CLASS__.'#'.$id.'.closed', "jQuery('{$selector}').bind('closed', {$fn});");
+			$handler = CJavaScript::encode($handler);
+			$cs->registerScript(__CLASS__.'#'.$id.'_'.$name, "jQuery('{$selector}').on('".$name."', {$handler});");
 		}
 	}
 }

File widgets/BootCarousel.php

 		/** @var CClientScript $cs */
 		$cs = Yii::app()->getClientScript();
 		$options = !empty($this->options) ? CJavaScript::encode($this->options) : '';
-		$cs->registerScript(__CLASS__.'#'.$id, "jQuery('{$id}').carousel({$options});");
+		$cs->registerScript(__CLASS__.'#'.$id, "jQuery('#{$id}').carousel({$options});");
 
-        // Register the "slide" event-handler.
-        if (isset($this->events['slide']))
-        {
-            $fn = CJavaScript::encode($this->events['slide']);
-	        $cs->registerScript(__CLASS__.'#'.$id.'.slide', "jQuery('#{$id}').on('slide', {$fn});");
-        }
-
-        // Register the "slid" event-handler.
-        if (isset($this->events['slid']))
-        {
-            $fn = CJavaScript::encode($this->events['slid']);
-	        $cs->registerScript(__CLASS__.'#'.$id.'.slid', "jQuery('#{$id}').on('slid', {$fn});");
-        }
+		foreach ($this->events as $name => $handler)
+		{
+			$handler = CJavaScript::encode($handler);
+			$cs->registerScript(__CLASS__.'#'.$id.'_'.$name, "jQuery('#{$id}').on('".$name."', {$handler});");
+		}
 	}
 
 	/**

File widgets/BootModal.php

 	 */
 	public function init()
 	{
-		parent::init();
+		if (!isset($this->htmlOptions['id']))
+			$this->htmlOptions['id'] = $this->getId();
 
 		if (!$this->autoOpen && !isset($this->options['show']))
 			$this->options['show'] = false;
 
-		if (!isset($this->htmlOptions['id']))
-			$this->htmlOptions['id'] = $this->getId();
-
 		$classes = 'modal fade';
 		if (isset($this->htmlOptions['class']))
 			$this->htmlOptions['class'] .= ' '.$classes;
 		/** @var CClientScript $cs */
 		$cs = Yii::app()->getClientScript();
 
-		$options = CJavaScript::encode($this->options);
+		$options = !empty($this->options) ? CJavaScript::encode($this->options) : '';
 		$cs->registerScript(__CLASS__.'#'.$id, "jQuery('#{$id}').modal({$options});");
 
-		// Register the "show" event-handler.
-		if (isset($this->events['show']))
+		foreach ($this->events as $name => $handler)
 		{
-			$fn = CJavaScript::encode($this->events['show']);
-			$cs->registerScript(__CLASS__.'#'.$id.'.show', "jQuery('#{$id}').on('show', {$fn});");
-		}
-
-		// Register the "shown" event-handler.
-		if (isset($this->events['shown']))
-		{
-			$fn = CJavaScript::encode($this->events['shown']);
-			$cs->registerScript(__CLASS__.'#'.$id.'.shown', "jQuery('#{$id}').on('shown', {$fn});");
-		}
-
-		// Register the "hide" event-handler.
-		if (isset($this->events['hide']))
-		{
-			$fn = CJavaScript::encode($this->events['hide']);
-			$cs->registerScript(__CLASS__.'#'.$id.'.hide', "jQuery('#{$id}').on('hide', {$fn});");
-		}
-
-		// Register the "hidden" event-handler.
-		if (isset($this->events['hidden']))
-		{
-			$fn = CJavaScript::encode($this->events['hidden']);
-			$cs->registerScript(__CLASS__.'#'.$id.'.hidden', "jQuery('#{$id}').on('hidden', {$fn});");
+			$handler = CJavaScript::encode($handler);
+			$cs->registerScript(__CLASS__.'#'.$id.'_'.$name, "jQuery('#{$id}').on('".$name."', {$handler});");
 		}
 	}
 }

File widgets/BootTabbable.php

 	    $cs = Yii::app()->getClientScript();
 	    $cs->registerScript(__CLASS__.'#'.$id, "jQuery('#{$id}').tab('show');");
 
-        // Register the "show" event-handler.
-        if (isset($this->events['show']))
+	    foreach ($this->events as $name => $handler)
         {
-            $fn = CJavaScript::encode($this->events['show']);
-	        $cs->registerScript(__CLASS__.'#'.$id.'.show', "jQuery('#{$id} a[data-toggle=\"tab\"]').on('show', {$fn});");
-        }
-
-        // Register the "shown" event-handler.
-        if (isset($this->events['shown']))
-        {
-            $fn = CJavaScript::encode($this->events['shown']);
-	        $cs->registerScript(__CLASS__.'#'.$id.'.shown', "jQuery('#{$id} a[data-toggle=\"tab\"]').on('shown', {$fn});");
+            $handler = CJavaScript::encode($handler);
+            $cs->registerScript(__CLASS__.'#'.$id.'_'.$name, "jQuery('#{$id}').on('".$name."', {$handler});");
         }
     }