Commit 368a4cfe by Qiang Xue

Asset feature refactoring (WIP)

parent dc2843a2
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace backend\config;
use yii\web\AssetBundle;
/**
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class AppAsset extends AssetBundle
{
public $basePath = '@wwwroot';
public $baseUrl = '@www';
public $css = array(
'css/site.css',
);
public $js = array(
);
public $depends = array(
'yii\web\YiiAsset',
'yii\bootstrap\ResponsiveAsset',
);
}
<?php
return array(
'app' => array(
'basePath' => '@wwwroot',
'baseUrl' => '@www',
'css' => array(
'css/site.css',
),
'js' => array(
),
'depends' => array(
'yii',
'yii/bootstrap/responsive',
),
),
);
......@@ -23,9 +23,6 @@ return array(
'class' => 'yii\web\User',
'identityClass' => 'common\models\User',
),
'assetManager' => array(
'bundles' => require(__DIR__ . '/assets.php'),
),
'log' => array(
'targets' => array(
array(
......
<?php
use backend\config\AppAsset;
use yii\helpers\Html;
use yii\widgets\Menu;
use yii\widgets\Breadcrumbs;
......@@ -7,7 +8,7 @@ use yii\widgets\Breadcrumbs;
* @var $this \yii\base\View
* @var $content string
*/
$this->registerAssetBundle('app');
AppAsset::register($this);
?>
<?php $this->beginPage(); ?>
<!DOCTYPE html>
......
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace frontend\config;
use yii\web\AssetBundle;
/**
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class AppAsset extends AssetBundle
{
public $basePath = '@wwwroot';
public $baseUrl = '@www';
public $css = array(
'css/site.css',
);
public $js = array(
);
public $depends = array(
'yii\web\YiiAsset',
'yii\bootstrap\ResponsiveAsset',
);
}
<?php
return array(
'app' => array(
'basePath' => '@wwwroot',
'baseUrl' => '@www',
'css' => array(
'css/site.css',
),
'js' => array(
),
'depends' => array(
'yii',
'yii/bootstrap/responsive',
),
),
);
......@@ -22,9 +22,6 @@ return array(
'class' => 'yii\web\User',
'identityClass' => 'common\models\User',
),
'assetManager' => array(
'bundles' => require(__DIR__ . '/assets.php'),
),
'log' => array(
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => array(
......
<?php
use frontend\config\AppAsset;
use yii\helpers\Html;
use yii\widgets\Menu;
use yii\widgets\Breadcrumbs;
......@@ -7,7 +8,7 @@ use yii\widgets\Breadcrumbs;
* @var $this \yii\base\View
* @var $content string
*/
$this->registerAssetBundle('app');
AppAsset::register($this);
?>
<?php $this->beginPage(); ?>
<!DOCTYPE html>
......
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace app\config;
use yii\web\AssetBundle;
/**
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class AppAsset extends AssetBundle
{
public $basePath = '@wwwroot';
public $baseUrl = '@www';
public $css = array(
'css/site.css',
);
public $js = array(
);
public $depends = array(
'yii\web\YiiAsset',
'yii\bootstrap\ResponsiveAsset',
);
}
<?php
return array(
'app' => array(
'basePath' => '@wwwroot',
'baseUrl' => '@www',
'css' => array(
'css/site.css',
),
'js' => array(
),
'depends' => array(
'yii',
'yii/bootstrap/responsive',
),
),
);
......@@ -18,9 +18,6 @@ return array(
'class' => 'yii\web\User',
'identityClass' => 'app\models\User',
),
'assetManager' => array(
'bundles' => require(__DIR__ . '/assets.php'),
),
'log' => array(
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => array(
......
......@@ -7,7 +7,7 @@ use yii\widgets\Breadcrumbs;
* @var $this \yii\base\View
* @var $content string
*/
$this->registerAssetBundle('app');
app\config\AppAsset::register($this);
?>
<?php $this->beginPage(); ?>
<!DOCTYPE html>
......
......@@ -311,9 +311,10 @@ Yii 2.0 introduces a new concept called *asset bundle*. It is similar to script
packages (managed by `CClientScript`) in 1.1, but with better support.
An asset bundle is a collection of asset files (e.g. JavaScript files, CSS files, image files, etc.)
under a directory. By registering an asset bundle via `View::registerAssetBundle()`, you
will be able to make the assets in that bundle accessible via Web, and the current page
will automatically contain references to the JavaScript and CSS files in that bundle.
under a directory. Each asset bundle is represented as a class extending `AssetBundle`.
By registering an asset bundle via `AssetBundle::register()`, you will be able to make
the assets in that bundle accessible via Web, and the current page will automatically
contain the references to the JavaScript and CSS files specified in that bundle.
......
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\jui;
use yii\web\AssetBundle;
/**
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class CoreAsset extends AssetBundle
{
public $sourcePath = '@yii/jui/assets';
public $js = array(
'jquery.ui.core.js',
);
public $depends = array(
'yii\web\JqueryAsset',
);
}
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\jui;
use yii\web\AssetBundle;
/**
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class WidgetAsset extends AssetBundle
{
public $sourcePath = '@yii/jui/assets';
public $js = array(
'jquery.ui.widget.js',
);
public $depends = array(
'yii\web\JqueryAsset',
);
}
<?php
return array(
'yii/jui/core' => array(
'sourcePath' => __DIR__ . '/assets',
'js' => array(
'jquery.ui.core.js',
),
'depends' => array('yii/jquery'),
),
'yii/jui/widget' => array(
'sourcePath' => __DIR__ . '/assets',
'js' => array(
'jquery.ui.widget.js',
),
'depends' => array('yii/jquery'),
),
yii\jui\CoreAsset::className(),
yii\jui\WidgetAsset::className(),
'yii/jui/accordion' => array(
'sourcePath' => __DIR__ . '/assets',
'js' => array(
......
......@@ -69,7 +69,7 @@ class YiiBase
* @see getAlias
* @see setAlias
*/
public static $aliases;
public static $aliases = array('@yii' => __DIR__);
/**
* @var array initial property values that will be applied to objects newly created via [[createObject]].
* The array keys are class names without leading backslashes "\", and the array values are the corresponding
......@@ -616,10 +616,3 @@ class YiiBase
return get_object_vars($object);
}
}
YiiBase::$aliases = array(
'@yii' => array(
'@yii/bootstrap' => __DIR__ . '/bootstrap',
'@yii' => __DIR__,
),
);
<?php
return array(
'yii' => array(
'sourcePath' => __DIR__ . '/assets',
'js' => array(
'yii.js',
),
'depends' => array('yii/jquery'),
),
'yii/jquery' => array(
'sourcePath' => __DIR__ . '/assets',
'js' => array(
YII_DEBUG ? 'jquery.js' : 'jquery.min.js',
),
),
'yii/validation' => array(
'sourcePath' => __DIR__ . '/assets',
'js' => array(
'yii.validation.js',
),
'depends' => array('yii'),
),
'yii/form' => array(
'sourcePath' => __DIR__ . '/assets',
'js' => array(
'yii.activeForm.js',
),
'depends' => array('yii'),
),
'yii/captcha' => array(
'sourcePath' => __DIR__ . '/assets',
'js' => array(
'yii.captcha.js',
),
'depends' => array('yii'),
),
'yii/punycode' => array(
'sourcePath' => __DIR__ . '/assets',
'js' => array(
YII_DEBUG ? 'punycode/punycode.js' : 'punycode/punycode.min.js',
),
),
'yii/maskedinput' => array(
'sourcePath' => __DIR__ . '/assets',
'js' => array(
'jquery.maskedinput.js',
),
'depends' => array('yii/jquery'),
),
'yii/debug' => array(
'sourcePath' => __DIR__ . '/debug/assets',
'css' => array(
'main.css',
),
'depends' => array(
'yii',
'yii/bootstrap/responsive',
),
),
yii\web\YiiAsset::className(),
yii\web\JqueryAsset::className(),
yii\validators\PunycodeAsset::className(),
yii\validators\ValidationAsset::className(),
yii\widgets\ActiveFormAsset::className(),
yii\widgets\CaptchaAsset::className(),
yii\widgets\MaskedInputAsset::className(),
);
......@@ -10,6 +10,8 @@ namespace yii\base;
use Yii;
use yii\helpers\FileHelper;
use yii\helpers\Html;
use yii\web\JqueryAsset;
use yii\web\AssetBundle;
use yii\widgets\Block;
use yii\widgets\ContentDecorator;
use yii\widgets\FragmentCache;
......@@ -72,15 +74,15 @@ class View extends Component
/**
* This is internally used as the placeholder for receiving the content registered for the head section.
*/
const PL_HEAD = '<![CDATA[YII-BLOCK-HEAD]]>';
const PH_HEAD = '<![CDATA[YII-BLOCK-HEAD]]>';
/**
* This is internally used as the placeholder for receiving the content registered for the beginning of the body section.
*/
const PL_BODY_BEGIN = '<![CDATA[YII-BLOCK-BODY-BEGIN]]>';
const PH_BODY_BEGIN = '<![CDATA[YII-BLOCK-BODY-BEGIN]]>';
/**
* This is internally used as the placeholder for receiving the content registered for the end of the body section.
*/
const PL_BODY_END = '<![CDATA[YII-BLOCK-BODY-END]]>';
const PH_BODY_END = '<![CDATA[YII-BLOCK-BODY-END]]>';
/**
......@@ -519,9 +521,9 @@ class View extends Component
$content = ob_get_clean();
echo strtr($content, array(
self::PL_HEAD => $this->renderHeadHtml(),
self::PL_BODY_BEGIN => $this->renderBodyBeginHtml(),
self::PL_BODY_END => $this->renderBodyEndHtml(),
self::PH_HEAD => $this->renderHeadHtml(),
self::PH_BODY_BEGIN => $this->renderBodyBeginHtml(),
self::PH_BODY_END => $this->renderBodyEndHtml(),
));
unset(
......@@ -540,7 +542,7 @@ class View extends Component
*/
public function beginBody()
{
echo self::PL_BODY_BEGIN;
echo self::PH_BODY_BEGIN;
$this->trigger(self::EVENT_BEGIN_BODY);
}
......@@ -550,7 +552,7 @@ class View extends Component
public function endBody()
{
$this->trigger(self::EVENT_END_BODY);
echo self::PL_BODY_END;
echo self::PH_BODY_END;
}
/**
......@@ -558,13 +560,14 @@ class View extends Component
*/
public function head()
{
echo self::PL_HEAD;
echo self::PH_HEAD;
}
/**
* Registers the named asset bundle.
* All dependent asset bundles will be registered.
* @param string $name the name of the asset bundle.
* @return AssetBundle the registered asset bundle instance
* @throws InvalidConfigException if the asset bundle does not exist or a circular dependency is detected
*/
public function registerAssetBundle($name)
......@@ -582,6 +585,7 @@ class View extends Component
} elseif ($this->assetBundles[$name] === false) {
throw new InvalidConfigException("A circular dependency is detected for bundle '$name'.");
}
return $this->assetBundles[$name];
}
/**
......@@ -665,7 +669,7 @@ class View extends Component
$key = $key ?: md5($js);
$this->js[$position][$key] = $js;
if ($position === self::POS_READY) {
$this->registerAssetBundle('yii/jquery');
JqueryAsset::register($this);
}
}
......
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\bootstrap;
use yii\web\AssetBundle;
/**
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class AffixAsset extends AssetBundle
{
public $sourcePath = '@yii/bootstrap/assets';
public $js = array(
'js/bootstrap-affix.js',
);
public $depends = array(
'yii\bootstrap\TransitionAsset',
'yii\bootstrap\BootstrapAsset',
'yii\web\JqueryAsset',
);
}
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\bootstrap;
use yii\web\AssetBundle;
/**
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class AlertAsset extends AssetBundle
{
public $sourcePath = '@yii/bootstrap/assets';
public $js = array(
'js/bootstrap-alert.js',
);
public $depends = array(
'yii\bootstrap\TransitionAsset',
'yii\bootstrap\BootstrapAsset',
'yii\web\JqueryAsset',
);
}
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\bootstrap;
use yii\web\AssetBundle;
/**
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class BootstrapAsset extends AssetBundle
{
public $sourcePath = '@yii/bootstrap/assets';
public $css = array(
'css/bootstrap.css',
);
}
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\bootstrap;
use yii\web\AssetBundle;
/**
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class ButtonAsset extends AssetBundle
{
public $sourcePath = '@yii/bootstrap/assets';
public $js = array(
'js/bootstrap-button.js',
);
public $depends = array(
'yii\bootstrap\TransitionAsset',
'yii\bootstrap\BootstrapAsset',
'yii\web\JqueryAsset',
);
}
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\bootstrap;
use yii\web\AssetBundle;
/**
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class CarouselAsset extends AssetBundle
{
public $sourcePath = '@yii/bootstrap/assets';
public $js = array(
'js/bootstrap-carousel.js',
);
public $depends = array(
'yii\bootstrap\TransitionAsset',
'yii\bootstrap\BootstrapAsset',
'yii\web\JqueryAsset',
);
}
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\bootstrap;
use yii\web\AssetBundle;
/**
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class CollapseAsset extends AssetBundle
{
public $sourcePath = '@yii/bootstrap/assets';
public $js = array(
'js/bootstrap-collapse.js',
);
public $depends = array(
'yii\bootstrap\TransitionAsset',
'yii\bootstrap\BootstrapAsset',
'yii\web\JqueryAsset',
);
}
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\bootstrap;
use yii\web\AssetBundle;
/**
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class DropdownAsset extends AssetBundle
{
public $sourcePath = '@yii/bootstrap/assets';
public $js = array(
'js/bootstrap-dropdown.js',
);
public $depends = array(
'yii\bootstrap\TransitionAsset',
'yii\bootstrap\BootstrapAsset',
'yii\web\JqueryAsset',
);
}
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\bootstrap;
use yii\web\AssetBundle;
/**
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class ModalAsset extends AssetBundle
{
public $sourcePath = '@yii/bootstrap/assets';
public $js = array(
'js/bootstrap-modal.js',
);
public $depends = array(
'yii\bootstrap\TransitionAsset',
'yii\bootstrap\BootstrapAsset',
'yii\web\JqueryAsset',
);
}
......@@ -88,7 +88,7 @@ class Nav extends Widget
public function run()
{
echo $this->renderItems();
$this->getView()->registerAssetBundle('yii/bootstrap');
BootstrapAsset::register($this->getView());
}
/**
......
......@@ -119,7 +119,11 @@ class NavBar extends Widget
echo Html::beginTag('div', $this->options);
echo $this->renderItems();
echo Html::endTag('div');
$this->getView()->registerAssetBundle(self::$responsive ? 'yii/bootstrap/responsive' : 'yii/bootstrap');
if (self::$responsive) {
ResponsiveAsset::register($this->getView());
} else {
BootstrapAsset::register($this->getView());
}
}
/**
......@@ -136,7 +140,7 @@ class NavBar extends Widget
$brand = Html::a($this->brandLabel, $this->brandUrl, $this->brandOptions);
if (self::$responsive) {
$this->getView()->registerAssetBundle('yii/bootstrap/collapse');
CollapseAsset::register($this->getView());
$contents = Html::tag('div',
$this->renderToggleButton() .
$brand . "\n" .
......
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\bootstrap;
use yii\web\AssetBundle;
/**
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class PopoverAsset extends AssetBundle
{
public $sourcePath = '@yii/bootstrap/assets';
public $js = array(
'js/bootstrap-popover.js',
);
public $depends = array(
'yii\bootstrap\TooltipAsset',
);
}
......@@ -105,7 +105,11 @@ class Progress extends Widget
echo Html::beginTag('div', $this->options) . "\n";
echo $this->renderProgress() . "\n";
echo Html::endTag('div') . "\n";
$this->getView()->registerAssetBundle(static::$responsive ? 'yii/bootstrap/responsive' : 'yii/bootstrap');
if (self::$responsive) {
ResponsiveAsset::register($this->getView());
} else {
BootstrapAsset::register($this->getView());
}
}
/**
......
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\bootstrap;
use yii\web\AssetBundle;
/**
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class ResponsiveAsset extends AssetBundle
{
public $sourcePath = '@yii/bootstrap/assets';
public $css = array(
'css/bootstrap-responsive.css',
);
public $depends = array(
'yii\bootstrap\BootstrapAsset',
);
}
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\bootstrap;
use yii\web\AssetBundle;
/**
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class ScrollspyAsset extends AssetBundle
{
public $sourcePath = '@yii/bootstrap/assets';
public $js = array(
'js/bootstrap-scrollspy.js',
);
public $depends = array(
'yii\bootstrap\TransitionAsset',
'yii\bootstrap\BootstrapAsset',
'yii\web\JqueryAsset',
);
}
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\bootstrap;
use yii\web\AssetBundle;
/**
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class TabAsset extends AssetBundle
{
public $sourcePath = '@yii/bootstrap/assets';
public $js = array(
'js/bootstrap-tab.js',
);
public $depends = array(
'yii\bootstrap\TransitionAsset',
'yii\bootstrap\BootstrapAsset',
'yii\web\JqueryAsset',
);
}
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\bootstrap;
use yii\web\AssetBundle;
/**
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class TooltipAsset extends AssetBundle
{
public $sourcePath = '@yii/bootstrap/assets';
public $js = array(
'js/bootstrap-tooltip.js',
);
public $depends = array(
'yii\bootstrap\TransitionAsset',
'yii\bootstrap\BootstrapAsset',
'yii\web\JqueryAsset',
);
}
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\bootstrap;
use yii\web\AssetBundle;
/**
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class TransitionAsset extends AssetBundle
{
public $sourcePath = '@yii/bootstrap/assets';
public $js = array(
'js/bootstrap-transition.js',
);
public $depends = array(
'yii\bootstrap\BootstrapAsset',
'yii\web\JqueryAsset',
);
}
......@@ -13,7 +13,7 @@ use yii\base\Model;
use yii\helpers\Html;
/**
* TypeAhead renders a typehead bootstrap javascript component.
* Typeahead renders a typehead bootstrap javascript component.
*
* For example,
*
......@@ -42,7 +42,7 @@ use yii\helpers\Html;
* @author Antonio Ramirez <amigo.cobos@gmail.com>
* @since 2.0
*/
class TypeAhead extends Widget
class Typeahead extends Widget
{
/**
* @var \yii\base\Model the data model that this widget is associated with
......
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\bootstrap;
use yii\web\AssetBundle;
/**
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class TypeaheadAsset extends AssetBundle
{
public $sourcePath = '@yii/bootstrap/assets';
public $js = array(
'js/bootstrap-typeahead.js',
);
public $depends = array(
'yii\bootstrap\TransitionAsset',
'yii\bootstrap\BootstrapAsset',
'yii\web\JqueryAsset',
);
}
......@@ -65,8 +65,14 @@ class Widget extends \yii\base\Widget
{
$id = $this->options['id'];
$view = $this->getView();
$view->registerAssetBundle(static::$responsive ? 'yii/bootstrap/responsive' : 'yii/bootstrap');
$view->registerAssetBundle("yii/bootstrap/$name");
if (self::$responsive) {
ResponsiveAsset::register($view);
} else {
BootstrapAsset::register($view);
}
/** @var \yii\web\AssetBundle $assetClass */
$assetClass = 'yii\bootstrap\\' . ucfirst($name);
$assetClass::register($view);
if ($this->clientOptions !== false) {
$options = empty($this->clientOptions) ? '' : Json::encode($this->clientOptions);
......
......@@ -6,108 +6,19 @@
*/
return array(
'yii/bootstrap' => array(
'sourcePath' => __DIR__ . '/assets',
'css' => array(
YII_DEBUG ? 'css/bootstrap.css' : 'css/bootstrap.min.css',
),
),
'yii/bootstrap/responsive' => array(
'sourcePath' => __DIR__ . '/assets',
'css' => array(
YII_DEBUG ? 'css/bootstrap-responsive.css' : 'css/bootstrap-responsive.min.css',
),
'depends' => array('yii/bootstrap'),
),
'yii/bootstrap/affix' => array(
'sourcePath' => __DIR__ . '/assets',
'js' => array(
'js/bootstrap-affix.js',
),
'depends' => array('yii/jquery', 'yii/bootstrap', 'yii/bootstrap/transition'),
),
'yii/bootstrap/alert' => array(
'sourcePath' => __DIR__ . '/assets',
'js' => array(
'js/bootstrap-alert.js',
),
'depends' => array('yii/jquery', 'yii/bootstrap', 'yii/bootstrap/transition'),
),
'yii/bootstrap/button' => array(
'sourcePath' => __DIR__ . '/assets',
'js' => array(
'js/bootstrap-button.js',
),
'depends' => array('yii/jquery', 'yii/bootstrap', 'yii/bootstrap/transition'),
),
'yii/bootstrap/carousel' => array(
'sourcePath' => __DIR__ . '/assets',
'js' => array(
'js/bootstrap-carousel.js',
),
'depends' => array('yii/jquery', 'yii/bootstrap', 'yii/bootstrap/transition'),
),
'yii/bootstrap/collapse' => array(
'sourcePath' => __DIR__ . '/assets',
'js' => array(
'js/bootstrap-collapse.js',
),
'depends' => array('yii/jquery', 'yii/bootstrap', 'yii/bootstrap/transition'),
),
'yii/bootstrap/dropdown' => array(
'sourcePath' => __DIR__ . '/assets',
'js' => array(
'js/bootstrap-dropdown.js',
),
'depends' => array('yii/jquery', 'yii/bootstrap', 'yii/bootstrap/transition'),
),
'yii/bootstrap/modal' => array(
'sourcePath' => __DIR__ . '/assets',
'js' => array(
'js/bootstrap-modal.js',
),
'depends' => array('yii/jquery', 'yii/bootstrap', 'yii/bootstrap/transition'),
),
'yii/bootstrap/popover' => array(
'sourcePath' => __DIR__ . '/assets',
'js' => array(
'js/bootstrap-popover.js',
),
'depends' => array('yii/bootstrap/tooltip'),
),
'yii/bootstrap/scrollspy' => array(
'sourcePath' => __DIR__ . '/assets',
'js' => array(
'js/bootstrap-scrollspy.js',
),
'depends' => array('yii/jquery', 'yii/bootstrap', 'yii/bootstrap/transition'),
),
'yii/bootstrap/tab' => array(
'sourcePath' => __DIR__ . '/assets',
'js' => array(
'js/bootstrap-tab.js',
),
'depends' => array('yii/jquery', 'yii/bootstrap', 'yii/bootstrap/transition'),
),
'yii/bootstrap/tooltip' => array(
'sourcePath' => __DIR__ . '/assets',
'js' => array(
'js/bootstrap-tooltip.js',
),
'depends' => array('yii/jquery', 'yii/bootstrap', 'yii/bootstrap/transition'),
),
'yii/bootstrap/transition' => array(
'sourcePath' => __DIR__ . '/assets',
'js' => array(
'js/bootstrap-transition.js',
),
'depends' => array('yii/jquery', 'yii/bootstrap'),
),
'yii/bootstrap/typeahead' => array(
'sourcePath' => __DIR__ . '/assets',
'js' => array(
'js/bootstrap-typeahead.js',
),
'depends' => array('yii/jquery', 'yii/bootstrap', 'yii/bootstrap/transition'),
),
yii\bootstrap\BootstrapAsset::className(),
yii\bootstrap\ResponsiveAsset::className(),
yii\bootstrap\DropdownAsset::className(),
yii\bootstrap\TransitionAsset::className(),
yii\bootstrap\AffixAsset::className(),
yii\bootstrap\AlertAsset::className(),
yii\bootstrap\ButtonAsset::className(),
yii\bootstrap\CarouselAsset::className(),
yii\bootstrap\CollapseAsset::className(),
yii\bootstrap\ModalAsset::className(),
yii\bootstrap\PopoverAsset::className(),
yii\bootstrap\TooltipAsset::className(),
yii\bootstrap\ScrollspyAsset::className(),
yii\bootstrap\TabAsset::className(),
yii\bootstrap\TypeaheadAsset::className(),
);
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\debug;
use yii\web\AssetBundle;
/**
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class DebugAsset extends AssetBundle
{
public $sourcePath = '@yii/debug/assets';
public $css = array(
'main.css',
);
public $depends = array(
'yii\web\YiiAsset',
'yii\bootstrap\ResponsiveAsset',
);
}
......@@ -7,7 +7,6 @@ use yii\helpers\Html;
* @var array $manifest
*/
$this->registerAssetBundle('yii/bootstrap/dropdown');
$this->title = 'Yii Debugger';
?>
<div class="default-index">
......
......@@ -11,8 +11,8 @@ use yii\helpers\Html;
* @var \yii\debug\Panel $activePanel
*/
$this->registerAssetBundle('yii/bootstrap/dropdown');
$this->title = 'Yii Debugger';
yii\bootstrap\DropdownAsset::register($this);
?>
<div class="default-view">
<div class="navbar">
......
......@@ -5,7 +5,7 @@
*/
use yii\helpers\Html;
Yii::$app->getView()->registerAssetBundle('yii/debug');
yii\debug\DebugAsset::register($this);
?>
<!DOCTYPE html>
<html>
......
......@@ -102,7 +102,7 @@ class BooleanValidator extends Validator
$options['strict'] = 1;
}
$view->registerAssetBundle('yii/validation');
ValidationAsset::register($view);
return 'yii.validation.boolean(value, messages, ' . json_encode($options) . ');';
}
}
......@@ -116,7 +116,7 @@ class CaptchaValidator extends Validator
$options['skipOnEmpty'] = 1;
}
$view->registerAssetBundle('yii/validation');
ValidationAsset::register($view);
return 'yii.validation.captcha(value, messages, ' . json_encode($options) . ');';
}
}
......@@ -205,7 +205,7 @@ class CompareValidator extends Validator
'{compareValue}' => $compareValue,
)));
$view->registerAssetBundle('yii/validation');
ValidationAsset::register($view);
return 'yii.validation.compare(value, messages, ' . json_encode($options) . ');';
}
}
......@@ -139,9 +139,9 @@ class EmailValidator extends Validator
$options['skipOnEmpty'] = 1;
}
$view->registerAssetBundle('yii/validation');
ValidationAsset::register($view);
if ($this->enableIDN) {
$view->registerAssetBundle('yii/punycode');
PunycodeAsset::register($view);
}
return 'yii.validation.email(value, messages, ' . Json::encode($options) . ');';
}
......
......@@ -151,7 +151,7 @@ class NumberValidator extends Validator
$options['skipOnEmpty'] = 1;
}
$view->registerAssetBundle('yii/validation');
ValidationAsset::register($view);
return 'yii.validation.number(value, messages, ' . Json::encode($options) . ');';
}
}
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\validators;
use yii\web\AssetBundle;
/**
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class PunycodeAsset extends AssetBundle
{
public $sourcePath = '@yii/assets';
public $js = array(
'punycode/punycode.js',
);
}
......@@ -103,7 +103,7 @@ class RangeValidator extends Validator
$options['skipOnEmpty'] = 1;
}
$view->registerAssetBundle('yii/validation');
ValidationAsset::register($view);
return 'yii.validation.range(value, messages, ' . json_encode($options) . ');';
}
}
......@@ -112,7 +112,7 @@ class RegularExpressionValidator extends Validator
$options['skipOnEmpty'] = 1;
}
$view->registerAssetBundle('yii/validation');
ValidationAsset::register($view);
return 'yii.validation.regularExpression(value, messages, ' . Json::encode($options) . ');';
}
}
......@@ -126,7 +126,7 @@ class RequiredValidator extends Validator
'{value}' => $object->$attribute,
)));
$view->registerAssetBundle('yii/validation');
ValidationAsset::register($view);
return 'yii.validation.required(value, messages, ' . json_encode($options) . ');';
}
}
......@@ -170,7 +170,7 @@ class StringValidator extends Validator
$options['skipOnEmpty'] = 1;
}
$view->registerAssetBundle('yii/validation');
ValidationAsset::register($view);
return 'yii.validation.string(value, messages, ' . json_encode($options) . ');';
}
}
......@@ -143,9 +143,9 @@ class UrlValidator extends Validator
$options['defaultScheme'] = $this->defaultScheme;
}
$view->registerAssetBundle('yii/validation');
ValidationAsset::register($view);
if ($this->enableIDN) {
$view->registerAssetBundle('yii/punycode');
PunycodeAsset::register($view);
}
return 'yii.validation.url(value, messages, ' . Json::encode($options) . ');';
}
......
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\validators;
use yii\web\AssetBundle;
/**
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class ValidationAsset extends AssetBundle
{
public $sourcePath = '@yii/assets';
public $js = array(
'yii.validation.js',
);
public $depends = array(
'yii\web\YiiAsset',
);
}
......@@ -10,6 +10,7 @@ namespace yii\web;
use Yii;
use yii\base\InvalidConfigException;
use yii\base\Object;
use yii\base\View;
/**
* AssetBundle represents a collection of asset files, such as CSS, JS, images.
......@@ -102,6 +103,15 @@ class AssetBundle extends Object
public $publishOptions = array();
/**
* @param View $view
* @return AssetBundle the registered asset bundle instance
*/
public static function register($view)
{
return $view->registerAssetBundle(get_called_class());
}
/**
* Initializes the bundle.
*/
public function init()
......@@ -150,7 +160,7 @@ class AssetBundle extends Object
*/
public function publish($am)
{
if ($this->sourcePath !== null) {
if ($this->sourcePath !== null && !isset($this->basePath, $this->baseUrl)) {
list ($this->basePath, $this->baseUrl) = $am->publish($this->sourcePath, $this->publishOptions);
}
$converter = $am->getConverter();
......
......@@ -22,10 +22,11 @@ use yii\helpers\FileHelper;
class AssetManager extends Component
{
/**
* @var array list of available asset bundles. The keys are the bundle names, and the values are the configuration
* arrays for creating the [[AssetBundle]] objects.
* @var array list of available asset bundles. The keys are the class names of the asset bundles,
* and the values are either the configuration arrays for creating the [[AssetBundle]] objects
* or the corresponding asset bundle instances.
*/
public $bundles;
public $bundles = array();
/**
* @return string the root directory storing the published asset files.
*/
......@@ -81,55 +82,29 @@ class AssetManager extends Component
$this->basePath = realpath($this->basePath);
}
$this->baseUrl = rtrim(Yii::getAlias($this->baseUrl), '/');
foreach (require(YII_PATH . '/assets.php') as $name => $bundle) {
if (!isset($this->bundles[$name])) {
$this->bundles[$name] = $bundle;
}
}
}
/**
* Returns the named bundle.
* This method will first look for the bundle in [[bundles]]. If not found,
* it will attempt to find the bundle from an installed extension using the following procedure:
*
* 1. Convert the bundle into a path alias;
* 2. Determine the root alias and use it to locate the bundle manifest file "assets.php";
* 3. Look for the bundle in the manifest file.
* Returns the named asset bundle.
*
* For example, given the bundle name "foo/button", the method will first convert it
* into the path alias "@foo/button"; since "@foo" is the root alias, it will look
* for the bundle manifest file "@foo/assets.php". The manifest file should return an array
* that lists the bundles used by the "foo/button" extension. The array format is the same as [[bundles]].
* This method will first look for the bundle in [[bundles]]. If not found,
* it will treat `$name` as the class of the asset bundle and create a new instance of it.
*
* @param string $name the bundle name
* @return AssetBundle the loaded bundle object. Null is returned if the bundle does not exist.
* @param string $name the class name of the asset bundle
* @return AssetBundle the asset bundle instance
* @throws InvalidConfigException if $name does not refer to a valid asset bundle
*/
public function getBundle($name)
{
if (!isset($this->bundles[$name])) {
$rootAlias = Yii::getRootAlias("@$name");
if ($rootAlias !== false) {
$manifest = Yii::getAlias("$rootAlias/assets.php", false);
if ($manifest !== false && is_file($manifest)) {
foreach (require($manifest) as $bn => $config) {
$this->bundles[$bn] = $config;
}
}
}
if (!isset($this->bundles[$name])) {
return null;
}
}
if (is_array($this->bundles[$name])) {
$config = $this->bundles[$name];
if (!isset($config['class'])) {
$config['class'] = 'yii\\web\\AssetBundle';
if (isset($this->bundles[$name])) {
if (is_array($this->bundles[$name])) {
$this->bundles[$name] = Yii::createObject(array_merge(array('class' => $name), $this->bundles[$name]));
} elseif (!$this->bundles[$name] instanceof AssetBundle) {
throw new InvalidConfigException("Invalid asset bundle: $name");
}
$this->bundles[$name] = Yii::createObject($config);
} else {
$this->bundles[$name] = Yii::createObject($name);
}
return $this->bundles[$name];
}
......@@ -142,9 +117,7 @@ class AssetManager extends Component
public function getConverter()
{
if ($this->_converter === null) {
$this->_converter = Yii::createObject(array(
'class' => 'yii\\web\\AssetConverter',
));
$this->_converter = Yii::createObject(AssetConverter::className());
} elseif (is_array($this->_converter) || is_string($this->_converter)) {
$this->_converter = Yii::createObject($this->_converter);
}
......
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\web;
/**
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class JqueryAsset extends AssetBundle
{
public $sourcePath = '@yii/assets';
public $js = array(
'jquery.js',
);
}
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\web;
/**
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class YiiAsset extends AssetBundle
{
public $sourcePath = '@yii/assets';
public $js = array(
'yii.js',
);
public $depends = array(
'yii\web\JqueryAsset',
);
}
......@@ -167,8 +167,9 @@ class ActiveForm extends Widget
$id = $this->options['id'];
$options = Json::encode($this->getClientOptions());
$attributes = Json::encode($this->attributes);
$this->getView()->registerAssetBundle('yii/form');
$this->getView()->registerJs("jQuery('#$id').yiiActiveForm($attributes, $options);");
$view = $this->getView();
ActiveFormAsset::register($view);
$view->registerJs("jQuery('#$id').yiiActiveForm($attributes, $options);");
}
echo Html::endForm();
}
......
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\widgets;
use yii\web\AssetBundle;
/**
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class ActiveFormAsset extends AssetBundle
{
public $sourcePath = '@yii/assets';
public $js = array(
'yii.activeForm.js',
);
public $depends = array(
'yii\web\YiiAsset',
);
}
......@@ -97,8 +97,9 @@ class Captcha extends InputWidget
$options = $this->getClientOptions();
$options = empty($options) ? '' : Json::encode($options);
$id = $this->imageOptions['id'];
$this->getView()->registerAssetBundle('yii/captcha');
$this->getView()->registerJs("jQuery('#$id').yiiCaptcha($options);");
$view = $this->getView();
CaptchaAsset::register($view);
$view->registerJs("jQuery('#$id').yiiCaptcha($options);");
}
/**
......
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\widgets;
use yii\web\AssetBundle;
/**
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class CaptchaAsset extends AssetBundle
{
public $sourcePath = '@yii/assets';
public $js = array(
'yii.captcha.js',
);
public $depends = array(
'yii\web\YiiAsset',
);
}
......@@ -109,8 +109,9 @@ class MaskedInput extends InputWidget
}
$id = $this->options['id'];
$js .= "jQuery(\"#{$id}\").mask(\"{$this->mask}\"{$options});";
$this->getView()->registerAssetBundle('yii/maskedinput');
$this->getView()->registerJs($js);
$view = $this->getView();
MaskedInputAsset::register($view);
$view->registerJs($js);
}
/**
......
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\widgets;
use yii\web\AssetBundle;
/**
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class MaskedInputAsset extends AssetBundle
{
public $sourcePath = '@yii/assets';
public $js = array(
'jquery.maskedinput.js',
);
public $depends = array(
'yii\web\YiiAsset',
);
}
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