Skip to content

Commit

Permalink
feat: 新增/修改/详情, 支持在抽屉模式
Browse files Browse the repository at this point in the history
  • Loading branch information
slowlyo committed Jun 12, 2024
1 parent cace353 commit a7fc787
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 48 deletions.
6 changes: 5 additions & 1 deletion lang/en/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@
'property_name' => 'Property Name',
'value' => 'Value',
'dialog_form' => 'Dialog Form',
'dialog_size' => 'Dialog Size',
'copy_record' => 'Copy Record',
'copy_record_description' => 'You can copy and share to <a href="https://github.com/Slowlyo/owl-admin/discussions/categories/%E4%BB%A3%E7%A0%81%E7%94%9F%E6%88%90%E8%AE%B0%E5%BD%95" target="_blank">Github</a>',
'import_record' => 'Import Record',
Expand Down Expand Up @@ -157,6 +156,11 @@
'filter_input_name' => 'Parameter Name',
'filter_input_label' => 'Filter Label',
'filter_component' => 'Filter Component',
'page' => 'Page',
'dialog' => 'Dialog',
'drawer' => 'Drawer',
'dialog_size' => 'Dialog Size',
'drawer_size' => 'Drawer Size',
],

'admin_users' => 'Admin Users',
Expand Down
6 changes: 5 additions & 1 deletion lang/zh_CN/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@
'property_name' => '属性名称',
'value' => '',
'dialog_form' => '弹窗表单',
'dialog_size' => '弹窗大小',
'copy_record' => '复制记录',
'copy_record_description' => '你可以复制后分享到 <a href="https://github.com/Slowlyo/owl-admin/discussions/categories/%E4%BB%A3%E7%A0%81%E7%94%9F%E6%88%90%E8%AE%B0%E5%BD%95" target="_blank">Github</a>',
'import_record' => '导入记录',
Expand Down Expand Up @@ -157,6 +156,11 @@
'filter_input_name' => '参数名称',
'filter_input_label' => '筛选项 Label',
'filter_component' => '筛选组件',
'page' => '页面',
'dialog' => '弹窗',
'drawer' => '抽屉',
'dialog_size' => '弹窗大小',
'drawer_size' => '抽屉大小',
],

'admin_users' => '管理员',
Expand Down
4 changes: 4 additions & 0 deletions src/Console/IdeHelperCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,13 @@ public function handleHelper()
$class = $value['class'];
$service = $value['serviceRaw'];

$this->pushMeta("expectedArguments(\\{$value['raw']}::createButton(), 0, 'dialog', 'drawer', true);");
$this->pushMeta("expectedArguments(\\{$value['raw']}::createButton(), 1, 'xs', 'sm', 'md', 'lg', 'xl', 'full');");
$this->pushMeta("expectedArguments(\\{$value['raw']}::rowEditButton(), 0, 'dialog', 'drawer', true);");
$this->pushMeta("expectedArguments(\\{$value['raw']}::rowEditButton(), 1, 'xs', 'sm', 'md', 'lg', 'xl', 'full');");
$this->pushMeta("expectedArguments(\\{$value['raw']}::rowShowButton(), 0, 'dialog', 'drawer', true);");
$this->pushMeta("expectedArguments(\\{$value['raw']}::rowShowButton(), 1, 'xs', 'sm', 'md', 'lg', 'xl', 'full');");
$this->pushMeta("expectedArguments(\\{$value['raw']}::rowActions(), 0, 'dialog', 'drawer', true);");
$this->pushMeta("expectedArguments(\\{$value['raw']}::rowActions(), 1, 'xs', 'sm', 'md', 'lg', 'xl', 'full');");

return <<<PHP
Expand Down
16 changes: 12 additions & 4 deletions src/Controllers/DevTools/CodeGeneratorController.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,13 +288,21 @@ public function form($isEdit = false)
// 页面配置
amis()->Tab()->title(admin_trans('admin.code_generators.page_config'))->body(
amis()->ComboControl('page_info', false)->multiLine()->subFormMode('horizontal')->items([
amis()
->SwitchControl('dialog_form', admin_trans('admin.code_generators.dialog_form'))
->value(1),
amis()->RadiosControl('dialog_form', admin_trans('admin.code_generators.dialog_form'))
->options([
['label' => admin_trans('admin.code_generators.dialog'), 'value' => 'dialog'],
['label' => admin_trans('admin.code_generators.drawer'), 'value' => 'drawer'],
['label' => admin_trans('admin.code_generators.page'), 'value' => 'page'],
])
->selectFirst(),
amis()->SelectControl('dialog_size', admin_trans('admin.code_generators.dialog_size'))
->options(['xs', 'sm', 'md', 'lg', 'xl', 'full'])
->value('md')
->visibleOn('${!!dialog_form}'),
->visibleOn('${dialog_form == "dialog"}'),
amis()->SelectControl('dialog_size', admin_trans('admin.code_generators.drawer_size'))
->options(['xs', 'sm', 'md', 'lg', 'full'])
->value('md')
->visibleOn('${dialog_form == "drawer"}'),
amis()->CheckboxesControl('row_actions', admin_trans('admin.actions'))->options([
'show' => admin_trans('admin.show'),
'edit' => admin_trans('admin.edit'),
Expand Down
19 changes: 10 additions & 9 deletions src/Support/CodeGenerator/ControllerGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,10 @@ protected function replaceListContent(&$content)
}

// 顶部工具栏
if ($this->model->page_info['dialog_form']) {
$dialog = $this->model->page_info['dialog_form'];
if ($dialog != 'page') {
$content .= "\t\t\t->headerToolbar([" . PHP_EOL;
$content .= "\t\t\t\t\$this->createButton(true{$this->getDialogSize()})," . PHP_EOL;
$content .= "\t\t\t\t\$this->createButton('{$dialog}'{$this->getDialogSize()})," . PHP_EOL;
$content .= "\t\t\t\t...\$this->baseHeaderToolBar()" . PHP_EOL;
$content .= "\t\t\t])" . PHP_EOL;
}
Expand Down Expand Up @@ -191,32 +192,32 @@ public function getColumnComponent($type, $column)
}

return match ($type) {
'list_component' => "amis()->TableColumn('{$column['name']}', '{$label}')",
'form_component' => "amis()->TextControl('{$column['name']}', '{$label}')",
'list_component' => "amis()->TableColumn('{$column['name']}', '{$label}')",
'form_component' => "amis()->TextControl('{$column['name']}', '{$label}')",
'detail_component' => "amis()->TextControl('{$column['name']}', '{$label}')->static()",
};
}

private function makeRowButton($pageInfo)
{
$_actions = data_get($pageInfo, 'row_actions');
$isDialog = $pageInfo['dialog_form'] ? 'true' : '';
$dialog = $pageInfo['dialog_form'] ? "'{$pageInfo['dialog_form']}'" : '';
$dialogSize = $this->getDialogSize();

if (in_array('show', $_actions) && in_array('edit', $_actions) && in_array('delete', $_actions)) {
return "\$this->rowActions({$isDialog}{$dialogSize})";
return "\$this->rowActions({$dialog}{$dialogSize})";
}

$str = "\$this->rowActions([\n\t\t\t\t";

if (in_array('show', $_actions)) {
$str .= "\t\$this->rowShowButton({$isDialog}{$dialogSize}),\n\t\t\t\t";
$str .= "\t\$this->rowShowButton({$dialog}{$dialogSize}),\n\t\t\t\t";
}
if (in_array('edit', $_actions)) {
$str .= "\t\$this->rowEditButton({$isDialog}{$dialogSize}),\n\t\t\t\t";
$str .= "\t\$this->rowEditButton({$dialog}{$dialogSize}),\n\t\t\t\t";
}
if (in_array('delete', $_actions)) {
$str .= "\t\$this->rowDeleteButton({$isDialog}{$dialogSize}),\n\t\t\t\t";
$str .= "\t\$this->rowDeleteButton({$dialog}{$dialogSize}),\n\t\t\t\t";
}
$str .= "])";

Expand Down
93 changes: 60 additions & 33 deletions src/Traits/ElementTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,82 +60,109 @@ protected function bulkDeleteButton()
/**
* 新增按钮
*
* @param bool $dialog
* @param string $dialogSize
* @param bool|string $dialog 是否弹窗, 弹窗: true|dialog, 抽屉: drawer,
* @param string $dialogSize 弹窗大小, 默认: md, 可选值: xs | sm | md | lg | xl | full
* @param string $title 弹窗标题 & 按钮文字, 默认: 新增
*
* @return \Slowlyo\OwlAdmin\Renderers\DialogAction|\Slowlyo\OwlAdmin\Renderers\LinkAction
*/
protected function createButton(bool $dialog = false, string $dialogSize = '')
protected function createButton(bool|string $dialog = false, string $dialogSize = 'md', string $title = '')
{
$title = $title ?: admin_trans('admin.create');
$button = amis()->LinkAction()->link($this->getCreatePath());

if ($dialog) {
$form = $this->form(false)->canAccessSuperData(false)->api($this->getStorePath())->onEvent([]);

$button = amis()->DialogAction()->dialog(
amis()->Dialog()->title(admin_trans('admin.create'))->body($form)->size($dialogSize)
);
} else {
$button = amis()->LinkAction()->link($this->getCreatePath());
if ($dialog === 'drawer') {
$button = amis()->DrawerAction()->drawer(
amis()->Drawer()->title($title)->body($form)->size($dialogSize)
);
} else {
$button = amis()->DialogAction()->dialog(
amis()->Dialog()->title($title)->body($form)->size($dialogSize)
);
}
}

return $button->label(admin_trans('admin.create'))->icon('fa fa-add')->level('primary');
return $button->label($title)->icon('fa fa-add')->level('primary');
}

/**
* 行编辑按钮
*
* @param bool $dialog
* @param string $dialogSize
* @param bool|string $dialog 是否弹窗, 弹窗: true|dialog, 抽屉: drawer,
* @param string $dialogSize 弹窗大小, 默认: md, 可选值: xs | sm | md | lg | xl | full
* @param string $title 弹窗标题 & 按钮文字, 默认: 编辑
*
* @return \Slowlyo\OwlAdmin\Renderers\DialogAction|\Slowlyo\OwlAdmin\Renderers\LinkAction
*/
protected function rowEditButton(bool $dialog = false, string $dialogSize = '')
protected function rowEditButton(bool|string $dialog = false, string $dialogSize = 'md', string $title = '')
{
$title = $title ?: admin_trans('admin.edit');
$button = amis()->LinkAction()->link($this->getEditPath());

if ($dialog) {
$form = $this->form(true)
->api($this->getUpdatePath())
->initApi($this->getEditGetDataPath())
->redirect('')
->onEvent([]);

$button = amis()->DialogAction()->dialog(
amis()->Dialog()->title(admin_trans('admin.edit'))->body($form)->size($dialogSize)
);
} else {
$button = amis()->LinkAction()->link($this->getEditPath());
if ($dialog === 'drawer') {
$button = amis()->DrawerAction()->drawer(
amis()->Drawer()->title($title)->body($form)->size($dialogSize)
);
} else {
$button = amis()->DialogAction()->dialog(
amis()->Dialog()->title($title)->body($form)->size($dialogSize)
);
}
}

return $button->label(admin_trans('admin.edit'))->icon('fa-regular fa-pen-to-square')->level('link');
return $button->label($title)->icon('fa-regular fa-pen-to-square')->level('link');
}

/**
* 行详情按钮
*
* @param bool $dialog
* @param string $dialogSize
* @param bool|string $dialog 是否弹窗, 弹窗: true|dialog, 抽屉: drawer
* @param string $dialogSize 弹窗大小, 默认: md, 可选值: xs | sm | md | lg | xl | full
* @param string $title 弹窗标题 & 按钮文字, 默认: 详情
*
* @return \Slowlyo\OwlAdmin\Renderers\DialogAction|\Slowlyo\OwlAdmin\Renderers\LinkAction
*/
protected function rowShowButton(bool $dialog = false, string $dialogSize = '')
protected function rowShowButton(bool|string $dialog = false, string $dialogSize = 'md', string $title = '')
{
$title = $title ?: admin_trans('admin.show');
$button = amis()->LinkAction()->link($this->getShowPath());

if ($dialog) {
$button = amis()->DialogAction()->dialog(
amis()->Dialog()->title(admin_trans('admin.show'))->body($this->detail('$id'))->size($dialogSize)
);
} else {
$button = amis()->LinkAction()->link($this->getShowPath());
if ($dialog === 'drawer') {
$button = amis()->DrawerAction()->drawer(
amis()->Drawer()->title($title)->body($this->detail('$id'))->size($dialogSize)
);
} else {
$button = amis()->DialogAction()->dialog(
amis()->Dialog()->title($title)->body($this->detail('$id'))->size($dialogSize)
);
}
}

return $button->label(admin_trans('admin.show'))->icon('fa-regular fa-eye')->level('link');
return $button->label($title)->icon('fa-regular fa-eye')->level('link');
}

/**
* 行删除按钮
*
* @param string $title
*
* @return \Slowlyo\OwlAdmin\Renderers\DialogAction
*/
protected function rowDeleteButton()
protected function rowDeleteButton(string $title = '')
{
return amis()->DialogAction()
->label(admin_trans('admin.delete'))
->label($title ?: admin_trans('admin.delete'))
->icon('fa-regular fa-trash-can')
->level('link')
->dialog(
Expand All @@ -157,12 +184,12 @@ protected function rowDeleteButton()
/**
* 操作列
*
* @param bool $dialog
* @param string $dialogSize
* @param bool|array|string $dialog 是否弹窗, 弹窗: true|dialog, 抽屉: drawer
* @param string $dialogSize 弹窗大小, 默认: md, 可选值: xs | sm | md | lg | xl | full
*
* @return \Slowlyo\OwlAdmin\Renderers\Operation
*/
protected function rowActions(bool|array $dialog = false, string $dialogSize = '')
protected function rowActions(bool|array|string $dialog = false, string $dialogSize = 'md')
{
if (is_array($dialog)) {
return amis()->Operation()->label(admin_trans('admin.actions'))->buttons($dialog);
Expand Down Expand Up @@ -249,7 +276,7 @@ protected function baseForm(bool $back = true)
{
$path = str_replace(Admin::config('admin.route.prefix'), '', request()->path());

$form = amis()->Form()->panelClassName('px-48 m:px-0')->title(' ')->mode('horizontal')->promptPageLeave();
$form = amis()->Form()->panelClassName('px-48 m:px-0')->title(' ')->mode('normal')->promptPageLeave();

if ($back) {
$form->onEvent([
Expand Down

0 comments on commit a7fc787

Please sign in to comment.