Skip to content

Commit

Permalink
Improve getAjaxOf()
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Schendel committed Mar 6, 2024
1 parent 078e926 commit 4bcca7b
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 17 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,11 @@ We now have created a basic Twack-component and you now the general concepts how

## Changelog

### Changes in 2.3.1(2024-03-04)

- Use AppApi getAjaxOf() if available
- Improved Fallback getAjaxOf()

### Changes in 2.3.0(2024-03-03)

- Added componentLists to TwackComponent
Expand Down
58 changes: 41 additions & 17 deletions Twack.module.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static function getModuleInfo() {
return [
'title' => 'Twack',
'author' => 'Sebastian Schendel',
'version' => '2.3.0',
'version' => '2.3.1',
'summary' => 'Reusable components for your ProcessWire-templates.',
'singular' => true,
'autoload' => true,
Expand Down Expand Up @@ -941,6 +941,11 @@ public static function sendResponse($body = '', $status = 200, $contentType = 't
* @return array
*/
public static function getAjaxOf($content) {
if (wire('modules')->isInstalled('AppApi')) {
$module = wire('modules')->get('AppApi');
return $module->getAjaxOf($content);
}

$output = [];

if ($content instanceof PageFiles) {
Expand All @@ -961,22 +966,25 @@ public static function getAjaxOf($content) {
];

if ($content instanceof PageImage) {
$output['basename_mini'] = $content->size(600, 0)->basename;
$output['width'] = $content->width;
$output['height'] = $content->height;
$output['dimension_ratio'] = round($content->width / $content->height, 2);

if ($content->original) {
$output['original'] = [
'basename' => $content->original->basename,
'name' => $content->original->name,
'filesize' => $content->original->filesize,
'filesizeStr' => $content->original->filesizeStr,
'ext' => $content->original->ext,
'width' => $content->original->width,
'height' => $content->original->height,
'dimension_ratio' => round($content->original->width / $content->original->height, 2)
];
try {
$output['basename_mini'] = $content->size(600, 0)->basename;
$output['width'] = $content->width;
$output['height'] = $content->height;
$output['dimension_ratio'] = round($content->width / $content->height, 2);

if ($content->original) {
$output['original'] = [
'basename' => $content->original->basename,
'name' => $content->original->name,
'filesize' => $content->original->filesize,
'filesizeStr' => $content->original->filesizeStr,
'ext' => $content->original->ext,
'width' => $content->original->width,
'height' => $content->original->height,
'dimension_ratio' => round($content->original->width / $content->original->height, 2)
];
}
} catch (\Exception $e) {
}
}

Expand All @@ -997,6 +1005,10 @@ public static function getAjaxOf($content) {
foreach ($content as $page) {
$output[] = self::getAjaxOf($page);
}
} elseif ($content instanceof SelectableOptionArray) {
foreach ($content as $item) {
$output[] = self::getAjaxOf($item);
}
} elseif ($content instanceof Page && $content->id) {
$output = [
'id' => $content->id,
Expand All @@ -1008,6 +1020,18 @@ public static function getAjaxOf($content) {
'httpUrl' => $content->httpUrl,
'template' => self::getAjaxOf($content->template)
];
} elseif ($content instanceof SelectableOption) {
$output = [
'id' => $content->id,
'title' => $content->title,
'value' => $content->value
];
} elseif ($content instanceof WireArray) {
foreach ($content as $item) {
$output[] = self::getAjaxOf($item);
}
} elseif ($content instanceof WireData) {
$output = $content->getArray();
}

return $output;
Expand Down

0 comments on commit 4bcca7b

Please sign in to comment.