Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix aspect ratio value #2040

Merged
merged 2 commits into from
Jul 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/).
In order to read more about upgrading and BC breaks have a look at the [UPGRADE Document](UPGRADE.md).

## 1.6.1

+ [#2040](https://github.com/luyadev/luya/pull/2040) Fixed an issue where LazyLoad widget returns aspect ratio values with commas instead of dots, because of locale formatting.

## 1.6.0 (20. July 2020)

> Small LazyLoader adjustment. Check the [UPGRADE document](UPGRADE.md) to read more about breaking changes.
Expand Down
104 changes: 60 additions & 44 deletions core/lazyload/LazyLoad.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,51 @@ class LazyLoad extends Widget
*/
public $initJs = true;

/**
* @var string The default classes which will be registered.
* @since 1.6.1
*/
public $defaultCss = '.lazyimage-wrapper {
display: block;
width: 100%;
position: relative;
overflow: hidden;
}
.lazyimage {
position: absolute;
top: 50%;
left: 50%;
bottom: 0;
right: 0;
opacity: 0;
height: 100%;
width: 100%;
-webkit-transition: .5s ease-in-out opacity;
transition: .5s ease-in-out opacity;
-webkit-transform: translate(-50%,-50%);
transform: translate(-50%,-50%);
-o-object-fit: cover;
object-fit: cover;
-o-object-position: center center;
object-position: center center;
z-index: 20;
}
.lazyimage.loaded {
opacity: 1;
}
.lazyimage-placeholder {
display: block;
width: 100%;
height: auto;
background-color: #f0f0f0;
}
.nojs .lazyimage,
.nojs .lazyimage-placeholder,
.no-js .lazyimage,
.no-js .lazyimage-placeholder {
display: none;
}';

/**
* @inheritdoc
*/
Expand All @@ -105,48 +150,20 @@ public function init()
$this->view->registerJs("$.lazyLoad();", View::POS_READY, self::JS_ASSET_KEY);
}

$this->view->registerCss("
.lazyimage-wrapper {
display: block;
width: 100%;
position: relative;
overflow: hidden;
}
.lazyimage {
position: absolute;
top: 50%;
left: 50%;
bottom: 0;
right: 0;
opacity: 0;
height: 100%;
width: 100%;
-webkit-transition: .5s ease-in-out opacity;
transition: .5s ease-in-out opacity;
-webkit-transform: translate(-50%,-50%);
transform: translate(-50%,-50%);
-o-object-fit: cover;
object-fit: cover;
-o-object-position: center center;
object-position: center center;
z-index: 20;
}
.lazyimage.loaded {
opacity: 1;
}
.lazyimage-placeholder {
display: block;
width: 100%;
height: auto;
background-color: #f0f0f0;
}
.nojs .lazyimage,
.nojs .lazyimage-placeholder,
.no-js .lazyimage,
.no-js .lazyimage-placeholder {
display: none;
}
", [], self::CSS_ASSET_KEY);
$this->view->registerCss($this->defaultCss, [], self::CSS_ASSET_KEY);
}

/**
* Returns the aspect ration based on height or width.
*
* If no width or height is provided, the default value 56.25 will be returned.
*
* @return float A dot seperated ratio value
* @since 1.6.1
*/
protected function generateAspectRation()
{
return ($this->height && $this->width) ? str_replace(',', '.', ($this->height / $this->width) * 100) : 56.25;
}

/**
Expand All @@ -167,8 +184,7 @@ public function run()
if ($this->placeholderSrc) {
$tag .= Html::tag('img', '', ['class' => 'lazyimage-placeholder', 'src' => $this->placeholderSrc]);
} else {
$aspectRatio = ($this->height && $this->width) ? (($this->height / $this->width) * 100) : 56.25;
$tag .= '<div class="lazyimage-placeholder"><div style="display: block; height: 0px; padding-bottom: ' . $aspectRatio . '%;"></div><div class="loader"></div></div>';
$tag .= '<div class="lazyimage-placeholder"><div style="display: block; height: 0px; padding-bottom: ' . $this->generateAspectRation() . '%;"></div><div class="loader"></div></div>';
}
$tag .= '<noscript><img class="lazyimage loaded ' . $this->extraClass . '" src="'.$this->src.'" /></noscript>';
$tag .= '</div>';
Expand Down
8 changes: 8 additions & 0 deletions tests/core/lazyload/LazyLoadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,12 @@ public function testWidgetWithOptions()
LazyLoad::widget(['src' => 'abc.jpg', 'extraClass' => 'add', 'options' => ['alt' => 'The image alt', 'title' => 'The image title']])
);
}

public function testLazyLoadWithHeightCalculationWithNumbers()
{
$this->assertSame(
'<div class="lazyimage-wrapper add"><img class="js-lazyimage lazyimage" data-src="abc.jpg" data-width="1080" data-height="1920"><div class="lazyimage-placeholder"><div style="display: block; height: 0px; padding-bottom: 177.77777777778%;"></div><div class="loader"></div></div><noscript><img class="lazyimage loaded add" src="abc.jpg" /></noscript></div>',
LazyLoad::widget(['src' => 'abc.jpg', 'extraClass' => 'add', 'height' => 1920, 'width' => 1080])
);
}
}