From b367d324fe8434e1c63d1c11e7a4e06198c6c21a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20Rog=C3=A9rio?= Date: Sun, 18 Oct 2015 13:48:12 -0200 Subject: [PATCH] Minor code improvements --- build/tweetlight.min.js | 4 ++-- src/tweetlight.js | 10 +--------- 2 files changed, 3 insertions(+), 11 deletions(-) diff --git a/build/tweetlight.min.js b/build/tweetlight.min.js index 15db185..dd1eeb8 100644 --- a/build/tweetlight.min.js +++ b/build/tweetlight.min.js @@ -1,2 +1,2 @@ -/*! tweetlight 4.1.1 | (c) 2015 Pedro Rogério | MIT License */ -!function(a,b){"use strict";"function"==typeof define&&define.amd?define("tweetlight",b):"object"==typeof exports?module.exports=b():a.Tweetlight=b()}(this,function(){"use strict";var a=function(b){return this&&this instanceof a?("string"==typeof b&&(b={key:b}),this.username=b.username,this.hashtag=b.hashtag,this.container=b.container,this.counter=b.counter,this.endpoint="../request.php",this.onComplete=b.onComplete||function(){},void this.fetch()):new a(b)};return a.init=function(b){return new a(b)},a.prototype={fetch:function(){var a=this.hashtag?"hashtag="+encodeURIComponent(this.hashtag):"username="+this.username,b=this.counter?"&count="+this.counter:"",c=this.endpoint+"?"+a+b;this.getJSON(c,this.loadTweets)},getJSON:function(a,b){var c=new XMLHttpRequest,d=this;c.open("GET",a,!0),c.onreadystatechange=function(){if(4===this.readyState){if(!(this.status>=200&&this.status<400))throw new Error(this.status+" - "+this.statusText);var a=JSON.parse(this.responseText);b.call(d,a)}},c.send(),c=null},loadTweets:function(a){var b=a.httpstatus;200===b?(a.statuses&&(a=a.statuses),this.displayTweets(a),this.onComplete()):this.displayError(a)},displayTweets:function(a){var b,c=document.querySelector(this.container),d="";for(b in a)a.hasOwnProperty(b)&&a[b].text&&(d+='
  • '+this.twitterLinks(a[b].text)+''+this.prettyDate(a[b].created_at)+"
  • ");c.innerHTML=d},displayError:function(a){var b=document.querySelector(this.container);b.innerHTML='
  • '+a.errors.message+"
  • "},prettyDate:function(a){var b,c,d,e,f,g,h=new Date,i=new Date(a);return(-1!==navigator.userAgent.indexOf("MSIE")||navigator.appVersion.indexOf("Trident/")>0)&&(i=Date.parse(a.replace(/( \+)/," UTC$1"))),b=h-i,c=1e3,d=60*c,e=60*d,f=24*e,g=7*f,isNaN(b)||0>b?"":2*c>b?"just now":d>b?Math.floor(b/c)+" seconds ago":2*d>b?"1 minute ago":e>b?Math.floor(b/d)+" minutes ago":2*e>b?"1 hour ago":f>b?Math.floor(b/e)+" hours ago":b>f&&2*f>b?"yesterday":365*f>b?Math.floor(b/f)+" days ago":"over a year ago"},twitterLinks:function(a){return a=a.replace(/(https?:\/\/)([\w\-:;?&=+.%#\/]+)/gi,'$2').replace(/(^|\W)@(\w+)/g,'$1@$2').replace(/(^|\W)#(\w+)/g,'$1#$2')}},a}); \ No newline at end of file +/*! tweetlight 4.1.2 | (c) 2015 Pedro Rogério | MIT License */ +!function(a,b){"use strict";"function"==typeof define&&define.amd?define("tweetlight",b):"object"==typeof exports?module.exports=b():a.Tweetlight=b()}(this,function(){"use strict";var a=function(b){return this&&this instanceof a?(this.username=b.username,this.hashtag=b.hashtag,this.container=b.container,this.counter=b.counter,this.endpoint="../request.php",this.onComplete=b.onComplete||function(){},void this.fetch()):new a(b)};return a.prototype={fetch:function(){var a=this.hashtag?"hashtag="+encodeURIComponent(this.hashtag):"username="+this.username,b=this.counter?"&count="+this.counter:"",c=this.endpoint+"?"+a+b;this.getJSON(c,this.loadTweets)},getJSON:function(a,b){var c=new XMLHttpRequest,d=this;c.open("GET",a,!0),c.onreadystatechange=function(){if(4===this.readyState){if(!(this.status>=200&&this.status<400))throw new Error(this.status+" - "+this.statusText);var a=JSON.parse(this.responseText);b.call(d,a)}},c.send(),c=null},loadTweets:function(a){var b=a.httpstatus;200===b?(a.statuses&&(a=a.statuses),this.displayTweets(a),this.onComplete()):this.displayError(a)},displayTweets:function(a){var b,c=document.querySelector(this.container),d="";for(b in a)a.hasOwnProperty(b)&&a[b].text&&(d+='
  • '+this.twitterLinks(a[b].text)+''+this.prettyDate(a[b].created_at)+"
  • ");c.innerHTML=d},displayError:function(a){var b=document.querySelector(this.container);b.innerHTML='
  • '+a.errors.message+"
  • "},prettyDate:function(a){var b,c,d,e,f,g,h=new Date,i=new Date(a);return(-1!==navigator.userAgent.indexOf("MSIE")||navigator.appVersion.indexOf("Trident/")>0)&&(i=Date.parse(a.replace(/( \+)/," UTC$1"))),b=h-i,c=1e3,d=60*c,e=60*d,f=24*e,g=7*f,isNaN(b)||0>b?"":2*c>b?"just now":d>b?Math.floor(b/c)+" seconds ago":2*d>b?"1 minute ago":e>b?Math.floor(b/d)+" minutes ago":2*e>b?"1 hour ago":f>b?Math.floor(b/e)+" hours ago":b>f&&2*f>b?"yesterday":365*f>b?Math.floor(b/f)+" days ago":"over a year ago"},twitterLinks:function(a){return a=a.replace(/(https?:\/\/)([\w\-:;?&=+.%#\/]+)/gi,'$2').replace(/(^|\W)@(\w+)/g,'$1@$2').replace(/(^|\W)#(\w+)/g,'$1#$2')}},a}); \ No newline at end of file diff --git a/src/tweetlight.js b/src/tweetlight.js index 7b035ad..46edaaa 100644 --- a/src/tweetlight.js +++ b/src/tweetlight.js @@ -1,4 +1,4 @@ -/*jslint browser: true*/ +/*jslint browser: true, debug:true*/ /*global define, module, exports*/ (function (root, factory) { "use strict"; @@ -18,10 +18,6 @@ return new Tweetlight(options); } - if (typeof options === 'string') { - options = { key : options }; - } - this.username = options.username; this.hashtag = options.hashtag; this.container = options.container; @@ -32,10 +28,6 @@ this.fetch(); }; - Tweetlight.init = function (options) { - return new Tweetlight(options); - }; - Tweetlight.prototype = { fetch: function () { var type = this.hashtag ? 'hashtag=' + encodeURIComponent(this.hashtag) : 'username=' + this.username,