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

implemented edit and delete documents

This commit is contained in:
fazo96 2014-10-06 12:50:24 +02:00
parent 67d87ecf04
commit 1a7de61d0f
4 changed files with 110 additions and 26 deletions

View File

@ -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

View File

@ -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; }

View File

@ -28,11 +28,11 @@
<template name="new">
<p>Write your document in <a href="https://help.github.com/articles/github-flavored-markdown/">GitHub Flavored Markdown</a> then submit it with the button. You will be redirected to its permanent link</p>
<input type="text" id="title" class="form-control" placeholder="Title">
<input type="text" id="title" class="form-control" placeholder="Title" value="{{title}}">
<input type="checkbox" id="show-title" checked> Show title in document
<textarea id="editor" class="form-control" rows="10" placeholder="Write your markdown :)" autofocus></textarea>
<button id="new-btn" class="btn btn-primary">
<i class="fa fa-upload"></i> Upload</button>
<textarea id="editor" class="form-control" rows="10" placeholder="Write your markdown :)" autofocus>{{text}}</textarea>
<button id="upload" class="btn btn-primary">
<i class="fa fa-upload"></i> {{#if _id}}Update{{else}}Upload{{/if}}</button>
</template>
<template name="docLayout">
@ -41,29 +41,39 @@
</template>
<template name="doc">
{{#if valid}}
{{#if showTitle}}<h1>{{title}}</h1><hr>{{/if}}
{{#if text}}
<div class="notification-area">
{{> notifications}}
</div>
{{#if source}}
<textarea id="src" class="form-control" rows="{{rows}}" readonly>{{text}}</textarea>
{{else}}
<div id="doc">{{#markdown}}{{text}}{{/markdown}}</div>
{{/if}}
{{else}}
{{else}}
<div class="text-center">
<h1>Sorry</h1>
<p>This document is empty, expired, or never existed.</p>
<h1>404</h1>
<div class="notification-area">
{{> notifications}}
</div>
<p>This document is invalid, expired, never existed or was deleted.</p>
</div>
{{/if}}
{{/if}}
<hr>
<div class="text-center">
{{#unless currentUser}}
<a href="/login">Log in</a> to edit and delete your documents<br>
<p><a href="/login">Log in</a> to edit and delete your documents<br></p>
{{/unless}}
{{#if owned}}
<p>This document is <b>yours</b>.</p>
{{/if}}
{{#if valid}}
<div class="btn-group" id="tools">
{{#if currentUser}}
<button id="edit-doc" class="btn btn-primary" {{canEdit}}>
{{#if currentUser}}{{#if owned}}
<button id="edit-doc" class="btn btn-primary">
<i class="fa fa-edit"></i> Edit</button>
{{/if}}
{{/if}}{{/if}}
{{#if source}}
<button id="src-doc" class="btn btn-success">
<i class="fa fa-book"></i> View Document</button>
@ -71,12 +81,13 @@
<button id="src-doc" class="btn btn-success">
<i class="fa fa-code"></i> View Source</button>
{{/if}}
{{#if currentUser}}
{{#if currentUser}}{{#if owned}}
<button id="del-doc" class="btn btn-danger">
<i class="fa fa-trash"></i> Delete</button>
{{/if}}
{{/if}}{{/if}}
</div>
<br>Powered by <a href="/">MarkCloud</a>
<br>
{{/if}}Powered by <a href="/">MarkCloud</a>
</div>
</template>
@ -132,3 +143,9 @@
<div class="text-center">Need an account? <a href="/signup">Sign Up!</a></div>
</div>
</template>
<template name="list">
{{#each documents}}
<a href="/d/{{_id}}">{{title}}</a>
{{/each}}
</template>

View File

@ -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