1
0
mirror of https://github.com/schollz/cowyo.git synced 2023-08-10 21:13:00 +03:00

Add recent pages to "view"

This commit is contained in:
Zack Scholl 2017-09-29 08:48:32 -04:00
parent bb56db31f7
commit 6d81fe5b19
3 changed files with 97 additions and 46 deletions

File diff suppressed because one or more lines are too long

View File

@ -9,6 +9,7 @@ import (
// "github.com/gin-contrib/static"
"github.com/gin-contrib/multitemplate"
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
"github.com/schollz/cowyo/encrypt"
)
@ -16,6 +17,8 @@ import (
func serve(host, port, crt_path, key_path string, TLS bool) {
gin.SetMode(gin.ReleaseMode)
router := gin.Default()
store := sessions.NewCookieStore([]byte("secret"))
router.Use(sessions.Sessions("mysession", store))
router.HTMLRender = loadTemplates("index.tmpl")
// router.Use(static.Serve("/static/", static.LocalFile("./static", true)))
router.GET("/", func(c *gin.Context) {
@ -219,9 +222,39 @@ func handlePageRequest(c *gin.Context) {
"ListItems": renderList(rawText),
"Route": "/" + page + command,
"HasDotInName": strings.Contains(page, "."),
"RecentlyEdited": getRecentlyEdited(page, c),
})
}
func getRecentlyEdited(title string, c *gin.Context) []string {
session := sessions.Default(c)
var recentlyEdited string
v := session.Get("recentlyEdited")
editedThings := []string{}
if v == nil {
recentlyEdited = title
} else {
editedThings = strings.Split(v.(string), "|||")
if !stringInSlice(title, editedThings) {
recentlyEdited = v.(string) + "|||" + title
} else {
recentlyEdited = v.(string)
}
}
session.Set("recentlyEdited", recentlyEdited)
session.Save()
editedThingsWithoutCurrent := make([]string, len(editedThings))
i := 0
for _, thing := range editedThings {
if thing == title {
continue
}
editedThingsWithoutCurrent[i] = thing
i++
}
return editedThingsWithoutCurrent[:i]
}
func handlePageExists(c *gin.Context) {
type QueryJSON struct {
Page string `json:"page"`

View File

@ -451,7 +451,23 @@ body#pad textarea {
{{ end }}
</ul>
</li>
<li class="pure-menu-item {{ with .ViewPage }}pure-menu-selected{{ end }}"><a href="/{{ .Page }}/view" class="pure-menu-link">View</a></li>
<!--
<li class="pure-menu-item {{ with .ViewPage }}pure-menu-selected{{ end }}">
<a href="/{{ .Page }}/view" class="pure-menu-link">View</a>
</li>-->
<li class="pure-menu-item pure-menu-has-children pure-menu-allow-hover {{ with .ViewPage }}pure-menu-selected{{ end }}">
<a href="#" id="menuLink1" class="pure-menu-link">View</a>
<ul class="pure-menu-children">
<li class="pure-menu-item">
<a href="/{{ .Page }}/view" class="pure-menu-link">Current</a>
</li>
{{ range .RecentlyEdited}}
<li class="pure-menu-item"><a class="pure-menu-link" href="/{{.}}/view/">{{.}}</a></li>
{{ end }}
</ul>
</li>
{{ if (or (.IsLocked) (.IsEncrypted) )}}
{{ if .IsLocked }}
<li class="pure-menu-item"><a href="#" class="pure-menu-link" id="lockPage">{{ if .IsLocked }}Unlock{{ else }}Lock{{end}}</a></li>
@ -470,6 +486,8 @@ body#pad textarea {
{{end}}
</ul>
</div>