Skip to content

Commit 34c5039

Browse files
committed
增加项目文档排序增强,支持文件夹优先、自有排序,增加项目目录展开折叠配置,支持自动,全部展开,全部折叠配置 #149 #150
1 parent 54dfc1b commit 34c5039

File tree

11 files changed

+177
-45
lines changed

11 files changed

+177
-45
lines changed

app/Http/Controllers/BatchExportController.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function batchExport(Request $request, $project_id)
5656

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

6161
if ($pid !== 0) {
6262
$navigators = $this->filterNavigators($navigators, function (array $nav) use ($pid) {

app/Http/Controllers/ProjectController.php

+12-6
Original file line numberDiff line numberDiff line change
@@ -364,12 +364,14 @@ private function basicSettingHandle(Request $request, Project $project): bool
364364
$this->validate(
365365
$request,
366366
[
367-
'name' => 'required|between:1,100',
368-
'description' => 'max:255',
369-
'project_id' => "required|in:{$project->id}|project_exist",
370-
'visibility' => 'required|in:1,2',
371-
'sort_level' => 'integer|between:-999999999,999999999',
372-
'catalog' => 'required|integer',
367+
'name' => 'required|between:1,100',
368+
'description' => 'max:255',
369+
'project_id' => "required|in:{$project->id}|project_exist",
370+
'visibility' => 'required|in:1,2',
371+
'sort_level' => 'integer|between:-999999999,999999999',
372+
'catalog' => 'required|integer',
373+
'catalog_sort_style' => 'in:0,1',
374+
'catalog_fold_style' => 'in:0,1,2',
373375
],
374376
[
375377
'name.required' => __('project.validation.project_name_required'),
@@ -383,11 +385,15 @@ private function basicSettingHandle(Request $request, Project $project): bool
383385
$visibility = $request->input('visibility');
384386
$sortLevel = $request->input('sort_level');
385387
$catalog = $request->input('catalog');
388+
$catalogSortStyle = $request->input('catalog_sort_style', Project::SORT_STYLE_DIR_FIRST);
389+
$catalogFoldStyle = $request->input('catalog_fold_style', Project::FOLD_STYLE_AUTO);
386390

387391
$project->name = $name;
388392
$project->description = $description;
389393
$project->visibility = $visibility;
390394
$project->catalog_id = empty($catalog) ? null : $catalog;
395+
$project->catalog_fold_style = $catalogFoldStyle;
396+
$project->catalog_sort_style = $catalogSortStyle;
391397
if (\Auth::user()->can('project-sort') && $sortLevel != null) {
392398
$project->sort_level = (int)$sortLevel;
393399
}

app/Repositories/Project.php

+26-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
* @property \Carbon\Carbon|null $deleted_at
3737
* @property int $sort_level 项目排序,排序值越大越靠后
3838
* @property int|null $catalog_id 目录ID
39+
* @property int $catalog_fold_style 目录展示样式
40+
* @property int $catalog_sort_style 目录排序样式
3941
* @method static \Illuminate\Database\Eloquent\Builder|\App\Repositories\Project whereCatalogId($value)
4042
* @method static \Illuminate\Database\Eloquent\Builder|\App\Repositories\Project whereCreatedAt($value)
4143
* @method static \Illuminate\Database\Eloquent\Builder|\App\Repositories\Project whereDeletedAt($value)
@@ -70,6 +72,27 @@ class Project extends Repository
7072
*/
7173
const PRIVILEGE_RO = 2;
7274

75+
/**
76+
* 目录折叠样式:自动
77+
*/
78+
const FOLD_STYLE_AUTO = 0;
79+
/**
80+
* 目录折叠样式:全部展开
81+
*/
82+
const FOLD_STYLE_UNFOLD = 1;
83+
/**
84+
* 目录折叠样式:全部折叠
85+
*/
86+
const FOLD_STYLE_FOLD = 2;
87+
/**
88+
* 排序样式:文件夹优先
89+
*/
90+
const SORT_STYLE_DIR_FIRST = 0;
91+
/**
92+
* 排序样式:自由排序
93+
*/
94+
const SORT_STYLE_FREE = 1;
95+
7396
protected $table = 'wz_projects';
7497
protected $fillable
7598
= [
@@ -79,6 +102,8 @@ class Project extends Repository
79102
'user_id',
80103
'sort_level',
81104
'catalog_id',
105+
'catalog_fold_style',
106+
'catalog_sort_style',
82107
];
83108

84109
public $dates = ['deleted_at'];
@@ -111,7 +136,7 @@ public function user()
111136
public function groups()
112137
{
113138
return $this->belongsToMany(Group::class, 'wz_project_group_ref', 'project_id', 'group_id')
114-
->withPivot('created_at', 'updated_at', 'privilege');
139+
->withPivot('created_at', 'updated_at', 'privilege');
115140
}
116141

117142
/**

app/helpers.php

+7-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
use App\Repositories\Catalog;
1010
use App\Repositories\Document;
11+
use App\Repositories\Project;
1112
use App\Repositories\Template;
1213
use App\Repositories\User;
1314
use Carbon\Carbon;
@@ -130,10 +131,11 @@ function ($nav) {
130131
* 导航排序,排序后,文件夹靠前,普通文件靠后
131132
*
132133
* @param array $navItems
134+
* @param int $sortStyle
133135
*
134136
* @return array
135137
*/
136-
function navigatorSort($navItems)
138+
function navigatorSort($navItems, $sortStyle = Project::SORT_STYLE_DIR_FIRST)
137139
{
138140
$sortItem = function ($a, $b) {
139141
try {
@@ -153,7 +155,10 @@ function navigatorSort($navItems)
153155

154156
usort(
155157
$navItems,
156-
function ($a, $b) use ($sortItem) {
158+
function ($a, $b) use ($sortItem, $sortStyle) {
159+
if ($sortStyle == Project::SORT_STYLE_FREE) {
160+
return $sortItem($a, $b);
161+
}
157162

158163
$aIsFolder = !empty($a['nodes']);
159164
$bIsFolder = !empty($b['nodes']);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Schema;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Database\Migrations\Migration;
6+
7+
class AddProjectMenuControl extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::table('wz_projects', function (Blueprint $table) {
17+
$table->tinyInteger('catalog_fold_style')->default(0)->comment('目录展开样式:0-自动 1-全部展开 2-全部折叠');
18+
$table->tinyInteger('catalog_sort_style')->default(0)->comment('目录排序样式:0-目录优先 1-自由排序');
19+
});
20+
}
21+
22+
/**
23+
* Reverse the migrations.
24+
*
25+
* @return void
26+
*/
27+
public function down()
28+
{
29+
Schema::table('wz_projects', function (Blueprint $table) {
30+
$table->dropColumn(['catalog_fold_style', 'catalog_sort_style']);
31+
});
32+
}
33+
}

public/assets/js/navigator-tree.js

+69-26
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@
99
* 导航树
1010
*
1111
* @param left_nav
12+
* @param mode 0-自动 1-全部展开 2-全部折叠
1213
*/
13-
$.wz.navigator_tree = function (left_nav) {
14+
$.wz.navigator_tree = function (left_nav, mode) {
1415
var icon_close = 'fa fa-folder-o';
1516
var icon_open = 'fa fa-folder-open-o';
17+
mode = mode || 0;
1618

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

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

37-
$(this).parent().children('ul').slideToggle('fast');
38-
});
42+
$(this).parent().children('ul').slideToggle('fast');
43+
});
3944

40-
left_nav.find('.wz-auto-open').children('li.wz-has-child').each(function () {
41-
var childrenCount = $(this).children('ul').children('li').length;
42-
// 如果一级菜单的子元素小于7个,则自动展开
43-
if (childrenCount < 7) {
44-
$(this).children('a.wz-nav-fold').trigger('click');
45-
}
46-
});
45+
left_nav.find('.wz-auto-open').children('li.wz-has-child').each(function () {
46+
var childrenCount = $(this).children('ul').children('li').length;
47+
// 如果一级菜单的子元素小于7个,则自动展开
48+
if (childrenCount < 7) {
49+
$(this).children('a.wz-nav-fold').trigger('click');
50+
}
51+
});
52+
53+
// 一级元素的子元素自动展示
54+
childrenShow(left_nav.children('li'));
55+
})();
56+
break;
57+
case 1: // 全部展开
58+
(function(){
59+
// 在包含子元素的li中添加展开图标和链接
60+
left_nav.find('li.wz-has-child').prepend('<a href="javascript:;" class="wz-nav-fold ' + icon_open + '"></a>');
61+
// 菜单折叠事件处理
62+
left_nav.find('li.wz-has-child').find('.wz-nav-fold')
63+
.on('click', function () {
64+
if ($(this).hasClass(icon_close)) {
65+
$(this).removeClass(icon_close).addClass(icon_open);
66+
} else {
67+
$(this).removeClass(icon_open).addClass(icon_close);
68+
}
69+
70+
$(this).parent().children('ul').slideToggle('fast');
71+
});
72+
})()
73+
break;
74+
case 2: // 全部折叠
75+
(function(){
76+
// 先隐藏所有的li下的子元素
77+
left_nav.find('li.wz-has-child').children('ul').hide();
78+
// 在包含子元素的li中添加展开图标和链接
79+
left_nav.find('li.wz-has-child').prepend('<a href="javascript:;" class="wz-nav-fold ' + icon_close + '"></a>');
80+
// 菜单折叠事件处理
81+
left_nav.find('li.wz-has-child').find('.wz-nav-fold')
82+
.on('click', function () {
83+
if ($(this).hasClass(icon_close)) {
84+
$(this).removeClass(icon_close).addClass(icon_open);
85+
} else {
86+
$(this).removeClass(icon_open).addClass(icon_close);
87+
}
88+
89+
$(this).parent().children('ul').slideToggle('fast');
90+
});
91+
})()
92+
break;
93+
}
4794

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

55102
// 当前选中元素的下级元素自动展开
56103
childrenShow(left_nav.find('li.active'));
57-
58-
// 一级元素的子元素自动展示
59-
childrenShow(left_nav.children('li'));
60-
61104
left_nav.find('li:not(.wz-has-child)').map(function () {
62105
var nav_icon = ($(this).data('type') === 'swagger' ? 'fa-code' : ($(this).data('type') === 'markdown' ? 'fa-file-text-o' : 'fa-table'));
63106
$(this).prepend('<a class="fa ' + nav_icon + ' wz-nav-fold" href="javascript:;"></a>');

resources/views/components/navbar-edit.blade.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
@php $___index = 0; @endphp
2-
@foreach(navigatorSort($navbars) as $nav)
2+
@foreach(navigatorSort($navbars, $project->catalog_sort_style) as $nav)
33
<li class="wz-nav-editor-line" data-type="{{ $nav['type'] }}">
44
<input type="number" class="wz-sort-level" data-id="{{ $nav['id'] }}" data-index="{{ $___index ++ }}" data-original="{{ $nav['sort_level'] }}" value="{{ $nav['sort_level'] }}">
55
<a href="{{ $nav['url'] }}" target="_blank" title="{{ $nav['url'] }}" >
@@ -13,7 +13,7 @@
1313

1414
@if(!empty($nav['nodes']))
1515
<ul>
16-
@include('components.navbar-edit', ['navbars' => $nav['nodes'], 'indent' => $indent + 1])
16+
@include('components.navbar-edit', ['navbars' => $nav['nodes'], 'indent' => $indent + 1, 'project' => $project])
1717
</ul>
1818
@endif
1919
</li>

resources/views/components/navbar.blade.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

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

1212
@if(!empty($nav['nodes']))
1313
<ul>
14-
@include('components.navbar', ['navbars' => $nav['nodes']])
14+
@include('components.navbar', ['navbars' => $nav['nodes'], 'project' => $project])
1515
</ul>
1616
@endif
1717
</li>

resources/views/layouts/project.blade.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
</div>
5555
</div>
5656
<ul class="nav nav-pills nav-stacked wz-left-nav {{-- hide --}}">
57-
@include('components.navbar', ['navbars' => $navigators])
57+
@include('components.navbar', ['navbars' => $navigators, 'project' => $project])
5858
</ul>
5959
</div>
6060
<div class="col-12 col-lg-9 wz-panel-right">
@@ -116,7 +116,7 @@
116116
<script>
117117
// 侧边导航自动折叠
118118
$(function () {
119-
$.wz.navigator_tree($('.wz-left-nav'));
119+
$.wz.navigator_tree($('.wz-left-nav'), {{ $project->catalog_fold_style }});
120120
// window.setTimeout(function () {
121121
// $('.wz-left-nav').removeClass('hide').addClass('animated fadeIn');
122122
// }, 20);

resources/views/project/setting-basic.blade.php

+18-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
value="{{ old('name', $project->name) }}" >
1515
</div>
1616
<div class="form-group">
17-
<label for="catalog-status" class="bmd-label-floating">目录</label>
17+
<label for="catalog-status" class="bmd-label-floating">项目目录</label>
1818
<select id="catalog-status" name="catalog" class="form-control">
1919
<option value="0" {{ empty($project->catalog) ? 'selected' : '' }}>无</option>
2020
@foreach($catalogs as $cat)
@@ -40,10 +40,26 @@
4040
</div>
4141
</div>
4242
<div class="form-group">
43-
<label for="project-sort" class="bmd-label-floating">排序(值越大越靠后)</label>
43+
<label for="project-sort" class="bmd-label-floating">项目排序(值越大越靠后)</label>
4444
<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' }}/>
4545
<i class="fa fa-question-circle ml-2" data-toggle="tooltip" title="" data-original-title="只有管理员可以修改"></i>
4646
</div>
47+
<div class="form-group">
48+
<label for="catalog-sort-style" class="bmd-label-floating">排序样式</label>
49+
<select id="catalog-sort-style" name="catalog_sort_style" class="form-control">
50+
<option value="0" {{ $project->catalog_sort_style == 0 ? 'selected' : '' }}>文件夹优先</option>
51+
<option value="1" {{ $project->catalog_sort_style == 1 ? 'selected' : '' }}>自由</option>
52+
</select>
53+
</div>
54+
<div class="form-group">
55+
<label for="catalog-fold-style" class="bmd-label-floating">文件夹样式</label>
56+
<select id="catalog-fold-style" name="catalog_fold_style" class="form-control">
57+
<option value="0" {{ $project->catalog_fold_style == 0 ? 'selected' : '' }}>自动</option>
58+
<option value="1" {{ $project->catalog_fold_style == 1 ? 'selected' : '' }}>全部展开</option>
59+
<option value="2" {{ $project->catalog_fold_style == 2 ? 'selected' : '' }}>全部折叠</option>
60+
</select>
61+
</div>
62+
4763

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

0 commit comments

Comments
 (0)