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

112 lines
3.6 KiB
CoffeeScript
Raw Normal View History

2014-05-25 19:49:32 +02:00
notes = new Meteor.Collection "notes"
2014-05-25 12:01:38 +02:00
2014-05-27 12:46:51 +02:00
# Server
2014-05-25 12:01:38 +02:00
if Meteor.isServer
2014-05-25 14:17:55 +02:00
Accounts.config {
2014-05-27 12:46:51 +02:00
sendVerificationEmail: false
2014-05-25 14:17:55 +02:00
loginExpirationInDays: 1
}
2014-05-25 19:49:32 +02:00
Meteor.publish "my-notes", ->
notes.find( { userId: @userId } ) unless not @userId
2014-05-25 12:01:38 +02:00
2014-05-27 12:46:51 +02:00
# Authentication
Accounts.validateNewUser (user) ->
if user.email and Meteor.check(user.email,String) is yes and user.email.contains '@' is yes and user.email.endsWith '.' is no and user.email.endsWith '@' is no
return yes
else throw new Meteor.Error 403, "Invalid Email"
if user.password and Meteor.check(user.password,String) is yes and user.password.length > 7
return yes
else throw new Meteor.Error 403, "Password invalid"
2014-05-26 16:07:48 +02:00
2014-05-27 12:46:51 +02:00
# Client
if Meteor.isClient
Deps.autorun -> Meteor.subscribe "my-notes" unless not Meteor.userId()
2014-05-26 16:07:48 +02:00
2014-05-26 19:36:59 +02:00
# User Interface
Template.userInfo.events {
2014-05-27 12:46:51 +02:00
'click #logout': (e,template) -> Meteor.logout()
2014-05-26 19:36:59 +02:00
'keypress #newNote': (e,template) ->
if e.keyCode is 13
notes.insert {
title: template.find('#newNote').value
content: "..."
userId: Meteor.userId()
}
template.find('#newNote').value = ""
}
Template.userInfo.in = -> Meteor.user().emails[0].address
2014-05-25 14:17:55 +02:00
# Notes template
2014-05-27 08:54:18 +02:00
Template.notes.truncateNoteDesc = (s) ->
if s.length > 52 then s.slice(0,48)+"..." else s
2014-05-26 19:36:59 +02:00
Template.notes.notes = ->
d = notes.find().fetch();
#d.splice d.indexOf(Session.get('note')), 1 ; d
2014-05-25 12:01:38 +02:00
Template.notes.events {
2014-05-26 16:07:48 +02:00
'click .close-note': -> notes.remove @_id
2014-05-26 19:36:59 +02:00
'click .edit-note': -> Session.set 'note', this
2014-05-26 12:08:14 +02:00
}
2014-05-26 16:07:48 +02:00
# Note Editor
2014-05-26 19:36:59 +02:00
Template.editor.note = -> Session.get 'note'
2014-05-27 08:54:18 +02:00
saveCurrentNote = (t,e) ->
if e and e.keyCode isnt 13 then return;
notes.update Session.get('note')._id,
$set:
title: t.find('.title').value
content: t.find('.area').value
2014-05-26 19:36:59 +02:00
Template.editor.events
'click .close-editor': -> Session.set 'note', undefined
2014-05-27 08:54:18 +02:00
'click .save-editor': (e,t) -> saveCurrentNote t
#'keypress .edit-note': (e,t) -> saveCurrentNote t, e # Doesnt work??
'keypress .title': (e,t) -> saveCurrentNote t, e
2014-05-25 19:49:32 +02:00
2014-05-26 16:07:48 +02:00
# Notifications
alerts = []
alertDep = new Deps.Dependency
errCallback = (err) -> notify { msg: err.reason }
# Show a notification
notify = (data) ->
alerts.push {
title: data.title
msg: data.msg
id: data.id or alerts.length
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
Template.notifications.events {
'click .close-notification': (e,template) ->
alerts.splice alerts.indexOf(this), 1
alertDep.changed()
}
2014-05-26 19:36:59 +02:00
# Login and Register
2014-05-26 16:07:48 +02:00
pressLogin = (template) ->
mail = template.find('#mail').value; pass = template.find('#pass').value
Meteor.loginWithPassword mail, pass, (err) ->
2014-05-27 12:46:51 +02:00
errCallback err
Template.auth.working = -> Meteor.loggingIn()
2014-05-25 14:17:55 +02:00
Template.auth.events {
'keypress .login': (e,template) ->
2014-05-26 16:07:48 +02:00
if e.keyCode is 13 then pressLogin template
2014-05-25 19:49:32 +02:00
# Login
2014-05-26 16:07:48 +02:00
'click #login': (e,template) -> pressLogin template
2014-05-25 19:49:32 +02:00
# Register
2014-05-25 14:17:55 +02:00
'click #register': (e,template) ->
mail = template.find('#mail').value; pass = template.find('#pass').value
2014-05-26 16:07:48 +02:00
if not mail or mail.contains '@' is no or mail.endsWith '.' is yes or mail.endsWith '@' is yes
notify { msg: "Invalid Email" }
else
try
Accounts.createUser {
email: mail,
password: pass
2014-05-27 12:46:51 +02:00
}, (e) -> errCallback e
2014-05-26 16:07:48 +02:00
catch err
notify { msg: err }
2014-05-25 14:17:55 +02:00
}