Skip to content

Commit

Permalink
move tasks to evently files
Browse files Browse the repository at this point in the history
  • Loading branch information
jchris committed Feb 12, 2010
1 parent 2decdf2 commit 67d96a6
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 216 deletions.
2 changes: 1 addition & 1 deletion _attachments/app.js
Expand Up @@ -27,7 +27,7 @@ $.couch.app(function(app) {
});

$.log = function() {
console.log(arguments);
// console.log(arguments);
};

// todo move to a plugin somewhere
Expand Down
257 changes: 54 additions & 203 deletions _attachments/script/tasks.js
@@ -1,213 +1,64 @@
$.couch.app(function(app) {



// now we need to do magic to make more kinds of task displays
// recent tags mentions users
var tasks = app.ddoc.evently.tasks;
// todo make a declarative trigger for this pattern
tasks.tags = $.extend(true, {}, tasks.recent, tasks.tags);
tasks.mentions = $.extend(true, {}, tasks.recent, tasks.mentions);
tasks.users = $.extend(true, {}, tasks.recent, tasks.users);

tasks.tags = $.extend(true, {}, tasks.recent, {
path : "/tags/:tag",
selectors : {
ul : {
_changes : {
query : function() {
$.log("tags query")
$.log(arguments)
return {
view : "tag-cloud",
limit : 25,
// startkey : [params.tag, {}],
// endkey : [params.tag],
reduce : false,
descending : true,
type : "newRows"
}
}
}
}
}
});

// $.log(tasks)
$.log(tasks)
$("#tasks").evently(tasks, app);
$.pathbinder.begin("/");

return;

function tasksHandler(path, query) {
// this is a kind of changes feed handler
// that works with at the row level
// with views where the new changes we care about
// will always appear at one end

// type can be view, newRows, document, or info
// if there is .query, default is view

return {
path : path,
mustache : app.ddoc.templates.tasks,
selectors : function(e, params) {

var task_changes = {
mustache : app.ddoc.templates.task,
render : "prepend",
// we want this query to be set during the event
// that triggered the parent to be created,
// not rebuilt each time based on changes as they come in
query : query(e, params),
data : function(r) {
// $.log("task data", arguments);
var v = r.value;
return {
avatar_url : v.authorProfile && v.authorProfile.gravatar_url,
body : $.linkify($.mustache.escape(r.value.body)),
name : v.authorProfile && v.authorProfile.name,
name_uri : v.authorProfile && encodeURIComponent(v.authorProfile.name),
futon_path : "/_utils/document.html?"+[app.db.name,r.id].map(encodeURIComponent).join('/'),
id : encodeURIComponent(r.id),
state : v.state
// todo this should be handled in dom-land / evently
// we can use this id as a handle for automatically updating non-top rows
// based on changes from documents
};
},
selectors : {
'a[href=#done]' : {
click : function() {
var li = $(this).parents("li");
var task_id = li.attr("data-id");
app.db.openDoc(task_id, {
success : function(doc) {
doc.state = "done";
doc.done_by = $("#account").attr("data-name");
doc.done_at = new Date();
app.db.saveDoc(doc, {
success : function() {
li.addClass("done");
li.slideUp("slow");
}
});
}
});
return false;
}
},
'a[href=#reply]' : {
click : function() {
var li = $(this).parents("li");
$("div.reply",li).evently(reply);
return false;
}
}
}
};

return {
ul : {
_changes : task_changes
}
}
}
};
}

var tasks = {
recent : tasksHandler("/", function(e, params) {
return {
view : "recent-tasks",
limit : 25,
descending : true,
type : "newRows"
}
}),
tags : tasksHandler("/tags/:tag", function(e, params) {
return {
view : "tag-cloud",
limit : 25,
startkey : [params.tag, {}],
endkey : [params.tag],
reduce : false,
descending : true,
type : "newRows"
};
}),
users : tasksHandler("/users/:name", function(e, params) {
return {
view : "users-tasks",
limit : 25,
startkey : [params.name, {}],
endkey : [params.name],
descending : true,
type : "newRows"
};
}),
mentions : tasksHandler("/mentions/:name", function(e, params) {
// $.log("mentions query", e, params);
return {
view : "user-cloud",
limit : 25,
startkey : [params.name, {}],
endkey : [params.name],
descending : true,
reduce : false,
type : "newRows"
};
})
};

// $("#tasks").evently(tasks, app);
// $.pathbinder.begin("/");


var reply = {
_init: {
mustache : app.ddoc.templates.create_reply,
selectors : {
form : {
submit : function() {
var texta = $("textarea[name=body]", this);
var li = $(this).parents("li");
var task_id = li.attr("data-id");
// todo extract to model layer?
var newReply = {
reply_to : task_id,
body : texta.val(),
type : "reply",
created_at : new Date(),
authorProfile : userProfile
};
app.db.saveDoc(newReply, {
success : function() {
texta.val('');
}
});
return false;
}
}
}
}
};

var replies = {
_init: {
mustache : app.ddoc.templates.replies,
data : function(e, rows) {
return {
rows : rows.map(function(r) {
// todo eliminate duplication here
var v = r.value;
return {
avatar_url : v.authorProfile && v.authorProfile.gravatar_url,
body : $.linkify($.mustache.escape(r.value.body)),
name : v.authorProfile && v.authorProfile.name,
name_uri : v.authorProfile && encodeURIComponent(v.authorProfile.name),
id : r.id
}
})
}
}
}
};
// var reply = {
// _init: {
// mustache : app.ddoc.templates.create_reply,
// selectors : {
// form : {
// submit : function() {
// var texta = $("textarea[name=body]", this);
// var li = $(this).parents("li");
// var task_id = li.attr("data-id");
// // todo extract to model layer?
// var newReply = {
// reply_to : task_id,
// body : texta.val(),
// type : "reply",
// created_at : new Date(),
// authorProfile : userProfile
// };
// app.db.saveDoc(newReply, {
// success : function() {
// texta.val('');
// }
// });
// return false;
// }
// }
// }
// }
// };
//
// var replies = {
// _init: {
// mustache : app.ddoc.templates.replies,
// data : function(e, rows) {
// return {
// rows : rows.map(function(r) {
// // todo eliminate duplication here
// var v = r.value;
// return {
// avatar_url : v.authorProfile && v.authorProfile.gravatar_url,
// body : $.linkify($.mustache.escape(r.value.body)),
// name : v.authorProfile && v.authorProfile.name,
// name_uri : v.authorProfile && encodeURIComponent(v.authorProfile.name),
// id : r.id
// }
// })
// }
// }
// }
// };

});
1 change: 1 addition & 0 deletions evently/tasks/mentions/path.txt
@@ -0,0 +1 @@
/mentions/:name
12 changes: 12 additions & 0 deletions evently/tasks/mentions/selectors/ul/_changes/query.js
@@ -0,0 +1,12 @@
function(e) {
var params = e.data.args[1];
return {
view : "user-cloud",
limit : 25,
startkey : [params.name, {}],
endkey : [params.name],
reduce : false,
descending : true,
type : "newRows"
}
}
1 change: 1 addition & 0 deletions evently/tasks/tags/path.txt
@@ -0,0 +1 @@
/tags/:tag
12 changes: 12 additions & 0 deletions evently/tasks/tags/selectors/ul/_changes/query.js
@@ -0,0 +1,12 @@
function(e) {
var params = e.data.args[1];
return {
view : "tag-cloud",
limit : 25,
startkey : [params.tag, {}],
endkey : [params.tag],
reduce : false,
descending : true,
type : "newRows"
}
}
1 change: 1 addition & 0 deletions evently/tasks/users/path.txt
@@ -0,0 +1 @@
/users/:name
11 changes: 11 additions & 0 deletions evently/tasks/users/selectors/ul/_changes/query.js
@@ -0,0 +1,11 @@
function(e) {
var params = e.data.args[1];
return {
view : "users-tasks",
limit : 25,
startkey : [params.name, {}],
endkey : [params.name],
descending : true,
type : "newRows"
}
}

0 comments on commit 67d96a6

Please sign in to comment.