made templates contextual

This commit is contained in:
thewhitetulip 2015-11-21 18:50:51 +05:30
parent a5f50060aa
commit bb5e280a69
8 changed files with 39 additions and 23 deletions

View File

@ -26,18 +26,20 @@ func Close() {
//GetTasks retrieves all the tasks depending on the //GetTasks retrieves all the tasks depending on the
//status pending or trashed or completed //status pending or trashed or completed
func GetTasks(status string) []types.Task { func GetTasks(status string) types.Context {
var task []types.Task var task []types.Task
var context types.Context
var TaskID int var TaskID int
var TaskTitle string var TaskTitle string
var TaskContent string var TaskContent string
var TaskCreated time.Time var TaskCreated time.Time
var getTasksql string var getTasksql string
if status == "pending" { if status == "pending" {
getTasksql = "select id, title, content, created_date from task where finish_date is null and is_deleted='N' order by created_date asc" getTasksql = "select id, title, content, created_date from task where finish_date is null and is_deleted='N' order by created_date asc"
} else if status == "trashed" { } else if status == "deleted" {
getTasksql = "select id, title, content, created_date from task where is_deleted='Y' order by created_date asc" getTasksql = "select id, title, content, created_date from task where is_deleted='Y' order by created_date asc"
} else if status == "complete" { } else if status == "completed" {
getTasksql = "select id, title, content, created_date from task where finish_date is not null order by created_date asc" getTasksql = "select id, title, content, created_date from task where finish_date is not null order by created_date asc"
} }
@ -56,8 +58,8 @@ func GetTasks(status string) []types.Task {
a := types.Task{Id: TaskID, Title: TaskTitle, Content: TaskContent, Created: TaskCreated.Format(time.UnixDate)[0:20]} a := types.Task{Id: TaskID, Title: TaskTitle, Content: TaskContent, Created: TaskCreated.Format(time.UnixDate)[0:20]}
task = append(task, a) task = append(task, a)
} }
context = types.Context{Tasks: task, Navigation: status}
return task 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

View File

@ -4,7 +4,8 @@
<head> <head>
<title>Tasks</title> <title>{{if eq .Navigation "pending"}} Tasks {{ else if eq .Navigation "completed"}}Completed
{{ else if eq .Navigation "deleted"}}Deleted{{end}}</title>
<!-- Mobile viewport optimized --> <!-- Mobile viewport optimized -->
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
@ -40,8 +41,8 @@
<nav class="navbar navbar-default navbar-fixed-top mainHeader"> <nav class="navbar navbar-default navbar-fixed-top mainHeader">
<div class="container-fluid"> <div class="container-fluid">
<div class="navbar-header"> <div class="navbar-header">
<a class="navbar-brand" href='{{ if eq .Navigation "pending"}} / {{else}} /{{.Navigation}} {{end}}'> {{if eq .Navigation "pending"}} Tasks {{ else if eq .Navigation "completed"}}Completed
<a class="navbar-brand" href="/"> Tasks</a> {{ else if eq .Navigation "deleted"}}Deleted{{end}}</a>
<span id="icons"> <span id="icons">
<form action="/search/" method="POST"> <form action="/search/" method="POST">
<input type="text" name="query" placeholder="Search" style="border:none;border-bottom:1px solid gray; box-shadow:none;"> <input type="text" name="query" placeholder="Search" style="border:none;border-bottom:1px solid gray; box-shadow:none;">
@ -64,14 +65,14 @@
<li class="sidebar-group"><span>Tasks</span> <li class="sidebar-group"><span>Tasks</span>
<ul class="sidebar-group-menu"> <ul class="sidebar-group-menu">
<li class="sidebar-item"> <li class="sidebar-item">
<a href="/" ><span class="glyphicon glyphicon-tasks"></span> <span class="nav-item">Pending</span></a> <a href="/" {{ if eq .Navigation "pending"}} class="active" {{end}} ><span class="glyphicon glyphicon-tasks"></span> <span class="nav-item">Pending</span></a>
<!-- class="active" -->
</li> </li>
<li class="sidebar-item"> <li class="sidebar-item">
<a href="/completed/"><span class="glyphicon glyphicon-check"></span> <span class="nav-item"> Completed</span></a> <a href="/completed/" {{ if eq .Navigation "completed"}} class="active" {{end}}><span class="glyphicon glyphicon-check"></span> <span class="nav-item"> Completed</span></a>
</li> </li>
<li class="sidebar-item"> <li class="sidebar-item">
<a href="/deleted/"><span class="glyphicon glyphicon-trash"></span> <span class="nav-item"> Deleted</span></a> <a href="/deleted/" {{ if eq .Navigation "deleted"}} class="active" {{end}}><span class="glyphicon glyphicon-trash"></span> <span class="nav-item"> Deleted</span></a>
</li> </li>
</ul> </ul>
</li> </li>

View File

@ -1,7 +1,7 @@
{{template "_head.html"}} {{template "_head.html" .}}
<div class="timeline"> <div class="timeline">
{{ if .}} {{range .}} {{ if .}} {{range .Tasks}}
<div class="note"> <div class="note">
<p class="noteHeading">{{.Title}}</p> <p class="noteHeading">{{.Title}}</p>
<hr> <hr>

View File

@ -1,4 +1,4 @@
{{template "_head.html"}} {{template "_head.html" .}}
<!--end mainHeader --> <!--end mainHeader -->
{{if .}} {{if .}}
<a href="/delete/all"> <a href="/delete/all">
@ -7,7 +7,7 @@
{{end}} {{end}}
<div class="timeline"> <div class="timeline">
{{ if .}} {{range .}} {{ if .}} {{range .Tasks}}
<div class="note"> <div class="note">
<p class="noteHeading">{{.Title}}</p> <p class="noteHeading">{{.Title}}</p>
<hr> <hr>

View File

@ -1,4 +1,4 @@
{{template "_head.html"}} {{template "_head.html" .}}
<!--end mainHeader --> <!--end mainHeader -->
<button class=" btn-danger btn glyphicon glyphicon-plus floating-action-icon floating-action-icon-add"></button> <button class=" btn-danger btn glyphicon glyphicon-plus floating-action-icon floating-action-icon-add"></button>
@ -32,7 +32,7 @@
</div> </div>
<div class="timeline"> <div class="timeline">
{{ if .}} {{range .}} {{ if .}} {{range .Tasks}}
<div class="note"> <div class="note">
<p class="noteHeading">{{.Title}}</p> <p class="noteHeading">{{.Title}}</p>
<hr> <hr>

View File

@ -1,4 +1,4 @@
{{template "_head.html"}} {{template "_head.html" .}}
<div class="timeline"> <div class="timeline">
{{ if .}} {{range .}} {{ if .}} {{range .}}
<div class="note"> <div class="note">

View File

@ -1,8 +1,15 @@
package types package types
//Task is the struct used to identify tasks
type Task struct { type Task struct {
Id int Id int
Title string Title string
Content string Content string
Created string Created string
} }
//Context is the struct passed to templates
type Context struct {
Tasks []Task
Navigation string
}

View File

@ -26,6 +26,9 @@ func PopulateTemplates() {
var allFiles []string var allFiles []string
templatesDir := "./public/templates/" templatesDir := "./public/templates/"
files, err := ioutil.ReadDir(templatesDir) files, err := ioutil.ReadDir(templatesDir)
if err != nil {
fmt.Println("Error reading template dir")
}
for _, file := range files { for _, file := range files {
filename := file.Name() filename := file.Name()
if strings.HasSuffix(filename, ".html") { if strings.HasSuffix(filename, ".html") {
@ -37,6 +40,9 @@ func PopulateTemplates() {
fmt.Println(err) fmt.Println(err)
} }
templates, err = template.ParseFiles(allFiles...) templates, err = template.ParseFiles(allFiles...)
if err != nil {
fmt.Println(err)
}
homeTemplate = templates.Lookup("home.html") homeTemplate = templates.Lookup("home.html")
deletedTemplate = templates.Lookup("deleted.html") deletedTemplate = templates.Lookup("deleted.html")
@ -58,7 +64,7 @@ func ShowAllTasksFunc(w http.ResponseWriter, r *http.Request) {
//ShowTrashTaskFunc is used to handle the "/trash" URL which is used to show the deleted tasks //ShowTrashTaskFunc is used to handle the "/trash" URL which is used to show the deleted tasks
func ShowTrashTaskFunc(w http.ResponseWriter, r *http.Request) { func ShowTrashTaskFunc(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" { if r.Method == "GET" {
context := db.GetTasks("trashed") //false when you want deleted notes context := db.GetTasks("deleted") //false when you want deleted notes
deletedTemplate.Execute(w, context) deletedTemplate.Execute(w, context)
} }
} }
@ -96,7 +102,7 @@ func AddTaskFunc(w http.ResponseWriter, r *http.Request) {
//ShowCompleteTasksFunc is used to populate the "/completed/" URL //ShowCompleteTasksFunc is used to populate the "/completed/" URL
func ShowCompleteTasksFunc(w http.ResponseWriter, r *http.Request) { func ShowCompleteTasksFunc(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" { if r.Method == "GET" {
context := db.GetTasks("complete") //false when you want finished notes context := db.GetTasks("completed") //false when you want finished notes
completedTemplate.Execute(w, context) completedTemplate.Execute(w, context)
} else { } else {
http.Redirect(w, r, "/", http.StatusFound) http.Redirect(w, r, "/", http.StatusFound)
@ -190,7 +196,7 @@ func RestoreTaskFunc(w http.ResponseWriter, r *http.Request) {
//UpdateTaskFunc is used to update a task, handes "/update/" URL //UpdateTaskFunc is used to update a task, handes "/update/" URL
func UpdateTaskFunc(w http.ResponseWriter, r *http.Request) { func UpdateTaskFunc(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST"{ if r.Method == "POST" {
r.ParseForm() r.ParseForm()
id, err := strconv.Atoi(r.Form.Get("id")) id, err := strconv.Atoi(r.Form.Get("id"))
if err != nil { if err != nil {