2014-05-27 15:33:50 +02:00
|
|
|
# Homework - Client Side
|
|
|
|
notes = new Meteor.Collection "notes"
|
|
|
|
Deps.autorun -> Meteor.subscribe "my-notes" unless not Meteor.userId()
|
2014-05-28 18:45:41 +02:00
|
|
|
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
|
|
|
|
getUser = -> Meteor.user()
|
|
|
|
amIValid = ->
|
|
|
|
return no unless getUser()
|
|
|
|
return yes for mail in getUser().emails when mail.verified is yes; no
|
|
|
|
# Helpers
|
2014-05-28 08:39:11 +02:00
|
|
|
UI.registerHelper "loggingIn", -> Meteor.loggingIn()
|
2014-05-28 18:45:41 +02:00
|
|
|
UI.registerHelper "mail", -> getUser().emails[0].address
|
|
|
|
UI.registerHelper "verified", -> amIValid()
|
2014-05-27 15:33:50 +02:00
|
|
|
|
|
|
|
# User Interface
|
2014-05-28 12:50:30 +02:00
|
|
|
Template.userInfo.events
|
2014-05-27 15:33:50 +02:00
|
|
|
'click #logout': (e,template) -> Meteor.logout()
|
2014-05-28 12:50:30 +02:00
|
|
|
|
2014-05-27 15:33:50 +02:00
|
|
|
# Notes template
|
2014-05-28 12:50:30 +02:00
|
|
|
Template.notes.truncateNoteDesc = (s) -> s
|
|
|
|
#if s.length > 52 then s.slice(0,48)+"..." else s
|
2014-05-27 15:33:50 +02:00
|
|
|
Template.notes.notes = ->
|
|
|
|
d = notes.find().fetch()
|
2014-05-28 10:19:06 +02:00
|
|
|
Template.notes.events
|
|
|
|
'click .close-note': ->
|
2014-05-28 12:50:30 +02:00
|
|
|
if Session.get('note') and Session.get('note')._id is @_id
|
2014-05-28 10:19:06 +02:00
|
|
|
Session.set 'note', undefined
|
|
|
|
notes.remove @_id
|
2014-05-27 15:33:50 +02:00
|
|
|
'click .edit-note': -> Session.set 'note', this
|
|
|
|
'keypress #newNote': (e,template) ->
|
2014-05-28 10:19:06 +02:00
|
|
|
if e.keyCode is 13 and template.find('#newNote').value isnt ""
|
2014-05-28 12:50:30 +02:00
|
|
|
notes.insert
|
2014-05-27 15:33:50 +02:00
|
|
|
title: template.find('#newNote').value
|
2014-05-28 10:19:06 +02:00
|
|
|
content: ""
|
2014-05-27 15:33:50 +02:00
|
|
|
userId: Meteor.userId()
|
|
|
|
template.find('#newNote').value = ""
|
|
|
|
|
|
|
|
# Note Editor
|
|
|
|
Template.editor.note = -> Session.get 'note'
|
|
|
|
saveCurrentNote = (t,e) ->
|
|
|
|
if e and e.keyCode isnt 13 then return;
|
|
|
|
notes.update Session.get('note')._id,
|
|
|
|
$set:
|
2014-05-28 14:57:43 +02:00
|
|
|
title: t.find('.editor-title').value
|
2014-05-27 15:33:50 +02:00
|
|
|
content: t.find('.area').value
|
|
|
|
Template.editor.events
|
|
|
|
'click .close-editor': -> Session.set 'note', undefined
|
|
|
|
'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
|
2014-05-27 15:33:50 +02:00
|
|
|
# Show a notification
|
|
|
|
notify = (data) ->
|
2014-05-28 12:50:30 +02:00
|
|
|
alerts.push
|
2014-05-27 15:33:50 +02:00
|
|
|
title: data.title
|
|
|
|
msg: data.msg
|
|
|
|
type: data.type or "danger"
|
2014-05-28 12:50:30 +02:00
|
|
|
alertDep.changed()
|
2014-05-27 15:33:50 +02:00
|
|
|
# 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
|
2014-05-27 15:33:50 +02:00
|
|
|
'click .close-notification': (e,template) ->
|
|
|
|
alerts.splice alerts.indexOf(this), 1
|
|
|
|
alertDep.changed()
|
2014-05-28 10:19:06 +02:00
|
|
|
|
2014-05-28 12:50:30 +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 10:19:06 +02:00
|
|
|
# "Loading" template
|
|
|
|
Template.loading.status = -> Meteor.status()
|
2014-05-27 15:33:50 +02:00
|
|
|
|
2014-05-28 18:45:41 +02:00
|
|
|
# Verify Email
|
|
|
|
Template.verifyEmail.events
|
|
|
|
'click #btn-verify': (e,template) ->
|
|
|
|
Accounts.verifyEmail template.find('#token-field').value, errCallback
|
|
|
|
'click #btn-resend': ->
|
|
|
|
Meteor.call 'resendConfirmEmail', errCallback
|
|
|
|
'click #btn-delete': -> Meteor.call 'deleteMe'
|
|
|
|
'click #btn-logout': -> Meteor.logout()
|
|
|
|
|
2014-05-27 15:33:50 +02:00
|
|
|
# Login and Register
|
|
|
|
pressLogin = (template) ->
|
|
|
|
mail = template.find('#mail').value; pass = template.find('#pass').value
|
2014-05-28 18:45:41 +02:00
|
|
|
Meteor.loginWithPassword mail, pass, errCallback
|
|
|
|
|
2014-05-28 10:19:06 +02:00
|
|
|
Template.auth.events
|
2014-05-27 15:33:50 +02:00
|
|
|
# Login
|
2014-05-28 10:19:06 +02:00
|
|
|
'keypress .login': (e,template) -> if e.keyCode is 13 then pressLogin template
|
2014-05-27 15:33:50 +02:00
|
|
|
'click #login': (e,template) -> pressLogin template
|
|
|
|
# Register
|
|
|
|
'click #register': (e,template) ->
|
|
|
|
mail = template.find('#mail').value; pass = template.find('#pass').value
|
|
|
|
if not mail
|
2014-05-28 12:50:30 +02:00
|
|
|
showError msg: "Please enter an Email"
|
2014-05-27 15:33:50 +02:00
|
|
|
else if not pass
|
2014-05-28 12:50:30 +02:00
|
|
|
showError msg: "Please enter a password"
|
2014-05-27 15:33:50 +02:00
|
|
|
else if pass.length < 8
|
2014-05-28 12:50:30 +02:00
|
|
|
showError msg: "Password too short"
|
2014-05-27 15:33:50 +02:00
|
|
|
else # Sending actual registration request
|
|
|
|
try
|
|
|
|
Accounts.createUser {
|
|
|
|
email: mail,
|
|
|
|
password: pass
|
2014-05-28 18:45:41 +02:00
|
|
|
}, errCallback
|
2014-05-27 15:33:50 +02:00
|
|
|
catch err
|
2014-05-28 12:50:30 +02:00
|
|
|
showError msg: err
|