Commit 5f8e6d37 by Qiang Xue

Fixes #1706: Added support for registering a single JS/CSS file with dependency

parent b83414f6
...@@ -44,6 +44,7 @@ Yii Framework 2 Change Log ...@@ -44,6 +44,7 @@ Yii Framework 2 Change Log
- Enh #1646: Added postgresql `QueryBuilder::checkIntegrity` and `QueryBuilder::resetSequence` (Ragazzo) - Enh #1646: Added postgresql `QueryBuilder::checkIntegrity` and `QueryBuilder::resetSequence` (Ragazzo)
- Enh #1645: Added `Connection::$pdoClass` property (Ragazzo) - Enh #1645: Added `Connection::$pdoClass` property (Ragazzo)
- Enh #1681: Added support for automatically adjusting the "for" attribute of label generated by `ActiveField::label()` (qiangxue) - Enh #1681: Added support for automatically adjusting the "for" attribute of label generated by `ActiveField::label()` (qiangxue)
- Enh #1706: Added support for registering a single JS/CSS file with dependency (qiangxue)
- Enh: Added `favicon.ico` and `robots.txt` to default application templates (samdark) - Enh: Added `favicon.ico` and `robots.txt` to default application templates (samdark)
- Enh: Added `Widget::autoIdPrefix` to support prefixing automatically generated widget IDs (qiangxue) - Enh: Added `Widget::autoIdPrefix` to support prefixing automatically generated widget IDs (qiangxue)
- Enh: Support for file aliases in console command 'message' (omnilight) - Enh: Support for file aliases in console command 'message' (omnilight)
......
...@@ -165,8 +165,6 @@ class AssetBundle extends Object ...@@ -165,8 +165,6 @@ class AssetBundle extends Object
* It will also try to convert non-CSS or JS files (e.g. LESS, Sass) into the corresponding * It will also try to convert non-CSS or JS files (e.g. LESS, Sass) into the corresponding
* CSS or JS files using [[AssetManager::converter|asset converter]]. * CSS or JS files using [[AssetManager::converter|asset converter]].
* @param AssetManager $am the asset manager to perform the asset publishing * @param AssetManager $am the asset manager to perform the asset publishing
* @throws InvalidConfigException if [[baseUrl]] or [[basePath]] is not set when the bundle
* contains internal CSS or JS files.
*/ */
public function publish($am) public function publish($am)
{ {
...@@ -179,7 +177,7 @@ class AssetBundle extends Object ...@@ -179,7 +177,7 @@ class AssetBundle extends Object
if (isset($this->basePath, $this->baseUrl)) { if (isset($this->basePath, $this->baseUrl)) {
$this->js[$i] = $converter->convert($js, $this->basePath, $this->baseUrl); $this->js[$i] = $converter->convert($js, $this->basePath, $this->baseUrl);
} else { } else {
throw new InvalidConfigException('Both of the "baseUrl" and "basePath" properties must be set.'); $this->js[$i] = '/' . $js;
} }
} }
} }
...@@ -188,7 +186,7 @@ class AssetBundle extends Object ...@@ -188,7 +186,7 @@ class AssetBundle extends Object
if (isset($this->basePath, $this->baseUrl)) { if (isset($this->basePath, $this->baseUrl)) {
$this->css[$i] = $converter->convert($css, $this->basePath, $this->baseUrl); $this->css[$i] = $converter->convert($css, $this->basePath, $this->baseUrl);
} else { } else {
throw new InvalidConfigException('Both of the "baseUrl" and "basePath" properties must be set.'); $this->css[$i] = '/' . $css;
} }
} }
} }
......
...@@ -312,15 +312,26 @@ class View extends \yii\base\View ...@@ -312,15 +312,26 @@ class View extends \yii\base\View
/** /**
* Registers a CSS file. * Registers a CSS file.
* @param string $url the CSS file to be registered. * @param string $url the CSS file to be registered.
* @param array $depends the names of the asset bundles that this CSS file depends on
* @param array $options the HTML attributes for the link tag. * @param array $options the HTML attributes for the link tag.
* @param string $key the key that identifies the CSS script file. If null, it will use * @param string $key the key that identifies the CSS script file. If null, it will use
* $url as the key. If two CSS files are registered with the same key, the latter * $url as the key. If two CSS files are registered with the same key, the latter
* will overwrite the former. * will overwrite the former.
*/ */
public function registerCssFile($url, $options = [], $key = null) public function registerCssFile($url, $depends = [], $options = [], $key = null)
{ {
$key = $key ?: $url; $key = $key ?: $url;
$this->cssFiles[$key] = Html::cssFile($url, $options); if (empty($depends)) {
$this->cssFiles[$key] = Html::cssFile($url, $options);
} else {
$am = Yii::$app->getAssetManager();
$am->bundles[$key] = new AssetBundle([
'css' => [$url],
'cssOptions' => $options,
'depends' => (array)$depends,
]);
$this->registerAssetBundle($key);
}
} }
/** /**
...@@ -350,9 +361,8 @@ class View extends \yii\base\View ...@@ -350,9 +361,8 @@ class View extends \yii\base\View
/** /**
* Registers a JS file. * Registers a JS file.
* Please note that when this file depends on other JS files to be registered before,
* for example jQuery, you should use [[registerAssetBundle]] instead.
* @param string $url the JS file to be registered. * @param string $url the JS file to be registered.
* @param array $depends the names of the asset bundles that this JS file depends on
* @param array $options the HTML attributes for the script tag. A special option * @param array $options the HTML attributes for the script tag. A special option
* named "position" is supported which specifies where the JS script tag should be inserted * named "position" is supported which specifies where the JS script tag should be inserted
* in a page. The possible values of "position" are: * in a page. The possible values of "position" are:
...@@ -365,12 +375,22 @@ class View extends \yii\base\View ...@@ -365,12 +375,22 @@ class View extends \yii\base\View
* $url as the key. If two JS files are registered with the same key, the latter * $url as the key. If two JS files are registered with the same key, the latter
* will overwrite the former. * will overwrite the former.
*/ */
public function registerJsFile($url, $options = [], $key = null) public function registerJsFile($url, $depends = [], $options = [], $key = null)
{ {
$position = isset($options['position']) ? $options['position'] : self::POS_END;
unset($options['position']);
$key = $key ?: $url; $key = $key ?: $url;
$this->jsFiles[$position][$key] = Html::jsFile($url, $options); if (empty($depends)) {
$position = isset($options['position']) ? $options['position'] : self::POS_END;
unset($options['position']);
$this->jsFiles[$position][$key] = Html::jsFile($url, $options);
} else {
$am = Yii::$app->getAssetManager();
$am->bundles[$key] = new AssetBundle([
'js' => [$url],
'jsOptions' => $options,
'depends' => (array)$depends,
]);
$this->registerAssetBundle($key);
}
} }
/** /**
......
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