From 368a4cfe66bf0162f5e3f4645f23cd5c9a06e509 Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Mon, 15 Jul 2013 22:49:22 -0400 Subject: [PATCH] Asset feature refactoring (WIP) --- apps/advanced/backend/config/AppAsset.php | 28 +++++ apps/advanced/backend/config/assets.php | 18 ---- apps/advanced/backend/config/main.php | 3 - apps/advanced/backend/views/layouts/main.php | 3 +- apps/advanced/frontend/config/AppAsset.php | 28 +++++ apps/advanced/frontend/config/assets.php | 18 ---- apps/advanced/frontend/config/main.php | 3 - apps/advanced/frontend/views/layouts/main.php | 3 +- apps/basic/assets/.gitkeep | 1 - apps/basic/config/AppAsset.php | 29 +++++ apps/basic/config/assets.php | 18 ---- apps/basic/config/web.php | 3 - apps/basic/views/layouts/main.php | 2 +- docs/guide/upgrade-from-v1.md | 7 +- extensions/jui/yii/jui/CoreAsset.php | 24 +++++ extensions/jui/yii/jui/WidgetAsset.php | 24 +++++ extensions/jui/yii/jui/assets.php | 16 +-- framework/yii/YiiBase.php | 9 +- framework/yii/assets.php | 64 ++--------- framework/yii/base/View.php | 24 +++-- framework/yii/bootstrap/AffixAsset.php | 26 +++++ framework/yii/bootstrap/AlertAsset.php | 26 +++++ framework/yii/bootstrap/BootstrapAsset.php | 21 ++++ framework/yii/bootstrap/ButtonAsset.php | 26 +++++ framework/yii/bootstrap/CarouselAsset.php | 26 +++++ framework/yii/bootstrap/CollapseAsset.php | 26 +++++ framework/yii/bootstrap/DropdownAsset.php | 26 +++++ framework/yii/bootstrap/ModalAsset.php | 26 +++++ framework/yii/bootstrap/Nav.php | 2 +- framework/yii/bootstrap/NavBar.php | 8 +- framework/yii/bootstrap/PopoverAsset.php | 24 +++++ framework/yii/bootstrap/Progress.php | 6 +- framework/yii/bootstrap/ResponsiveAsset.php | 24 +++++ framework/yii/bootstrap/ScrollspyAsset.php | 26 +++++ framework/yii/bootstrap/TabAsset.php | 26 +++++ framework/yii/bootstrap/TooltipAsset.php | 26 +++++ framework/yii/bootstrap/TransitionAsset.php | 25 +++++ framework/yii/bootstrap/TypeAhead.php | 92 ---------------- framework/yii/bootstrap/Typeahead.php | 92 ++++++++++++++++ framework/yii/bootstrap/TypeaheadAsset.php | 26 +++++ framework/yii/bootstrap/Widget.php | 10 +- framework/yii/bootstrap/assets.php | 119 +++------------------ framework/yii/debug/DebugAsset.php | 25 +++++ framework/yii/debug/views/default/index.php | 1 - framework/yii/debug/views/default/view.php | 2 +- framework/yii/debug/views/layouts/main.php | 2 +- framework/yii/validators/BooleanValidator.php | 2 +- framework/yii/validators/CaptchaValidator.php | 2 +- framework/yii/validators/CompareValidator.php | 2 +- framework/yii/validators/EmailValidator.php | 4 +- framework/yii/validators/NumberValidator.php | 2 +- framework/yii/validators/PunycodeAsset.php | 21 ++++ framework/yii/validators/RangeValidator.php | 2 +- .../yii/validators/RegularExpressionValidator.php | 2 +- framework/yii/validators/RequiredValidator.php | 2 +- framework/yii/validators/StringValidator.php | 2 +- framework/yii/validators/UrlValidator.php | 4 +- framework/yii/validators/ValidationAsset.php | 24 +++++ framework/yii/web/AssetBundle.php | 12 ++- framework/yii/web/AssetManager.php | 63 ++++------- framework/yii/web/JqueryAsset.php | 20 ++++ framework/yii/web/YiiAsset.php | 23 ++++ framework/yii/widgets/ActiveForm.php | 5 +- framework/yii/widgets/ActiveFormAsset.php | 24 +++++ framework/yii/widgets/Captcha.php | 5 +- framework/yii/widgets/CaptchaAsset.php | 24 +++++ framework/yii/widgets/MaskedInput.php | 5 +- framework/yii/widgets/MaskedInputAsset.php | 24 +++++ 68 files changed, 910 insertions(+), 428 deletions(-) create mode 100644 apps/advanced/backend/config/AppAsset.php delete mode 100644 apps/advanced/backend/config/assets.php create mode 100644 apps/advanced/frontend/config/AppAsset.php delete mode 100644 apps/advanced/frontend/config/assets.php delete mode 100644 apps/basic/assets/.gitkeep create mode 100644 apps/basic/config/AppAsset.php delete mode 100644 apps/basic/config/assets.php create mode 100644 extensions/jui/yii/jui/CoreAsset.php create mode 100644 extensions/jui/yii/jui/WidgetAsset.php create mode 100644 framework/yii/bootstrap/AffixAsset.php create mode 100644 framework/yii/bootstrap/AlertAsset.php create mode 100644 framework/yii/bootstrap/BootstrapAsset.php create mode 100644 framework/yii/bootstrap/ButtonAsset.php create mode 100644 framework/yii/bootstrap/CarouselAsset.php create mode 100644 framework/yii/bootstrap/CollapseAsset.php create mode 100644 framework/yii/bootstrap/DropdownAsset.php create mode 100644 framework/yii/bootstrap/ModalAsset.php create mode 100644 framework/yii/bootstrap/PopoverAsset.php create mode 100644 framework/yii/bootstrap/ResponsiveAsset.php create mode 100644 framework/yii/bootstrap/ScrollspyAsset.php create mode 100644 framework/yii/bootstrap/TabAsset.php create mode 100644 framework/yii/bootstrap/TooltipAsset.php create mode 100644 framework/yii/bootstrap/TransitionAsset.php delete mode 100644 framework/yii/bootstrap/TypeAhead.php create mode 100644 framework/yii/bootstrap/Typeahead.php create mode 100644 framework/yii/bootstrap/TypeaheadAsset.php create mode 100644 framework/yii/debug/DebugAsset.php create mode 100644 framework/yii/validators/PunycodeAsset.php create mode 100644 framework/yii/validators/ValidationAsset.php create mode 100644 framework/yii/web/JqueryAsset.php create mode 100644 framework/yii/web/YiiAsset.php create mode 100644 framework/yii/widgets/ActiveFormAsset.php create mode 100644 framework/yii/widgets/CaptchaAsset.php create mode 100644 framework/yii/widgets/MaskedInputAsset.php diff --git a/apps/advanced/backend/config/AppAsset.php b/apps/advanced/backend/config/AppAsset.php new file mode 100644 index 0000000..525b693 --- /dev/null +++ b/apps/advanced/backend/config/AppAsset.php @@ -0,0 +1,28 @@ + + * @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', + ); +} diff --git a/apps/advanced/backend/config/assets.php b/apps/advanced/backend/config/assets.php deleted file mode 100644 index ee0d610..0000000 --- a/apps/advanced/backend/config/assets.php +++ /dev/null @@ -1,18 +0,0 @@ - array( - 'basePath' => '@wwwroot', - 'baseUrl' => '@www', - 'css' => array( - 'css/site.css', - ), - 'js' => array( - - ), - 'depends' => array( - 'yii', - 'yii/bootstrap/responsive', - ), - ), -); diff --git a/apps/advanced/backend/config/main.php b/apps/advanced/backend/config/main.php index 88838b9..377d34c 100644 --- a/apps/advanced/backend/config/main.php +++ b/apps/advanced/backend/config/main.php @@ -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( diff --git a/apps/advanced/backend/views/layouts/main.php b/apps/advanced/backend/views/layouts/main.php index cf21f9d..84ad1ad 100644 --- a/apps/advanced/backend/views/layouts/main.php +++ b/apps/advanced/backend/views/layouts/main.php @@ -1,4 +1,5 @@ registerAssetBundle('app'); +AppAsset::register($this); ?> beginPage(); ?> diff --git a/apps/advanced/frontend/config/AppAsset.php b/apps/advanced/frontend/config/AppAsset.php new file mode 100644 index 0000000..0a3a5e8 --- /dev/null +++ b/apps/advanced/frontend/config/AppAsset.php @@ -0,0 +1,28 @@ + + * @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', + ); +} diff --git a/apps/advanced/frontend/config/assets.php b/apps/advanced/frontend/config/assets.php deleted file mode 100644 index ee0d610..0000000 --- a/apps/advanced/frontend/config/assets.php +++ /dev/null @@ -1,18 +0,0 @@ - array( - 'basePath' => '@wwwroot', - 'baseUrl' => '@www', - 'css' => array( - 'css/site.css', - ), - 'js' => array( - - ), - 'depends' => array( - 'yii', - 'yii/bootstrap/responsive', - ), - ), -); diff --git a/apps/advanced/frontend/config/main.php b/apps/advanced/frontend/config/main.php index 5be2849..9051506 100644 --- a/apps/advanced/frontend/config/main.php +++ b/apps/advanced/frontend/config/main.php @@ -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( diff --git a/apps/advanced/frontend/views/layouts/main.php b/apps/advanced/frontend/views/layouts/main.php index 0af2fe2..8903779 100644 --- a/apps/advanced/frontend/views/layouts/main.php +++ b/apps/advanced/frontend/views/layouts/main.php @@ -1,4 +1,5 @@ registerAssetBundle('app'); +AppAsset::register($this); ?> beginPage(); ?> diff --git a/apps/basic/assets/.gitkeep b/apps/basic/assets/.gitkeep deleted file mode 100644 index 72e8ffc..0000000 --- a/apps/basic/assets/.gitkeep +++ /dev/null @@ -1 +0,0 @@ -* diff --git a/apps/basic/config/AppAsset.php b/apps/basic/config/AppAsset.php new file mode 100644 index 0000000..2546969 --- /dev/null +++ b/apps/basic/config/AppAsset.php @@ -0,0 +1,29 @@ + + * @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', + ); +} diff --git a/apps/basic/config/assets.php b/apps/basic/config/assets.php deleted file mode 100644 index ee0d610..0000000 --- a/apps/basic/config/assets.php +++ /dev/null @@ -1,18 +0,0 @@ - array( - 'basePath' => '@wwwroot', - 'baseUrl' => '@www', - 'css' => array( - 'css/site.css', - ), - 'js' => array( - - ), - 'depends' => array( - 'yii', - 'yii/bootstrap/responsive', - ), - ), -); diff --git a/apps/basic/config/web.php b/apps/basic/config/web.php index 10e8856..d576599 100644 --- a/apps/basic/config/web.php +++ b/apps/basic/config/web.php @@ -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( diff --git a/apps/basic/views/layouts/main.php b/apps/basic/views/layouts/main.php index 77f7b8d..726ad41 100644 --- a/apps/basic/views/layouts/main.php +++ b/apps/basic/views/layouts/main.php @@ -7,7 +7,7 @@ use yii\widgets\Breadcrumbs; * @var $this \yii\base\View * @var $content string */ -$this->registerAssetBundle('app'); +app\config\AppAsset::register($this); ?> beginPage(); ?> diff --git a/docs/guide/upgrade-from-v1.md b/docs/guide/upgrade-from-v1.md index c3cbf31..79c6c3f 100644 --- a/docs/guide/upgrade-from-v1.md +++ b/docs/guide/upgrade-from-v1.md @@ -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. diff --git a/extensions/jui/yii/jui/CoreAsset.php b/extensions/jui/yii/jui/CoreAsset.php new file mode 100644 index 0000000..e757ea7 --- /dev/null +++ b/extensions/jui/yii/jui/CoreAsset.php @@ -0,0 +1,24 @@ + + * @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', + ); +} diff --git a/extensions/jui/yii/jui/WidgetAsset.php b/extensions/jui/yii/jui/WidgetAsset.php new file mode 100644 index 0000000..fde1630 --- /dev/null +++ b/extensions/jui/yii/jui/WidgetAsset.php @@ -0,0 +1,24 @@ + + * @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', + ); +} diff --git a/extensions/jui/yii/jui/assets.php b/extensions/jui/yii/jui/assets.php index d2d8f7c..707cadc 100644 --- a/extensions/jui/yii/jui/assets.php +++ b/extensions/jui/yii/jui/assets.php @@ -1,20 +1,8 @@ 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( diff --git a/framework/yii/YiiBase.php b/framework/yii/YiiBase.php index 23e8f05..25573b7 100644 --- a/framework/yii/YiiBase.php +++ b/framework/yii/YiiBase.php @@ -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__, - ), -); diff --git a/framework/yii/assets.php b/framework/yii/assets.php index fd3c731..dd657f3 100644 --- a/framework/yii/assets.php +++ b/framework/yii/assets.php @@ -1,61 +1,11 @@ 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(), ); diff --git a/framework/yii/base/View.php b/framework/yii/base/View.php index d49c69c..2d37efc 100644 --- a/framework/yii/base/View.php +++ b/framework/yii/base/View.php @@ -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 = ''; + const PH_HEAD = ''; /** * This is internally used as the placeholder for receiving the content registered for the beginning of the body section. */ - const PL_BODY_BEGIN = ''; + const PH_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 = ''; + const PH_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); } } diff --git a/framework/yii/bootstrap/AffixAsset.php b/framework/yii/bootstrap/AffixAsset.php new file mode 100644 index 0000000..f92315f --- /dev/null +++ b/framework/yii/bootstrap/AffixAsset.php @@ -0,0 +1,26 @@ + + * @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', + ); +} diff --git a/framework/yii/bootstrap/AlertAsset.php b/framework/yii/bootstrap/AlertAsset.php new file mode 100644 index 0000000..13e8f39 --- /dev/null +++ b/framework/yii/bootstrap/AlertAsset.php @@ -0,0 +1,26 @@ + + * @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', + ); +} diff --git a/framework/yii/bootstrap/BootstrapAsset.php b/framework/yii/bootstrap/BootstrapAsset.php new file mode 100644 index 0000000..5e5f756 --- /dev/null +++ b/framework/yii/bootstrap/BootstrapAsset.php @@ -0,0 +1,21 @@ + + * @since 2.0 + */ +class BootstrapAsset extends AssetBundle +{ + public $sourcePath = '@yii/bootstrap/assets'; + public $css = array( + 'css/bootstrap.css', + ); +} diff --git a/framework/yii/bootstrap/ButtonAsset.php b/framework/yii/bootstrap/ButtonAsset.php new file mode 100644 index 0000000..378d59e --- /dev/null +++ b/framework/yii/bootstrap/ButtonAsset.php @@ -0,0 +1,26 @@ + + * @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', + ); +} diff --git a/framework/yii/bootstrap/CarouselAsset.php b/framework/yii/bootstrap/CarouselAsset.php new file mode 100644 index 0000000..c75a619 --- /dev/null +++ b/framework/yii/bootstrap/CarouselAsset.php @@ -0,0 +1,26 @@ + + * @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', + ); +} diff --git a/framework/yii/bootstrap/CollapseAsset.php b/framework/yii/bootstrap/CollapseAsset.php new file mode 100644 index 0000000..6a60ec0 --- /dev/null +++ b/framework/yii/bootstrap/CollapseAsset.php @@ -0,0 +1,26 @@ + + * @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', + ); +} diff --git a/framework/yii/bootstrap/DropdownAsset.php b/framework/yii/bootstrap/DropdownAsset.php new file mode 100644 index 0000000..29d9dee --- /dev/null +++ b/framework/yii/bootstrap/DropdownAsset.php @@ -0,0 +1,26 @@ + + * @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', + ); +} diff --git a/framework/yii/bootstrap/ModalAsset.php b/framework/yii/bootstrap/ModalAsset.php new file mode 100644 index 0000000..cff10b1 --- /dev/null +++ b/framework/yii/bootstrap/ModalAsset.php @@ -0,0 +1,26 @@ + + * @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', + ); +} diff --git a/framework/yii/bootstrap/Nav.php b/framework/yii/bootstrap/Nav.php index 7b003f4..cea83d8 100644 --- a/framework/yii/bootstrap/Nav.php +++ b/framework/yii/bootstrap/Nav.php @@ -88,7 +88,7 @@ class Nav extends Widget public function run() { echo $this->renderItems(); - $this->getView()->registerAssetBundle('yii/bootstrap'); + BootstrapAsset::register($this->getView()); } /** diff --git a/framework/yii/bootstrap/NavBar.php b/framework/yii/bootstrap/NavBar.php index 337e449..f801df5 100644 --- a/framework/yii/bootstrap/NavBar.php +++ b/framework/yii/bootstrap/NavBar.php @@ -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" . diff --git a/framework/yii/bootstrap/PopoverAsset.php b/framework/yii/bootstrap/PopoverAsset.php new file mode 100644 index 0000000..a28d6c4 --- /dev/null +++ b/framework/yii/bootstrap/PopoverAsset.php @@ -0,0 +1,24 @@ + + * @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', + ); +} diff --git a/framework/yii/bootstrap/Progress.php b/framework/yii/bootstrap/Progress.php index ae44619..57046b1 100644 --- a/framework/yii/bootstrap/Progress.php +++ b/framework/yii/bootstrap/Progress.php @@ -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()); + } } /** diff --git a/framework/yii/bootstrap/ResponsiveAsset.php b/framework/yii/bootstrap/ResponsiveAsset.php new file mode 100644 index 0000000..fb8138b --- /dev/null +++ b/framework/yii/bootstrap/ResponsiveAsset.php @@ -0,0 +1,24 @@ + + * @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', + ); +} diff --git a/framework/yii/bootstrap/ScrollspyAsset.php b/framework/yii/bootstrap/ScrollspyAsset.php new file mode 100644 index 0000000..8a52694 --- /dev/null +++ b/framework/yii/bootstrap/ScrollspyAsset.php @@ -0,0 +1,26 @@ + + * @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', + ); +} diff --git a/framework/yii/bootstrap/TabAsset.php b/framework/yii/bootstrap/TabAsset.php new file mode 100644 index 0000000..51ff844 --- /dev/null +++ b/framework/yii/bootstrap/TabAsset.php @@ -0,0 +1,26 @@ + + * @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', + ); +} diff --git a/framework/yii/bootstrap/TooltipAsset.php b/framework/yii/bootstrap/TooltipAsset.php new file mode 100644 index 0000000..5f96215 --- /dev/null +++ b/framework/yii/bootstrap/TooltipAsset.php @@ -0,0 +1,26 @@ + + * @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', + ); +} diff --git a/framework/yii/bootstrap/TransitionAsset.php b/framework/yii/bootstrap/TransitionAsset.php new file mode 100644 index 0000000..b04cef0 --- /dev/null +++ b/framework/yii/bootstrap/TransitionAsset.php @@ -0,0 +1,25 @@ + + * @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', + ); +} diff --git a/framework/yii/bootstrap/TypeAhead.php b/framework/yii/bootstrap/TypeAhead.php deleted file mode 100644 index 0704404..0000000 --- a/framework/yii/bootstrap/TypeAhead.php +++ /dev/null @@ -1,92 +0,0 @@ - $model, - * 'attribute' => 'country', - * 'clientOptions' => array( - * 'source' => array('USA', 'ESP'), - * ), - * )); - * ``` - * - * The following example will use the name property instead - * - * ```php - * echo TypeAhead::widget(array( - * 'name' => 'country', - * 'clientOptions' => array( - * 'source' => array('USA', 'ESP'), - * ), - * )); - *``` - * - * @see http://twitter.github.io/bootstrap/javascript.html#typeahead - * @author Antonio Ramirez - * @since 2.0 - */ -class TypeAhead extends Widget -{ - /** - * @var \yii\base\Model the data model that this widget is associated with - */ - public $model; - /** - * @var string the model attribute that this widget is associated with - */ - public $attribute; - /** - * @var string the input name. This must be set if [[model]] and [[attribute]] are not set. - */ - public $name; - /** - * @var string the input value. - */ - public $value; - - - /** - * Renders the widget - */ - public function run() - { - echo $this->renderField(); - $this->registerPlugin('typeahead'); - } - - /** - * Renders the TypeAhead field. If [[model]] has been specified then it will render an active field. - * If [[model]] is null or not from an [[Model]] instance, then the field will be rendered according to - * the [[name]] attribute. - * @return string the rendering result - * @throws InvalidConfigException when none of the required attributes are set to render the textInput. - * That is, if [[model]] and [[attribute]] are not set, then [[name]] is required. - */ - public function renderField() - { - if ($this->model instanceof Model && $this->attribute !== null) { - return Html::activeTextInput($this->model, $this->attribute, $this->options); - } elseif ($this->name !== null) { - return Html::textInput($this->name, $this->value, $this->options); - } else { - throw new InvalidConfigException("Either 'name' or 'model' and 'attribute' properties must be specified."); - } - } -} diff --git a/framework/yii/bootstrap/Typeahead.php b/framework/yii/bootstrap/Typeahead.php new file mode 100644 index 0000000..8fb2fa4 --- /dev/null +++ b/framework/yii/bootstrap/Typeahead.php @@ -0,0 +1,92 @@ + $model, + * 'attribute' => 'country', + * 'clientOptions' => array( + * 'source' => array('USA', 'ESP'), + * ), + * )); + * ``` + * + * The following example will use the name property instead + * + * ```php + * echo TypeAhead::widget(array( + * 'name' => 'country', + * 'clientOptions' => array( + * 'source' => array('USA', 'ESP'), + * ), + * )); + *``` + * + * @see http://twitter.github.io/bootstrap/javascript.html#typeahead + * @author Antonio Ramirez + * @since 2.0 + */ +class Typeahead extends Widget +{ + /** + * @var \yii\base\Model the data model that this widget is associated with + */ + public $model; + /** + * @var string the model attribute that this widget is associated with + */ + public $attribute; + /** + * @var string the input name. This must be set if [[model]] and [[attribute]] are not set. + */ + public $name; + /** + * @var string the input value. + */ + public $value; + + + /** + * Renders the widget + */ + public function run() + { + echo $this->renderField(); + $this->registerPlugin('typeahead'); + } + + /** + * Renders the TypeAhead field. If [[model]] has been specified then it will render an active field. + * If [[model]] is null or not from an [[Model]] instance, then the field will be rendered according to + * the [[name]] attribute. + * @return string the rendering result + * @throws InvalidConfigException when none of the required attributes are set to render the textInput. + * That is, if [[model]] and [[attribute]] are not set, then [[name]] is required. + */ + public function renderField() + { + if ($this->model instanceof Model && $this->attribute !== null) { + return Html::activeTextInput($this->model, $this->attribute, $this->options); + } elseif ($this->name !== null) { + return Html::textInput($this->name, $this->value, $this->options); + } else { + throw new InvalidConfigException("Either 'name' or 'model' and 'attribute' properties must be specified."); + } + } +} diff --git a/framework/yii/bootstrap/TypeaheadAsset.php b/framework/yii/bootstrap/TypeaheadAsset.php new file mode 100644 index 0000000..c67abae --- /dev/null +++ b/framework/yii/bootstrap/TypeaheadAsset.php @@ -0,0 +1,26 @@ + + * @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', + ); +} diff --git a/framework/yii/bootstrap/Widget.php b/framework/yii/bootstrap/Widget.php index 004f040..7636239 100644 --- a/framework/yii/bootstrap/Widget.php +++ b/framework/yii/bootstrap/Widget.php @@ -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); diff --git a/framework/yii/bootstrap/assets.php b/framework/yii/bootstrap/assets.php index 0c4a6bc..c61e1fa 100644 --- a/framework/yii/bootstrap/assets.php +++ b/framework/yii/bootstrap/assets.php @@ -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(), ); diff --git a/framework/yii/debug/DebugAsset.php b/framework/yii/debug/DebugAsset.php new file mode 100644 index 0000000..ab02680 --- /dev/null +++ b/framework/yii/debug/DebugAsset.php @@ -0,0 +1,25 @@ + + * @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', + ); +} diff --git a/framework/yii/debug/views/default/index.php b/framework/yii/debug/views/default/index.php index c76d864..0c482ad 100644 --- a/framework/yii/debug/views/default/index.php +++ b/framework/yii/debug/views/default/index.php @@ -7,7 +7,6 @@ use yii\helpers\Html; * @var array $manifest */ -$this->registerAssetBundle('yii/bootstrap/dropdown'); $this->title = 'Yii Debugger'; ?>
diff --git a/framework/yii/debug/views/default/view.php b/framework/yii/debug/views/default/view.php index 1d2c760..f43b1ad 100644 --- a/framework/yii/debug/views/default/view.php +++ b/framework/yii/debug/views/default/view.php @@ -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); ?>