-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTweetBox.js
More file actions
66 lines (58 loc) · 2.51 KB
/
TweetBox.js
File metadata and controls
66 lines (58 loc) · 2.51 KB
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
function TweetBox(id, index){
var self = this;
this.idNum = id;
this.domElement = document.createElement("textarea");
this.domElement.disabled = true;
this.domElement.id = "tweet"+idNum;
this.domElement.className = "form-control";
this.domElement.rows = "3"
this.index = index;
tweetBoxes.splice(this.index, 0, this);
this.humanIndex = this.index + 1;
this.currentCountTag = " (" + this.humanIndex + "/" + tweetBoxes.length + ")"
this.domElement.value += this.currentCountTag;
$("#wrapper").append("<div id='tweets" + this.idNum + "' class='row clearfix'><div id = 'tweetColumn" + this.idNum + "' class='col-xs-8 column'></div><div id= 'buttons" + this.idNum + "' class='col-xs-4 column'><div class='row clearfix'><a class='btn btn-primary' id='custom-tweet-button" + this.idNum + "' >Tweet</a></div><div class='row clearfix'><label id='count" + this.idNum + "' for='tweet'></label></div><script> document.getElementById('custom-tweet-button" + this.idNum + "').addEventListener('click', function (el) { el.target.href = ''; var tweet = encodeURIComponent(($('#tweet" + this.idNum + "').val())); el.target.href = href='http://twitter.com/intent/tweet?text=' + tweet});</script></div></div>");
document.getElementById("tweetColumn" + this.idNum + "").appendChild(this.domElement);
this.tweetWrapper = document.getElementById('tweets'+this.idNum);
this.charLength = function (){
return this.domElement.value.length;
}
this.checkMaxLength = function() {
if(self.charLength() === 140){
idNum++;
var index = tweetBoxes.indexOf(self)+1;
var tweetbox = new TweetBox(idNum, index);
var i = 1;
$.each(tweetBoxes, function(){
$('#wrapper').append(this.tweetWrapper);
this.newCountTag = " (" + i + "/" + tweetBoxes.length + ")";
this.domElement.value = this.domElement.value.replace(this.currentCountTag,this.newCountTag);
this.currentCountTag = this.newCountTag;
i++;
});
tweetbox.domElement.focus();
self.countURLs();
}
}
function onInput(){
var urls = self.countURLs();
var urlChars = 0;
var adjustment= 0;
if(urls){
for (var i = urls.length - 1; i >= 0; i--) {
urlChars += urls[i].length;
};
adjustment = urls.length*23;
}
var chars = self.charLength() - urlChars + adjustment;
document.getElementById('count' + self.idNum).innerHTML = chars;
self.checkMaxLength();
self.isEmpty();
}
this.countURLs = function(){
var str = this.domElement.value;
var re = /(^|\s)((https?:\/\/)?[\w-]+(\.[\w-]+)+\.?(:\d+)?(\/\S*)?)/gi;
var res = str.match(re);
return res;
}
}