Added landscape and portrait compatibility.

This commit is contained in:
Lewin Probst 2019-08-03 22:47:48 +02:00
parent a8f493fc34
commit 4bd0ae9c93
2 changed files with 59 additions and 41 deletions

View file

@ -7,7 +7,6 @@
display: flex; display: flex;
flex-direction: row; flex-direction: row;
align-items: center; align-items: center;
justify-content: center;
position: fixed; position: fixed;
bottom: 0; bottom: 0;
width: 100%; width: 100%;

View file

@ -10,7 +10,10 @@ return new class extends Lewp\Module
const KEY_START_LANDSCAPE_ID = "start_landscape_id"; const KEY_START_LANDSCAPE_ID = "start_landscape_id";
const KEY_START_PORTRAIT_ID = "start_portrait_id"; const KEY_START_PORTRAIT_ID = "start_portrait_id";
const KEY_START_MODULE_ID = "start_module_id"; const KEY_START_MODULE_ID = "start_module_id";
const KEY_GALLERY_FOLDER_ID = "gallery_folder_id"; const KEY_GALLERY_LANDSCAPE_FOLDER_ID = "gallery_landscape_folder_id";
const KEY_GALLERY_PORTRAIT_FOLDER_ID = "gallery_portrait_folder_id";
const KEY_GALLERY_IMAGE_EXTENSION = "image_extension";
const KEY_ORIENTATION_PRIORITY = "orientation_priority";
private function createBigImageContainer() private function createBigImageContainer()
{ {
@ -50,12 +53,23 @@ return new class extends Lewp\Module
private function galleryAvailable(array $options) : bool private function galleryAvailable(array $options) : bool
{ {
$retval = true; $retval = true;
if (!isset($options[self::KEY_GALLERY_FOLDER_ID])) { if (!isset($options[self::KEY_GALLERY_LANDSCAPE_FOLDER_ID])) {
$retval = false;
}
if (!isset($options[self::KEY_GALLERY_PORTRAIT_FOLDER_ID])) {
$retval = false; $retval = false;
} }
return $retval; return $retval;
} }
private function generateGalleryPath(string $base_path, string $folder_id) : string
{
return Resolve::toFilepath([
$base_path,
Resolve::idToFilepathRel($folder_id)
]);
}
private function createGallery(array $options) private function createGallery(array $options)
{ {
$div = $this->createAndSetupElement( $div = $this->createAndSetupElement(
@ -67,51 +81,56 @@ return new class extends Lewp\Module
); );
// get all filenames // get all filenames
$folderpaths = $this->findFolderPaths('res-images'); $fileids_first = $this->collectResourceFileIds(
for ($i = 0; $i < sizeof($folderpaths); ++$i) { 'res-images',
if ($folderpaths[$i] === false) { $options[self::KEY_GALLERY_LANDSCAPE_FOLDER_ID],
continue; '*.'.$options[self::KEY_GALLERY_IMAGE_EXTENSION]
} );
// folder path of gallery $tmp = $this->collectResourceFileIds(
$gallery_folder = Resolve::toFilepath([ 'res-images',
$folderpaths[$i], $options[self::KEY_GALLERY_PORTRAIT_FOLDER_ID],
$options[self::KEY_GALLERY_FOLDER_ID] '*.'.$options[self::KEY_GALLERY_IMAGE_EXTENSION]
]); );
// all images in the folder if ($options[self::KEY_ORIENTATION_PRIORITY] === 'portrait') {
$gallery_filenames = scandir($gallery_folder); $fileids_second = $fileids_first;
// check if folder is empty or not available $fileids_first = $tmp;
if ( } else {
($gallery_filenames === false) $fileids_second = $tmp;
|| (sizeof($gallery_filenames) == 2)
) {
continue;
}
foreach ($gallery_filenames as $image) {
if ($image === '..' || $image === '.') {
continue;
}
$landscape =
$options[self::KEY_GALLERY_FOLDER_ID]
.Resolve::ID_SEPARATOR
.$image;
$rwd = new RwdPicture(
$landscape,
$landscape,
$this->getModuleId()
);
$div->appendChild(
$this->importNode($rwd->getPictureTag(), true)
);
}
break; // the loop should only run one time to the end
} }
foreach ($fileids_first as $image) {
$landscape = Resolve::arrayToId([
$options[self::KEY_GALLERY_LANDSCAPE_FOLDER_ID],
$image
]);
$portrait = $landscape;
// check if the portrait file is available
if (in_array($image, $fileids_second)) {
$portrait = Resolve::arrayToId([
$options[self::KEY_GALLERY_PORTRAIT_FOLDER_ID],
$image
]);;
}
$rwd = new RwdPicture(
$landscape,
$portrait,
$this->getModuleId()
);
$div->appendChild(
$this->importNode($rwd->getPictureTag(), true)
);
}
return $div; return $div;
} }
public function run(array $options = []) : bool public function run(array $options = []) : bool
{ {
// add default options
$options += [
self::KEY_ORIENTATION_PRIORITY => 'landscape',
self::KEY_GALLERY_IMAGE_EXTENSION => 'jpg'
];
$big_image_container = $this->createBigImageContainer(); $big_image_container = $this->createBigImageContainer();
if ($this->bigImageAvailable($options)) { if ($this->bigImageAvailable($options)) {
$big_image = $this->createBigImage( $big_image = $this->createBigImage(