1
0
mirror of https://github.com/fazo96/homework.git synced 2025-01-10 12:14:22 +01:00

added a router. fixes #9

This commit is contained in:
fazo96 2014-05-30 10:33:32 +02:00
parent 2d92401442
commit ce8442a6ab
9 changed files with 107 additions and 57 deletions

View File

@ -10,3 +10,4 @@ bootstrap-3
font-awesome font-awesome
accounts-base accounts-base
accounts-password accounts-password
iron-router

View File

@ -6,29 +6,55 @@ getUser = -> Meteor.user()
amIValid = -> amIValid = ->
return no unless getUser() return no unless getUser()
return yes for mail in getUser().emails when mail.verified is yes; no return yes for mail in getUser().emails when mail.verified is yes; no
logoutCallback = (err) -> if err then errCallback err else Router.go 'home'
loginCallback = (err) -> if err then errCallback err else Router.go 'notes'
# Common Helpers # Common Helpers
UI.registerHelper "loggingIn", -> Meteor.loggingIn() UI.registerHelper "loggingIn", -> Meteor.loggingIn()
UI.registerHelper "mail", -> getUser().emails[0].address UI.registerHelper "email", -> getUser().emails[0].address
UI.registerHelper "verified", -> amIValid() UI.registerHelper "verified", -> amIValid()
# Router
Router.configure
layoutTemplate: 'layout'
loadingTemplate: 'loading'
Router.map ->
@route 'home',
onBeforeAction: -> if getUser() then Router.go 'notes'
path: '/'
template: 'auth'
@route 'notes',
onBeforeAction: ->
if not getUser() then Router.go 'home'
if amIValid() is no then Router.go 'verifyEmail'
@route 'note',
path: '/note/:_id'
data: -> notes.findOne _id: @params._id
onBeforeAction: ->
if not getUser() then Router.go 'home'
if amIValid() is no then Router.go 'verifyEmail'
if not @data() then Router.go 'notes'
@route 'verifyEmail',
template: 'verifyEmail'
onBeforeAction: ->
if not getUser() then Router.go 'home'
if amIValid() is yes then Router.go 'notes'
# Client Templates # Client Templates
# User Interface # User Interface
Template.userInfo.events Template.account.events
'click #logout': (e,template) -> Meteor.logout() 'click #logout': (e,template) -> Meteor.logout logoutCallback
# Notes template # Notes template
Template.notes.truncateNoteDesc = (s) -> s Template.notelist.empty = -> notes.find().count() is 0
#if s.length > 52 then s.slice(0,48)+"..." else s Template.notelist.notes = ->
Template.notes.notes = ->
d = notes.find().fetch() d = notes.find().fetch()
Template.notes.events Template.notelist.events
'click .close-note': -> 'click .close-note': ->
if Session.get('note') and Session.get('note')._id is @_id
Session.set 'note', undefined
notes.remove @_id notes.remove @_id
'click .edit-note': -> Session.set 'note', this 'click .edit-note': ->
Router.go 'note', {_id: @_id}
'keypress #newNote': (e,template) -> 'keypress #newNote': (e,template) ->
if e.keyCode is 13 and template.find('#newNote').value isnt "" if e.keyCode is 13 and template.find('#newNote').value isnt ""
notes.insert notes.insert
@ -38,15 +64,15 @@ Template.notes.events
template.find('#newNote').value = "" template.find('#newNote').value = ""
# Note Editor # Note Editor
Template.editor.note = -> Session.get 'note' Template.editor.note = -> Router.current.data()
saveCurrentNote = (t,e) -> saveCurrentNote = (t,e) ->
if e and e.keyCode isnt 13 then return; if e and e.keyCode isnt 13 then return;
notes.update Session.get('note')._id, notes.update Router.current().data()._id,
$set: $set:
title: t.find('.editor-title').value title: t.find('.editor-title').value
content: t.find('.area').value content: t.find('.area').value
Template.editor.events Template.editor.events
'click .close-editor': -> Session.set 'note', undefined 'click .close-editor': -> Router.go 'notes'
'click .save-editor': (e,t) -> saveCurrentNote t 'click .save-editor': (e,t) -> saveCurrentNote t
'keypress .title': (e,t) -> saveCurrentNote t, e 'keypress .title': (e,t) -> saveCurrentNote t, e
@ -82,22 +108,24 @@ clearError = -> shownError = undefined; errorDep.changed()
Template.error.error = -> errorDep.depend(); shownError Template.error.error = -> errorDep.depend(); shownError
Template.error.events 'click .close': -> clearError() Template.error.events 'click .close': -> clearError()
# "Loading" template
Template.loading.status = -> Meteor.status()
# Verify Email # Verify Email
Template.verifyEmail.events Template.verifyEmail.events
'click #btn-verify': (e,template) -> 'click #btn-verify': (e,template) ->
Accounts.verifyEmail template.find('#token-field').value, errCallback Accounts.verifyEmail template.find('#token-field').value, (err) ->
if err then errCallback err else Router.go 'notes'
'click #btn-resend': -> 'click #btn-resend': ->
Meteor.call 'resendConfirmEmail', errCallback Meteor.call 'resendConfirmEmail', (err) ->
'click #btn-delete': -> Meteor.call 'deleteMe' if err
'click #btn-logout': -> Meteor.logout() errCallback err
else showError { type:"success", msg: "Confirmation email sent" }
'click #btn-delete': ->
Meteor.call 'deleteMe', (r) -> if r is yes then Router.go 'home'
'click #btn-logout': -> Meteor.logout logoutCallback
# Login and Register # Login and Register
pressLogin = (template) -> pressLogin = (template) ->
mail = template.find('#mail').value; pass = template.find('#pass').value mail = template.find('#mail').value; pass = template.find('#pass').value
Meteor.loginWithPassword mail, pass, errCallback Meteor.loginWithPassword mail, pass, loginCallback
Template.auth.events Template.auth.events
# Login # Login
@ -117,6 +145,6 @@ Template.auth.events
Accounts.createUser { Accounts.createUser {
email: mail, email: mail,
password: pass password: pass
}, errCallback }, (err) -> if err then errCallback err else Router.go 'confirmEmail'
catch err catch err
showError msg: err showError msg: err

5
client/view/head.html Normal file
View File

@ -0,0 +1,5 @@
<!-- Homework HTML page -->
<head>
<title>Homework</title>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
</head>

View File

@ -1,9 +1,5 @@
<head> <!-- Homework UI Templates -->
<title>Homework</title> <template name="layout">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
</head>
<body>
{{> ribbon}}
<div class="container"> <div class="container">
<div class="page-header"> <div class="page-header">
<h1 id="title">Homework<br> <h1 id="title">Homework<br>
@ -12,21 +8,27 @@
</div> </div>
<div class="center-block" id="ui-container"> <div class="center-block" id="ui-container">
{{> error}} {{> error}}
{{#if currentUser}} {{> yield}}
{{#if verified}}
{{> editor}} {{> notes}} {{> userInfo}}
{{else}}
{{> verifyEmail}}
{{/if}}
{{else}}
{{> auth}}
{{/if}}
</div> </div>
<div class="center-block" align="center">{{> footer}}</div> <div class="center-block" align="center">{{> footer}}</div>
</div> </div>
</body> {{> ribbon}}
</template>
<template name="notes"> <template name="notes">
{{> notelist }} <hr> {{> account }}
</template>
<template name="note">
{{> editor }} {{> notelist }} <hr> {{> account }}
</template>
<template name="noteadder">
<div align="center">
<input type="text" id="newNote" class="form-control" placeholder="Add new note">
</div>
</template>
<template name="notelist">
<ul class="list-group"> <ul class="list-group">
{{#each notes}} {{#each notes}}
<li class="note list-group-item"> <li class="note list-group-item">
@ -40,9 +42,10 @@
</li> </li>
{{/each}} {{/each}}
</ul> </ul>
<div align="center"> {{#if empty}}
<input type="text" id="newNote" class="form-control" placeholder="Add new note"> <p align="center">You don't have any notes, try adding a new one</p>
</div> {{/if}}
{{> noteadder }}
</template> </template>
<template name="auth"> <template name="auth">
@ -56,14 +59,14 @@
<br> <br>
{{/if}} {{/if}}
<p>Password must be at least 8 characters. Email must be a valid email</p> <p>Password must be at least 8 characters. Email must be a valid email</p>
{{#unless working}} {{#unless loggingIn}}
<button type="button" id="register" class="btn-auth btn btn-success">Register</button> <button type="button" id="register" class="btn-auth btn btn-success">Register</button>
<button type="button" id="login" class="btn-auth btn btn-primary">Login</button> <button type="button" id="login" class="btn-auth btn btn-primary">Login</button>
{{/unless}} {{/unless}}
</div> </div>
</template> </template>
<template name="userInfo"><hr> <template name="account">
{{#if loggingIn}} {{> loading}} {{/if}} {{#if loggingIn}} {{> loading}} {{/if}}
<div align="center"> <div align="center">
<p>{{email}}</p> <p>{{email}}</p>
@ -86,18 +89,18 @@
<template name="editor"> <template name="editor">
<div align="center">{{> error }}</div> <div align="center">{{> error }}</div>
{{#if note}} {{#if _id}}
<div class="panel panel-info"> <div class="panel panel-info">
<div class="panel-heading"> <div class="panel-heading">
<h3 class="panel-title"> <h3 class="panel-title">
<div align="center"> <div align="center">
<input type="text" class="form-control editor-title" value="{{note.title}}" placeholder="Title"> <input type="text" class="form-control editor-title" value="{{title}}" placeholder="Title">
<button type="button" class="close close-editor">&times;</button> <button type="button" class="close close-editor">&times;</button>
</div> </div>
</h3> </h3>
</div> </div>
<div align="center" class="panel-body"> <div align="center" class="panel-body">
<textarea class="area form-control" rows="3" placeholder="...">{{note.content}}</textarea> <textarea class="area form-control" rows="3" placeholder="...">{{content}}</textarea>
<button type="button" class="btn btn-info save-editor">Save</button> <button type="button" class="btn btn-info save-editor">Save</button>
</div> </div>
</div> </div>

2
packages/.gitignore vendored
View File

@ -1,2 +1,4 @@
/bootstrap-3 /bootstrap-3
/font-awesome /font-awesome
/iron-router
/blaze-layout

View File

@ -1,4 +1,9 @@
# Homework - Server Side # Homework - Server Side
validateEmail = (email) ->
expr = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/
expr.test email
console.log "Started Homework server!" console.log "Started Homework server!"
if process.env.MAIL_URL if process.env.MAIL_URL
console.log "Sending emails using "+process.env.MAIL_URL console.log "Sending emails using "+process.env.MAIL_URL
@ -8,9 +13,6 @@ else
notes = new Meteor.Collection "notes" notes = new Meteor.Collection "notes"
getUser = (id) -> Meteor.users.findOne { _id: id } getUser = (id) -> Meteor.users.findOne { _id: id }
validateEmail = (email) ->
expr = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/
expr.test email
Accounts.config { Accounts.config {
sendVerificationEmail: true sendVerificationEmail: true
@ -47,7 +49,7 @@ Meteor.methods
resendConfirmEmail: -> resendConfirmEmail: ->
u = getUser(@userId) u = getUser(@userId)
if not u if not u
console.log "Validating nonexisting user!!"; return no console.log "Validating nonexisting user!"; return no
if userValidated(u) is no if userValidated(u) is no
Accounts.sendVerificationEmail @userId Accounts.sendVerificationEmail @userId
console.log "Sent verification email to "+u.emails[0].address console.log "Sent verification email to "+u.emails[0].address
@ -61,3 +63,5 @@ Meteor.methods
# Automagically log out the user by invalidating every token he has # Automagically log out the user by invalidating every token he has
Meteor.users.update {_id: @userId}, Meteor.users.update {_id: @userId},
{$set : { "resume.loginTokens" : [] } }, { multi: yes } {$set : { "resume.loginTokens" : [] } }, { multi: yes }
return yes
else no

View File

@ -1,5 +0,0 @@
# Utility functions for client and server - Homework
validateEmail = (email) ->
expr = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/
expr.test email

View File

@ -1,6 +1,7 @@
{ {
"packages": { "packages": {
"bootstrap-3": {}, "bootstrap-3": {},
"font-awesome": {} "font-awesome": {},
"iron-router": {}
} }
} }

View File

@ -3,7 +3,8 @@
"dependencies": { "dependencies": {
"basePackages": { "basePackages": {
"bootstrap-3": {}, "bootstrap-3": {},
"font-awesome": {} "font-awesome": {},
"iron-router": {}
}, },
"packages": { "packages": {
"bootstrap-3": { "bootstrap-3": {
@ -15,6 +16,16 @@
"git": "https://github.com/nate-strauser/meteor-font-awesome.git", "git": "https://github.com/nate-strauser/meteor-font-awesome.git",
"tag": "v4.1.0", "tag": "v4.1.0",
"commit": "ce6b1e92715d1938babc5d69c655c17e387f5926" "commit": "ce6b1e92715d1938babc5d69c655c17e387f5926"
},
"iron-router": {
"git": "https://github.com/EventedMind/iron-router.git",
"tag": "v0.7.1",
"commit": "d1ffb3f06ea4c112132b030f2eb1a70b81675ecb"
},
"blaze-layout": {
"git": "https://github.com/EventedMind/blaze-layout.git",
"tag": "v0.2.4",
"commit": "b40e9b0612329288d75cf52ad14a7da64bb8618f"
} }
} }
} }