Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Branch: master
Fetching contributors…

Cannot retrieve contributors at this time

184 lines (177 sloc) 8.195 kB
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>mailbag!</title>
<link rel="stylesheet" href="style/main.css">
<script src="lib/jquery-1.4.2.min.js"></script>
<script src="lib/sammy-0.6.0.min.js"></script>
<script src="/_utils/script/jquery.couch.js"></script>
<script src="lib/jquery.mustache.js"></script>
<script src="lib/jquery.couch.app.js"></script>
<script src="lib/html-sanitizer.js"></script>
</head>
<body>
<script>
$.couch.app(function(app) {
var db = app.db;
console.log(app.db);
var templates = app.ddoc.templates;
function show_bag_links()
{
db.view('mailbag/mail-items', {
group_level: 1,
success: function(data)
{
$('#baglist').html('');
for (var b in data.rows)
{
console.log(data.rows[b]);
$('#baglist').append('<a href="#/bag/' + data.rows[b].key[0] + '/Z">'
+ data.rows[b].key[0] + ' (' + data.rows[b].value + ')</a> ');
}
}
});
}
function render_message(data, hints) {
var msg = {};
var attpath = '/' + db.name + '/' + data._id + '/';
msg.subject = data.subject;
msg.from = data.from;
msg.date = data.headers["Date"];
msg.hints = hints;
msg.bags = data.bags;
if(data._attachments) {
msg.has_attach = "-"; //annoyed that true won't work here.
msg.attachments = [];
for(attname in data._attachments)
{
msg.attachments.push({
name: attname,
path: attpath + attname,
size: data._attachments.size
});
}
}
var parts = {};
for(var i in data.parts) {
var typetoks = data.parts[i].contentType.split(';');
parts[typetoks[0]] = data.parts[i];
}
if(parts['text/html']) {
msg.htmlbody = html_sanitize(parts['text/html'].content,
function(uri) {
if(uri.substring(0,4) == "cid:" && data.attach_id_map)
{
uri = uri.substring(4);
if(data.attach_id_map[uri]) {
return attpath + data.attach_id_map[uri];
}
}
return uri;});
} else if(parts['text/plain']) {
msg.plainbody = parts['text/plain'].content;
}
$('#mainpane').append($.mustache(templates.viewmessage, msg, null));
}
var sammyapp = $.sammy(function() {
this.get('#/', function() { this.redirect('#/bag/all-mail/Z'); });
this.get('#/bag/:bag/:key', function(ctx) {
var start = ctx.params["key"];
var skips = 0;
var amt = 15;
var bag = ctx.params["bag"];
$('#titlebar').html('mailbag! - in bag: <span id="bagname">' + bag + '</span> <div id="baglist"></div>');
$('#bagname').click(show_bag_links);
if(start != "Z") {
skips = 1;
}
db.view('mailbag/mail-items', {
limit: amt,
skip: skips,
reduce: false,
startkey: [bag, start],
endkey: [bag, false],
descending: true,
success: function(data)
{
console.log(data);
$('#mainpane').html($.mustache(templates.messages, data, null));
if(data.rows.length == amt) {
$('#mainpane').append('<a class="next" href="#/bag/' + bag + '/' + data.rows[data.rows.length-1].key[1] + '">next</a>');
}
if(start != "Z") {
$('#mainpane').append('<a class="prev" href="javascript: history.go(-1);">prev</a>');
}
}
});
});
this.get('#/message/:id', function(ctx) {
db.openDoc(ctx.params["id"], {success: function(data) {
var title = data.subject;
if(data.headers["Delivered-To"])
title = title + ' - (' + data.headers["Delivered-To"] + ')';
$('#titlebar').text('mailbag! - ' + title);
$('#mainpane').html('');
var refheader = data.headers["References"];
var refs = [];
if(refheader) {
refs = refheader.replace(/\r/g,"").replace(/ /g,"").split("\n");
}
refs.push(data.messageID);
console.log(refs);
db.view('mailbag/refs', {
keys: refs,
group: true,
success: function(others)
{
if(others.rows.length == 0)
{
render_message(data, {"class": "picked"});
return;
}
var max = 0;
var maxkey;
for(var o in others.rows) {
if(max < others.rows[o].value)
{
max = others.rows[o].value;
maxkey = others.rows[o].key;
}
}
db.view('mailbag/refs', {
reduce: false,
startkey: maxkey,
endkey: maxkey + 'Z',
include_docs: true,
success: function (msgs)
{
msgs.rows.sort(function (a, b){
return new Date(b.doc.headers["Date"]) -
new Date(a.doc.headers["Date"])
});
console.log(msgs);
for(var m in msgs.rows) {
if(msgs.rows[m].doc._id == ctx.params["id"])
{
render_message(msgs.rows[m].doc,
{"class": "picked"});
} else {
render_message(msgs.rows[m].doc);
}
}
window.scrollTo(0,$('.picked').offset().top - 20);
}
});
}});
}});
});
});
$(function(){sammyapp.run('#/');});
});
</script>
<div id="titlebar">mailbag!</div>
<div id="mainpane">
</div>
</body>
</html>
Jump to Line
Something went wrong with that request. Please try again.