/TweetBox.js

https://gitlab.com/alidzapp/TweetSplitter · JavaScript · 66 lines · 58 code · 8 blank · 0 comment · 3 complexity · 3ac2d82f98ef7c306ceb4c21fc288d5c MD5 · raw file

  1. function TweetBox(id, index){
  2. var self = this;
  3. this.idNum = id;
  4. this.domElement = document.createElement("textarea");
  5. this.domElement.disabled = true;
  6. this.domElement.id = "tweet"+idNum;
  7. this.domElement.className = "form-control";
  8. this.domElement.rows = "3"
  9. this.index = index;
  10. tweetBoxes.splice(this.index, 0, this);
  11. this.humanIndex = this.index + 1;
  12. this.currentCountTag = " (" + this.humanIndex + "/" + tweetBoxes.length + ")"
  13. this.domElement.value += this.currentCountTag;
  14. $("#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>");
  15. document.getElementById("tweetColumn" + this.idNum + "").appendChild(this.domElement);
  16. this.tweetWrapper = document.getElementById('tweets'+this.idNum);
  17. this.charLength = function (){
  18. return this.domElement.value.length;
  19. }
  20. this.checkMaxLength = function() {
  21. if(self.charLength() === 140){
  22. idNum++;
  23. var index = tweetBoxes.indexOf(self)+1;
  24. var tweetbox = new TweetBox(idNum, index);
  25. var i = 1;
  26. $.each(tweetBoxes, function(){
  27. $('#wrapper').append(this.tweetWrapper);
  28. this.newCountTag = " (" + i + "/" + tweetBoxes.length + ")";
  29. this.domElement.value = this.domElement.value.replace(this.currentCountTag,this.newCountTag);
  30. this.currentCountTag = this.newCountTag;
  31. i++;
  32. });
  33. tweetbox.domElement.focus();
  34. self.countURLs();
  35. }
  36. }
  37. function onInput(){
  38. var urls = self.countURLs();
  39. var urlChars = 0;
  40. var adjustment= 0;
  41. if(urls){
  42. for (var i = urls.length - 1; i >= 0; i--) {
  43. urlChars += urls[i].length;
  44. };
  45. adjustment = urls.length*23;
  46. }
  47. var chars = self.charLength() - urlChars + adjustment;
  48. document.getElementById('count' + self.idNum).innerHTML = chars;
  49. self.checkMaxLength();
  50. self.isEmpty();
  51. }
  52. this.countURLs = function(){
  53. var str = this.domElement.value;
  54. var re = /(^|\s)((https?:\/\/)?[\w-]+(\.[\w-]+)+\.?(:\d+)?(\/\S*)?)/gi;
  55. var res = str.match(re);
  56. return res;
  57. }
  58. }