1
0
mirror of https://github.com/fazo96/markcloud.git synced 2025-01-10 11:04:21 +01:00
markcloud/client/client.coffee

203 lines
6.8 KiB
CoffeeScript
Raw Normal View History

2014-10-04 08:48:44 +02:00
docs = new Meteor.Collection 'docs'
2014-10-07 12:36:39 +02:00
amIValid = ->
return no unless Meteor.user()
return yes for mail in Meteor.user().emails when mail.verified is yes; no
UI.registerHelper 'mail', -> Meteor.user().emails[0].address
2014-10-07 12:36:39 +02:00
UI.registerHelper 'amIValid', amIValid
2014-10-04 08:48:44 +02:00
Router.configure
layoutTemplate: 'layout'
loadingTemplate: 'loading'
2014-10-04 08:48:44 +02:00
2014-10-06 10:28:05 +02:00
docController = RouteController.extend
template: 'doc'
layoutTemplate: 'docLayout'
waitOn: -> Meteor.subscribe 'doc', @params._id
data: -> docs.findOne @params._id
action: ->
2014-10-08 15:44:24 +02:00
console.log @render
if @ready()
if @data()? then @render()
else @render '404'
2014-10-06 10:28:05 +02:00
else @render 'loading'
loggedOutController = RouteController.extend
onBeforeAction: -> if Meteor.user() then Router.go 'profile'
2014-10-07 12:36:39 +02:00
action: ->
@render()
if Meteor.loggingIn() then @render 'spinner', to: 'outside'
loggedInController = RouteController.extend
action: -> if !Meteor.user() then @render '404' else @render()
2014-10-04 08:48:44 +02:00
Router.map ->
2014-10-04 10:11:08 +02:00
@route 'home',
path: '/'
2014-10-04 10:34:13 +02:00
action: ->
2014-10-07 10:21:07 +02:00
if !@ready() then @render 'spinner', to: 'outside'
@render()
2014-10-04 08:48:44 +02:00
@route 'doc',
path: '/d/:_id'
2014-10-06 10:28:05 +02:00
controller: docController
@route 'src',
path:'/src/:_id'
controller: docController
@route 'verify',
path: '/verify/:token?'
2014-10-07 12:36:39 +02:00
template: 'loading'
onBeforeAction: ->
Accounts.verifyEmail @params.token, (err) ->
2014-10-07 12:36:39 +02:00
if err then errCallback err else Router.go 'profile'
2014-10-06 12:50:24 +02:00
@route 'edit',
path: '/edit/:_id'
2014-10-07 10:21:07 +02:00
template: 'editor'
controller: loggedInController
2014-10-06 12:50:24 +02:00
waitOn: -> Meteor.subscribe 'doc', @params._id
data: -> docs.findOne @params._id
@route 'profile',
path: '/u/:user?'
waitOn: ->
[Meteor.subscribe('docs', @params.user),
Meteor.subscribe('user',@params.user)]
2014-10-07 10:21:07 +02:00
data: -> Meteor.users.findOne @params.user
2014-10-06 12:50:24 +02:00
onBeforeAction: ->
if Meteor.user() and !@params.user
Router.go 'profile', user: Meteor.user()._id
2014-10-06 12:50:24 +02:00
action: ->
2014-10-07 10:21:07 +02:00
if !@data() then @render '404'
else if @ready() then @render()
else @render 'loading'
@route 'new', template: 'editor'
@route 'signup',
controller: loggedOutController
@route 'signin',
path: 'login'
controller: loggedOutController
@route '404', path: '*'
share.notify = notify = (opt) ->
2014-10-08 15:44:24 +02:00
if opt.type? then type = opt.type
else type = 'error'
if opt.title? then title = opt.title
else title = if type is 'error' then 'Error' else 'Ok'
swal title, opt.msg, type
errCallback = (err) ->
2014-10-06 08:42:31 +02:00
return unless err
2014-10-08 15:44:24 +02:00
console.log err
if err.reason
notify title: err.code or 'Error', msg: err.reason, type: 'error'
else notify title: 'Error', msg: err, type: 'error'
2014-10-04 08:48:44 +02:00
Template.layout.notHome = -> Router.current().route.name isnt 'home'
2014-10-04 10:34:13 +02:00
Template.layout.showSpinner = ->
Meteor.status().connected is no or Router.current().ready() is no
2014-10-07 12:59:22 +02:00
Template.editor.isPublic = -> return "checked" if @public is yes
2014-10-07 10:21:07 +02:00
Template.editor.showTitleChecked = -> return "checked" unless @showTitle is no
Template.editor.events
2014-10-06 12:50:24 +02:00
'click #upload': (e,t) ->
2014-10-08 15:44:24 +02:00
if Meteor.loggingIn()
return notify
msg: "Can't upload while Logging In.\nTry again in a few seconds."
2014-10-06 08:42:31 +02:00
if t.find('#title').value is ''
2014-10-08 15:44:24 +02:00
return notify msg: 'Please insert a title.'
2014-10-06 08:42:31 +02:00
if t.find('#editor').value is ''
2014-10-08 15:44:24 +02:00
return notify msg: "Empty documents are not valid."
2014-10-06 12:50:24 +02:00
if @_id then docs.update @_id, $set: {
title: t.find('#title').value
text: t.find('#editor').value
showTitle: $('#show-title').is(':checked')
2014-10-07 10:21:07 +02:00
public: $('#make-public').is(':checked')
2014-10-06 12:50:24 +02:00
}, (err) =>
if err then errCallback err
else
2014-10-08 15:44:24 +02:00
notify type:'success', msg:'Document updated.'
2014-10-06 12:50:24 +02:00
Router.go 'doc', _id: @_id
else docs.insert {
title: t.find('#title').value
2014-10-04 08:48:44 +02:00
text: t.find('#editor').value
2014-10-06 08:42:31 +02:00
showTitle: $('#show-title').is(':checked')
2014-10-07 10:21:07 +02:00
public: $('#make-public').is(':checked')
2014-10-06 08:42:31 +02:00
}, (err,id) ->
2014-10-06 12:50:24 +02:00
if err then errCallback err
2014-10-08 15:44:24 +02:00
else notify type:'success', msg:'Document created successfully!'
2014-10-06 08:42:31 +02:00
if id then Router.go 'doc', _id: id
2014-10-06 12:50:24 +02:00
Template.profile.isMe = ->
2014-10-07 10:21:07 +02:00
Meteor.user() and Meteor.user()._id is @_id
Template.profile.noDocs = -> docs.find(owner: @_id).count() is 0
Template.profile.documents = ->
2014-10-07 10:21:07 +02:00
docs.find {owner: @_id}, sort: dateCreated: -1
Template.profile.events
2014-10-07 12:36:39 +02:00
'click #resend': ->
Meteor.call 'sendVerificationEmail', (e,r) ->
if e then errCallback e
else notify msg: r, type:'success'
'click #logout': -> Meteor.logout(); Router.go 'home'
2014-10-08 15:44:24 +02:00
'click #deleteme': -> swal {
title: 'Are you sure?'
text: 'Do you want to permanently delete all your data?'
type: 'warning'
showCancelButton: yes
confirmButtonColor: "#DD6B55"
confirmButtonText: "Yes!"
}, -> Meteor.call 'deleteMe', (e,r) ->
if e then errCallback e
else notify type: 'success', msg: 'Account deleted'
2014-10-06 08:42:31 +02:00
2014-10-08 16:09:03 +02:00
Template.doc.valid = -> @text?
2014-10-06 10:28:05 +02:00
Template.doc.source = -> Router.current().route.name is 'src'
Template.doc.rows = -> ""+@text.split('\n').length
2014-10-06 12:50:24 +02:00
Template.doc.owned = -> Meteor.user()._id is @owner
Template.doc.expirationDays = ->
if @owner then return 'never'
else return moment.unix(@dateCreated).add(7,'days').fromNow()
2014-10-06 10:28:05 +02:00
Template.doc.events
2014-10-06 12:50:24 +02:00
'click #edit-doc': -> Router.go 'edit', _id: @_id
'click #del-doc': ->
if Meteor.user()._id is @owner
2014-10-08 15:44:24 +02:00
swal {
title: 'Are you sure?'
text: 'This document will be deleted permanently'
type: 'warning'
showCancelButton: yes
confirmButtonColor: "#DD6B55"
confirmButtonText: "Yes!"
}, => docs.remove @_id, (err) ->
2014-10-06 12:50:24 +02:00
if err then errCallback err
else notify type:'success', msg:'Document removed'
2014-10-06 10:28:05 +02:00
'click #src-doc': ->
if Router.current().route.name is 'src'
Router.go 'doc', _id: @_id
else Router.go 'src', _id: @_id
2014-10-06 12:50:24 +02:00
Template.signin.events
'click #signin': (e,t) ->
if not t.find('#mail').value
return notify msg: 'please enter your email or username'
2014-10-06 12:50:24 +02:00
else if not t.find('#pw').value
return notify msg: "Please enter a password"
else
Meteor.loginWithPassword t.find('#mail').value, t.find('#pw').value,(e)->
if e then errCallback e
else notify type:'success', msg:'Welcome back!'; Router.go 'home'
Template.signup.events
'click #signup': (e,t) ->
if not t.find('#mail').value
return notify msg: 'please enter your email'
if not t.find('#name').value
return notify msg: 'please enter your user name'
else if not t.find('#pw').value
return notify msg: "Please enter a password"
else if t.find('#pw').value isnt t.find('#rpw').value
return notify msg: "The passwords don't match"
else # Sending actual registration request
Accounts.createUser {
username: t.find('#name').value
email: t.find('#mail').value
password: t.find('#pw').value
}, (err) -> if err then errCallback err
else notify type: 'success', msg: 'check your email'