From 67d96a6bac0f7e741de9ff9be8b4702f70d60224 Mon Sep 17 00:00:00 2001 From: Chris Anderson Date: Thu, 11 Feb 2010 16:55:31 -0800 Subject: [PATCH] move tasks to evently files --- _attachments/app.js | 2 +- _attachments/script/tasks.js | 257 ++++-------------- evently/tasks/mentions/path.txt | 1 + .../mentions/selectors/ul/_changes/query.js | 12 + evently/tasks/tags/path.txt | 1 + .../tasks/tags/selectors/ul/_changes/query.js | 12 + evently/tasks/users/path.txt | 1 + .../users/selectors/ul/_changes/query.js | 11 + .../couchapp/_attachments/jquery.evently.js | 27 +- 9 files changed, 108 insertions(+), 216 deletions(-) create mode 100644 evently/tasks/mentions/path.txt create mode 100644 evently/tasks/mentions/selectors/ul/_changes/query.js create mode 100644 evently/tasks/tags/path.txt create mode 100644 evently/tasks/tags/selectors/ul/_changes/query.js create mode 100644 evently/tasks/users/path.txt create mode 100644 evently/tasks/users/selectors/ul/_changes/query.js diff --git a/_attachments/app.js b/_attachments/app.js index 06ffd09..f945c2a 100644 --- a/_attachments/app.js +++ b/_attachments/app.js @@ -27,7 +27,7 @@ $.couch.app(function(app) { }); $.log = function() { - console.log(arguments); + // console.log(arguments); }; // todo move to a plugin somewhere diff --git a/_attachments/script/tasks.js b/_attachments/script/tasks.js index c405073..72642ac 100644 --- a/_attachments/script/tasks.js +++ b/_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 + // } + // }) + // } + // } + // } + // }; }); diff --git a/evently/tasks/mentions/path.txt b/evently/tasks/mentions/path.txt new file mode 100644 index 0000000..c969065 --- /dev/null +++ b/evently/tasks/mentions/path.txt @@ -0,0 +1 @@ +/mentions/:name \ No newline at end of file diff --git a/evently/tasks/mentions/selectors/ul/_changes/query.js b/evently/tasks/mentions/selectors/ul/_changes/query.js new file mode 100644 index 0000000..3609e5a --- /dev/null +++ b/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" + } +} diff --git a/evently/tasks/tags/path.txt b/evently/tasks/tags/path.txt new file mode 100644 index 0000000..0b3705b --- /dev/null +++ b/evently/tasks/tags/path.txt @@ -0,0 +1 @@ +/tags/:tag \ No newline at end of file diff --git a/evently/tasks/tags/selectors/ul/_changes/query.js b/evently/tasks/tags/selectors/ul/_changes/query.js new file mode 100644 index 0000000..243b4a0 --- /dev/null +++ b/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" + } +} diff --git a/evently/tasks/users/path.txt b/evently/tasks/users/path.txt new file mode 100644 index 0000000..bc6fa91 --- /dev/null +++ b/evently/tasks/users/path.txt @@ -0,0 +1 @@ +/users/:name \ No newline at end of file diff --git a/evently/tasks/users/selectors/ul/_changes/query.js b/evently/tasks/users/selectors/ul/_changes/query.js new file mode 100644 index 0000000..cbe06eb --- /dev/null +++ b/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" + } +} diff --git a/vendor/couchapp/_attachments/jquery.evently.js b/vendor/couchapp/_attachments/jquery.evently.js index c366412..96fe83d 100644 --- a/vendor/couchapp/_attachments/jquery.evently.js +++ b/vendor/couchapp/_attachments/jquery.evently.js @@ -59,11 +59,14 @@ function $$(node) { $.evently = { connect : function(source, target, events) { - events.forEach(function(e) { - source.bind(e, function() { + events.forEach(function(ev) { + source.bind(ev, function() { var args = $.makeArray(arguments); + // remove the original event to keep from stacking args extra deep + // it would be nice if jquery had a way to pass the original + // event to the trigger method. args.shift(); - target.trigger(e, args); + target.trigger(ev, args); return false; }); }); @@ -72,18 +75,18 @@ function $$(node) { changesDBs : {} }; - $.fn.evently = function(events, app, init_args) { + $.fn.evently = function(events, app, args) { var elem = $(this); // store the app on the element for later use $$(elem).app = app; // setup the handlers onto elem forIn(events, function(name, h) { - eventlyHandler(elem, name, h); + eventlyHandler(elem, name, h, args); }); if (events._init) { - elem.trigger("_init"); + elem.trigger("_init", args); } if (app && events._changes) { @@ -97,31 +100,31 @@ function $$(node) { // eventlyHandler applies the user's handler (h) to the // elem, bound to trigger based on name. - function eventlyHandler(elem, name, h) { + function eventlyHandler(elem, name, h, args) { if (h.path) { elem.pathbinder(name, h.path); } var f = funViaString(h); if (typeof f == "function") { - elem.bind(name, f); + elem.bind(name, {args:args}, f); } else if (typeof f == "string") { - elem.bind(name, function() { + elem.bind(name, {args:args}, function() { $(this).trigger(f, arguments); return false; }); } else if ($.isArray(h)) { // handle arrays recursively for (var i=0; i < h.length; i++) { - eventlyHandler(elem, name, h[i]); + eventlyHandler(elem, name, h[i], args); }; } else { // an object is using the evently / mustache template system if (h.fun) { - elem.bind(name, funViaString(h.fun)); + elem.bind(name, {args:args}, funViaString(h.fun)); } // templates, selectors, etc are intepreted // when our named event is triggered. - elem.bind(name, function() { + elem.bind(name, {args:args}, function() { renderElement($(this), h, arguments); }); }