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

code clean up

This commit is contained in:
fazo96 2014-06-05 17:03:10 +02:00
parent ccbae51ce5
commit 475181698e
5 changed files with 60 additions and 43 deletions

View File

@ -1,6 +1,6 @@
# Homework - Client Side # Homework - Client Side
homework_version = "1.0"
# Variables and utility stuff # Utilities
notes = new Meteor.Collection "notes" notes = new Meteor.Collection "notes"
getUser = -> Meteor.user() getUser = -> Meteor.user()
deleteAccount = -> deleteAccount = ->
@ -33,6 +33,7 @@ Router.configure
Router.map -> Router.map ->
@route 'home', @route 'home',
path: '/' path: '/'
template: 'homepage'
action: -> @render 'homepage', to: 'outside' action: -> @render 'homepage', to: 'outside'
onBeforeAction: -> onBeforeAction: ->
# Dispatch user to the right landing page based on his account status # Dispatch user to the right landing page based on his account status
@ -53,13 +54,6 @@ Router.map ->
path: '/archive/:_id?' path: '/archive/:_id?'
waitOn: -> Meteor.subscribe "archive" waitOn: -> Meteor.subscribe "archive"
onBeforeAction: -> if not getUser() then Router.go 'home' onBeforeAction: -> if not getUser() then Router.go 'home'
###
@route 'note',
path: '/note/:_id'
waitOn: -> Meteor.subscribe "my-notes"
data: -> notes.findOne _id: @params._id
onBeforeAction: -> if not @data()? then Router.go 'home'
###
@route 'verifyEmail', @route 'verifyEmail',
path: '/verify/:token?' path: '/verify/:token?'
template: 'verifyEmail' template: 'verifyEmail'
@ -82,7 +76,7 @@ Deps.autorun ->
# Client Templates # Client Templates
# Some utilities # Some utility callbacks
logoutCallback = (err) -> logoutCallback = (err) ->
if err then errCallback err if err then errCallback err
else Router.go 'home'; Meteor.unsubscribe "my-notes" else Router.go 'home'; Meteor.unsubscribe "my-notes"
@ -91,18 +85,18 @@ errCallback = (err) ->
showError msg: err.reason showError msg: err.reason
else showErrror msg: err else showErrror msg: err
# Menu # 3 Buttons navigation Menu
Template.menu.events Template.menu.events
'click .go-home': -> Router.go 'home' 'click .go-home': -> Router.go 'home'
'click .go-account': -> Router.go 'account' 'click .go-account': -> Router.go 'account'
'click .go-archive': -> Router.go 'archive' 'click .go-archive': -> Router.go 'archive'
# User Interface # Account Page
Template.account.events Template.account.events
'click #btn-logout': (e,template) -> Meteor.logout logoutCallback 'click #btn-logout': (e,template) -> Meteor.logout logoutCallback
'click #btn-delete-me': -> deleteAccount() 'click #btn-delete-me': -> deleteAccount()
# Notes template # Notes list
Template.notelist.active = -> Template.notelist.active = ->
return no unless Router.current() and Router.current().data() return no unless Router.current() and Router.current().data()
return @_id is Router.current().data()._id return @_id is Router.current().data()._id
@ -157,13 +151,11 @@ saveCurrentNote = (t,e) ->
content: t.find('.area').value content: t.find('.area').value
date: t.find('.date').value date: t.find('.date').value
Template.editor.events Template.editor.events
'click .close-editor': -> 'click .close-editor': -> Router.go 'notes'
Router.go 'notes'
#if Router.current().path.startsWith '/note' then 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
# Notifications # Notifications (not used yet)
alerts = [] alerts = []
alertDep = new Deps.Dependency alertDep = new Deps.Dependency
# Show a notification # Show a notification
@ -191,7 +183,7 @@ 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()
# Verify Email # Verify Email page
Template.verifyEmail.events Template.verifyEmail.events
'click #btn-verify': (e,template) -> 'click #btn-verify': (e,template) ->
t = template.find('#token-field').value; t = t.split("/") t = template.find('#token-field').value; t = t.split("/")
@ -218,7 +210,7 @@ Template.login.events
'keypress .login': (e,template) -> loginRequest e,template 'keypress .login': (e,template) -> loginRequest e,template
'click #login-btn': (e,template) -> loginRequest null,template 'click #login-btn': (e,template) -> loginRequest null,template
# Register # New Account page
registerRequest = (e,template) -> registerRequest = (e,template) ->
if e and e.keyCode isnt 13 then return if e and e.keyCode isnt 13 then return
mail = template.find('#r-mail').value; pass = template.find('#r-pass').value mail = template.find('#r-mail').value; pass = template.find('#r-pass').value

View File

@ -1,3 +1,4 @@
/* Homework CSS File. I'm sorry it's a mess... */
/* Generics and bootstrap classes */ /* Generics and bootstrap classes */
input { input {
text-align:center; text-align:center;

View File

@ -1,22 +1,6 @@
<!-- Homework UI Templates --> <!-- Homework UI Templates -->
<template name="layout"> <!-- 3 buttons Navigation Menu -->
<div class="container">
<div class="page-header">
<h1 id="title">Homework<br>
<small>management for students</small>
</h1>
</div>
{{> yield region='outside'}}
<div class="center-block" id="ui-container">
{{> yield}}
</div>
<hr>
<div class="center-block" align="center">{{> footer}}</div>
</div>
{{> ribbon}}
</template>
<template name="menu"> <template name="menu">
<div align="center" class="menu-container"> <div align="center" class="menu-container">
<div class="btn-group"> <div class="btn-group">
@ -32,15 +16,18 @@
</div></div> </div></div>
</template> </template>
<!-- /notes and /archive -->
<template name="notes">{{> error }} {{> editor }} {{> notelist }} {{> menu}}</template> <template name="notes">{{> error }} {{> editor }} {{> notelist }} {{> menu}}</template>
<template name="archive">{{> error}} {{> archivedlist }} {{> menu }}</template> <template name="archive">{{> error}} {{> archivedlist }} {{> menu }}</template>
<!-- Note Adder -->
<template name="noteadder"> <template name="noteadder">
<div align="center"> <div align="center">
<input type="text" id="newNote" class="form-control" placeholder="Add new note"> <input type="text" id="newNote" class="form-control" placeholder="Add new note">
</div> </div>
</template> </template>
<!-- Note List (seen at /notes) -->
<template name="notelist"> <template name="notelist">
<div class="list-group"> <div class="list-group">
{{#each notelist}} {{#each notelist}}
@ -62,6 +49,7 @@
{{> noteadder }} {{> noteadder }}
</template> </template>
<!-- Archive List (seen at /archive) -->
<template name="archivedlist"> <template name="archivedlist">
{{#unless empty}} {{#unless empty}}
<p align="center" class="lead">This is your archive.</p> <p align="center" class="lead">This is your archive.</p>
@ -88,6 +76,7 @@
{{/if}} {{/if}}
</template> </template>
<!-- Note Editor (seen at /notes/:_id) -->
<template name="editor"> <template name="editor">
{{#if _id}} {{#if _id}}
<div class="panel panel-info"> <div class="panel panel-info">
@ -108,6 +97,7 @@
{{/if}} {{/if}}
</template> </template>
<!-- /login -->
<template name="login"> <template name="login">
<div align="center"> <div align="center">
<h3 class="cool-header"><i class="fa fa-sign-in fa-2x"></i><br>Sign In</h3> <h3 class="cool-header"><i class="fa fa-sign-in fa-2x"></i><br>Sign In</h3>
@ -129,6 +119,7 @@
</div> </div>
</template> </template>
<!-- /register -->
<template name="register"> <template name="register">
<div align="center"> <div align="center">
<h3 class="cool-header"><i class="fa fa-user fa-2x"></i><br>New Account</h3> <h3 class="cool-header"><i class="fa fa-user fa-2x"></i><br>New Account</h3>
@ -148,6 +139,7 @@
</div> </div>
</template> </template>
<!-- /account -->
<template name="account"> <template name="account">
<div align="center"> <div align="center">
<h3 class="cool-header"><i class="fa fa-user fa-2x"></i><br>{{email}}</h3> <h3 class="cool-header"><i class="fa fa-user fa-2x"></i><br>{{email}}</h3>
@ -163,6 +155,7 @@
</div> </div>
</template> </template>
<!-- /verify -->
<template name="verifyEmail"> <template name="verifyEmail">
<div align="center"> <div align="center">
<h3 class="cool-header"><i class="fa fa-envelope fa-2x"></i><br> <h3 class="cool-header"><i class="fa fa-envelope fa-2x"></i><br>
@ -183,6 +176,7 @@
</div> </div>
</template> </template>
<!-- Notification list. It doesn't show anything unless there are notifications -->
<template name="notifications"> <template name="notifications">
{{#each notification}} {{#each notification}}
<div class="alert alert-{{type}} notification"> <div class="alert alert-{{type}} notification">
@ -192,6 +186,7 @@
{{/each}} {{/each}}
</template> </template>
<!-- Shows an Error if there is one. It's dismissable -->
<template name="error"> <template name="error">
{{#if error}} {{#if error}}
<div align="center"><div class="alert alert-{{error.type}} error"> <div align="center"><div class="alert alert-{{error.type}} error">
@ -201,6 +196,7 @@
{{/if}} {{/if}}
</template> </template>
<!-- Shows the "fork me on github" ribbon -->
<template name="ribbon"> <template name="ribbon">
<div class="github-fork-ribbon-wrapper right"> <div class="github-fork-ribbon-wrapper right">
<div class="github-fork-ribbon"> <div class="github-fork-ribbon">
@ -209,12 +205,14 @@
</div> </div>
</template> </template>
<!-- Shows a spinning gear -->
<template name="loading"> <template name="loading">
<div align="center" class="spinning-cog"> <div align="center" class="spinning-cog">
<i class="fa fa-cog fa-spin fa-3x"></i> <i class="fa fa-cog fa-spin fa-3x"></i>
</div> </div>
</template> </template>
<!-- Footer (used in 'template') -->
<template name="footer"> <template name="footer">
<p>This app is <a href="https://en.wikipedia.org/wiki/Free_software">Free Software</a>, under the <a href="http://opensource.org/licenses/MIT">MIT License</a></p> <p>This app is <a href="https://en.wikipedia.org/wiki/Free_software">Free Software</a>, under the <a href="http://opensource.org/licenses/MIT">MIT License</a></p>
<p>Built by Enrico Fasoli</p> <p>Built by Enrico Fasoli</p>
@ -223,6 +221,7 @@
<a class="custom-link" href="http://github.com/fazo96"><i class="fa fa-github fa-2x"></i></a> <a class="custom-link" href="http://github.com/fazo96"><i class="fa fa-github fa-2x"></i></a>
</template> </template>
<!-- 404 -->
<template name="404"> <template name="404">
<div align="center"> <div align="center">
<h1><i class="fa fa-exclamation-triangle fa-2x"></i><br> <h1><i class="fa fa-exclamation-triangle fa-2x"></i><br>
@ -233,6 +232,7 @@
</div> </div>
</template> </template>
<!-- Homepage -->
<template name="homepage"> <template name="homepage">
<div align="center"><i class="fa fa-book fa-5x"></i></div> <div align="center"><i class="fa fa-book fa-5x"></i></div>
<p align="center" style="margin-top: 20px" class="lead">A fast, free, organized way to manage your school tasks.</p> <p align="center" style="margin-top: 20px" class="lead">A fast, free, organized way to manage your school tasks.</p>
@ -264,3 +264,21 @@
</div> </div>
{{/unless}} {{/unless}}
</template> </template>
<!-- Layout for all the pages -->
<template name="layout">
<div class="container">
<div class="page-header">
<h1 id="title">Homework<br>
<small>management for students</small>
</h1>
</div>
{{> yield region='outside'}}
<div class="center-block" id="ui-container">
{{> yield}}
</div>
<hr>
<div class="center-block" align="center">{{> footer}}</div>
</div>
{{> ribbon}}
</template>

View File

@ -1,20 +1,23 @@
# Homework - Server side accounts code # Homework - Server side accounts code
# Regular Expression to see if an email can be valid
validateEmail = (email) -> validateEmail = (email) ->
x = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/ x = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/
x.test email x.test email
Accounts.validateNewUser (user) ->
mail = user.emails[0].address
if Match.test(mail,String) is no or validateEmail(mail) is no
throw new Meteor.Error 403, "Invalid Email"
return yes
Accounts.config { Accounts.config {
sendVerificationEmail: true sendVerificationEmail: true
loginExpirationInDays: 1 loginExpirationInDays: 1
} }
# Code that checks if a new user request is valid
Accounts.validateNewUser (user) ->
mail = user.emails[0].address
if Match.test(mail,String) is no or validateEmail(mail) is no
throw new Meteor.Error 403, "Invalid Email"
return yes
# Email configuration code
Accounts.emailTemplates.siteName = "Homework App" Accounts.emailTemplates.siteName = "Homework App"
Accounts.emailTemplates.verifyEmail.text = (user,url) -> Accounts.emailTemplates.verifyEmail.text = (user,url) ->
urlist = url.split('/'); token = urlist[urlist.length-1] urlist = url.split('/'); token = urlist[urlist.length-1]

View File

@ -29,6 +29,7 @@ notes.allow insert: isUsers, update: isUsers, remove: isUsers
# Methods that the clients can invoke # Methods that the clients can invoke
Meteor.methods Meteor.methods
# Request another confirmation email.
resendConfirmEmail: -> resendConfirmEmail: ->
u = getUser(@userId) u = getUser(@userId)
if not u if not u
@ -40,6 +41,7 @@ Meteor.methods
else else
console.log "User "+u.emails[0].address+" already validated." console.log "User "+u.emails[0].address+" already validated."
return no return no
# Request user's account to be deleted
deleteMe: -> deleteMe: ->
if @userId if @userId
Meteor.users.remove @userId Meteor.users.remove @userId
@ -47,4 +49,5 @@ Meteor.methods
Meteor.users.update {_id: @userId}, Meteor.users.update {_id: @userId},
{$set : { "resume.loginTokens" : [] } }, { multi: yes } {$set : { "resume.loginTokens" : [] } }, { multi: yes }
return yes return yes
else no no
version: -> "1.0" # Request server version number.