Skip to content

Commit

Permalink
add group(nogroup) option to separate nsort from grouping
Browse files Browse the repository at this point in the history
Fixes #286, to restore default group behaviour if nsort is used.

Extends dcc823f

See also issue #66 and #202, and PR #259 and #111
  • Loading branch information
Klap-in committed Jan 8, 2024
1 parent ed06f21 commit f498ace
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
19 changes: 16 additions & 3 deletions Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ class Search
* @var bool also sorts the namespaces
*/
private $nsort;
/**
* @var bool Group the namespaces and page and sort separately, or mix them and sort together
*/
private $group;
/**
* @var bool Sort the headpages as defined by global config setting startpage to the top
*/
Expand All @@ -35,6 +39,7 @@ class Search
* $sort['msort']
* $sort['rsort']
* $sort['nsort']
* $sort['group']
* $sort['hsort'];
*/
public function __construct($sort)
Expand All @@ -43,6 +48,7 @@ public function __construct($sort)
$this->msort = $sort['msort'];
$this->rsort = $sort['rsort'];
$this->nsort = $sort['nsort'];
$this->group = $sort['group'];
$this->hsort = $sort['hsort'];
}

Expand Down Expand Up @@ -657,8 +663,15 @@ public function customSearch(&$data, $base, $func, $opts, $dir = '', $lvl = 1)
foreach ($dirs as $dir) {
call_user_func_array($func, [&$dirs_tmp, $base, $dir, 'd', $lvl, $opts]);
}
//combine directories and pages and sort together
$dirsAndFiles = array_merge($dirs_tmp, $files_tmp);
if($this->group) {
//group directories and pages, and sort separately
$dirsAndFiles = $dirs_tmp;
} else {
// no grouping
//mix directories and pages and sort together
$dirsAndFiles = array_merge($dirs_tmp, $files_tmp);
}

usort($dirsAndFiles, [$this, "compareNodes"]);

//add and search each directory
Expand Down Expand Up @@ -688,7 +701,7 @@ public function customSearch(&$data, $base, $func, $opts, $dir = '', $lvl = 1)
if (!$lastItem['hns']) {
array_pop($data);
}
} elseif (!$this->nsort) {
} elseif (!($this->nsort && !$this->group)) {
//add files to index
$data = array_merge($data, $files_tmp);
}
Expand Down
2 changes: 2 additions & 0 deletions action.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ private function getDataFancyTree()
'msort' => $INPUT->str('msort'),
'rsort' => $INPUT->bool('rsort'),
'nsort' => $INPUT->bool('nsort'),
'group' => $INPUT->bool('group'),
'hsort' => $INPUT->bool('hsort')
];

Expand Down Expand Up @@ -475,6 +476,7 @@ private function printIndex($ns)
$sort['msort'] = $INPUT->str('msort', '', true);
$sort['rsort'] = $INPUT->bool('rsort', false, true);
$sort['nsort'] = $INPUT->bool('nsort', false, true);
$sort['group'] = $INPUT->bool('group', false, true);
$sort['hsort'] = $INPUT->bool('hsort', false, true);
$search = new Search($sort);
$fsdir = "/" . utf8_encodeFN(str_replace(':', '/', $ns));
Expand Down
2 changes: 2 additions & 0 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ jQuery(function(){ // on page load
msort: options.sort.msort ? options.sort.msort : 0, //'indexmenu_n', or metadata 'key subkey' TODO is empty handled correctly?
rsort: options.sort.rsort ? 1 : 0,
nsort: options.sort.nsort ? 1 : 0,
group: options.sort.group ? 1 : 0,
hsort: options.sort.hsort ? 1 : 0,

init: 1
Expand Down Expand Up @@ -285,6 +286,7 @@ jQuery(function(){ // on page load
msort: options.sort.msort ? options.sort.msort : 0,
rsort: options.sort.rsort ? 1 : 0,
nsort: options.sort.nsort ? 1 : 0,
group: options.sort.group ? 1 : 0,
hsort: options.sort.hsort ? 1 : 0,

init: 0
Expand Down
11 changes: 8 additions & 3 deletions syntax/indexmenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ public function handle($match, $state, $pos, Doku_Handler $handler)
}
//sort directories in the same way as files
$nsort = $this->hasOption($defaults, $opts, 'nsort');
//group namespaces and pages both sorted separately with same sorting, or nogroup: mix them and sort together
$group = !$this->hasOption($defaults, $opts, 'nogroup');

//sort headpages up
$hsort = $this->hasOption($defaults, $opts, 'hsort');
//Metadata sort method
Expand All @@ -175,6 +178,7 @@ public function handle($match, $state, $pos, Doku_Handler $handler)
if ($msort) $jsAjax .= "&msort=" . $msort;
if ($rsort) $jsAjax .= "&rsort=1";
if ($nsort) $jsAjax .= "&nsort=1";
if ($group) $jsAjax .= "&group=1";
if ($hsort) $jsAjax .= "&hsort=1";
if ($nopg) $jsAjax .= "&nopg=1";

Expand Down Expand Up @@ -317,7 +321,8 @@ public function handle($match, $state, $pos, Doku_Handler $handler)
'msort' => $msort,
'rsort' => $rsort,
'nsort' => $nsort,
'hsort' => $hsort,
'group' => $group,
'hsort' => $hsort
],
[ //3=opts
'level' => $level, // requested depth of initial opened nodes, -1:all
Expand Down Expand Up @@ -408,7 +413,7 @@ public function render($format, Doku_Renderer $renderer, $data)
$ns = $data[0];
//theme, identifier, nocookie, navbar, noscroll, maxJs, notoc, jsAjax, context, nomenu
$js_dTreeOpts = $data[1];
//sort, msort, rsort, nsort, hsort
//sort, msort, rsort, nsort, group, hsort
$sort = $data[2];
//opts for search(): level, nons, nopg, subnss, max, maxajax, js, skipns, skipfile, skipnscombined,
//skipfilecombined, headpage, hide_headpage
Expand Down Expand Up @@ -488,7 +493,7 @@ public function render($format, Doku_Renderer $renderer, $data)
* @param string $ns
* @param array $js_dTreeOpts entries: theme, identifier, nocookie, navbar, noscroll, maxJs, notoc, jsAjax, context,
* nomenu
* @param array $sort entries: sort, msort, rsort, nsort, hsort
* @param array $sort entries: sort, msort, rsort, nsort, group, hsort
* @param array $opts entries of opts for search(): level, nons, nopg, nss, max, maxajax, js, skipns, skipfile,
* skipnscombined, skipfilecombined, headpage, hide_headpage
* @param int $jsVersion
Expand Down

0 comments on commit f498ace

Please sign in to comment.