From 2980f1e193e3221a138ddb0279e50e78b264a089 Mon Sep 17 00:00:00 2001 From: Zack Scholl Date: Thu, 23 Mar 2017 13:31:09 -0600 Subject: [PATCH] Add "Clear old" for deleting crossed list items Former-commit-id: f43b6caae264ef5f8cd46f8a40143501a39d5948 [formerly 328a9724705837f8287a6d519578513fb868c962] [formerly b006fff403bdf2421495a4d4e8f9ef246e15adec [formerly fb4118be424a81a90072ad987c886af08923917a]] Former-commit-id: da4974f66bfcea8602d9b08f129b73ba06d69ea8 [formerly 8215c6a461f8bd2a9baee02176e64ec99a401ec9] Former-commit-id: 4e070f07eef46a9f140a778dca7dc6c20adc76d5 --- bindata.go.REMOVED.git-id | 2 +- handlers.go | 34 ++++++++++++++++++++++++++++++ templates/index.tmpl | 44 ++++++++++++++++++++++++++++++++++++++- 3 files changed, 78 insertions(+), 2 deletions(-) diff --git a/bindata.go.REMOVED.git-id b/bindata.go.REMOVED.git-id index c86cd7f..f0c00ad 100644 --- a/bindata.go.REMOVED.git-id +++ b/bindata.go.REMOVED.git-id @@ -1 +1 @@ -8241bf42c5b3ba0d768c26f3df5e4b20779790a5 \ No newline at end of file +34bb35db683f8a7a61bc69989052e94f30862994 \ No newline at end of file diff --git a/handlers.go b/handlers.go index ed79116..ac698fb 100755 --- a/handlers.go +++ b/handlers.go @@ -29,6 +29,7 @@ func serve(port string) { router.POST("/prime", handlePrime) router.POST("/lock", handleLock) router.POST("/encrypt", handleEncrypt) + router.DELETE("/oldlist", handleClearOldListItems) router.DELETE("/listitem", deleteListItem) router.Run(":" + port) @@ -315,3 +316,36 @@ func deleteListItem(c *gin.Context) { }) } } + +func handleClearOldListItems(c *gin.Context) { + type QueryJSON struct { + Page string `json:"page"` + } + + var json QueryJSON + if c.BindJSON(&json) != nil { + c.String(http.StatusBadRequest, "Problem binding keys") + return + } + p := Open(json.Page) + if p.IsEncrypted { + c.JSON(http.StatusOK, gin.H{"success": false, "message": "Encrypted"}) + return + } + if p.IsLocked { + c.JSON(http.StatusOK, gin.H{"success": false, "message": "Locked"}) + return + } + lines := strings.Split(p.Text.GetCurrent(), "\n") + newLines := make([]string, len(lines)) + newLinesI := 0 + for _, line := range lines { + if strings.Count(line, "~~") != 2 { + newLines[newLinesI] = line + newLinesI++ + } + } + p.Update(strings.Join(newLines[0:newLinesI], "\n")) + p.Save() + c.JSON(http.StatusOK, gin.H{"success": true, "message": "Cleared"}) +} diff --git a/templates/index.tmpl b/templates/index.tmpl index 2e126ff..7562885 100755 --- a/templates/index.tmpl +++ b/templates/index.tmpl @@ -242,6 +242,33 @@ }); } + function clearOld() { + $.ajax({ + type: 'DELETE', + url: '/oldlist', + data: JSON.stringify({ + page: "{{ .Page }}" + }), + success: function(data) { + $('#saveEditButton').removeClass() + if (data.success == true) { + $('#saveEditButton').addClass("success"); + } else { + $('#saveEditButton').addClass("failure"); + } + $('#saveEditButton').text(data.message); + if (data.success == true) { + window.location = "/{{ .Page }}/list"; + } + }, + error: function(xhr, error) { + $('#saveEditButton').text(error); + }, + contentType: "application/json", + dataType: 'json' + }); + } + $("#encryptPage").click(function(e) { e.preventDefault(); @@ -289,6 +316,16 @@ } }); + $("#clearOld").click(function(e) { + e.preventDefault(); + var r = confirm("This will erase all cleared list items, are you sure you want to do that?"); + if (r == true) { + clearOld() + } else { + x = "You pressed Cancel!"; + } + }); + $("textarea").keydown(function(e) { if(e.keyCode === 9) { // tab was pressed // get caret position/selection @@ -368,7 +405,12 @@ {{ else }}
  • Encrypted
  • {{ end }} - {{else}}
  • List
  • + {{else}} + {{ if .ListPage }} +
  • Clear old
  • + {{ else }} +
  • List
  • + {{ end }}
  • Edit
  • {{end}}