浏览代码

Fix namespaces for icons.

dereuromark 8 年之前
父节点
当前提交
3ddf7c40ab
共有 3 个文件被更改,包括 61 次插入10 次删除
  1. 6 1
      src/Template/Element/pagination.ctp
  2. 41 7
      src/View/Helper/FormatHelper.php
  3. 14 2
      tests/TestCase/View/Helper/FormatHelperTest.php

+ 6 - 1
src/Template/Element/pagination.ctp

@@ -41,7 +41,9 @@ if (!empty($addArrows)) {
 $escape = isset($escape) ? $escape : true;
 ?>
 
-<div class="paginator paging">
+<div class="paginator paging row">
+	<div class="col-lg-6">
+
 	<ul class="pagination">
 	<?php echo $this->Paginator->first($first, ['escape' => $escape]);?>
  <?php echo $separator; ?>
@@ -54,9 +56,12 @@ $escape = isset($escape) ? $escape : true;
 	<?php echo $this->Paginator->last($last, ['escape' => $escape]);?>
 	</ul>
 
+	</div>
+	<div class="col-lg-6">
 	<p class="paging-description">
 		<?php echo $this->Paginator->counter(['format' => $format]); ?>
 	</p>
+	</div>
 </div>
 <?php if (!empty($options['ajaxPagination'])) {
 	$ajaxContainer = !empty($options['paginationContainer']) ? $options['paginationContainer'] : '.page';

+ 41 - 7
src/View/Helper/FormatHelper.php

@@ -56,6 +56,7 @@ class FormatHelper extends Helper {
 	 */
 	protected $_defaults = [
 		'fontIcons' => null,
+		'iconNamespaces' => [], // Used to disable auto prefixing if detected
 		'iconNamespace' => 'fa', // Used to be icon,
 		'autoPrefix' => true, // For custom icons "prev" becomes "fa-prev" when iconNamespace is "fa"
 		'templates' => [
@@ -216,7 +217,7 @@ class FormatHelper extends Helper {
 
 	/**
 	 * Display a font icon (fast and resource-efficient).
-	 * Uses http://fontawesome.io/icons/
+	 * Uses http://fontawesome.io/icons/ by default
 	 *
 	 * Options:
 	 * - size (int|string: 1...5 or large)
@@ -261,7 +262,7 @@ class FormatHelper extends Helper {
 	}
 
 	/**
-	 * Icons using the default namespace
+	 * Icons using the default namespace or an already prefixed one.
 	 *
 	 * @param string $icon (constant or filename)
 	 * @param array $options :
@@ -271,11 +272,25 @@ class FormatHelper extends Helper {
 	 * @return string
 	 */
 	public function icon($icon, array $options = [], array $attributes = []) {
+		if (!$icon) {
+			return '';
+		}
+
 		$defaults = [
 			'translate' => true,
 		];
 		$options += $defaults;
 
+		$type = $icon;
+		if ($this->config('autoPrefix') && empty($options['iconNamespace'])) {
+			$namespace = $this->detectNamespace($icon);
+			if ($namespace) {
+				$options['iconNamespace'] = $namespace;
+				$options['autoPrefix'] = false;
+				$icon = substr($icon, strlen($namespace) + 1);
+			}
+		}
+
 		if (!isset($attributes['title'])) {
 			if (isset($options['title'])) {
 				$attributes['title'] = $options['title'];
@@ -284,7 +299,24 @@ class FormatHelper extends Helper {
 			}
 		}
 
-		return $this->_fontIcon($icon, $options, $attributes);
+		return $this->_fontIcon($type, $options, $attributes);
+	}
+
+	/**
+	 * @param string $icon
+	 *
+	 * @return string|null
+	 */
+	protected function detectNamespace($icon) {
+		foreach ((array)$this->config('iconNamespaces') as $namespace) {
+			if (strpos($icon, $namespace . '-') !== 0) {
+				continue;
+			}
+
+			return $namespace;
+		}
+
+		return null;
 	}
 
 	/**
@@ -344,11 +376,13 @@ class FormatHelper extends Helper {
 	 */
 	protected function _fontIcon($type, $options, $attributes) {
 		$iconClass = $type;
-		if ($this->_config['autoPrefix'] && $this->_config['iconNamespace']) {
-			$iconClass = $this->_config['iconNamespace'] . '-' . $iconClass;
+
+		$options += $this->_config;
+		if ($options['autoPrefix'] && $options['iconNamespace']) {
+			$iconClass = $options['iconNamespace'] . '-' . $iconClass;
 		}
-		if ($this->_config['iconNamespace']) {
-			$iconClass = $this->_config['iconNamespace'] . ' ' . $iconClass;
+		if ($options['iconNamespace']) {
+			$iconClass = $options['iconNamespace'] . ' ' . $iconClass;
 		}
 
 		if (isset($this->_config['fontIcons'][$type])) {

+ 14 - 2
tests/TestCase/View/Helper/FormatHelperTest.php

@@ -89,8 +89,6 @@ class FormatHelperTest extends TestCase {
 	}
 
 	/**
-	 * FormatHelperTest::testCIconWithFontIcon()
-	 *
 	 * @return void
 	 */
 	public function testIconWithCustomFontIcon() {
@@ -103,6 +101,20 @@ class FormatHelperTest extends TestCase {
 	/**
 	 * @return void
 	 */
+	public function testIconWithPrefixedIcon() {
+		$this->Format->config('iconNamespaces', ['fa', 'glyphicon']);
+		$result = $this->Format->icon('glyphicon-foo');
+		$expected = '<i class="icon icon-glyphicon-foo glyphicon glyphicon-foo" title="' . __d('tools', 'Foo') . '" data-placement="bottom" data-toggle="tooltip"></i>';
+		$this->assertEquals($expected, $result);
+
+		$result = $this->Format->icon('glyphicon-edit');
+		$expected = '<i class="icon icon-glyphicon-edit glyphicon glyphicon-edit" title="' . __d('tools', 'Edit') . '" data-placement="bottom" data-toggle="tooltip"></i>';
+		$this->assertEquals($expected, $result);
+	}
+
+	/**
+	 * @return void
+	 */
 	public function testFontIcon() {
 		$result = $this->Format->fontIcon('signin');
 		$expected = '<i class="fa fa-signin"></i>';