From 1a7de61d0f6ac790672dea8aa44ce902776f0f7d Mon Sep 17 00:00:00 2001 From: fazo96 Date: Mon, 6 Oct 2014 12:50:24 +0200 Subject: [PATCH] implemented edit and delete documents --- client/client.coffee | 68 +++++++++++++++++++++++++++++++++++++++---- client/style.less | 12 ++++++-- client/templates.html | 49 +++++++++++++++++++++---------- server/server.coffee | 7 +++-- 4 files changed, 110 insertions(+), 26 deletions(-) diff --git a/client/client.coffee b/client/client.coffee index 63cdafb..b943ac0 100644 --- a/client/client.coffee +++ b/client/client.coffee @@ -35,6 +35,21 @@ Router.map -> onBeforeAction: -> Accounts.verifyEmail @params.token, (err) -> if err then errCallback err else Router.go 'home' + @route 'edit', + path: '/edit/:_id' + template: 'new' + waitOn: -> Meteor.subscribe 'doc', @params._id + data: -> docs.findOne @params._id + @route 'list', + path: '/list/:user?' + waitOn: -> Meteor.subscribe 'docs', @params.user + data: -> userId: @params.user + onBeforeAction: -> + if Meteor.user() and !@params.user + Router.go 'list', user: Meteor.user()._id + action: -> + if !@params.user then @render '404' + else @render() @route 'new' @route 'signup' @route 'signin', path: 'login' @@ -57,29 +72,72 @@ Template.layout.showSpinner = -> Meteor.status().connected is no or Router.current().ready() is no Template.home.ndocs = -> docs.find().count() Template.new.events - 'click #new-btn': (e,t) -> + 'click #upload': (e,t) -> if t.find('#title').value is '' return notify msg: 'please insert a title' if t.find('#editor').value is '' - return notify msg: "can't create empty document" - docs.insert { + return notify msg: "empty documents are not valid" + if @_id then docs.update @_id, $set: { + title: t.find('#title').value + text: t.find('#editor').value + showTitle: $('#show-title').is(':checked') + }, (err) => + if err then errCallback err + else + notify type:'success', msg:'Document updated' + Router.go 'doc', _id: @_id + else docs.insert { title: t.find('#title').value text: t.find('#editor').value showTitle: $('#show-title').is(':checked') dateCreated: moment().unix() }, (err,id) -> - errCallback err + if err then errCallback err + else notify type:'success', msg:'Document created successfully' if id then Router.go 'doc', _id: id + ###docs.upsert @_id, $set: { + title: t.find('#title').value + text: t.find('#editor').value + showTitle: $('#show-title').is(':checked') + dateCreated: moment().unix() + }, (err,resp) -> + if err then errCallback err + else if @_id + notify type:'success', msg:'Document updated' + Router.go 'doc', _id: @_id + else + notify type:'success', msg:'Document created successfully' + Router.go 'doc', _id: resp.insertedId### + +Template.list.documents = -> docs.find owner: @userId Template.doc.source = -> Router.current().route.name is 'src' Template.doc.rows = -> ""+@text.split('\n').length -Template.doc.canEdit = -> "disabled" unless Meteor.user()._id is @owner +Template.doc.valid = -> @text? +Template.doc.owned = -> Meteor.user()._id is @owner Template.doc.events + 'click #edit-doc': -> Router.go 'edit', _id: @_id + 'click #del-doc': -> + if Meteor.user()._id is @owner + docs.remove @_id, (err) -> + if err then errCallback err + else notify type:'success', msg:'Document removed' 'click #src-doc': -> if Router.current().route.name is 'src' Router.go 'doc', _id: @_id else Router.go 'src', _id: @_id +Template.signin.events + 'click #signin': (e,t) -> + if not t.find('#mail').value + return notify msg: 'please enter an email' + 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 diff --git a/client/style.less b/client/style.less index 4d9b416..1414787 100644 --- a/client/style.less +++ b/client/style.less @@ -18,6 +18,10 @@ margin-top: 2em; } +.notification-area { + margin-top: 1em; +} + // Editor #title { @@ -26,17 +30,21 @@ #editor { margin-top: 1em; } -#new-btn { +#upload { margin-top: 1em; } // Viewer #tools { - margin-top: 1em; + margin-top: 0.5em; margin-bottom: 1em; } +#src { + margin-top: 1em; +} + // Sign up #signup-container, #signin-container { #mail { margin-top: 2em; } diff --git a/client/templates.html b/client/templates.html index 061cfb8..fd59bd4 100644 --- a/client/templates.html +++ b/client/templates.html @@ -28,11 +28,11 @@ @@ -132,3 +143,9 @@
Need an account? Sign Up!
+ + diff --git a/server/server.coffee b/server/server.coffee index 8cc928e..2261b90 100644 --- a/server/server.coffee +++ b/server/server.coffee @@ -6,7 +6,9 @@ validatedUser = (uid) -> return yes for mail in u.emails when mail.verified is yes; no Meteor.publish 'doc', (id) -> docs.find {_id: id}, limit: 1 -Meteor.publish 'docs', -> docs.find {}, fields: text: 0 +Meteor.publish 'docs', (userId) -> + if userId? then docs.find {owner: userId}, fields: text: 0 + else docs.find {}, fields: text: 0 Meteor.publish 'user', -> if @userId Meteor.users.find {_id: @userId}, fields: {dateCreated: 1} @@ -18,9 +20,8 @@ docs.allow doc.dateCreated = moment().unix() if doc.owner and !uid then return no if uid then doc.owner = uid - console.log 'ok' + console.log doc.dateCreated return yes - console.log 'nope' return no docs.allow # Owners can update and remove their documents