Skip to content

Commit 270a8b6

Browse files
committed
Render status icons and toggles
1 parent 7dec650 commit 270a8b6

File tree

5 files changed

+155
-2
lines changed

5 files changed

+155
-2
lines changed

DashboardPanelCollection.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

DashboardPanelCollection.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

DashboardPanelCollection.module

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,14 @@ class DashboardPanelCollection extends DashboardPanel
135135
if ($markup === 'actions__') {
136136
$content = $this->renderPageActions($page);
137137
}
138+
// Special case: page status
139+
elseif ($markup === 'status__') {
140+
$content = $this->renderStatusIcon($page);
141+
}
142+
// Special case: published status toggle
143+
elseif (in_array($markup, ['published__', 'visible__'])) {
144+
$content = $this->renderStatusToggle($page, $markup);
145+
}
138146
// Special case: page icon
139147
elseif ($markup === 'page_icon') {
140148
$content = $this->renderIcon($page->getIcon());
@@ -232,6 +240,20 @@ class DashboardPanelCollection extends DashboardPanel
232240
$changed = true;
233241
}
234242
break;
243+
case 'show':
244+
$show = !!$value;
245+
$visible = !$page->isHidden();
246+
if ($page->editable() && $show && !$visible) {
247+
$page->removeStatus(Page::statusHidden);
248+
$page->save('status');
249+
$changed = true;
250+
}
251+
if (!$show && $visible) {
252+
$page->addStatus(Page::statusHidden);
253+
$page->save('status');
254+
$changed = true;
255+
}
256+
break;
235257
case 'trash':
236258
$trash = !!$value;
237259
$trashed = $page->isTrash();
@@ -284,6 +306,7 @@ class DashboardPanelCollection extends DashboardPanel
284306
$this->listPage = $this->getPageFromObjectOrSelectorOrID($this->data['list'] ?? null);
285307
$this->editMode = $this->data['editMode'] ?? self::windowModeBlank;
286308
$this->viewMode = $this->data['viewMode'] ?? self::windowModeBlank;
309+
$this->addUrlParams = $this->data['addUrlParams'] ?? [];
287310

288311
if (is_string($this->collection)) {
289312
$this->collection = $this->pages->find($this->collection);
@@ -404,6 +427,7 @@ class DashboardPanelCollection extends DashboardPanel
404427
'modalAutoclose' => self::modalAutocloseAdd,
405428
'reloadOnModalClose' => true,
406429
];
430+
$action = $this->setQueryParameter($action, $this->addUrlParams);
407431
if (self::windowModeModal === $mode) {
408432
$action = $this->setQueryParameter($action, 'modal', 1);
409433
}
@@ -554,4 +578,61 @@ class DashboardPanelCollection extends DashboardPanel
554578

555579
return implode(' ', $thumbnails);
556580
}
581+
582+
/**
583+
* Render status icon.
584+
*/
585+
protected function renderStatusIcon(Page $page)
586+
{
587+
if ($page->hasStatus(Page::statusTrash)) {
588+
$icon = 'trash-can';
589+
$tooltip = $this->_('In Trash');
590+
} elseif ($page->hasStatus(Page::statusUnpublished)) {
591+
$icon = 'pen';
592+
$tooltip = $this->_('Draft');
593+
} elseif ($page->hasStatus(Page::statusHidden)) {
594+
$icon = 'eye-slash';
595+
$tooltip = $this->_('Hidden');
596+
} else {
597+
$icon = 'circle-check';
598+
$tooltip = $this->_('Published');
599+
}
600+
$icon = $this->renderIcon($icon);
601+
return "<span class='tooltip' title='{$tooltip}'>{$icon}</span>";
602+
}
603+
604+
/**
605+
* Render publish/visibility toggle.
606+
*/
607+
protected function renderStatusToggle(Page $page, $status = 'published')
608+
{
609+
$status = $this->wire()->sanitizer->option($status, ['published', 'visible']) ?? 'published';
610+
611+
switch ($status) {
612+
case 'visible':
613+
$action = 'show';
614+
$statusFlag = Page::statusHidden;
615+
$labelInactive = $this->_('Hidden');
616+
$labelActive = $this->_('Visible');
617+
break;
618+
case 'published':
619+
default:
620+
$action = 'publish';
621+
$statusFlag = Page::statusUnpublished;
622+
$labelInactive = $this->_('Draft');
623+
$labelActive = $this->_('Published');
624+
break;
625+
}
626+
627+
$active = !$page->hasStatus($statusFlag);
628+
$checked = $active ? 'checked' : '';
629+
$tooltip = $active ? $labelActive : $labelInactive;
630+
631+
return "
632+
<label class='uk-switch tooltip' for='page-{$status}-{$page}' title='{$tooltip}'>
633+
<input type='checkbox' id='page-{$status}-{$page}' name='actions[{$action}][{$page}]' value='1' {$checked}>
634+
<div class='uk-switch-slider'></div>
635+
</label>
636+
";
637+
}
557638
}

src/DashboardPanelCollection.css

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,63 @@
8686
border-radius: var(--dashboard-panel-collection-color-radius);
8787
}
8888
}
89+
90+
/* Switch wrapper */
91+
92+
.uk-switch {
93+
--switch-width: 1.9em;
94+
--switch-height: 1.1em;
95+
--switch-pointer-offset: 2px;
96+
--switch-pointer-size: calc(var(--switch-height) - 2 * var(--switch-pointer-offset));
97+
--switch-active-color: var(--dashboard-color-button-hover-bg, currentColor);
98+
position: relative;
99+
display: inline-block;
100+
width: var(--switch-width);
101+
height: var(--switch-height);
102+
transform: translateY(0.2em);
103+
}
104+
105+
/* Hide default HTML checkbox */
106+
107+
.uk-switch input {
108+
display: none;
109+
}
110+
111+
/* Switch slider */
112+
113+
.uk-switch-slider {
114+
background-color: rgba(0,0,0,0.22);
115+
position: absolute;
116+
top: 0;
117+
left: 0;
118+
right: 0;
119+
border-radius: 999vw;
120+
bottom: 0;
121+
cursor: pointer;
122+
transition-property: background-color;
123+
transition-duration: .2s;
124+
}
125+
126+
/* Switch pointer */
127+
.uk-switch-slider:before {
128+
content: '';
129+
background-color: #fff;
130+
position: absolute;
131+
width: var(--switch-pointer-size);
132+
height: var(--switch-pointer-size);
133+
left: var(--switch-pointer-offset);
134+
bottom: var(--switch-pointer-offset);
135+
border-radius: 50%;
136+
transition-property: transform, box-shadow;
137+
transition-duration: .2s;
138+
}
139+
140+
/* Active slider */
141+
input:checked + .uk-switch-slider {
142+
background-color: var(--switch-active-color) !important;
143+
}
144+
145+
/* Animate to active state */
146+
input:checked + .uk-switch-slider:before {
147+
transform: translateX(-100%) translateX(calc(var(--switch-width) - var(--switch-pointer-offset) * 2));
148+
}

src/DashboardPanelCollection.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,18 @@ function initPanel($panel) {
3333
$panel.trigger('reload', { animate: false, params });
3434
});
3535
}
36+
37+
const $actionInputs = $panel.find('input[name^="actions["]');
38+
if ($actionInputs.length) {
39+
$actionInputs.on('change', (event) => {
40+
event.preventDefault();
41+
const name = event.target.name;
42+
const checked = event.target.checked;
43+
const value = checked ? 1 : '';
44+
const params = { [name]: value };
45+
$panel.trigger('reload', { animate: false, params });
46+
});
47+
}
3648
}
3749

3850
$(document).on('dashboard:panel(collection)', (event, { $element }) => {

0 commit comments

Comments
 (0)