Skip to content

Commit

Permalink
refactoring reply/share
Browse files Browse the repository at this point in the history
  • Loading branch information
dominic committed Mar 24, 2011
1 parent 9a2c0b2 commit 890d520
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 43 deletions.
56 changes: 29 additions & 27 deletions public/js/update.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,39 @@
(function() {
$(document).ready(function() {
var update, updateCounter;
var focusTextArea, shareText, textarea, updateCounter, update_field;
$("html").removeClass("no-js").addClass("js");
update = $("#update-textarea");
textarea = $("#update-form textarea");
update_field = $("#update-form #update-referral");
updateCounter = function() {
$("#update-count").text((140 - update.val().length) + "/140");
return $("#update-info").toggleClass("negative", update.val().length > 140);
$("#update-count").text((140 - textarea.val().length) + "/140");
return $("#update-info").toggleClass("negative", textarea.val().length > 140);
};
update.keypress(updateCounter).keyup(updateCounter);
return $("#update-form").submit(function() {
if (update.val().length <= 0 || update.val().length > 140) {
textarea.keypress(updateCounter).keyup(updateCounter);
$("#update-form").submit(function() {
if (textarea.val().length <= 0 || textarea.val().length > 140) {
return false;
}
});
shareText = function(update) {
return "RT @" + $(update).data("name") + ": " + $(update).find(".text").text().trim();
};
focusTextArea = function() {
textarea.keypress();
return textarea.focus();
};
return $(".update").each(function() {
var update;
update = $(this);
$(this).find(".reply").bind("click", function(ev) {
ev.preventDefault();
textarea.text("@" + $(update).data("name") + " ");
return focusTextArea();
});
return $(this).find(".share").bind("click", function(ev) {
ev.preventDefault();
textarea.text(shareText(update));
return focusTextArea();
});
});
});
}).call(this);

// TODO: MOVE TO COFFEE-SCRIPT FOO
function reply(username) {
var update = $("#update-textarea");
update.text("@" + username + " ");
update.keypress();
update.focus();
}

function share_text(username, update_id) {
var update_text = $("#update-" + update_id).text().trim();
return "RT @" + username + ": " + update_text;
}

function share(username, update_id) {
var update = $("#update-textarea");
update.text(share_text(username, update_id));
update.keypress();
update.focus();
}
34 changes: 29 additions & 5 deletions src/update.coffee
Original file line number Diff line number Diff line change
@@ -1,12 +1,36 @@
$(document).ready ->
$("html").removeClass("no-js").addClass("js")
update = $("#update-textarea")

textarea = $("#update-form textarea")
update_field = $("#update-form #update-referral")

updateCounter = ->
$("#update-count").text((140 - update.val().length) + "/140")
$("#update-info").toggleClass "negative", update.val().length > 140
$("#update-count").text((140 - textarea.val().length) + "/140")
$("#update-info").toggleClass "negative", textarea.val().length > 140

update.keypress(updateCounter).keyup(updateCounter)
textarea.keypress(updateCounter).keyup(updateCounter)

$("#update-form").submit ->
false if update.val().length <= 0 || update.val().length > 140
false if textarea.val().length <= 0 || textarea.val().length > 140

shareText = (update) ->
"RT @" + $(update).data("name") + ": " + $(update).find(".text").text().trim();

focusTextArea = ->
textarea.keypress()
textarea.focus()

$(".update").each ->
update = $(this)

$(this).find(".reply").bind "click", (ev) ->
ev.preventDefault();
textarea.text("@" + $(update).data("name") + " ")
focusTextArea()

$(this).find(".share").bind "click", (ev) ->
ev.preventDefault();
textarea.text(shareText(update))
focusTextArea()


17 changes: 6 additions & 11 deletions views/_updates.haml
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
- updates.each do |update|
- mentioned = current_user.nil? ? false : update.mentioned?(current_user.username)
.update{:class => mentioned ? "mention" : ""}
.update{:class => mentioned ? "mention" : "", :id => "update-#{update.id}", "data-id" => update.id, "data-name" => update.author.username}
.avatar
%a{:href => update.author.url}
%img{:alt => "avatar", :src => update.author.avatar_url}/
.content
.name= "#{update.author.name} (#{update.author.username})"
.text{:id => "update-#{update.id}"}
.text
!= update.to_html
.date
%a{:href => update.url}= update.created_at.ago_in_words
.reply
.links
-# when @timeline is true, this is a list on the user's page
-unless current_user.nil? or (current_user.author.id == update.author.id)
-unless @timeline.nil?
%a{:href => "javascript:share('#{update.author.username}', '#{update.id}')"} share
|
%a{:href => "javascript:reply('#{update.author.username}')"} reply
-else
%a{:href => "/?share=#{update.id}"} share
|
%a{:href => "/?reply=#{update.author.username}"} reply
%a.share{:href => "/?share=#{update.id}"} share
|
%a.reply{:href => "/?reply=#{update.id}"} reply
- if updates.empty?
.empty
There's no updates here yet

0 comments on commit 890d520

Please sign in to comment.