Skip to content

Commit

Permalink
Option to delete existing bookmark
Browse files Browse the repository at this point in the history
  • Loading branch information
smurfpandey committed May 12, 2015
1 parent ef52e91 commit 4ba0976
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 7 deletions.
13 changes: 13 additions & 0 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,19 @@ func APISaveBookmark(c *gin.Context) {
c.Writer.WriteHeader(204)
}

func APIDeleteBookmark(c *gin.Context) {
bookName := c.Params.ByName("name")

err := deleteBookmark(bookName, getBookmarkPath())

if err != nil {
c.JSON(400, NewError(err))
return
}

c.Writer.WriteHeader(204)
}

//APIServeAsset serves the static assets
func APIServeAsset(c *gin.Context) {
file := fmt.Sprintf(
Expand Down
15 changes: 15 additions & 0 deletions bookmark.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,18 @@ func saveBookmark(objBookmark Bookmark, path string) (int, error) {

return 1, nil
}

func deleteBookmark(bookmarkName string, path string) error {

fileName := bookmarkName + ".json"

fullFilePath := filepath.FromSlash(path + "/" + fileName)

err := os.Remove(fullFilePath)

if err != nil {
return err
}

return nil
}
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ func startServer() {
router.GET("/search/:query", APISearch)
router.GET("/bookmarks", APIGetBookmarks)
router.POST("/bookmarks/:name", APISaveBookmark)
router.DELETE("/bookmarks/:name", APIDeleteBookmark)

fmt.Println("Starting server...")
go router.Run(fmt.Sprintf("%v:%v", options.HttpHost, options.HttpPort))
Expand Down
12 changes: 12 additions & 0 deletions static/css/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -463,3 +463,15 @@ strong.form-control {
#sidebar .search{
margin: 5px;
}

.bookmark-list-item .delete-bookmark{
visibility: hidden;
}

.bookmark-list-item:hover .delete-bookmark{
visibility: visible;
}

.delete-bookmark{
font-size: 30px;
}
9 changes: 6 additions & 3 deletions static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -492,9 +492,12 @@ <h4 class="modal-title">Create table?</h4>
<script id="tmpl-bookmark-list" type="text/x-handlebars-template">
{{#each bookmarks}}
{{#with conn_info}}
<a href="#" class="list-group-item js-bookmark-link" data-username="{{Username}}" data-host="{{Host}}" data-port="{{Port}}" data-database="{{Database}}">
<p><strong>{{../name}}</strong></p>
<span href="#">{{Username}}@{{Host}}:{{Port}}/{{Database}}</span>
<a href="#" class="list-group-item js-bookmark-link clearfix bookmark-list-item" data-username="{{Username}}" data-host="{{Host}}" data-port="{{Port}}" data-database="{{Database}}">
<div class="pull-left">
<div><strong>{{../name}}</strong></div>
<span href="#">{{Username}}@{{Host}}:{{Port}}/{{Database}}</span>
</div>
<span href="#" class="pull-right delete-bookmark js-delete-bookmark" data-bookmarkname="{{../name}}" title="Delete this bookmark">&times;</span>
</a>
{{/with}}
{{/each}}
Expand Down
38 changes: 34 additions & 4 deletions static/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,13 @@ function apiSearchDatabase(query, cb) {
function getAllBookmarks(cb) {
apiCall("get", "/bookmarks", {}, cb);
}

function saveBookmark(bookmarkName, bookmarkData, cb) {
apiCall("post", "/bookmarks/" + bookmarkName, bookmarkData, cb);
}
function deleteBookmark(bookmarkName, cb) {
apiCall("delete", "/bookmarks/" + bookmarkName, {}, cb);
}


var fnGetSelectedTable = function() {
return theTable;
Expand Down Expand Up @@ -1714,8 +1717,8 @@ $(document).ready(function() {
var port = $('#pg_port').val();
var database = $('#pg_db').val();

if (!host && !userName && !port && !database) {
swal.showInputError("You need to provide connection details also!");
if (!host || !userName || !port || !database) {
swal.showInputError("You need to provide all connection details!");
return false;
}

Expand All @@ -1727,7 +1730,7 @@ $(document).ready(function() {
};

saveBookmark(bookmarkName, objData, function(data) {
if (typeof (result) === 'undefined') {
if (typeof (data) === 'undefined') {
//Bookmark saved
swal.close();

Expand All @@ -1744,6 +1747,33 @@ $(document).ready(function() {
});
});

$('#ulBookmarks').on('click', '.js-delete-bookmark', function(e) {
e.stopPropagation();

var $this = $(this);
var bookmarkName = $this.data('bookmarkname');

bookmarkName = encodeURIComponent(bookmarkName);

deleteBookmark(bookmarkName, function(data) {
if (typeof (data) === 'undefined') {
//Bookmark deleted
$this.parents('.js-bookmark-link').remove();
return;
}

if (data.error) {
swal({
title: "Error!",
text: data.error,
type: "error",
confirmButtonText: "Ohoo!"
});
}
});

});

initModals();

initEditor("custom_query");
Expand Down

0 comments on commit 4ba0976

Please sign in to comment.