Skip to content

Commit 186f987

Browse files
authored
Merge pull request #40 from cosmocode/tree
New Implementation based on new TreeBuilder mechanism in Librarian
2 parents 525b60b + 10f2bde commit 186f987

File tree

6 files changed

+276
-395
lines changed

6 files changed

+276
-395
lines changed

_test/SimplenaviTest.php

Lines changed: 103 additions & 164 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace dokuwiki\plugin\simplenavi\test;
44

5+
use dokuwiki\TreeBuilder\PageTreeBuilder;
56
use DokuWikiTest;
6-
use TestRequest;
77

88
/**
99
* General tests for the simplenavi plugin
@@ -50,222 +50,161 @@ public function dataProvider()
5050

5151
yield [
5252
'set' => 'by ID, all branches closed',
53-
'titlesort' => false,
54-
'natsort' => false,
55-
'nsfirst' => false,
53+
'sort' => 'id',
54+
'usetitle' => false,
5655
'home' => false,
5756
'current' => 'simplenavi:page',
5857
'expect' => [
59-
'simplenavi:foo',
60-
'simplenavi:namespace1:start',
61-
'simplenavi:namespace12:start',
62-
'simplenavi:namespace123:namespace123',
63-
'simplenavi:namespace2',
64-
'simplenavi:namespace21:start',
65-
]
66-
];
67-
68-
yield [
69-
'set' => 'by ID, Natural Sort, all branches closed',
70-
'titlesort' => false,
71-
'natsort' => true,
72-
'nsfirst' => false,
73-
'home' => false,
74-
'current' => 'simplenavi:page',
75-
'expect' => [
76-
'simplenavi:foo',
77-
'simplenavi:namespace1:start',
78-
'simplenavi:namespace2',
79-
'simplenavi:namespace12:start',
80-
'simplenavi:namespace21:start',
81-
'simplenavi:namespace123:namespace123',
58+
'+simplenavi:foo',
59+
'+simplenavi:namespace1:start',
60+
'+simplenavi:namespace12:start',
61+
'+simplenavi:namespace123:namespace123',
62+
'+simplenavi:namespace2',
63+
'+simplenavi:namespace21:start',
8264
]
8365
];
8466

8567
yield [
8668
'set' => 'by ID, branch open',
87-
'titlesort' => false,
88-
'natsort' => false,
89-
'nsfirst' => false,
69+
'sort' => 'id',
70+
'usetitle' => false,
9071
'home' => false,
9172
'current' => 'simplenavi:namespace123:deep:foo',
9273
'expect' => [
93-
'simplenavi:foo',
94-
'simplenavi:namespace1:start',
95-
'simplenavi:namespace12:start',
96-
'simplenavi:namespace123:namespace123',
97-
'simplenavi:namespace123:deep:start',
98-
'simplenavi:namespace123:deep:foo',
99-
'simplenavi:namespace123:foo',
100-
'simplenavi:namespace2',
101-
'simplenavi:namespace21:start',
74+
'+simplenavi:foo',
75+
'+simplenavi:namespace1:start',
76+
'+simplenavi:namespace12:start',
77+
'+simplenavi:namespace123:namespace123',
78+
'++simplenavi:namespace123:deep:start',
79+
'+++simplenavi:namespace123:deep:foo',
80+
'++simplenavi:namespace123:foo',
81+
'+simplenavi:namespace2',
82+
'+simplenavi:namespace21:start',
10283
]
10384
];
10485

105-
yield [
106-
'set' => 'by ID, Natural Sort, branch open',
107-
'titlesort' => false,
108-
'natsort' => true,
109-
'nsfirst' => false,
110-
'home' => false,
111-
'current' => 'simplenavi:namespace123:deep:foo',
112-
'expect' => [
113-
'simplenavi:foo',
114-
'simplenavi:namespace1:start',
115-
'simplenavi:namespace2',
116-
'simplenavi:namespace12:start',
117-
'simplenavi:namespace21:start',
118-
'simplenavi:namespace123:namespace123',
119-
'simplenavi:namespace123:deep:start',
120-
'simplenavi:namespace123:deep:foo',
121-
'simplenavi:namespace123:foo',
122-
]
123-
];
124-
125-
yield [
126-
'set' => 'by ID, Natural Sort, NS first, branch open',
127-
'titlesort' => false,
128-
'natsort' => true,
129-
'nsfirst' => true,
130-
'home' => false,
131-
'current' => 'simplenavi:namespace123:deep:foo',
132-
'expect' => [
133-
'simplenavi:namespace1:start',
134-
'simplenavi:namespace2',
135-
'simplenavi:namespace12:start',
136-
'simplenavi:namespace21:start',
137-
'simplenavi:namespace123:namespace123',
138-
'simplenavi:namespace123:deep:start',
139-
'simplenavi:namespace123:deep:foo',
140-
'simplenavi:namespace123:foo',
141-
'simplenavi:foo',
142-
]
143-
];
144-
145-
yield [
146-
'set' => 'by Title, all branches closed',
147-
'titlesort' => true,
148-
'natsort' => false,
149-
'nsfirst' => false,
150-
'home' => false,
151-
'current' => 'simplenavi:page',
152-
'expect' => [
153-
'simplenavi:namespace123:namespace123',
154-
'simplenavi:foo',
155-
'simplenavi:namespace12:start',
156-
'simplenavi:namespace2',
157-
'simplenavi:namespace21:start',
158-
'simplenavi:namespace1:start',
159-
]
160-
];
16186

16287
yield [
16388
'set' => 'by Title, Natural Search, all branches closed',
164-
'titlesort' => true,
165-
'natsort' => true,
166-
'nsfirst' => false,
89+
'sort' => 'title',
90+
'usetitle' => true,
16791
'home' => false,
16892
'current' => 'simplenavi:page',
16993
'expect' => [
170-
'simplenavi:namespace123:namespace123',
171-
'simplenavi:foo',
172-
'simplenavi:namespace2',
173-
'simplenavi:namespace12:start',
174-
'simplenavi:namespace21:start',
175-
'simplenavi:namespace1:start',
176-
]
177-
];
178-
179-
yield [
180-
'set' => 'by Title, branch open',
181-
'titlesort' => true,
182-
'natsort' => false,
183-
'nsfirst' => false,
184-
'home' => false,
185-
'current' => 'simplenavi:namespace123:deep:foo',
186-
'expect' => [
187-
'simplenavi:namespace123:namespace123',
188-
'simplenavi:namespace123:deep:start',
189-
'simplenavi:namespace123:deep:foo',
190-
'simplenavi:namespace123:foo',
191-
'simplenavi:foo',
192-
'simplenavi:namespace12:start',
193-
'simplenavi:namespace2',
194-
'simplenavi:namespace21:start',
195-
'simplenavi:namespace1:start',
94+
'+simplenavi:namespace123:namespace123',
95+
'+simplenavi:foo',
96+
'+simplenavi:namespace2',
97+
'+simplenavi:namespace12:start',
98+
'+simplenavi:namespace21:start',
99+
'+simplenavi:namespace1:start',
196100
]
197101
];
198102

199103
yield [
200104
'set' => 'by Title, Natural Sort, branch open',
201-
'titlesort' => true,
202-
'natsort' => true,
203-
'nsfirst' => false,
105+
'sort' => 'title',
106+
'usetitle' => true,
204107
'home' => false,
205108
'current' => 'simplenavi:namespace123:deep:foo',
206109
'expect' => [
207-
'simplenavi:namespace123:namespace123',
208-
'simplenavi:namespace123:deep:start',
209-
'simplenavi:namespace123:deep:foo',
210-
'simplenavi:namespace123:foo',
211-
'simplenavi:foo',
212-
'simplenavi:namespace2',
213-
'simplenavi:namespace12:start',
214-
'simplenavi:namespace21:start',
215-
'simplenavi:namespace1:start',
110+
'+simplenavi:namespace123:namespace123',
111+
'++simplenavi:namespace123:deep:start',
112+
'+++simplenavi:namespace123:deep:foo',
113+
'++simplenavi:namespace123:foo',
114+
'+simplenavi:foo',
115+
'+simplenavi:namespace2',
116+
'+simplenavi:namespace12:start',
117+
'+simplenavi:namespace21:start',
118+
'+simplenavi:namespace1:start',
216119
]
217120
];
218121

219122
yield [
220123
'set' => 'by Title, Natural Sort, NS first, branch open',
221-
'titlesort' => true,
222-
'natsort' => true,
223-
'nsfirst' => true,
124+
'sort' => 'ns_title',
125+
'usetitle' => true,
224126
'home' => false,
225127
'current' => 'simplenavi:namespace123:deep:foo',
226128
'expect' => [
227-
'simplenavi:namespace123:namespace123',
228-
'simplenavi:namespace123:deep:start',
229-
'simplenavi:namespace123:deep:foo',
230-
'simplenavi:namespace123:foo',
231-
'simplenavi:namespace2',
232-
'simplenavi:namespace12:start',
233-
'simplenavi:namespace21:start',
234-
'simplenavi:namespace1:start',
235-
'simplenavi:foo',
129+
'+simplenavi:namespace123:namespace123',
130+
'++simplenavi:namespace123:deep:start',
131+
'+++simplenavi:namespace123:deep:foo',
132+
'++simplenavi:namespace123:foo',
133+
'+simplenavi:namespace2',
134+
'+simplenavi:namespace12:start',
135+
'+simplenavi:namespace21:start',
136+
'+simplenavi:namespace1:start',
137+
'+simplenavi:foo',
236138
]
237139
];
238140

239141
yield [
240142
'set' => 'by ID, branch open with home level',
241-
'titlesort' => false,
242-
'natsort' => false,
243-
'nsfirst' => false,
143+
'sort' => 'id',
144+
'usetitle' => false,
244145
'home' => true,
245146
'current' => 'simplenavi:namespace123:deep:foo',
246147
'expect' => [
247-
'simplenavi:simplenavi',
248-
'simplenavi:foo',
249-
'simplenavi:namespace1:start',
250-
'simplenavi:namespace12:start',
251-
'simplenavi:namespace123:namespace123',
252-
'simplenavi:namespace123:deep:start',
253-
'simplenavi:namespace123:deep:foo',
254-
'simplenavi:namespace123:foo',
255-
'simplenavi:namespace2',
256-
'simplenavi:namespace21:start',
148+
'+simplenavi:simplenavi',
149+
'++simplenavi:foo',
150+
'++simplenavi:namespace1:start',
151+
'++simplenavi:namespace12:start',
152+
'++simplenavi:namespace123:namespace123',
153+
'+++simplenavi:namespace123:deep:start',
154+
'++++simplenavi:namespace123:deep:foo',
155+
'+++simplenavi:namespace123:foo',
156+
'++simplenavi:namespace2',
157+
'++simplenavi:namespace21:start',
257158
]
258159
];
259160
}
260161

261162
/**
262163
* @dataProvider dataProvider
263164
*/
264-
public function testSorting($set, $titlesort, $natsort, $nsfirst, $home, $current, $expect)
165+
public function testSorting($set, $sort, $usetitle, $home, $current, $expect)
166+
{
167+
$simpleNavi = new \syntax_plugin_simplenavi();
168+
169+
$simpleNavi->initState('simplenavi', $current, $usetitle, $sort, $home);
170+
171+
/** @var PageTreeBuilder $tree */
172+
$tree = $this->callInaccessibleMethod($simpleNavi, 'getTree', []);
173+
174+
175+
$this->assertSame(join("\n", $expect), (string) $tree, $set);
176+
}
177+
178+
/**
179+
* Data provider for isParent test
180+
*/
181+
public function isParentDataProvider(): array
182+
{
183+
return [
184+
// Test cases where parent is a parent of child
185+
'parent of deep child' => [true, 'namespace1:namespace2:page', 'namespace1'],
186+
'direct parent' => [true, 'namespace1:namespace2:page', 'namespace1:namespace2'],
187+
'parent of page' => [true, 'namespace1:page', 'namespace1'],
188+
189+
// Test cases where parent is not a parent of child
190+
'different namespace' => [false, 'namespace1:page', 'namespace2'],
191+
'sibling namespace' => [false, 'namespace1:namespace2:page', 'namespace1:namespace3'],
192+
193+
// Test edge cases
194+
'empty parent' => [true, 'page', ''], // Empty parent is parent of all
195+
'self as parent' => [true, 'page', 'page'], // Page is parent of itself
196+
];
197+
}
198+
199+
/**
200+
* Test the isParent method
201+
* @dataProvider isParentDataProvider
202+
*/
203+
public function testIsParent(bool $expected, string $child, string $parent): void
265204
{
266205
$simpleNavi = new \syntax_plugin_simplenavi();
267-
$items = $simpleNavi->getSortedItems('simplenavi', $current, $titlesort, $natsort, $nsfirst, $home);
268-
$this->assertSame($expect, array_column($items, 'id'), $set);
206+
$result = $this->callInaccessibleMethod($simpleNavi, 'isParent', [$child, $parent]);
207+
$this->assertSame($expected, $result);
269208
}
270209

271210
}

conf/default.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
* Default settings for the simplenavi plugin
55
*/
66

7-
$conf['natsort'] = 1;
8-
$conf['nsfirst'] = 1;
97
$conf['usetitle'] = 1;
8+
$conf['sort'] = 'ns_title';
9+
$conf['peek'] = 0;

conf/metadata.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
* Options for the simplenavi plugin
55
*/
66

7-
$meta['natsort'] = array('onoff');
8-
$meta['nsfirst'] = array('onoff');
97
$meta['usetitle'] = array('onoff');
8+
$meta['sort'] = array('multichoice', '_choices' => array(
9+
'id',
10+
'title',
11+
'ns_id',
12+
'ns_title',
13+
));
14+
$meta['peek'] = array('numeric', '_min' => 0);

lang/en/settings.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
<?php
2-
/**
3-
* english language file for latexit plugin
4-
*
5-
* @author Michael Große <grosse@cosmocode.de>
6-
*/
7-
8-
$lang['natsort'] = 'Use natural sorting? Eg. <code>foo12</code> comes after <code>foo1</code>';
9-
$lang['nsfirst'] = 'Sort namespaces before pages?';
10-
$lang['usetitle'] = 'Sort by and show the title instead of the ID?';
2+
$lang['usetitle'] = 'Show the title instead of the ID?';
3+
$lang['sort'] = 'Sort order';
4+
$lang['sort_o_id'] = 'by ID';
5+
$lang['sort_o_title'] = 'by title';
6+
$lang['sort_o_ns_id'] = 'by ID, namespace first';
7+
$lang['sort_o_ns_title'] = 'by title, namespace first';
8+
$lang['peek'] = 'Number of levels to peek into a non-readable namespace to see if there are readable children deeper down.';

script.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ jQuery(function () {
88
.attr('placeholder', LANG.plugins.simplenavi.filter)
99
.val(window.sessionStorage.getItem('simplenavi-filter'));
1010

11-
$box.on('input', function () {
12-
window.sessionStorage.setItem('simplenavi-filter', $box.val());
13-
const lookup = new RegExp($box.val(), 'i');
11+
$box.on('input', function (e) {
12+
const value = e.target.value;
13+
window.sessionStorage.setItem('simplenavi-filter', value);
14+
const lookup = new RegExp(value, 'i');
1415
$plugin.find('li.hidden').removeClass('hidden');
15-
$plugin.find('> ul > li > .li > a').filter(function () {
16-
return !this.text.match(lookup);
16+
$plugin.find('> ul > li > .li').filter(function () {
17+
return !this.textContent.match(lookup);
1718
}).parents('li').addClass('hidden');
1819
});
1920

0 commit comments

Comments
 (0)