From 70a43528d0c1e51c69b84c74d5b5cf3289995975 Mon Sep 17 00:00:00 2001 From: thewhitetulip Date: Sun, 22 Nov 2015 09:21:29 +0530 Subject: [PATCH] added notifications --- db/db.go | 2 +- public/static/css/styles.css | 23 +++++++++++++ public/static/js/script.js | 60 ++++----------------------------- types/types.go | 1 + views/views.go | 64 +++++++++++++++++++++++++++++------- 5 files changed, 84 insertions(+), 66 deletions(-) diff --git a/db/db.go b/db/db.go index 1907148..afa710e 100644 --- a/db/db.go +++ b/db/db.go @@ -62,7 +62,7 @@ func GetTasks(status string) types.Context { return context } -//GetTaskByID function gets the tasks from the ID passed to the function +//GetTaskByID function gets the tasks from the ID passed to the function, used to populate EditTask func GetTaskByID(id int) types.Context { var tasks []types.Task var task types.Task diff --git a/public/static/css/styles.css b/public/static/css/styles.css index 8d769bf..87b2f8a 100644 --- a/public/static/css/styles.css +++ b/public/static/css/styles.css @@ -44,8 +44,31 @@ textarea { border-radius: 35px; box-shadow:0 0 4px #111; padding-top:10px !important; +} +.notification{ + width: -moz-fit-content; + position: fixed; + right: 10px; + max-width: 400px; + padding: 12px; + background-color: #FFF; + box-shadow: 1px 0px 9px 0px rgba(252, 103, 22, 0.4); + border: 1px solid #DEB3A8; + border-radius: 10px; + z-index: 1100; + max-height: 90px; + margin-top: 20px; +} +.notification-close{ + background-color: #2037B3; + border-radius: 40px; + border:none; + color: #FFF; + align-content: center; + position: fixed; + margin-top: -20px; } /*-------------------------------------- diff --git a/public/static/js/script.js b/public/static/js/script.js index 31f1998..ea61f16 100644 --- a/public/static/js/script.js +++ b/public/static/js/script.js @@ -18,21 +18,16 @@ $(document).ready(function(){ // alert(); // }); - $('#addNote').click(function(){ - $('#addNoteModal').modal('show'); - }); - $('.floating-action-icon-add').click(function(){ $('#addNoteModal').modal('show'); }); - - //$(document).on("click", '.note-close',closeDelete); //when you delete a note, the x on the top right corner - - $(document).on('click','.open-note', openNote); //when you want to open a note in full screen, the second icon on the bottom right corner from the right - - //$(document).on('click','.hashtag', hashTag); //function to handle search by hashtag *TODO* - - //$('#addNoteModalSaveBtn').click(addNoteToDOM); //Adds note to the DOM + + if ($('#message').html()==''){ + $('.notification').addClass('hidden'); + } else { + $('.notification').fadeOut(9000); + } + $('.notification-close').click(function(){$('.notification').fadeOut("slow")}) /*$( document ).keypress( function(event){ @@ -47,44 +42,3 @@ $(document).ready(function(){ );*/ }); - -function addNoteToDOM(){ - var title = $('#add-note-title').val(); - var content = $('#add-note-content').val(); - if (title!="" || content!=""){ - var note=$('
'+title +'

'+content+'
'); - $('.col-md-12.row').prepend(note); - $('#addNoteModal').modal('hide'); - $('#add-note-title').val(""); - $('#add-note-content').val(""); - } - else{ - alert("Empty note can't be saved!"); - } -} - -function closeDelete(){ - var note = $(this).parent().parent().parent(); - note.fadeOut('slow'); - note.remove(); - -} - -function openNote(){ - var element = $(this); - var cont=element.parent().parent().siblings().contents().toArray(); - var note_body =cont[2].data; - console.log(note_body); - var note_title = cont[0].data; - console.log(note_title); - - - var ONmodal = $('#openNoteModal'); - ONmodal.find('.modal-title').text(note_title); - ONmodal.find('.modal-body').text(note_body); - ONmodal.modal('show'); -} - -function hashTag(){ - alert($(this).html()); -} diff --git a/types/types.go b/types/types.go index d14094c..9e06fc3 100644 --- a/types/types.go +++ b/types/types.go @@ -13,4 +13,5 @@ type Context struct { Tasks []Task Navigation string Search string + Message string } diff --git a/views/views.go b/views/views.go index b4500a0..a0a0802 100644 --- a/views/views.go +++ b/views/views.go @@ -18,6 +18,7 @@ var completedTemplate *template.Template var editTemplate *template.Template var searchTemplate *template.Template var templates *template.Template +var message string //message will store the message to be shown as notification var err error //PopulateTemplates is used to parse all templates present in @@ -38,6 +39,7 @@ func PopulateTemplates() { if err != nil { fmt.Println(err) + os.Exit(1) } templates, err = template.ParseFiles(allFiles...) if err != nil { @@ -58,7 +60,14 @@ func PopulateTemplates() { func ShowAllTasksFunc(w http.ResponseWriter, r *http.Request) { if r.Method == "GET" { context := db.GetTasks("pending") //true when you want non deleted notes + if message != "" { + context.Message = message + } homeTemplate.Execute(w, context) + message = "" + } else { + message = "Method not allowed" + http.Redirect(w, r, "/", http.StatusFound) } } @@ -66,7 +75,14 @@ func ShowAllTasksFunc(w http.ResponseWriter, r *http.Request) { func ShowTrashTaskFunc(w http.ResponseWriter, r *http.Request) { if r.Method == "GET" { context := db.GetTasks("deleted") //false when you want deleted notes + if message != "" { + context.Message = message + message = "" + } deletedTemplate.Execute(w, context) + } else { + message = "Method not allowed" + http.Redirect(w, r, "/", http.StatusFound) } } @@ -78,6 +94,7 @@ func SearchTaskFunc(w http.ResponseWriter, r *http.Request) { context := db.SearchTask(query) searchTemplate.Execute(w, context) } else { + message = "Method not allowed" http.Redirect(w, r, "/", http.StatusFound) } @@ -91,11 +108,13 @@ func AddTaskFunc(w http.ResponseWriter, r *http.Request) { content := r.Form.Get("content") truth := db.AddTask(title, content) if truth != nil { - fmt.Println(err) + message = "Error adding task" } else { - http.Redirect(w, r, "/", http.StatusFound) + message = "Task added" } + http.Redirect(w, r, "/", http.StatusFound) } else { + message = "Method not allowed" http.Redirect(w, r, "/", http.StatusFound) } } @@ -106,6 +125,7 @@ func ShowCompleteTasksFunc(w http.ResponseWriter, r *http.Request) { context := db.GetTasks("completed") //false when you want finished notes completedTemplate.Execute(w, context) } else { + message = "Method not allowed" http.Redirect(w, r, "/", http.StatusFound) } } @@ -121,6 +141,7 @@ func EditTaskFunc(w http.ResponseWriter, r *http.Request) { editTemplate.Execute(w, task) } } else { + message = "Method not allowed" http.Redirect(w, r, "/", http.StatusFound) } } @@ -132,18 +153,21 @@ func CompleteTaskFunc(w http.ResponseWriter, r *http.Request) { if err != nil { fmt.Println(err) } else { - err := db.CompleteTask(id) + err = db.CompleteTask(id) if err != nil { - fmt.Println(err) + message = "Complete task failed" + } else { + message = "Task marked complete" } http.Redirect(w, r, "/", http.StatusFound) } } else { + message = "Method not allowed" http.Redirect(w, r, "/", http.StatusFound) } } -//DeleteTaskFunc is used to +//DeleteTaskFunc is used to delete a task, trash = move to recycle bin, delete = permanent delete func DeleteTaskFunc(w http.ResponseWriter, r *http.Request) { if r.Method == "GET" { id := r.URL.Path[len("/delete/"):] @@ -157,17 +181,20 @@ func DeleteTaskFunc(w http.ResponseWriter, r *http.Request) { } else { err = db.DeleteTask(id) if err != nil { - fmt.Println(err) + message = "Error deleting task" + } else { + message = "Task deleted" } - http.Redirect(w, r, "/deleted/", http.StatusFound) + http.Redirect(w, r, "/", http.StatusFound) } } } else { + message = "Method not allowed" http.Redirect(w, r, "/", http.StatusFound) } } -//TrashTaskFunc is used to populate the "/trash/" URL +//TrashTaskFunc is used to populate the trash tasks func TrashTaskFunc(w http.ResponseWriter, r *http.Request) { if r.Method == "GET" { id, err := strconv.Atoi(r.URL.Path[len("/trash/"):]) @@ -176,12 +203,15 @@ func TrashTaskFunc(w http.ResponseWriter, r *http.Request) { } else { err = db.TrashTask(id) if err != nil { - fmt.Println(err) + message = "Error trashing task" + } else { + message = "Task trashed" } http.Redirect(w, r, "/", http.StatusFound) } } else { - http.Redirect(w, r, "/", http.StatusFound) + message = "Method not allowed" + http.Redirect(w, r, "/trash", http.StatusFound) } } @@ -192,10 +222,16 @@ func RestoreTaskFunc(w http.ResponseWriter, r *http.Request) { if err != nil { fmt.Println(err) } else { - db.RestoreTask(id) + err = db.RestoreTask(id) + if err != nil { + message = "Restore failed" + } else { + message = "Task restored" + } http.Redirect(w, r, "/deleted/", http.StatusFound) } } else { + message = "Method not allowed" http.Redirect(w, r, "/", http.StatusFound) } } @@ -212,10 +248,14 @@ func UpdateTaskFunc(w http.ResponseWriter, r *http.Request) { content := r.Form.Get("content") err = db.UpdateTask(id, title, content) if err != nil { - fmt.Println(err) + message = "Error updating task" + } else { + message = "Task updated" } http.Redirect(w, r, "/", http.StatusFound) + } else { + message = "Method not allowed" http.Redirect(w, r, "/", http.StatusFound) } }