PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/Resources/ui/common/mashups/twitter.js

http://github.com/appcelerator/KitchenSink
JavaScript | 319 lines | 278 code | 22 blank | 19 comment | 59 complexity | d278c574bf6b81c91f31e9a42d2f0d49 MD5 | raw file
Possible License(s): Apache-2.0
  1. // Kosso
  2. // Another attempt to create a simple Twitter client list.
  3. // This time using a vertical layout view within each row containing the avatar image and labels for the
  4. // date, the username and the tweet itself.
  5. // @kosso : kosso@phreadz.com
  6. function strtotime (str, now) {
  7. // Emlulates the PHP strtotime function in JavaScript
  8. // obtained from http://phpjs.org/functions/strtotime:554
  9. var i, match, s, strTmp = '', parse = '';
  10. strTmp = str;
  11. strTmp = strTmp.replace(/\s{2,}|^\s|\s$/g, ' '); // unecessary spaces
  12. strTmp = strTmp.replace(/[\t\r\n]/g, ''); // unecessary chars
  13. if (strTmp == 'now') {
  14. return (new Date()).getTime()/1000; // Return seconds, not milli-seconds
  15. } else if (!isNaN(parse = Date.parse(strTmp))) {
  16. return (parse/1000);
  17. } else if (now) {
  18. now = new Date(now*1000); // Accept PHP-style seconds
  19. } else {
  20. now = new Date();
  21. }
  22. strTmp = strTmp.toLowerCase();
  23. var __is =
  24. {
  25. day:
  26. {
  27. 'sun': 0,
  28. 'mon': 1,
  29. 'tue': 2,
  30. 'wed': 3,
  31. 'thu': 4,
  32. 'fri': 5,
  33. 'sat': 6
  34. },
  35. mon:
  36. {
  37. 'jan': 0,
  38. 'feb': 1,
  39. 'mar': 2,
  40. 'apr': 3,
  41. 'may': 4,
  42. 'jun': 5,
  43. 'jul': 6,
  44. 'aug': 7,
  45. 'sep': 8,
  46. 'oct': 9,
  47. 'nov': 10,
  48. 'dec': 11
  49. }
  50. };
  51. var process = function (m) {
  52. var ago = (m[2] && m[2] == 'ago');
  53. var num = (num = m[0] == 'last' ? -1 : 1) * (ago ? -1 : 1);
  54. switch (m[0]) {
  55. case 'last':
  56. case 'next':
  57. switch (m[1].substring(0, 3)) {
  58. case 'yea':
  59. now.setFullYear(now.getFullYear() + num);
  60. break;
  61. case 'mon':
  62. now.setMonth(now.getMonth() + num);
  63. break;
  64. case 'wee':
  65. now.setDate(now.getDate() + (num * 7));
  66. break;
  67. case 'day':
  68. now.setDate(now.getDate() + num);
  69. break;
  70. case 'hou':
  71. now.setHours(now.getHours() + num);
  72. break;
  73. case 'min':
  74. now.setMinutes(now.getMinutes() + num);
  75. break;
  76. case 'sec':
  77. now.setSeconds(now.getSeconds() + num);
  78. break;
  79. default:
  80. var day;
  81. if (typeof (day = __is.day[m[1].substring(0, 3)]) != 'undefined') {
  82. var diff = day - now.getDay();
  83. if (diff == 0) {
  84. diff = 7 * num;
  85. } else if (diff > 0) {
  86. if (m[0] == 'last') {diff -= 7;}
  87. } else {
  88. if (m[0] == 'next') {diff += 7;}
  89. }
  90. now.setDate(now.getDate() + diff);
  91. }
  92. }
  93. break;
  94. default:
  95. if (/\d+/.test(m[0])) {
  96. num *= parseInt(m[0], 10);
  97. switch (m[1].substring(0, 3)) {
  98. case 'yea':
  99. now.setFullYear(now.getFullYear() + num);
  100. break;
  101. case 'mon':
  102. now.setMonth(now.getMonth() + num);
  103. break;
  104. case 'wee':
  105. now.setDate(now.getDate() + (num * 7));
  106. break;
  107. case 'day':
  108. now.setDate(now.getDate() + num);
  109. break;
  110. case 'hou':
  111. now.setHours(now.getHours() + num);
  112. break;
  113. case 'min':
  114. now.setMinutes(now.getMinutes() + num);
  115. break;
  116. case 'sec':
  117. now.setSeconds(now.getSeconds() + num);
  118. break;
  119. }
  120. } else {
  121. return false;
  122. }
  123. break;
  124. }
  125. return true;
  126. };
  127. match = strTmp.match(/^(\d{2,4}-\d{2}-\d{2})(?:\s(\d{1,2}:\d{2}(:\d{2})?)?(?:\.(\d+))?)?$/);
  128. if (match != null) {
  129. if (!match[2]) {
  130. match[2] = '00:00:00';
  131. } else if (!match[3]) {
  132. match[2] += ':00';
  133. }
  134. s = match[1].split(/-/g);
  135. for (i in __is.mon) {
  136. if (__is.mon[i] == s[1] - 1) {
  137. s[1] = i;
  138. }
  139. }
  140. s[0] = parseInt(s[0], 10);
  141. s[0] = (s[0] >= 0 && s[0] <= 69) ? '20'+(s[0] < 10 ? '0'+s[0] : s[0]+'') : (s[0] >= 70 && s[0] <= 99) ? '19'+s[0] : s[0]+'';
  142. return parseInt(this.strtotime(s[2] + ' ' + s[1] + ' ' + s[0] + ' ' + match[2])+(match[4] ? match[4]/1000 : ''), 10);
  143. }
  144. var regex = '([+-]?\\d+\\s'+
  145. '(years?|months?|weeks?|days?|hours?|min|minutes?|sec|seconds?'+
  146. '|sun\\.?|sunday|mon\\.?|monday|tue\\.?|tuesday|wed\\.?|wednesday'+
  147. '|thu\\.?|thursday|fri\\.?|friday|sat\\.?|saturday)'+
  148. '|(last|next)\\s'+
  149. '(years?|months?|weeks?|days?|hours?|min|minutes?|sec|seconds?'+
  150. '|sun\\.?|sunday|mon\\.?|monday|tue\\.?|tuesday|wed\\.?|wednesday'+
  151. '|thu\\.?|thursday|fri\\.?|friday|sat\\.?|saturday))'+
  152. '(\\sago)?';
  153. match = strTmp.match(new RegExp(regex, 'gi')); // Brett: seems should be case insensitive per docs, so added 'i'
  154. if (match == null) {
  155. return false;
  156. }
  157. for (i = 0; i < match.length; i++) {
  158. if (!process(match[i].split(' '))) {
  159. return false;
  160. }
  161. }
  162. return (now.getTime()/1000);
  163. }
  164. // creates a 'pretty date' from a unix time stamp
  165. function prettyDate(time){
  166. var monthname = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
  167. var date = new Date(time*1000),
  168. diff = (((new Date()).getTime() - date.getTime()) / 1000),
  169. day_diff = Math.floor(diff / 86400);
  170. if ( isNaN(day_diff) || day_diff < 0 ){
  171. return '';
  172. }
  173. if(day_diff >= 31){
  174. var date_year = date.getFullYear();
  175. var month_name = monthname[date.getMonth()];
  176. var date_month = date.getMonth() + 1;
  177. if(date_month < 10){
  178. date_month = "0"+date_month;
  179. }
  180. var date_monthday = date.getDate();
  181. if(date_monthday < 10){
  182. date_monthday = "0"+date_monthday;
  183. }
  184. return date_monthday + " " + month_name + " " + date_year;
  185. }
  186. return day_diff == 0 && (
  187. diff < 60 && "just now" ||
  188. diff < 120 && "1 minute ago" ||
  189. diff < 3600 && Math.floor( diff / 60 ) + " minutes ago" ||
  190. diff < 7200 && "1 hour ago" ||
  191. diff < 86400 && "about " + Math.floor( diff / 3600 ) + " hours ago") ||
  192. day_diff == 1 && "Yesterday" ||
  193. day_diff < 7 && day_diff + " days ago" ||
  194. day_diff < 31 && Math.ceil( day_diff / 7 ) + " week" + ((Math.ceil( day_diff / 7 )) == 1 ? "" : "s") + " ago";
  195. }
  196. function getTweets() {
  197. // set up a twitter screen name.
  198. var screen_name = 'appcelerator';
  199. win = Ti.UI.createWindow({
  200. title:'Twitter',
  201. backgroundColor:'#fff'
  202. });
  203. // create table view data object
  204. var data = [];
  205. var xhr = Ti.Network.createHTTPClient();
  206. xhr.timeout = 1000000;
  207. var bearerToken = "AAAAAAAAAAAAAAAAAAAAAEHKSgAAAAAAKzh8n7rBx8SsTJECCkRlVHW%2FlG8%3DvVgRTmoK6wDp1DEw9GZMShZcdCgDcd4CgwvBbzrgbk";
  208. xhr.open("GET","https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name="+screen_name);
  209. xhr.setRequestHeader("Authorization", "Bearer " + bearerToken);
  210. xhr.onload = function() {
  211. try {
  212. var tweets = JSON.parse(this.responseText);
  213. for (var c=0;c<tweets.length;c++){
  214. var tweet = tweets[c].text;
  215. var user = tweets[c].user.screen_name;
  216. var avatar = tweets[c].user.profile_image_url;
  217. var created_at = prettyDate(strtotime(tweets[c].created_at));
  218. var bgcolor = (c % 2) == 0 ? '#fff' : '#eee';
  219. var row = Ti.UI.createTableViewRow({hasChild:true,height:Ti.UI.SIZE,backgroundColor:bgcolor});
  220. // Create a vertical layout view to hold all the info labels and images for each tweet
  221. var post_view = Ti.UI.createView({
  222. height: Ti.UI.SIZE,
  223. layout:'vertical',
  224. left:5,
  225. top:5,
  226. bottom:5,
  227. right:5,
  228. });
  229. var av = Ti.UI.createImageView({
  230. image:avatar,
  231. left:0,
  232. top:0,
  233. height:48,
  234. width:48
  235. });
  236. // Add the avatar image to the view
  237. post_view.add(av);
  238. var user_label = Ti.UI.createLabel({
  239. text:user,
  240. left:54,
  241. width:120,
  242. top:-48,
  243. bottom:2,
  244. height:18,
  245. textAlign:'left',
  246. color:'#444444',
  247. font:{fontFamily:'Trebuchet MS',fontSize:14,fontWeight:'bold'}
  248. });
  249. // Add the username to the view
  250. post_view.add(user_label);
  251. var date_label = Ti.UI.createLabel({
  252. text:created_at,
  253. right:0,
  254. top:-18,
  255. bottom:2,
  256. height:14,
  257. textAlign:'right',
  258. width:110,
  259. color:'#444444',
  260. font:{fontFamily:'Trebuchet MS',fontSize:12}
  261. });
  262. // Add the date to the view
  263. post_view.add(date_label);
  264. var tweet_text = Ti.UI.createLabel({
  265. text:tweet,
  266. left:54,
  267. top:4,
  268. bottom:2,
  269. height:'auto',
  270. width:230,
  271. textAlign:'left',
  272. color: '#000',
  273. font:{fontSize:14}
  274. });
  275. // Add the tweet to the view
  276. post_view.add(tweet_text);
  277. // Add the vertical layout view to the row
  278. row.add(post_view);
  279. row.className = 'item'+c;
  280. data[c] = row;
  281. }
  282. // Create the tableView and add it to the window.
  283. var tableview = Titanium.UI.createTableView({data:data,minRowHeight:58});
  284. //Ti.UI.currentWindow.add(tableview);
  285. win.add(tableview);
  286. }
  287. catch(E){
  288. alert(E);
  289. }
  290. };
  291. win.addEventListener('open', function() {
  292. // Get the data
  293. xhr.send();
  294. });
  295. return win;
  296. };
  297. module.exports = getTweets;