Skip to content

Commit

Permalink
removing extra code from edit and post pages
Browse files Browse the repository at this point in the history
  • Loading branch information
jchris committed Mar 12, 2010
1 parent b4d92f1 commit dcd2011
Show file tree
Hide file tree
Showing 16 changed files with 160 additions and 375 deletions.
66 changes: 1 addition & 65 deletions _attachments/blog.js
@@ -1,77 +1,13 @@
function Blog(app) {
// var converter = new Showdown.converter();

function stripScripts(s) {
return s && s.replace(/<script(.|\n)*?>/g, '');
};

function safe(s,cap) {
if (cap && cap < s.length) {
s = s.substring(0,cap);
s += '...';
}
return s && s.replace(/<(.|\n)*?>/g, '');
};

function author(author) {
if (!author) return '';
if (!author.url) return '<span class="author">by ' + safe(author.name) + '</span>';
return '<span class="author">by <a href="'+author.url+'">'
+ safe(author.name) + '</a></span>';
};


function niceDate(date) {
return '<span class="date">'
+ app.prettyDate(date)
+'</span>';
};

this.postToEntry = function(post, id) {
return '<li><h3><a href="'+app.showPath('post',id)+'">'
+ safe(post.title)
+ '</a></h3>'
+ niceDate(post.created_at)
+ '<div class="body">'
+ post.summary
+ '</div>'
+ '</li>';
}

this.postToHTML = function(post) {
return niceDate(post.created_at)
+ '<div class="body">'
+ stripScripts(post.html)
+ '</div>';
};

this.commentListing = function(c) {
return '<li><h4>'
+ author(c.commenter) + ', '
+ app.prettyDate(c.created_at)
+ '</h4>'
+ '<img class="gravatar" src="http://www.gravatar.com/avatar/'+c.commenter.gravatar+'.jpg?s=40&d=identicon"/>'
+'<p>'+ stripScripts(c.html)
+ '</p>'
+ '</li>';
};

this.makeTwitterLinks = function(text) {
return text.replace(/ @([^ .,]+)/g, ' <a href="http://twitter.com/$1">@$1</a>');
};

this.formatBody = function(body, format) {
if (format == 'markdown') {
var converter = new Showdown.converter();
return this.makeTwitterLinks(converter.makeHtml(body));
return converter.makeHtml(body);
} else if (format == 'textile') {
return superTextile(body);
} else {
return body;
}
}

this.editing = function(docid) {
$('h1').html('Editing <a href="'+app.showPath('post',docid)+'">'+docid+'</a>');
};
};
21 changes: 1 addition & 20 deletions _attachments/script/app.js
@@ -1,26 +1,7 @@
$.couch.app(function(app) {
// $('.date').prettyDate();
$('.date').prettyDate();

$("#account").evently($.extend(true,
app.ddoc.vendor.couchapp.evently.account,
app.ddoc.evently.account), app);


// todo browse by tags
app.design.view("tags",{
descending: true,
group: true,
success: function(json) {
var total = 0;
for(var idx in json.rows) {
total += json.rows[idx].value;
}
var tags = [];
for(var idx in json.rows) {
var percent = Math.ceil(Math.pow((json.rows[idx].value / total), 0.25) * 200);
tags.push('<span style="font-size:'+percent+'%;">' + json.rows[idx].key + '</span>');
}
$("#tags-front").append(tags.join(", "));
}
});
});
13 changes: 10 additions & 3 deletions evently/account/loggedIn/data.js
@@ -1,10 +1,17 @@
function(e, r) {
var app = $$(this).app;
var path = app.require("vendor/couchapp/commonjs/path").init(app.req);
return {
var data = {
name : r.userCtx.name,
uri_name : encodeURIComponent(r.userCtx.name),
auth_db : encodeURIComponent(r.info.authentication_db),
newPostPath : path.show("edit")
auth_db : encodeURIComponent(r.info.authentication_db)
};
if (app.req.path.indexOf("post-page") == -1) {
data.postPath = path.show("edit")+"/";
data.postMessage = "New post.";
} else {
data.postPath = path.show("edit", app.req.query.startkey[0]);
data.postMessage = "Edit this post.";
}
return data;
}
2 changes: 1 addition & 1 deletion evently/account/loggedIn/mustache.html
@@ -1,5 +1,5 @@
<span>Welcome
<a target="_new" href="/_utils/document.html?{{auth_db}}/org.couchdb.user%3A{{uri_name}}">{{name}}</a>!
<a href="#logout">Logout?</a>
<a href="{{newPostPath}}">New post.</a>
<a href="{{postPath}}">{{postMessage}}</a>
</span>
8 changes: 5 additions & 3 deletions lists/index.js
Expand Up @@ -16,10 +16,12 @@ function(head, req) {
var key = "";
// render the html head using a template
var stash = {
title : ddoc.blog.title,
header : {
index : indexPath,
blogName : ddoc.blog.title
},
feedPath : feedPath,
newPostPath : path.show("edit"),
index : indexPath,
assets : path.asset(),
posts : List.withRows(function(row) {
var post = row.value;
Expand All @@ -38,7 +40,7 @@ function(head, req) {
"10" : path.limit(10),
"25" : path.limit(25)
};
return Mustache.to_html(ddoc.templates.index, stash);
return Mustache.to_html(ddoc.templates.index, stash, ddoc.templates.partials, send);
});

// if the client requests an atom feed and not html,
Expand Down
8 changes: 5 additions & 3 deletions lists/post.js
Expand Up @@ -13,11 +13,13 @@ function(head, req) {
throw("not a post");
} else {
var stash = {
header : {
index : indexPath,
blogName : ddoc.blog.title
},
title : post.title,
index : indexPath,
date : post.created_at,
html : post.html,
blogName : ddoc.blog.title,
comments : List.withRows(function(row) {
var v = row.value;
if (v.type != "comment") {
Expand All @@ -33,7 +35,7 @@ function(head, req) {
};
})
};
return Mustache.to_html(ddoc.templates.postc, stash, null, send);
return Mustache.to_html(ddoc.templates.post, stash, ddoc.templates.partials, send);
}
});
}
13 changes: 8 additions & 5 deletions shows/edit.js
Expand Up @@ -4,10 +4,13 @@ function(doc, req) {
var path = require("vendor/couchapp/commonjs/path").init(req);

return Mustache.to_html(ddoc.templates.edit, {
header : {
index : path.list('index','recent-posts',{descending:true,limit:5}),
blogName : ddoc.blog.title
},
doc : doc,
docid : JSON.stringify((doc && doc._id) || null),
blog : ddoc.blog,
assets : path.asset(),
index : path.list('index','recent-posts',{descending:true,limit:8})
});
docid : JSON.stringify((doc && doc._id) || null),
pageTitle : doc ? "Edit: "+doc.title : "Create a new post",
assets : path.asset()
}, ddoc.templates.partials);
}
5 changes: 3 additions & 2 deletions sofa2.txt
@@ -1,10 +1,11 @@
edit page works
docform as it's own plugin
commonjs everything
commonjs require()
prune dead code
use profile and signup widgets for authors and (optionally) comments
fixup comments
// make the html in the view, not the client
atom comments firehose feed (per post also?)
docform as it's own plugin
ability to share link code between client and server


Expand Down
122 changes: 58 additions & 64 deletions templates/edit.html
Expand Up @@ -5,13 +5,11 @@
<link rel="stylesheet" href="{{ assets }}/screen.css" type="text/css">
</head>
<body>
<div id="header">
<h2><a href="{{ index }}">{{ blog.title }}</a></h2>
</div>
{{>header}}
<div id="content">
<!-- form to create a post -->
<form id="new-post" action="new.html" method="post">
<h1>Create a new post</h1>
<h1>{{pageTitle}}</h1>
<p><label>Title</label>
<input type="text" size="50" name="title" value=""></p>
<p><label for="body">Body</label>
Expand All @@ -31,71 +29,70 @@ <h1>Create a new post</h1>
<script src="/_utils/script/json2.js"></script>
<script src="/_utils/script/jquery.js"></script>
<script src="/_utils/script/jquery.couch.js"></script>
<script src="/_utils/script/jquery.cookies.js"></script>
<script src="../../vendor/couchapp/jquery.couchapp.js"></script>
<script src="../../vendor/couchapp/jquery.couchapp.docform.js"></script>
<script src="../../vendor/couchapp/jquery.prettydate.js"></script>
<script src="../../vendor/couchapp/jquery.couch.app.js"></script>
<script src="../../vendor/couchapp/jquery.couch.app.util.js"></script>
<script src="../../vendor/couchapp/jquery.evently.js"></script>
<script src="../../vendor/couchapp/jquery.mustache.js"></script>
<script src="../../jquery.scrollTo.js"></script>
<script src="../../vendor/couchapp/jquery.couchapp.js"></script>
<script src="../../blog.js"></script>
<script src="../../showdown.js"></script>
<script src="../../textile.js"></script>
<script src="../../script/app.js"></script>
<script type="text/javascript" charset="utf-8">
$.CouchApp(function(app) {
app.session(function(userCtx) {
// w00t, we're logged in (according to the cookie)
$("#header").prepend('<span id="login">'+userCtx.name+'</span>');
// setup CouchApp document/form system, adding app-specific callbacks
// rename docForm?
var B = new Blog(app);
$.couch.app(function(app) {
var path = app.require("vendor/couchapp/commonjs/path").init(app.req);
// thin controller - move to B?

var postForm = app.docForm("form#new-post", {
id : {{ docid }},
fields : ["title", "body", "tags"],
template : {
type : "post",
format : "markdown",
author : userCtx.name
},
onLoad : function(doc) {
if (doc._id) {
B.editing(doc._id);
$('h1').html('Editing <a href="../post/'+doc._id+'">'+doc._id+'</a>');
$('#preview').before('<input type="button" id="delete" value="Delete Post"/> ');
$("#delete").click(function() {
postForm.deleteDoc({
success: function(resp) {
$("h1").text("Deleted "+resp.id);
$('form#new-post input').attr('disabled', true);
$("#account").evently({
loggedIn : function(e,r) {
var userCtx = r.userCtx;
var postForm = app.docForm("form#new-post", {
id : {{ docid }},
fields : ["title", "body", "tags"],
template : {
type : "post",
format : "markdown",
author : userCtx.name
},
onLoad : function(doc) {
if (doc._id) {
$('h1').html('Editing <a href="'+path.show('post',doc._id)+'">'+doc._id+'</a>');
$('#preview').before('<input type="button" id="delete" value="Delete Post"/> ');
$("#delete").click(function() {
postForm.deleteDoc({
success: function(resp) {
$("h1").text("Deleted "+resp.id);
$('form#new-post input').attr('disabled', true);
}
});
return false;
});
}
$('label[for=body]').append(' <em>with '+(doc.format||'html')+'</em>');
},
beforeSave : function(doc) {
doc.html = B.formatBody(doc.body, doc.format);
if (!doc.created_at) {
doc.created_at = new Date();
}
if (!doc.slug) {
doc.slug = app.slugifyString(doc.title);
doc._id = doc.slug;
}
if(doc.tags) {
doc.tags = doc.tags.split(",");
for(var idx in doc.tags) {
doc.tags[idx] = $.trim(doc.tags[idx]);
}
});
return false;
});
}
$('label[for=body]').append(' <em>with '+(doc.format||'html')+'</em>');
},
beforeSave : function(doc) {
doc.html = B.formatBody(doc.body, doc.format);
if (!doc.created_at) {
doc.created_at = new Date();
}
if (!doc.slug) {
doc.slug = app.slugifyString(doc.title);
doc._id = doc.slug;
}
if(doc.tags) {
doc.tags = doc.tags.split(",");
for(var idx in doc.tags) {
doc.tags[idx] = $.trim(doc.tags[idx]);
}
},
success : function(resp) {
$("#saved").text("Saved _rev: "+resp.rev).fadeIn(500).fadeOut(3000);
B.editing(resp.id);
}
}
},
success : function(resp) {
$("#saved").text("Saved _rev: "+resp.rev).fadeIn(500).fadeOut(3000);
B.editing(resp.id);
});
}
});
})



$("#preview").click(function() {
var doc = postForm.localDoc();
Expand All @@ -104,9 +101,6 @@ <h1>Create a new post</h1>
// scroll down
$('body').scrollTo('#show-preview', {duration: 500});
});
}, function() {
alert("Please login before refreshing the page to edit.");
});
});
</script>
</html>

0 comments on commit dcd2011

Please sign in to comment.