Skip to content

Commit

Permalink
增加项目文档排序增强,支持文件夹优先、自有排序,增加项目目录展开折叠配置,支持自动,全部展开,全部折叠配置 #149 #150
Browse files Browse the repository at this point in the history
  • Loading branch information
mylxsw committed Mar 29, 2021
1 parent 54dfc1b commit 34c5039
Show file tree
Hide file tree
Showing 11 changed files with 177 additions and 45 deletions.
2 changes: 1 addition & 1 deletion app/Http/Controllers/BatchExportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function batchExport(Request $request, $project_id)

/** @var Collection $documents */
$documents = $project->pages;
$navigators = navigatorSort(navigator($project_id, 0));
$navigators = navigatorSort(navigator($project_id, 0), $project->catalog_sort_style);

if ($pid !== 0) {
$navigators = $this->filterNavigators($navigators, function (array $nav) use ($pid) {
Expand Down
18 changes: 12 additions & 6 deletions app/Http/Controllers/ProjectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -364,12 +364,14 @@ private function basicSettingHandle(Request $request, Project $project): bool
$this->validate(
$request,
[
'name' => 'required|between:1,100',
'description' => 'max:255',
'project_id' => "required|in:{$project->id}|project_exist",
'visibility' => 'required|in:1,2',
'sort_level' => 'integer|between:-999999999,999999999',
'catalog' => 'required|integer',
'name' => 'required|between:1,100',
'description' => 'max:255',
'project_id' => "required|in:{$project->id}|project_exist",
'visibility' => 'required|in:1,2',
'sort_level' => 'integer|between:-999999999,999999999',
'catalog' => 'required|integer',
'catalog_sort_style' => 'in:0,1',
'catalog_fold_style' => 'in:0,1,2',
],
[
'name.required' => __('project.validation.project_name_required'),
Expand All @@ -383,11 +385,15 @@ private function basicSettingHandle(Request $request, Project $project): bool
$visibility = $request->input('visibility');
$sortLevel = $request->input('sort_level');
$catalog = $request->input('catalog');
$catalogSortStyle = $request->input('catalog_sort_style', Project::SORT_STYLE_DIR_FIRST);
$catalogFoldStyle = $request->input('catalog_fold_style', Project::FOLD_STYLE_AUTO);

$project->name = $name;
$project->description = $description;
$project->visibility = $visibility;
$project->catalog_id = empty($catalog) ? null : $catalog;
$project->catalog_fold_style = $catalogFoldStyle;
$project->catalog_sort_style = $catalogSortStyle;
if (\Auth::user()->can('project-sort') && $sortLevel != null) {
$project->sort_level = (int)$sortLevel;
}
Expand Down
27 changes: 26 additions & 1 deletion app/Repositories/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
* @property \Carbon\Carbon|null $deleted_at
* @property int $sort_level 项目排序,排序值越大越靠后
* @property int|null $catalog_id 目录ID
* @property int $catalog_fold_style 目录展示样式
* @property int $catalog_sort_style 目录排序样式
* @method static \Illuminate\Database\Eloquent\Builder|\App\Repositories\Project whereCatalogId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Repositories\Project whereCreatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Repositories\Project whereDeletedAt($value)
Expand Down Expand Up @@ -70,6 +72,27 @@ class Project extends Repository
*/
const PRIVILEGE_RO = 2;

/**
* 目录折叠样式:自动
*/
const FOLD_STYLE_AUTO = 0;
/**
* 目录折叠样式:全部展开
*/
const FOLD_STYLE_UNFOLD = 1;
/**
* 目录折叠样式:全部折叠
*/
const FOLD_STYLE_FOLD = 2;
/**
* 排序样式:文件夹优先
*/
const SORT_STYLE_DIR_FIRST = 0;
/**
* 排序样式:自由排序
*/
const SORT_STYLE_FREE = 1;

protected $table = 'wz_projects';
protected $fillable
= [
Expand All @@ -79,6 +102,8 @@ class Project extends Repository
'user_id',
'sort_level',
'catalog_id',
'catalog_fold_style',
'catalog_sort_style',
];

public $dates = ['deleted_at'];
Expand Down Expand Up @@ -111,7 +136,7 @@ public function user()
public function groups()
{
return $this->belongsToMany(Group::class, 'wz_project_group_ref', 'project_id', 'group_id')
->withPivot('created_at', 'updated_at', 'privilege');
->withPivot('created_at', 'updated_at', 'privilege');
}

/**
Expand Down
9 changes: 7 additions & 2 deletions app/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

use App\Repositories\Catalog;
use App\Repositories\Document;
use App\Repositories\Project;
use App\Repositories\Template;
use App\Repositories\User;
use Carbon\Carbon;
Expand Down Expand Up @@ -130,10 +131,11 @@ function ($nav) {
* 导航排序,排序后,文件夹靠前,普通文件靠后
*
* @param array $navItems
* @param int $sortStyle
*
* @return array
*/
function navigatorSort($navItems)
function navigatorSort($navItems, $sortStyle = Project::SORT_STYLE_DIR_FIRST)
{
$sortItem = function ($a, $b) {
try {
Expand All @@ -153,7 +155,10 @@ function navigatorSort($navItems)

usort(
$navItems,
function ($a, $b) use ($sortItem) {
function ($a, $b) use ($sortItem, $sortStyle) {
if ($sortStyle == Project::SORT_STYLE_FREE) {
return $sortItem($a, $b);
}

$aIsFolder = !empty($a['nodes']);
$bIsFolder = !empty($b['nodes']);
Expand Down
33 changes: 33 additions & 0 deletions database/migrations/2021_03_29_133806_add_project_menu_control.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AddProjectMenuControl extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('wz_projects', function (Blueprint $table) {
$table->tinyInteger('catalog_fold_style')->default(0)->comment('目录展开样式:0-自动 1-全部展开 2-全部折叠');
$table->tinyInteger('catalog_sort_style')->default(0)->comment('目录排序样式:0-目录优先 1-自由排序');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('wz_projects', function (Blueprint $table) {
$table->dropColumn(['catalog_fold_style', 'catalog_sort_style']);
});
}
}
95 changes: 69 additions & 26 deletions public/assets/js/navigator-tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
* 导航树
*
* @param left_nav
* @param mode 0-自动 1-全部展开 2-全部折叠
*/
$.wz.navigator_tree = function (left_nav) {
$.wz.navigator_tree = function (left_nav, mode) {
var icon_close = 'fa fa-folder-o';
var icon_open = 'fa fa-folder-open-o';
mode = mode || 0;

var childrenShow = function (elementLi) {
elementLi.children('ul').show();
Expand All @@ -21,29 +23,74 @@ $.wz.navigator_tree = function (left_nav) {
return elementLi;
};

// 先隐藏所有的li下的子元素
left_nav.find('li.wz-has-child').children('ul').hide();
// 在包含子元素的li中添加展开图标和链接
left_nav.find('li.wz-has-child').prepend('<a href="javascript:;" class="wz-nav-fold ' + icon_close + '"></a>');
// 菜单折叠事件处理
left_nav.find('li.wz-has-child').find('.wz-nav-fold')
.on('click', function () {
if ($(this).hasClass(icon_close)) {
$(this).removeClass(icon_close).addClass(icon_open);
} else {
$(this).removeClass(icon_open).addClass(icon_close);
}
switch (mode) {
case 0: // 自动
(function () {
// 先隐藏所有的li下的子元素
left_nav.find('li.wz-has-child').children('ul').hide();
// 在包含子元素的li中添加展开图标和链接
left_nav.find('li.wz-has-child').prepend('<a href="javascript:;" class="wz-nav-fold ' + icon_close + '"></a>');
// 菜单折叠事件处理
left_nav.find('li.wz-has-child').find('.wz-nav-fold')
.on('click', function () {
if ($(this).hasClass(icon_close)) {
$(this).removeClass(icon_close).addClass(icon_open);
} else {
$(this).removeClass(icon_open).addClass(icon_close);
}

$(this).parent().children('ul').slideToggle('fast');
});
$(this).parent().children('ul').slideToggle('fast');
});

left_nav.find('.wz-auto-open').children('li.wz-has-child').each(function () {
var childrenCount = $(this).children('ul').children('li').length;
// 如果一级菜单的子元素小于7个,则自动展开
if (childrenCount < 7) {
$(this).children('a.wz-nav-fold').trigger('click');
}
});
left_nav.find('.wz-auto-open').children('li.wz-has-child').each(function () {
var childrenCount = $(this).children('ul').children('li').length;
// 如果一级菜单的子元素小于7个,则自动展开
if (childrenCount < 7) {
$(this).children('a.wz-nav-fold').trigger('click');
}
});

// 一级元素的子元素自动展示
childrenShow(left_nav.children('li'));
})();
break;
case 1: // 全部展开
(function(){
// 在包含子元素的li中添加展开图标和链接
left_nav.find('li.wz-has-child').prepend('<a href="javascript:;" class="wz-nav-fold ' + icon_open + '"></a>');
// 菜单折叠事件处理
left_nav.find('li.wz-has-child').find('.wz-nav-fold')
.on('click', function () {
if ($(this).hasClass(icon_close)) {
$(this).removeClass(icon_close).addClass(icon_open);
} else {
$(this).removeClass(icon_open).addClass(icon_close);
}

$(this).parent().children('ul').slideToggle('fast');
});
})()
break;
case 2: // 全部折叠
(function(){
// 先隐藏所有的li下的子元素
left_nav.find('li.wz-has-child').children('ul').hide();
// 在包含子元素的li中添加展开图标和链接
left_nav.find('li.wz-has-child').prepend('<a href="javascript:;" class="wz-nav-fold ' + icon_close + '"></a>');
// 菜单折叠事件处理
left_nav.find('li.wz-has-child').find('.wz-nav-fold')
.on('click', function () {
if ($(this).hasClass(icon_close)) {
$(this).removeClass(icon_close).addClass(icon_open);
} else {
$(this).removeClass(icon_open).addClass(icon_close);
}

$(this).parent().children('ul').slideToggle('fast');
});
})()
break;
}

// 当前选中元素的所有父级元素全部自动展开
left_nav.find('li.active').parents('ul').show();
Expand All @@ -54,10 +101,6 @@ $.wz.navigator_tree = function (left_nav) {

// 当前选中元素的下级元素自动展开
childrenShow(left_nav.find('li.active'));

// 一级元素的子元素自动展示
childrenShow(left_nav.children('li'));

left_nav.find('li:not(.wz-has-child)').map(function () {
var nav_icon = ($(this).data('type') === 'swagger' ? 'fa-code' : ($(this).data('type') === 'markdown' ? 'fa-file-text-o' : 'fa-table'));
$(this).prepend('<a class="fa ' + nav_icon + ' wz-nav-fold" href="javascript:;"></a>');
Expand Down
4 changes: 2 additions & 2 deletions resources/views/components/navbar-edit.blade.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@php $___index = 0; @endphp
@foreach(navigatorSort($navbars) as $nav)
@foreach(navigatorSort($navbars, $project->catalog_sort_style) as $nav)
<li class="wz-nav-editor-line" data-type="{{ $nav['type'] }}">
<input type="number" class="wz-sort-level" data-id="{{ $nav['id'] }}" data-index="{{ $___index ++ }}" data-original="{{ $nav['sort_level'] }}" value="{{ $nav['sort_level'] }}">
<a href="{{ $nav['url'] }}" target="_blank" title="{{ $nav['url'] }}" >
Expand All @@ -13,7 +13,7 @@

@if(!empty($nav['nodes']))
<ul>
@include('components.navbar-edit', ['navbars' => $nav['nodes'], 'indent' => $indent + 1])
@include('components.navbar-edit', ['navbars' => $nav['nodes'], 'indent' => $indent + 1, 'project' => $project])
</ul>
@endif
</li>
Expand Down
4 changes: 2 additions & 2 deletions resources/views/components/navbar.blade.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

@foreach(navigatorSort($navbars) as $nav)
@foreach(navigatorSort($navbars, $project->catalog_sort_style) as $nav)
<li class="{{ $nav['selected'] ? 'active' : '' }} {{ !empty($nav['nodes']) ? 'wz-has-child' : '' }}" data-type="{{ $nav['type'] }}">
<a href="{{ $nav['url'] }}" title="{{ $nav['name'] }}" class="wz-nav-item">
@if($nav['status'] == \App\Repositories\Document::STATUS_OUTDATED)
Expand All @@ -11,7 +11,7 @@

@if(!empty($nav['nodes']))
<ul>
@include('components.navbar', ['navbars' => $nav['nodes']])
@include('components.navbar', ['navbars' => $nav['nodes'], 'project' => $project])
</ul>
@endif
</li>
Expand Down
4 changes: 2 additions & 2 deletions resources/views/layouts/project.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
</div>
</div>
<ul class="nav nav-pills nav-stacked wz-left-nav {{-- hide --}}">
@include('components.navbar', ['navbars' => $navigators])
@include('components.navbar', ['navbars' => $navigators, 'project' => $project])
</ul>
</div>
<div class="col-12 col-lg-9 wz-panel-right">
Expand Down Expand Up @@ -116,7 +116,7 @@
<script>
// 侧边导航自动折叠
$(function () {
$.wz.navigator_tree($('.wz-left-nav'));
$.wz.navigator_tree($('.wz-left-nav'), {{ $project->catalog_fold_style }});
// window.setTimeout(function () {
// $('.wz-left-nav').removeClass('hide').addClass('animated fadeIn');
// }, 20);
Expand Down
20 changes: 18 additions & 2 deletions resources/views/project/setting-basic.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
value="{{ old('name', $project->name) }}" >
</div>
<div class="form-group">
<label for="catalog-status" class="bmd-label-floating">目录</label>
<label for="catalog-status" class="bmd-label-floating">项目目录</label>
<select id="catalog-status" name="catalog" class="form-control">
<option value="0" {{ empty($project->catalog) ? 'selected' : '' }}>无</option>
@foreach($catalogs as $cat)
Expand All @@ -40,10 +40,26 @@
</div>
</div>
<div class="form-group">
<label for="project-sort" class="bmd-label-floating">排序(值越大越靠后)</label>
<label for="project-sort" class="bmd-label-floating">项目排序(值越大越靠后)</label>
<input type="number" name="sort_level" class="form-control float-left w-75" id="project-sort" value="{{ old('sort_level', $project->sort_level) }}" {{ Auth::user()->can('project-sort') ? '' : 'disabled' }}/>
<i class="fa fa-question-circle ml-2" data-toggle="tooltip" title="" data-original-title="只有管理员可以修改"></i>
</div>
<div class="form-group">
<label for="catalog-sort-style" class="bmd-label-floating">排序样式</label>
<select id="catalog-sort-style" name="catalog_sort_style" class="form-control">
<option value="0" {{ $project->catalog_sort_style == 0 ? 'selected' : '' }}>文件夹优先</option>
<option value="1" {{ $project->catalog_sort_style == 1 ? 'selected' : '' }}>自由</option>
</select>
</div>
<div class="form-group">
<label for="catalog-fold-style" class="bmd-label-floating">文件夹样式</label>
<select id="catalog-fold-style" name="catalog_fold_style" class="form-control">
<option value="0" {{ $project->catalog_fold_style == 0 ? 'selected' : '' }}>自动</option>
<option value="1" {{ $project->catalog_fold_style == 1 ? 'selected' : '' }}>全部展开</option>
<option value="2" {{ $project->catalog_fold_style == 2 ? 'selected' : '' }}>全部折叠</option>
</select>
</div>


<div class="form-group">
<button type="submit" class="btn btn-success btn-raised mr-2">@lang('common.btn_save')</button>
Expand Down
Loading

0 comments on commit 34c5039

Please sign in to comment.