-
-
Notifications
You must be signed in to change notification settings - Fork 170
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Backend classes for core view buttons
- Loading branch information
1 parent
e53459c
commit 445248d
Showing
8 changed files
with
353 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
namespace Kirby\Panel\Ui\Buttons; | ||
|
||
use Kirby\Cms\App; | ||
use Kirby\Cms\Language; | ||
use Kirby\Toolkit\Str; | ||
|
||
/** | ||
* View button to switch content translation languages | ||
* | ||
* @package Kirby Panel | ||
* @author Nico Hoffmann <[email protected]> | ||
* @link https://getkirby.com | ||
* @copyright Bastian Allgeier | ||
* @license https://getkirby.com/license | ||
* @since 5.0.0 | ||
* @internal | ||
*/ | ||
class LanguagesButton extends ViewButton | ||
{ | ||
protected App $kirby; | ||
|
||
public function __construct( | ||
) { | ||
$this->kirby = App::instance(); | ||
|
||
parent::__construct( | ||
component: 'k-view-languages-button', | ||
class: 'k-view-languages-button', | ||
icon: 'translate', | ||
options: $this->options(), | ||
responsive: 'text', | ||
text: Str::upper($this->kirby->language()?->code()) | ||
); | ||
} | ||
|
||
protected function option(Language $language): array | ||
{ | ||
return [ | ||
'text' => $language->name(), | ||
'code' => $language->code(), | ||
'current' => $language->code() === $this->kirby->language()->code(), | ||
]; | ||
} | ||
|
||
protected function options(): array | ||
{ | ||
$languages = $this->kirby->languages(); | ||
$options = []; | ||
|
||
if ($this->kirby->multilang() === false) { | ||
return $options; | ||
} | ||
|
||
// add the primary/default language first | ||
if ($default = $languages->default()) { | ||
$options[] = $this->option($default); | ||
$options[] = '-'; | ||
$languages = $languages->not($default); | ||
} | ||
|
||
// add all secondary languages after the separator | ||
foreach ($languages as $language) { | ||
$options[] = $this->option($language); | ||
} | ||
|
||
return $options; | ||
} | ||
|
||
public function render(): array|null | ||
{ | ||
if ($this->kirby->multilang() === false) { | ||
return null; | ||
} | ||
|
||
return parent::render(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
namespace Kirby\Panel\Ui\Buttons; | ||
|
||
use Kirby\Cms\Page; | ||
use Kirby\Toolkit\I18n; | ||
|
||
/** | ||
* Status view button for pages | ||
* | ||
* @package Kirby Panel | ||
* @author Nico Hoffmann <[email protected]> | ||
* @link https://getkirby.com | ||
* @copyright Bastian Allgeier | ||
* @license https://getkirby.com/license | ||
* @since 5.0.0 | ||
* @internal | ||
*/ | ||
class PageStatusButton extends ViewButton | ||
{ | ||
public function __construct( | ||
Page $page | ||
) { | ||
$status = $page->status(); | ||
$blueprint = $page->blueprint()->status()[$status] ?? null; | ||
$disabled = $page->permissions()->cannot('changeStatus'); | ||
$title = I18n::translate('page.status') . ': ' . I18n::translate('page.status.' . $status); | ||
|
||
if ($disabled === true) { | ||
$title .= ' (' . I18n::translate('disabled') . ')'; | ||
} | ||
|
||
parent::__construct( | ||
class: 'k-view-status-button k-page-status-button', | ||
dialog: $page->panel()->url(true) . '/changeStatus', | ||
disabled: $disabled, | ||
icon: 'status-' . $status, | ||
style: '--icon-size: 15px', | ||
text: $blueprint['label'] ?? $status, | ||
title: $title, | ||
theme: match($status) { | ||
'draft' => 'negative-icon', | ||
'unlisted' => 'info-icon', | ||
'listed' => 'positive-icon' | ||
} | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace Kirby\Panel\Ui\Buttons; | ||
|
||
use Kirby\Toolkit\I18n; | ||
|
||
/** | ||
* Preview view button | ||
* | ||
* @package Kirby Panel | ||
* @author Nico Hoffmann <[email protected]> | ||
* @link https://getkirby.com | ||
* @copyright Bastian Allgeier | ||
* @license https://getkirby.com/license | ||
* @since 5.0.0 | ||
* @internal | ||
*/ | ||
class PreviewButton extends ViewButton | ||
{ | ||
public function __construct( | ||
public string|null $link | ||
) { | ||
parent::__construct( | ||
class: 'k-view-preview-button', | ||
icon: 'open', | ||
link: $link, | ||
target: '_blank', | ||
title: I18n::translate('open') | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace Kirby\Panel\Ui\Buttons; | ||
|
||
use Kirby\Cms\ModelWithContent; | ||
use Kirby\Toolkit\I18n; | ||
|
||
/** | ||
* Settings view button for models | ||
* | ||
* @package Kirby Panel | ||
* @author Nico Hoffmann <[email protected]> | ||
* @link https://getkirby.com | ||
* @copyright Bastian Allgeier | ||
* @license https://getkirby.com/license | ||
* @since 5.0.0 | ||
* @internal | ||
*/ | ||
class SettingsButton extends ViewButton | ||
{ | ||
public function __construct( | ||
ModelWithContent $model | ||
) { | ||
parent::__construct( | ||
component: 'k-view-settings-button', | ||
class: 'k-view-settings-button', | ||
icon: 'cog', | ||
options: $model->panel()->url(true), | ||
title: I18n::translate('settings'), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
namespace Kirby\Panel\Ui\Buttons; | ||
|
||
use Kirby\Panel\Areas\AreaTestCase; | ||
|
||
/** | ||
* @coversDefaultClass \Kirby\Panel\Ui\Buttons\LanguagesButton | ||
* @covers ::__construct | ||
*/ | ||
class LanguagesButtonTest extends AreaTestCase | ||
{ | ||
/** | ||
* @covers ::render | ||
*/ | ||
public function testSingleLang() | ||
{ | ||
$button = new LanguagesButton(); | ||
$this->assertNull($button->render()); | ||
} | ||
|
||
/** | ||
* @covers ::option | ||
* @covers ::options | ||
* @covers ::props | ||
* @covers ::render | ||
*/ | ||
public function tesMultiLang() | ||
{ | ||
$this->enableMultilang(); | ||
$this->installLanguages(); | ||
|
||
$button = new LanguagesButton(); | ||
$this->assertSame('k-view-languages-button', $button->component); | ||
$this->assertSame('k-view-languages-button', $button->class); | ||
$this->assertSame('translate', $button->icon); | ||
$this->assertSame([ | ||
[ | ||
'text' => 'English', | ||
'code' => 'en', | ||
'current' => true | ||
], | ||
'-', | ||
[ | ||
'text' => 'Deutsch', | ||
'code' => 'de', | ||
'current' => false | ||
] | ||
], $button->options); | ||
$this->assertSame('text', $button->responsive); | ||
$this->assertSame('EN', $button->text); | ||
|
||
$render = $button->render(); | ||
$this->assertIsArray($render); | ||
$this->assertSame('k-view-languages-button', $render['component']); | ||
$this->assertIsArray($render['props']); | ||
$this->assertIsArray($render['props']['options']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
namespace Kirby\Panel\Ui\Buttons; | ||
|
||
use Kirby\Cms\App; | ||
use Kirby\Cms\Page; | ||
use Kirby\TestCase; | ||
|
||
/** | ||
* @coversDefaultClass \Kirby\Panel\Ui\Buttons\PageStatusButton | ||
*/ | ||
class PageStatusButtonTest extends TestCase | ||
{ | ||
/** | ||
* @covers ::__construct | ||
*/ | ||
public function testButtonDraftDisabled() | ||
{ | ||
|
||
$page = new Page(['slug' => 'test', 'isDraft' => true]); | ||
$button = new PageStatusButton($page); | ||
|
||
$this->assertSame('k-view-button', $button->component); | ||
$this->assertSame('k-view-status-button k-page-status-button', $button->class); | ||
$this->assertSame('/pages/test/changeStatus', $button->dialog); | ||
$this->assertTrue($button->disabled); | ||
$this->assertSame('status-draft', $button->icon); | ||
$this->assertTrue($button->responsive); | ||
$this->assertSame('Draft', $button->text); | ||
$this->assertSame('Status: Draft (Disabled)', $button->title); | ||
$this->assertSame('negative-icon', $button->theme); | ||
} | ||
|
||
/** | ||
* @covers ::__construct | ||
*/ | ||
public function testButtonUnlisted() | ||
{ | ||
App::instance()->impersonate('kirby'); | ||
$page = new Page(['slug' => 'test']); | ||
$button = new PageStatusButton($page); | ||
|
||
$this->assertFalse($button->disabled); | ||
$this->assertSame('status-unlisted', $button->icon); | ||
$this->assertTrue($button->responsive); | ||
$this->assertSame('Unlisted', $button->text); | ||
$this->assertSame('Status: Unlisted', $button->title); | ||
$this->assertSame('info-icon', $button->theme); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace Kirby\Panel\Ui\Buttons; | ||
|
||
use Kirby\TestCase; | ||
|
||
/** | ||
* @coversDefaultClass \Kirby\Panel\Ui\Buttons\PreviewButton | ||
*/ | ||
class PreviewButtonTest extends TestCase | ||
{ | ||
/** | ||
* @covers ::__construct | ||
*/ | ||
public function testButton() | ||
{ | ||
$button = new PreviewButton(link: 'https://getkirby.com'); | ||
|
||
$this->assertSame('k-view-button', $button->component); | ||
$this->assertSame('k-view-preview-button', $button->class); | ||
$this->assertSame('open', $button->icon); | ||
$this->assertSame('https://getkirby.com', $button->link); | ||
$this->assertSame('_blank', $button->target); | ||
$this->assertSame('Open', $button->title); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace Kirby\Panel\Ui\Buttons; | ||
|
||
use Kirby\Cms\Page; | ||
use Kirby\TestCase; | ||
|
||
/** | ||
* @coversDefaultClass \Kirby\Panel\Ui\Buttons\SettingsButton | ||
*/ | ||
class SettingsButtonTest extends TestCase | ||
{ | ||
/** | ||
* @covers ::__construct | ||
*/ | ||
public function testButton() | ||
{ | ||
$page = new Page(['slug' => 'test']); | ||
$button = new SettingsButton(model: $page); | ||
|
||
$this->assertSame('k-view-settings-button', $button->component); | ||
$this->assertSame('k-view-settings-button', $button->class); | ||
$this->assertSame('cog', $button->icon); | ||
$this->assertSame('/pages/test', $button->options); | ||
$this->assertSame('Settings', $button->title); | ||
} | ||
} |