Skip to content

Commit 9f66cae

Browse files
committed
browse folders #125
1 parent 42bddcb commit 9f66cae

File tree

8 files changed

+102
-16
lines changed

8 files changed

+102
-16
lines changed

adminifier/wiki.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -300,8 +300,13 @@ func getSortFunc(wr *wikiRequest) (bool, wiki.SortFunc) {
300300

301301
func handlePagesFrame(wr *wikiRequest) {
302302
descending, sortFunc := getSortFunc(wr)
303-
pages := wr.wi.PagesSorted(descending, sortFunc, wiki.SortTitle)
304-
handleFileFrames(wr, pages)
303+
dir := wr.r.URL.Query().Get("dir")
304+
pages, dirs := wr.wi.PagesAndDirsSorted(dir, descending, sortFunc, wiki.SortTitle)
305+
handleFileFrames(wr, struct {
306+
Pages []wikifier.PageInfo `json:"pages"`
307+
Dirs []string `json:"dirs"`
308+
Cd string `json:"cd"`
309+
}{pages, dirs, dir})
305310
}
306311

307312
func handleImagesFrame(wr *wikiRequest) {

resources/adminifier/static/script/editor-plugins/emoji.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
ae = a.editor;
99

1010
// add toolbar functions
11-
ae.addToolbarFunctions({ emoji: displayEmojiSelector });
11+
// disabled until fully implemented
12+
// ae.addToolbarFunctions({ emoji: displayEmojiSelector });
1213
}
1314

1415
function unloadedHandler () {

resources/adminifier/static/script/file-list.js

+4
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,8 @@ function quickSearch (entry) {
487487

488488
exports.dateToPreciseHR = dateToPreciseHR;
489489
function dateToPreciseHR (d) {
490+
if (!d)
491+
return '';
490492
if (typeof d == 'string' || typeof d == 'number')
491493
d = new Date(d);
492494
if (!d)
@@ -496,6 +498,8 @@ function dateToPreciseHR (d) {
496498

497499
exports.dateToHRTimeAgo = dateToHRTimeAgo;
498500
function dateToHRTimeAgo (time) {
501+
if (!time)
502+
return '';
499503
switch (typeof time) {
500504
case 'number':
501505
break;

resources/adminifier/static/script/file-list/pages.js

+20-3
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,27 @@ var pageList = new FileList({
1111
}
1212
});
1313

14-
if (a.json.results)
15-
a.json.results.each(function (pageData) {
14+
var currentDir = a.json.results.cd;
15+
16+
function nextDir(dir) {
17+
if (!currentDir)
18+
return dir;
19+
return currentDir + '/' + dir;
20+
}
21+
22+
if (a.json.results) {
23+
24+
25+
a.json.results.dirs.each(function (dir) {
26+
var entry = new FileListEntry({ Title: dir });
27+
entry.link = adminifier.wikiRoot + '/pages?dir=' + encodeURIComponent(nextDir(dir));
28+
pageList.addEntry(entry);
29+
});
30+
31+
a.json.results.pages.each(function (pageData) {
1632
var entry = new FileListEntry({
1733
data: pageData,
18-
Title: pageData.title || pageData.file_ne || pageData.file,
34+
Title: pageData.title || pageData.base_ne || pageData.file_ne || pageData.file,
1935
Author: pageData.author,
2036
Created: pageData.created,
2137
Modified: pageData.modified
@@ -30,6 +46,7 @@ a.json.results.each(function (pageData) {
3046
entry.link = adminifier.wikiRoot + '/edit-page?page=' + encodeURIComponent(pageData.file);
3147
pageList.addEntry(entry);
3248
});
49+
}
3350

3451
pageList.draw($('content'));
3552

resources/adminifier/template/frame-pages.tpl

+8
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,12 @@
2929
<input type="text" name="title" />
3030
<input type="submit" value="Create" />
3131
</form>
32+
</template>
33+
34+
<template id="tmpl-create-folder">
35+
<form action="func/create-page-folder" method="post">
36+
<label for="name">Folder Name:</label>
37+
<input type="text" name="title" />
38+
<input type="submit" value="Create" />
39+
</form>
3240
</template>

wiki/file.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,15 @@ func makeRelPath(absPath, base string) string {
146146
return ""
147147
}
148148

149+
var pageExtensions = []string{"page", "md"}
150+
149151
func (w *Wiki) allPageFiles() []string {
150-
files, _ := wikifier.UniqueFilesInDir(w.Opt.Dir.Page, []string{"page", "md"}, false)
152+
files, _ := wikifier.UniqueFilesInDir(w.Opt.Dir.Page, pageExtensions, false)
153+
return files
154+
}
155+
156+
func (w *Wiki) pageFilesInDir(where string) []string {
157+
files, _ := wikifier.UniqueFilesInDir(filepath.Join(w.Opt.Dir.Page, where), pageExtensions, true)
151158
return files
152159
}
153160

wiki/page.go

+49-9
Original file line numberDiff line numberDiff line change
@@ -287,12 +287,22 @@ func (w *Wiki) DisplayPageDraft(name string, draftOK bool) any {
287287
// Pages returns info about all the pages in the wiki.
288288
func (w *Wiki) Pages() []wikifier.PageInfo {
289289
pageNames := w.allPageFiles()
290+
return w.pagesIn("", pageNames)
291+
}
292+
293+
// PagesInDir returns info about all the pages in the specified directory.
294+
func (w *Wiki) PagesInDir(where string) []wikifier.PageInfo {
295+
pageNames := w.pageFilesInDir(where)
296+
return w.pagesIn(where, pageNames)
297+
}
298+
299+
func (w *Wiki) pagesIn(prefix string, pageNames []string) []wikifier.PageInfo {
290300
pages := make([]wikifier.PageInfo, len(pageNames))
291301

292302
// pages individually
293303
i := 0
294304
for _, name := range pageNames {
295-
pages[i] = w.PageInfo(name)
305+
pages[i] = w.PageInfo(filepath.Join(prefix, name))
296306
i++
297307
}
298308

@@ -313,11 +323,13 @@ func (pi sortablePageInfo) SortInfo() SortInfo {
313323
// PagesSorted returns info about all the pages in the wiki, sorted as specified.
314324
// Accepted sort functions are SortTitle, SortAuthor, SortCreated, and SortModified.
315325
func (w *Wiki) PagesSorted(descend bool, sorters ...SortFunc) []wikifier.PageInfo {
326+
return _pagesSorted(w.Pages(), descend, sorters...)
327+
}
316328

329+
func _pagesSorted(pages []wikifier.PageInfo, descend bool, sorters ...SortFunc) []wikifier.PageInfo {
317330
// convert to []Sortable
318-
pages := w.Pages()
319331
sorted := make([]Sortable, len(pages))
320-
for i, pi := range w.Pages() {
332+
for i, pi := range pages {
321333
sorted[i] = sortablePageInfo(pi)
322334
}
323335

@@ -336,6 +348,36 @@ func (w *Wiki) PagesSorted(descend bool, sorters ...SortFunc) []wikifier.PageInf
336348
return pages
337349
}
338350

351+
// PagesAndDirs returns info about all the pages and directories in a directory.
352+
func (w *Wiki) PagesAndDirs(where string) ([]wikifier.PageInfo, []string) {
353+
pages := w.PagesInDir(where)
354+
355+
// find dirs
356+
files, _ := os.ReadDir(filepath.Join(w.Opt.Dir.Page, where))
357+
dirs := make([]string, 0, len(files))
358+
for _, fi := range files {
359+
if fi.IsDir() {
360+
dirs = append(dirs, fi.Name())
361+
}
362+
}
363+
364+
return pages, dirs
365+
}
366+
367+
// PagesAndDirsSorted returns info about all the pages and directories in a directory, sorted as specified.
368+
// Accepted sort functions are SortTitle, SortAuthor, SortCreated, and SortModified.
369+
// Directories are always sorted alphabetically (but still respect the descend flag).
370+
func (w *Wiki) PagesAndDirsSorted(where string, descend bool, sorters ...SortFunc) ([]wikifier.PageInfo, []string) {
371+
pages, dirs := w.PagesAndDirs(where)
372+
pages = _pagesSorted(pages, descend, sorters...)
373+
if descend {
374+
sort.Sort(sort.Reverse(sort.StringSlice(dirs)))
375+
} else {
376+
sort.Strings(dirs)
377+
}
378+
return pages, dirs
379+
}
380+
339381
// PageMap returns a map of page name to PageInfo for all pages in the wiki.
340382
func (w *Wiki) PageMap() map[string]wikifier.PageInfo {
341383
pageNames := w.allPageFiles()
@@ -369,17 +411,15 @@ func (w *Wiki) PageInfo(name string) (info wikifier.PageInfo) {
369411
info = *pageCat.PageInfo
370412
}
371413

372-
// this stuff is available to all
414+
// add info we can derive from the file
373415
mod := pgFi.ModTime()
374416
info.Path = path
375417
info.File = filepath.ToSlash(name)
418+
info.FileNE = filepath.ToSlash(nameNE)
419+
info.Base = filepath.Base(name)
420+
info.BaseNE = filepath.Base(nameNE)
376421
info.Modified = &mod // actual page mod time
377422

378-
// fallback title to name
379-
if info.Title == "" {
380-
info.Title = nameNE
381-
}
382-
383423
// fallback created to modified
384424
if info.Created == nil {
385425
info.Created = &mod

wikifier/page.go

+4
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ type PageInfo struct {
5353
Path string `json:"-"` // absolute filepath
5454
File string `json:"file,omitempty"` // name with extension, always with forward slashes
5555
FileNE string `json:"file_ne,omitempty"` // name without extension, always with forward slashes
56+
Base string `json:"base,omitempty"` // base name with extension
57+
BaseNE string `json:"base_ne,omitempty"` // base name without extension
5658
Created *time.Time `json:"created,omitempty"` // creation time
5759
Modified *time.Time `json:"modified,omitempty"` // modify time
5860
Draft bool `json:"draft,omitempty"` // true if page is marked as draft
@@ -605,6 +607,8 @@ func (p *Page) Info() PageInfo {
605607
info := PageInfo{
606608
File: p.Name(),
607609
FileNE: p.NameNE(),
610+
Base: filepath.Base(p.Name()),
611+
BaseNE: filepath.Base(p.NameNE()),
608612
Draft: p.Draft(),
609613
Generated: p.Generated(),
610614
External: p.External(),

0 commit comments

Comments
 (0)