1
0
mirror of https://github.com/fazo96/homework.git synced 2025-01-24 14:24:20 +01:00
homework/client/client.coffee

160 lines
5.1 KiB
CoffeeScript
Raw Normal View History

# Homework - Client Side
notes = new Meteor.Collection "notes"
Deps.autorun -> Meteor.subscribe "my-notes" unless not Meteor.userId()
2014-05-30 08:27:49 +02:00
2014-05-28 18:45:41 +02:00
getUser = -> Meteor.user()
amIValid = ->
return no unless getUser()
return yes for mail in getUser().emails when mail.verified is yes; no
2014-05-30 10:33:32 +02:00
logoutCallback = (err) -> if err then errCallback err else Router.go 'home'
loginCallback = (err) -> if err then errCallback err else Router.go 'notes'
2014-05-30 08:27:49 +02:00
# Common Helpers
2014-05-28 08:39:11 +02:00
UI.registerHelper "loggingIn", -> Meteor.loggingIn()
2014-05-30 10:33:32 +02:00
UI.registerHelper "email", -> getUser().emails[0].address
2014-05-28 18:45:41 +02:00
UI.registerHelper "verified", -> amIValid()
2014-05-30 10:33:32 +02:00
# 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',
2014-05-30 11:40:15 +02:00
path: '/verify/:token?'
2014-05-30 10:33:32 +02:00
template: 'verifyEmail'
onBeforeAction: ->
2014-05-30 11:40:15 +02:00
if @params.token?
Accounts.verifyEmail @params.token, (err) ->
if err
Router.go 'home'
errCallback err
else Router.go 'notes'
else if not getUser()
Router.go 'home'
else if amIValid() is yes then Router.go 'notes'
2014-05-30 10:33:32 +02:00
2014-05-30 08:27:49 +02:00
# Client Templates
# User Interface
2014-05-30 10:33:32 +02:00
Template.account.events
'click #logout': (e,template) -> Meteor.logout logoutCallback
# Notes template
2014-05-30 10:33:32 +02:00
Template.notelist.empty = -> notes.find().count() is 0
Template.notelist.notes = ->
d = notes.find().fetch()
2014-05-30 10:33:32 +02:00
Template.notelist.events
2014-05-28 10:19:06 +02:00
'click .close-note': ->
notes.remove @_id
2014-05-30 10:33:32 +02:00
'click .edit-note': ->
Router.go 'note', {_id: @_id}
'keypress #newNote': (e,template) ->
2014-05-28 10:19:06 +02:00
if e.keyCode is 13 and template.find('#newNote').value isnt ""
notes.insert
title: template.find('#newNote').value
2014-05-28 10:19:06 +02:00
content: ""
userId: Meteor.userId()
template.find('#newNote').value = ""
# Note Editor
2014-05-30 12:18:04 +02:00
Template.editor.note = -> Router.current.data() # Only when we're in /note/:_id
saveCurrentNote = (t,e) ->
if e and e.keyCode isnt 13 then return;
2014-05-30 10:33:32 +02:00
notes.update Router.current().data()._id,
$set:
2014-05-28 14:57:43 +02:00
title: t.find('.editor-title').value
content: t.find('.area').value
Template.editor.events
2014-05-30 10:33:32 +02:00
'click .close-editor': -> Router.go 'notes'
'click .save-editor': (e,t) -> saveCurrentNote t
'keypress .title': (e,t) -> saveCurrentNote t, e
# Notifications
alerts = []
alertDep = new Deps.Dependency
2014-05-28 18:45:41 +02:00
errCallback = (err) ->
if err.reason
showError msg: err.reason
else showErrror msg: err
# Show a notification
notify = (data) ->
alerts.push
title: data.title
msg: data.msg
type: data.type or "danger"
alertDep.changed()
# Clear all notifications
clearNotifications = -> alerts.clear(); alertDep.changed()
# Get all the notifications
Template.notifications.notification = -> alertDep.depend(); alerts
2014-05-28 10:19:06 +02:00
Template.notifications.events
'click .close-notification': (e,template) ->
alerts.splice alerts.indexOf(this), 1
alertDep.changed()
2014-05-28 10:19:06 +02:00
# "Error" visualization template
errorDep = new Deps.Dependency; shownError = undefined
showError = (err) ->
shownError = err; shownError.type = err.type or "danger"
errorDep.changed()
clearError = -> shownError = undefined; errorDep.changed()
Template.error.error = -> errorDep.depend(); shownError
Template.error.events 'click .close': -> clearError()
2014-05-28 18:45:41 +02:00
# Verify Email
Template.verifyEmail.events
'click #btn-verify': (e,template) ->
2014-05-30 10:33:32 +02:00
Accounts.verifyEmail template.find('#token-field').value, (err) ->
if err then errCallback err else Router.go 'notes'
2014-05-28 18:45:41 +02:00
'click #btn-resend': ->
2014-05-30 10:33:32 +02:00
Meteor.call 'resendConfirmEmail', (err) ->
if err
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
2014-05-28 18:45:41 +02:00
# Login and Register
pressLogin = (template) ->
mail = template.find('#mail').value; pass = template.find('#pass').value
2014-05-30 10:33:32 +02:00
Meteor.loginWithPassword mail, pass, loginCallback
2014-05-28 18:45:41 +02:00
2014-05-28 10:19:06 +02:00
Template.auth.events
# Login
2014-05-28 10:19:06 +02:00
'keypress .login': (e,template) -> if e.keyCode is 13 then pressLogin template
'click #login': (e,template) -> pressLogin template
# Register
'click #register': (e,template) ->
mail = template.find('#mail').value; pass = template.find('#pass').value
if not mail
showError msg: "Please enter an Email"
else if not pass
showError msg: "Please enter a password"
else if pass.length < 8
showError msg: "Password too short"
else # Sending actual registration request
try
Accounts.createUser {
email: mail,
password: pass
2014-05-30 10:33:32 +02:00
}, (err) -> if err then errCallback err else Router.go 'confirmEmail'
catch err
showError msg: err