forked from hotsh/rstat.us
/
update.js
78 lines (78 loc) · 2.65 KB
/
update.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
(function() {
var MAX_LENGTH;
MAX_LENGTH = 140;
$(document).ready(function() {
var focusTextArea, recordTickyboxChange, shareText, textarea, updateCounter, updateTickyboxes, update_field, userTickiedBox;
$("html").removeClass("no-js").addClass("js");
textarea = $("#update-form textarea");
update_field = $("#update-form #update-referral");
userTickiedBox = false;
updateCounter = function() {
var countSpan, remainingLength;
remainingLength = MAX_LENGTH - textarea.val().length;
countSpan = $("#update-count .update-count").first();
if (countSpan.length) {
countSpan.text(remainingLength);
} else {
$("#update-count").append('<span class="update-count">' + remainingLength + '</span>/' + MAX_LENGTH);
}
return $("#update-info").toggleClass("negative", remainingLength < 0);
};
textarea.keypress(updateCounter).keyup(updateCounter);
updateTickyboxes = function() {
var enabled, firstLetter;
if (userTickiedBox) {
return;
}
firstLetter = "";
if (textarea.val() !== "") {
firstLetter = textarea.val()[0];
}
enabled = true;
if (firstLetter === "@") {
enabled = false;
}
if ($("#tweet").length > 0) {
$("#tweet").attr('checked', enabled);
}
};
textarea.keypress(updateTickyboxes).keyup(updateTickyboxes);
recordTickyboxChange = function() {
return userTickiedBox = true;
};
if ($("#tweet").length > 0) {
$("#tweet").change(recordTickyboxChange);
}
$("#update-form").submit(function() {
if (textarea.val().length <= 0 || textarea.val().length > MAX_LENGTH) {
return false;
}
});
shareText = function(update) {
return "RS @" + $(update).data("name") + ": " + $(update).find(".entry-content").text().trim();
};
focusTextArea = function(update) {
var length;
$(update_field).attr("value", $(update).data("id"));
length = textarea.text().length;
textarea.keypress();
textarea[0].setSelectionRange(length, length);
textarea.focus();
return window.scrollTo(0, $(textarea).position().top);
};
return $(".has-update-form .update").each(function() {
var update;
update = $(this);
$(this).find(".reply").bind("click", function(ev) {
ev.preventDefault();
textarea.text("@" + $(update).data("name") + " ");
return focusTextArea(update);
});
return $(this).find(".share").bind("click", function(ev) {
ev.preventDefault();
textarea.text(shareText(update));
return focusTextArea(update);
});
});
});
}).call(this);