Added CSS Group asset support #374

This commit is contained in:
Andy Miller
2015-10-24 12:18:54 -06:00
parent de61d88d29
commit a614c8c7cc
2 changed files with 53 additions and 27 deletions

View File

@@ -1,3 +1,13 @@
# v1.0.0-rc.2
## xx/xx/2015
1. [](#new)
* Added support for CSS Asset groups
1. [](#improved)
* ...
1. [](#bugfix)
* ...
# v1.0.0-rc.1
## 10/23/2015

View File

@@ -227,21 +227,22 @@ class Assets
* It checks for duplicates.
* You may add more than one asset passing an array as argument.
*
* @param mixed $asset
* @param int $priority the priority, bigger comes first
* @param bool $pipeline false if this should not be pipelined
* @param mixed $asset
* @param int $priority the priority, bigger comes first
* @param bool $pipeline false if this should not be pipelined
* @param null $group
*
* @return $this
*/
public function addCss($asset, $priority = null, $pipeline = null)
public function addCss($asset, $priority = null, $pipeline = null, $group = null)
{
if (is_array($asset)) {
foreach ($asset as $a) {
$this->addCss($a, $priority, $pipeline);
$this->addCss($a, $priority, $pipeline, $group);
}
return $this;
} elseif (isset($this->collections[$asset])) {
$this->add($this->collections[$asset], $priority, $pipeline);
$this->add($this->collections[$asset], $priority, $pipeline, $group);
return $this;
}
@@ -253,7 +254,8 @@ class Assets
'asset' => $asset,
'priority' => intval($priority ?: 10),
'order' => count($this->css),
'pipeline' => $pipeline ?: true
'pipeline' => $pipeline ?: true,
'group' => $group ?: 'head'
];
// check for dynamic array and merge with defaults
@@ -369,11 +371,12 @@ class Assets
* For adding chunks of string-based inline CSS
*
* @param mixed $asset
* @param int $priority the priority, bigger comes first
* @param int $priority the priority, bigger comes first
* @param null $group
*
* @return $this
*/
public function addInlineCss($asset, $priority = null)
public function addInlineCss($asset, $priority = null, $group = null)
{
if (is_a($asset, 'Twig_Markup')) {
$asset = strip_tags((string)$asset);
@@ -382,7 +385,8 @@ class Assets
$data = [
'priority' => intval($priority ?: 10),
'order' => count($this->inline_css),
'asset' => $asset
'asset' => $asset,
'group' => $group ?: 'head'
];
// check for dynamic array and merge with defaults
@@ -447,11 +451,12 @@ class Assets
/**
* Build the CSS link tags.
*
* @param string $group name of the group
* @param array $attributes
*
* @return string
*/
public function css($attributes = [])
public function css($group = 'head', $attributes = [])
{
if (!$this->css) {
return null;
@@ -479,29 +484,37 @@ class Assets
$attributes = $this->attributes(array_merge(['type' => 'text/css', 'rel' => 'stylesheet'], $attributes));
$output = '';
$inline_css = '';
if ($this->css_pipeline) {
$pipeline_result = $this->pipelineCss();
$pipeline_result = $this->pipelineCss($group);
if ($pipeline_result) {
$output .= '<link href="' . $pipeline_result . '"' . $attributes . ' />' . "\n";
}
foreach ($this->css_no_pipeline as $file) {
$media = isset($file['media']) ? sprintf(' media="%s"', $file['media']) : '';
$output .= '<link href="' . $file['asset'] . $this->timestamp . '"' . $attributes . $media . ' />' . "\n";
if ($group && $file['group'] == $group) {
$media = isset($file['media']) ? sprintf(' media="%s"', $file['media']) : '';
$output .= '<link href="' . $file['asset'] . $this->timestamp . '"' . $attributes . $media . ' />' . "\n";
}
}
} else {
foreach ($this->css as $file) {
$media = isset($file['media']) ? sprintf(' media="%s"', $file['media']) : '';
$output .= '<link href="' . $file['asset'] . $this->timestamp . '"' . $attributes . $media . ' />' . "\n";
if ($group && $file['group'] == $group) {
$media = isset($file['media']) ? sprintf(' media="%s"', $file['media']) : '';
$output .= '<link href="' . $file['asset'] . $this->timestamp . '"' . $attributes . $media . ' />' . "\n";
}
}
}
// Render Inline CSS
if (count($this->inline_css) > 0) {
$output .= "<style>\n";
foreach ($this->inline_css as $inline) {
$output .= $inline['asset'] . "\n";
foreach ($this->inline_css as $inline) {
if ($group && $inline['group'] == $group) {
$inline_css .= $inline['asset'] . "\n";
}
$output .= "</style>\n";
}
if ($inline_css) {
$output .= "\n<style>\n" . $inline_css . "\n</style>\n";
}
@@ -582,7 +595,7 @@ class Assets
*
* @return string
*/
protected function pipelineCss()
protected function pipelineCss($group = 'head')
{
/** @var Cache $cache */
$cache = self::getGrav()['cache'];
@@ -594,7 +607,7 @@ class Assets
// clear no-pipeline assets lists
$this->css_no_pipeline = [];
$file = md5(json_encode($this->css) . $this->css_minify . $this->css_rewrite) . '.css';
$file = md5(json_encode($this->css) . $this->css_minify . $this->css_rewrite . $group) . '.css';
$relative_path = "{$this->base_url}" . basename(ASSETS_DIR) . "/{$file}";
$absolute_path = ASSETS_DIR . $file;
@@ -606,10 +619,12 @@ class Assets
// Remove any non-pipeline files
foreach ($this->css as $id => $asset) {
if (!$asset['pipeline']) {
$this->css_no_pipeline[$id] = $asset;
} else {
$temp_css[$id] = $asset;
if ($asset['group'] == $group) {
if (!$asset['pipeline']) {
$this->css_no_pipeline[$id] = $asset;
} else {
$temp_css[$id] = $asset;
}
}
}
@@ -930,6 +945,7 @@ class Assets
* Download and concatenate the content of several links.
*
* @param array $links
* @param bool $css
*
* @return string
*/