jQuery jTwitter


/ Published in: jQuery
Save to your folder(s)

http://code.google.com/p/jquery-jtwitter/ for examples.


Copy this code and paste it in your HTML
  1. /**
  2.  * @author Jonnie Spratley
  3.  * @classDescription - Full jQuery wrapper for Twitter API. This plugin requires
  4.  * TwitterService.php for anything to work, files must be in same location. Or
  5.  * specified in the options of the plugin.
  6.  * @version - 0.9
  7.  * @projectDescription - http://code.google.com/p/jquery-jtwitter/
  8.  * @type - defaultTimeline: 'public' - String - The default timeline to recieve. public | friends | user
  9.  * @type - autoFetch: true,
  10.  * @type - showActions: false,
  11.  * @type - tweetarea: null - the id of the textarea for updating status, works well with replies
  12.  * @type - user: null - Object - A user object holding propertys username and/or password.
  13.  * @type - username: null - String - The twitter username
  14.  * @type - password: null - String - The twitter user password
  15.  * @type - search: null - Object - The next and prev link ids for paging search results.
  16.  * @type - next: null,
  17.  * @type - prev: null,
  18.  * @type - resultLocations: - null - Object - A object containing all of the result locations.
  19.  * @type - debug: null,
  20.  * @type - success: null,
  21.  * @type - error: null,
  22.  * @type - currentUser: null,
  23.  * @type - rateStatus: null,
  24.  * @type - deleteTweet: null,
  25.  * @type - getFollowing: null,
  26.  * @type - getFollowers: null,
  27.  * @type - getFriendsFollowers: null,
  28.  * @type - getFriendsTimeline: null,
  29.  * @type - getPublicTimeline: null,
  30.  * @type - getUserTimeline: null,
  31.  * @type - getReplies: null,
  32.  * @type - showTweet: null,
  33.  * @type - postTweet: null,
  34.  * @type - getMessages: null,
  35.  * @type - deleteMessage: null,
  36.  * @type - createMessage: null,
  37.  * @type - getSentMessages: null,
  38.  * @type - getFavorites: null,
  39.  * @type - createFavorite: null,
  40.  * @type - deleteFavorite: null,
  41.  * @type - followMember: null,
  42.  * @type - unfollowMember: null,
  43.  * @type - confirmFollow: null,
  44.  * @type - blockMember: null,
  45.  * @type - unblockMember: null,
  46.  * @type - getAllFollowers: null,
  47.  * @type - getAllFriends: null,
  48.  * @type - getRateLimit: null,
  49.  * @type - endSession: null,
  50.  * @type - verifyCredentials: null,
  51.  * @type - updateDevice: null,
  52.  * @type - updateLocation: null,
  53.  * @type - updateProfile: null,
  54.  * @type - updateProfileImage: null,
  55.  * @type - updateBackgroundImage: null,
  56.  * @type - updateProfileColors: null,
  57.  * @type - searchKeywords: null,
  58.  * @type - searchTrends: null
  59.  */
  60. (function($){
  61. var defaultSettings = {
  62. defaultTimeline: 'public',
  63. autoFetch: true,
  64. loader: null,
  65. showActions: false,
  66. user: {
  67. username: null,
  68. password: null
  69. },
  70. search: {
  71. next: null,
  72. prev: null,
  73. },
  74. tweetarea: null,
  75. resultLocations: {
  76. debug: null,
  77. success: null,
  78. error: null,
  79. currentUser: null,
  80. rateStatus: null,
  81. deleteTweet: null,
  82. getFollowing: null,
  83. getFollowers: null,
  84. getFriendsFollowers: null,
  85. getFriendsTimeline: null,
  86. getPublicTimeline: null,
  87. getUserTimeline: null,
  88. getReplies: null,
  89. showTweet: null,
  90. postTweet: null,
  91. getMessages: null,
  92. deleteMessage: null,
  93. createMessage: null,
  94. getSentMessages: null,
  95. getFavorites: null,
  96. createFavorite: null,
  97. deleteFavorite: null,
  98. followMember: null,
  99. unfollowMember: null,
  100. confirmFollow: null,
  101. blockMember: null,
  102. unblockMember: null,
  103. getAllFollowers: null,
  104. getAllFriends: null,
  105. getRateLimit: null,
  106. endSession: null,
  107. verifyCredentials: null,
  108. updateDevice: null,
  109. updateLocation: null,
  110. updateProfile: null,
  111. updateProfileImage: null,
  112. updateBackgroundImage: null,
  113. updateProfileColors: null,
  114. searchKeywords: null,
  115. searchTrends: null
  116. }
  117. };
  118. var thisWrap = '';
  119. var inCall = false;
  120. var jtwitterUserObject = {
  121. created_at: new Date(),
  122. description: '',
  123. favourites_count: 0,
  124. followers_count: 0,
  125. following: 0,
  126. id: 0,
  127. location: '',
  128. name: '',
  129. notifications: true,
  130. profile_background_color: '000000',
  131. profile_background_image_url: '',
  132. profile_background_tile: true,
  133. profile_image_url: '',
  134. profile_link_color: '000000',
  135. profile_sidebar_fill_color: '000000',
  136. profile_sidebar_border_color: '000000',
  137. isprotected: false,
  138. screen_name: '',
  139. statuses_count: 0,
  140. time_zone: '',
  141. url: '',
  142. utc_offset: 0,
  143. verified: false
  144. };
  145.  
  146. var jtwitterMessageObject = {
  147. id: 0,
  148. text: '',
  149. created_at: new Date(),
  150. recipient_id: 0,
  151. recipient_screen_name: '',
  152. recipient: jtwitterUserObject,
  153. sender_id: 0,
  154. sender_screen_name: '',
  155. sender: jtwitterUserObject
  156. };
  157.  
  158. var jtwitterTweetObject = {
  159. id: 0,
  160. text: '',
  161. created_at: new Date(),
  162. source: '',
  163. truncated: false,
  164. favorited: false,
  165. in_reply_to_screen_name: null,
  166. in_reply_to_status_id: null,
  167. in_reply_to_user_id: null,
  168. user: jtwitterTweetObject
  169. };
  170.  
  171. var jtwitterRateObject = {
  172. reset_time_in_seconds: 0,
  173. reset_time: new Date(),
  174. remaining_hits: 0,
  175. hourly_limit: 0
  176. };
  177.  
  178. var jtwitterTrendObject = {
  179. as_of: new Date(),
  180. trends: {
  181. name: '',
  182. url: ''
  183. }
  184. };
  185.  
  186. var jtwitterSearchObject = {
  187. completed_in: 0,
  188. max_id: 0,
  189. next_page: '',
  190. page: 0,
  191. query: '',
  192. refresh_url: '',
  193. results: {
  194. created_at: new Date(),
  195. from_user: '',
  196. from_user_id: 0,
  197. id: 0,
  198. iso_language_code: '',
  199. profile_image_url: '',
  200. source: '',
  201. text: '',
  202. to_user_id: 0
  203. }
  204. };
  205.  
  206. $.fn.jtwitter = function(url, options){
  207.  
  208. thisWrap = $(this);
  209.  
  210. var defaultOptions = $.extend(true, {}, defaultSettings, options);
  211.  
  212. $.jtwitter = {
  213. jtwitterMethods: ["deleteTweet", "getFollowing", "getFollowers", "getFriendsFollowers", "getFriendsTimeline", "getPublicTimeline", "getUserTimeline", "getReplies", "showTweet", "postTweet", "showProfile", "getMessages", "deleteMessage", "createMessage", "getSentMessages", "followMember", "unfollowMember", "confirmFollow", "getFavorites", "createFavorite", "deleteFavorite", "blockMember", "unBlockMember", "getAllFriends", "getAllFollowers", "getRateLimit", "endSession", "verifyCredentials", "updateDevice", "updateLocation", "updateProfile", "updateBackgroundImage", "updateProfileColors", "updateProfileImage", "notification_turnOn", "notification_turnOff", "search_keywords", "search_trends", "help_test"],
  214.  
  215. /**
  216. * I make a get call to the proxy which then calls on the twitter api.
  217. * @param {Object} method - the method to invoke
  218. * @param {Object} params - the parameter object to send with the call
  219. * @param {Object} callback - the callback to execute once result is recieved
  220. */
  221. _get: function(method, params, callback){
  222. $.extend(params, {
  223. m: method
  224. });
  225.  
  226. //@TODO: Check this
  227. //if ( method != 'verifyCredentials' || method != 'getPublicTimeline' || method != 'searchKeywords' || method != 'searchTrends' ){
  228.  
  229. if (defaultOptions.user.username) {
  230. $.extend(params, {
  231. u: defaultOptions.user.username
  232. });
  233. }
  234.  
  235. if (defaultOptions.user.password) {
  236. $.extend(params, {
  237. p: defaultOptions.user.password
  238. });
  239. }
  240.  
  241. //}
  242.  
  243. //Set loading to true
  244. defaultOptions.loader(true);
  245.  
  246. $.get(url, params, function(result){
  247. defaultOptions.loader(false);
  248.  
  249. var resultArray = eval('(' + result + ')');
  250. if (callback) {
  251. callback(resultArray);
  252. }
  253. //return resultArray;
  254. });
  255. },
  256.  
  257. /**
  258. * I post data to the proxy which then posts to twitter.com
  259. *
  260. * @param {Object} method - the method to invoke
  261. * @param {Object} params - the parameter object to send with the call
  262. * @param {Object} callback - the callback to execute once result is recieved
  263. */
  264. _post: function(method, params, callback){
  265. $.extend(params, {
  266. m: method,
  267. u: defaultOptions.user.username,
  268. p: defaultOptions.user.password
  269. });
  270.  
  271. //Set the loader
  272. defaultOptions.loader(true);
  273.  
  274. $.post(url, params, function(result){
  275. defaultOptions.loader(false);
  276.  
  277. var resultArray = eval('(' + result + ')');
  278. if (callback) {
  279. callback(resultArray);
  280. }
  281. });
  282. },
  283.  
  284.  
  285. /**
  286. * I am the methods object, I hold all of the methods that are available on the proxy.
  287. * I then call upon the _get function passing all required parameters, and then handle
  288. * the result by passing the result and the method called to the build function. Which
  289. * then populates the specified jquery object with the formatted and build data.
  290. */
  291. getPublicTimeline: function(callback){
  292. $.jtwitter._get('getPublicTimeline', {}, function(data){
  293. (defaultOptions.resultLocations.getPublicTimeline) ? $(defaultOptions.resultLocations.getPublicTimeline).html($.jtwitter._builders.build('tweet', data)) : (callback(data));
  294. });
  295. },
  296.  
  297. getFollowers: function( page, callback){
  298. if ($.jtwitter._utils.checkUser()) {
  299. $.jtwitter._get('getFollowers', {
  300. page: page
  301. }, function(data){
  302. (defaultOptions.resultLocations.getFollowers) ? $(defaultOptions.resultLocations.getFollowers).html($.jtwitter._builders.build('follower', data)) : (callback(data));
  303. });
  304. }
  305. },
  306.  
  307. getFollowing: function(count, page, callback){
  308. if ($.jtwitter._utils.checkUser()) {
  309. $.jtwitter._get('getFollowing', {
  310. count: count,
  311. page: page
  312. }, function(data){
  313. (defaultOptions.resultLocations.getFollowing) ? $(defaultOptions.resultLocations.getFollowing).html($.jtwitter._builders.build('follower', data)) : (callback(data));
  314. });
  315. }
  316. },
  317.  
  318. getFriendsFollowers: function(callback){
  319. if ($.jtwitter._utils.checkUser()) {
  320. $.jtwitter._get('getFriendsFollowers', {}, function(data){
  321. (defaultOptions.resultLocations.getFriendsFollowers) ? $(defaultOptions.resultLocations.getFriendsFollowers).html($.jtwitter._builders.build('tweet', data)) : (callback(data));
  322. });
  323. }
  324. },
  325.  
  326. getFriendsTimeline: function(count, page, callback){
  327. if ($.jtwitter._utils.checkUser()) {
  328. $.jtwitter._get('getFriendsTimeline', {
  329. count: count,
  330. page: page
  331. }, function(data){
  332. (defaultOptions.resultLocations.getFriendsTimeline) ? $(defaultOptions.resultLocations.getFriendsTimeline).html($.jtwitter._builders.build('tweet', data)) : (callback(data));
  333. });
  334. }
  335.  
  336. },
  337.  
  338. getUserTimeline: function(user, count, page, callback){
  339.  
  340. $.jtwitter._get('getUserTimeline', {
  341. user: user,
  342. count: count,
  343. page: page
  344. }, function(data){
  345. (defaultOptions.resultLocations.getUserTimeline) ? $(defaultOptions.resultLocations.getUserTimeline).html($.jtwitter._builders.build('tweet', data)) : (callback(data));
  346. });
  347.  
  348. },
  349.  
  350. getReplies: function(count, page, callback){
  351. if ($.jtwitter._utils.checkUser()) {
  352. $.jtwitter._get('getReplies', {
  353. count: count,
  354. page: page
  355. }, function(data){
  356. (defaultOptions.resultLocations.getReplies) ? $(defaultOptions.resultLocations.getReplies).html($.jtwitter._builders.build('tweet', data)) : (callback(data));
  357. });
  358. }
  359. },
  360.  
  361. showTweet: function(id, callback){
  362. if ($.jtwitter._utils.checkUser()) {
  363. $.jtwitter._get('showTweet', {
  364. id: id
  365. }, function(data){
  366.  
  367. (callback(data));
  368. });
  369. }
  370. },
  371.  
  372. postTweet: function(text, callback){
  373. if ($.jtwitter._utils.checkUser()) {
  374. $.jtwitter._post('postTweet', {
  375. status: text
  376. }, function(data){
  377. (defaultOptions.resultLocations.getFriendsTimeline) ? $(defaultOptions.resultLocations.getFriendsTimeline).prepend($.jtwitter._builders.build('singletweet', data)) : (callback(data));
  378. });
  379. }
  380. },
  381.  
  382. getMessages: function(count, page, callback){
  383. if ($.jtwitter._utils.checkUser()) {
  384. $.jtwitter._get('getMessages', {
  385. count: count,
  386. page: page
  387. }, function(data){
  388. (defaultOptions.resultLocations.getMessages) ? $(defaultOptions.resultLocations.getMessages).html($.jtwitter._builders.build('message', data)) : (callback(data));
  389. });
  390. }
  391. },
  392.  
  393. getSentMessages: function(count, page, callback){
  394. if ($.jtwitter._utils.checkUser()) {
  395. $.jtwitter._get('getSentMessages', {
  396. count: count,
  397. page: page
  398. }, function(data){
  399. (defaultOptions.resultLocations.getPublicTimeline) ? $(defaultOptions.resultLocations.getPublicTimeline).html($.jtwitter._builders.build(1, data)) : (callback(data));
  400. });
  401. }
  402. },
  403.  
  404. createMessage: function(userid, text, callback){
  405. if ($.jtwitter._utils.checkUser()) {
  406. $.jtwitter._post('createMessage', {
  407. id: userid,
  408. text: text
  409. }, function(data){
  410. (defaultOptions.resultLocations.createMessage) ? $(defaultOptions.resultLocations.createMessage).html($.jtwitter._builders.build('message', data)) : (callback(data));
  411. });
  412. }
  413. },
  414.  
  415. deleteMessage: function(msgid, callback){
  416. if ($.jtwitter._utils.checkUser()) {
  417. $.jtwitter._post('deleteMessage', {
  418. id: msgid
  419. }, function(data){
  420. (defaultOptions.resultLocations.deleteMessage) ? $(defaultOptions.resultLocations.deleteMessage).html($.jtwitter._builders.build('message', data)) : (callback(data));
  421. });
  422. }
  423. },
  424.  
  425. getFavorites: function(count, page, callback){
  426. if ($.jtwitter._utils.checkUser()) {
  427. $.jtwitter._get('getFavorites', {
  428. count: count,
  429. page: page
  430. }, function(data){
  431. (defaultOptions.resultLocations.getFavorites) ? $(defaultOptions.resultLocations.getFavorites).html($.jtwitter._builders.build('tweet', data)) : (callback(data));
  432. });
  433. }
  434. },
  435.  
  436. createFavorite: function(msgid, callback){
  437. if ($.jtwitter._utils.checkUser()) {
  438. $.jtwitter._post('createFavorite', {
  439. id: msgid
  440. }, function(data){
  441. (defaultOptions.resultLocations.createFavorite) ? $(defaultOptions.resultLocations.createFavorite).html($.jtwitter._builders.build('tweet', data)) : (callback(data));
  442. });
  443. }
  444. },
  445.  
  446. deleteFavorite: function(msgid, callback){
  447. if ($.jtwitter._utils.checkUser()) {
  448. $.jtwitter._post('deleteFavorite', {
  449. id: msgid
  450. }, function(data){
  451. (defaultOptions.resultLocations.deleteFavorite) ? $(defaultOptions.resultLocations.deleteFavorite).html($.jtwitter._builders.build('tweet', data)) : (callback(data));
  452. });
  453. }
  454. },
  455.  
  456. deleteTweet: function(id, callback){
  457. if ($.jtwitter._utils.checkUser()) {
  458. $.jtwitter._post('deleteTweet', {
  459. id: id
  460. }, function(data){
  461. (defaultOptions.resultLocations.deleteTweet) ? $(defaultOptions.resultLocations.deleteTweet).html($.jtwitter._builders.build('tweet', data)) : (callback(data));
  462. });
  463. }
  464. },
  465.  
  466. followMember: function(userid, follow, callback){
  467. if ($.jtwitter._utils.checkUser()) {
  468. $.jtwitter._post('followMember', {
  469. id: userid,
  470. follow: follow
  471. }, function(data){
  472. (defaultOptions.resultLocations.followMember) ? $(defaultOptions.resultLocations.followMember).html($.jtwitter._builders.build('user', data)) : (callback(data));
  473. });
  474. }
  475. },
  476.  
  477. unfollowMember: function(userid, callback){
  478. if ($.jtwitter._utils.checkUser()) {
  479. $.jtwitter._post('unfollowMember', {
  480. id: userid
  481. }, function(data){
  482. (defaultOptions.resultLocations.unfollowMember) ? $(defaultOptions.resultLocations.unfollowMember).html($.jtwitter._builders.build('user', data)) : (callback(data));
  483. });
  484. }
  485. },
  486.  
  487. confirmFollow: function(usera, userb, callback){
  488. if ($.jtwitter._utils.checkUser()) {
  489. $.jtwitter._get('confirmFollow', {
  490. a: usera,
  491. b: userb
  492. }, function(data){
  493. (defaultOptions.resultLocations.confirmFollow) ? $(defaultOptions.resultLocations.confirmFollow).html($.jtwitter._builders.build('user', data)) : (callback(data));
  494. });
  495. }
  496. },
  497.  
  498. blockMember: function(userid, callback){
  499. if ($.jtwitter._utils.checkUser()) {
  500. $.jtwitter._post('blockMember', {
  501. id: userid
  502. }, function(data){
  503. (defaultOptions.resultLocations.blockMember) ? $(defaultOptions.resultLocations.blockMember).html($.jtwitter._builders.build('user', data)) : (callback(data));
  504. });
  505. }
  506. },
  507.  
  508. unblockMember: function(userid, callback){
  509. if ($.jtwitter._utils.checkUser()) {
  510. $.jtwitter._post('unblockMember', {
  511. id: userid
  512. }, function(data){
  513. (defaultOptions.resultLocations.unblockMember) ? $(defaultOptions.resultLocations.unblockMember).html($.jtwitter._builders.build('user', data)) : (callback(data));
  514. });
  515. }
  516. },
  517.  
  518. getAllFollowers: function(callback){
  519. if ($.jtwitter._utils.checkUser()) {
  520. $.jtwitter._get('getAllFollowers', {}, function(data){
  521. (defaultOptions.resultLocations.getAllFollowers) ? $(defaultOptions.resultLocations.getAllFollowers).html($.jtwitter._builders.build('countfriends', data)) : (callback(data));
  522. });
  523. }
  524. },
  525.  
  526. getAllFriends: function(callback){
  527. if ($.jtwitter._utils.checkUser()) {
  528. $.jtwitter._get('getAllFriends', {}, function(data){
  529. (defaultOptions.resultLocations.getAllFriends) ? $(defaultOptions.resultLocations.getAllFriends).html($.jtwitter._builders.build('countfriends', data)) : (callback(data));
  530. });
  531. }
  532. },
  533.  
  534. getRateLimit: function(callback){
  535. if ($.jtwitter._utils.checkUser()) {
  536. $.jtwitter._get('getRateLimit', {}, function(data){
  537. (defaultOptions.resultLocations.getRateLimit) ? $(defaultOptions.resultLocations.getRateLimit).html($.jtwitter._builders.build('rate', data)) : (callback(data));
  538. });
  539. }
  540. },
  541.  
  542. endSession: function(callback){
  543. $.jtwitter._post('endSession', {}, function(data){
  544. (defaultOptions.resultLocations.endSession) ? $(defaultOptions.resultLocations.endSession).html($.jtwitter._builders.build('user', data)) : (callback(data));
  545. });
  546. },
  547.  
  548. verifyCredentials: function(user, pass, callback){
  549. $.extend(defaultOptions.user, {
  550. username: user,
  551. password: pass
  552. });
  553. $.jtwitter._get('verifyCredentials', {
  554. u: user,
  555. p: pass
  556. }, function(data){
  557. (defaultOptions.resultLocations.verifyCredentials) ? $(defaultOptions.resultLocations.verifyCredentials).html($.jtwitter._builders.build('user', data)) : (callback(data));
  558. });
  559. },
  560.  
  561. updateDevice: function(device, callback){
  562. if ($.jtwitter._utils.checkUser()) {
  563. $.jtwitter._post('updateDevice', {
  564. device: device
  565. }, function(data){
  566. (defaultOptions.resultLocations.updateDevice) ? $(defaultOptions.resultLocations.updateDevice).html($.jtwitter._builders.build('user', data)) : (callback(data));
  567. });
  568. }
  569. },
  570.  
  571. updateLocation: function(location, callback){
  572. if ($.jtwitter._utils.checkUser()) {
  573. $.jtwitter._post('updateLocation', {
  574. location: location
  575. }, function(data){
  576. (defaultOptions.resultLocations.updateLocation) ? $(defaultOptions.resultLocations.updateLocation).html($.jtwitter._builders.build('user', data)) : (callback(data));
  577. });
  578. }
  579. },
  580.  
  581. updateProfile: function(name, email, url, location, desc, callback){
  582. if ($.jtwitter._utils.checkUser()) {
  583. $.jtwitter._post('updateProfile', {
  584. n: name,
  585. e: email,
  586. url: url,
  587. l: location,
  588. d: desc
  589. }, function(data){
  590. (defaultOptions.resultLocations.updateProfile) ? $(defaultOptions.resultLocations.updateProfile).html($.jtwitter._builders.build('user', data)) : (callback(data));
  591. });
  592. }
  593. },
  594.  
  595. updateProfileImage: function(img, callback){
  596. if ($.jtwitter._utils.checkUser()) {
  597. $.jtwitter._post('updateProfileImage', {
  598. image: img
  599. }, function(data){
  600. (defaultOptions.resultLocations.updateProfileImage) ? $(defaultOptions.resultLocations.updateProfileImage).html($.jtwitter._builders.build('user', data)) : (callback(data));
  601. });
  602. }
  603. },
  604.  
  605. updateBackgroundImage: function(img, callback){
  606. if ($.jtwitter._utils.checkUser()) {
  607. $.jtwitter._post('updateBackgroundImage', {
  608. image: img
  609. }, function(data){
  610. (defaultOptions.resultLocations.updateBackgroundImage) ? $(defaultOptions.resultLocations.updateBackgroundImage).html($.jtwitter._builders.build('user', data)) : (callback(data));
  611. });
  612. }
  613. },
  614.  
  615. updateProfileColors: function(bg, text, link, sidebg, sideborder, callback){
  616. if ($.jtwitter._utils.checkUser()) {
  617. $.jtwitter._post('updateProfileColors', {
  618. bg: bg,
  619. t: text,
  620. sbg: sidebg,
  621. sb: sideborder
  622. }, function(data){
  623. (defaultOptions.resultLocations.updateProfileColors) ? $(defaultOptions.resultLocations.updateProfileColors).html($.jtwitter._builders.build('user', data)) : (callback(data));
  624. });
  625. }
  626. },
  627.  
  628. searchKeywords: function(q, page, since, sinceid, maxid, callback){
  629. $.jtwitter._get('searchKeywords', {
  630. q: q,
  631. page: page,
  632. since: since,
  633. sinceid: sinceid,
  634. maxid: maxid
  635.  
  636. }, function(data){
  637. (defaultOptions.resultLocations.searchKeywords) ? $(defaultOptions.resultLocations.searchKeywords).html($.jtwitter._builders.build('search', data)) : (callback(data));
  638. });
  639. },
  640.  
  641. searchTrends: function(callback){
  642. $.jtwitter._get('searchTrends', {}, function(data){
  643. (defaultOptions.resultLocations.searchTrends) ? $(defaultOptions.resultLocations.searchTrends).html($.jtwitter._builders.build('trends', data)) : (callback(data));
  644. });
  645. },
  646.  
  647.  
  648.  
  649. /**
  650. * I hold all of the functions that build the html from the result of a service call
  651. */
  652. _builders: {
  653. /**
  654. * I evaluate and prep each build depending on what method was invoked
  655. * @param {Object} mode - what method that was called
  656. * @param {Object} data - the result object from that method
  657. */
  658. build: function(mode, data){
  659. var html = '';
  660.  
  661. switch (mode) {
  662. case 'tweet'://Timelines, Replies, Favorites,
  663. $.each(data, function(i, obj){
  664. $.extend(jtwitterTweetObject, obj);
  665. html += $.jtwitter._builders.buildTweets(jtwitterTweetObject);
  666. });
  667. break;
  668. case 'singletweet'://Timelines, Replies, Favorites,
  669. $.extend(jtwitterTweetObject, data);
  670.  
  671. html += $.jtwitter._builders.buildTweets(jtwitterTweetObject);
  672.  
  673. break;
  674.  
  675. case 'message'://Messages, Sent Messages
  676. $.each(data, function(i, obj){
  677. $.extend(jtwitterMessageObject, obj);
  678. html += $.jtwitter._builders.buildMessages(jtwitterMessageObject);
  679. });
  680. break;
  681.  
  682. case 'follower'://Followers Image List
  683. $.each(data, function(i, obj){
  684. $.extend(jtwitterUserObject, obj);
  685. html += $.jtwitter._builders.buildImages(jtwitterUserObject);
  686. });
  687. break;
  688.  
  689. case 'user'://User Update Status, Profile changes, etc user
  690. $.extend(jtwitterUserObject, data);
  691. html += $.jtwitter._builders.buildUser(jtwitterUserObject);
  692. break;
  693.  
  694. case 'search'://User Update Status, Profile changes, etc user
  695. $.extend(jtwitterSearchObject, data);
  696. $.each(jtwitterSearchObject.results, function(i, obj){
  697. html += $.jtwitter._builders.buildSearch(obj);
  698. });
  699. break;
  700.  
  701. case 'trends'://User Update Status, Profile changes, etc user
  702. $.extend(jtwitterTrendObject, data);
  703. $.each(jtwitterTrendObject.trends, function(i, obj){
  704. html += $.jtwitter._builders.buildTrends(obj);
  705. });
  706. break;
  707.  
  708. case 'rate': //Rate Status
  709. $.extend(jtwitterRateObject, data);
  710. html += $.jtwitter._builders.buildRateStatus(jtwitterRateObject);
  711. break;
  712. }
  713. return html;
  714. },
  715.  
  716. buildTweets: function(tweetObj){
  717.  
  718. var html = '';
  719. var altUser = '';
  720.  
  721. altUser = (defaultOptions.user.username === tweetObj.user.screen_name ? 'alt' : '');
  722.  
  723. html += '<li class="jtwitter-tweet ' + altUser + '" id="jtweet_' + tweetObj.id + '">';
  724. html += '<ul>';
  725. if (defaultOptions.showActions === true) {
  726. html += $.jtwitter._builders.buildActions(tweetObj);
  727. }
  728. html += '<li class="jtwitter-profile-image"><img src="' + tweetObj.user.profile_image_url + '" alt="Photo of ' + tweetObj.user.name + '"/></li>';
  729. html += '<li class="jtwitter-screen-name">';
  730. html += '<a class="jtwitter-url" href="http://twitter.com/' + tweetObj.user.screen_name + '" title="' + tweetObj.user.screen_name + '" rel="bookmark" target="_blank">';
  731. html += tweetObj.user.screen_name + '</a></li>';
  732. html += '<li class="jtwitter-text">' + tweetObj.text + '</li>';
  733. html += '<li class="jtwitter-time">';
  734. html += '<span class="jtwitter-created-at">' + $.jtwitter._utils.prettifyDate(tweetObj.created_at) + ' </span>';
  735. html += '<span class="jtwitter-source">via ' + tweetObj.source + '</span>';
  736. html += '</li>';
  737. html += '<li class="jtwitter-meta" style="display:none;">';
  738. html += '<span class="jtwitter-friends">' + tweetObj.user.friends_count + ' following</span> / ';
  739. html += '<span class="jtwitter-followers">' + tweetObj.user.followers_count + ' followers</span> / ';
  740. html += '<span class="jtwitter-updates">' + tweetObj.user.statuses_count + ' tweets</span>';
  741. //html += '<span class="jtwitter-favorites">' + tweetObj.user.favourites_count + ' favorites</span>';
  742. html += '</li>';
  743. html += '<li class="jtwitter-clear"></li>';
  744. html += '</ul>';
  745. html += '</li>';
  746.  
  747. return html;
  748. },
  749. buildMessages: function(msgObj){
  750. var html = '';
  751. html += '<li class="jtwitter-tweet" id="jtwitter-' + msgObj.id + '"><ul>';
  752. html += '<li class="jtwitter-profile-image"><img src="' + msgObj.sender.profile_image_url + '" alt="Photo of ' + msgObj.sender.name + '"/></li>';
  753. html += '<li class="jtwitter-screen-name"><a class="jtwitter-url" href="http://twitter.com/' + msgObj.sender.screen_name + '" title="' + msgObj.sender.screen_name + '" rel="bookmark" target="_blank">' + msgObj.sender.screen_name + '</a></li>';
  754. html += '<li class="jtwitter-text">' + msgObj.text + '</li>';
  755. html += '<li class="jtwitter-time"><span class="jtwitter-created-at">' + $.jtwitter._utils.prettifyDate(msgObj.created_at) + ' </span></li>';
  756. html += '</ul></li>';
  757.  
  758. return html;
  759. },
  760. buildImages: function(userObj){
  761. var html = '<li id="jtwitter-user-' + userObj.id + '" class="jtwitter-profile-image">';
  762. html += '<a class="jtwitter-url" href="http://twitter.com/' + userObj.screen_name + '" title="' + userObj.screen_name + '" rel="bookmark" target="_blank">';
  763. html += '<img src="' + userObj.profile_image_url + '" alt="Photo of ' + userObj.name + '"/></a></li>';
  764.  
  765. return html;
  766. },
  767. buildTrends: function(trendObj){
  768. var html = '';
  769. html += '<li class="jtwitter-trend"><a href="' + trendObj.url + '">' + trendObj.name + '</a></li>';
  770.  
  771. return html;
  772. },
  773. buildSearch: function(searchObj){
  774. var html = '';
  775. html += '<li class="jtwitter-search">' + searchObj.text + '</li>';
  776.  
  777. return html;
  778. },
  779. buildActions: function(tweetObj){
  780. var html = '';
  781. html += '<li class="jtwitter-actions">';
  782. html += '<ul class="jtwitter-actions">';
  783. if (tweetObj.user.screen_name == defaultOptions.user.username) {
  784. html += '<li class="ui-state-default ui-corner-all"><a href="#" title="' + tweetObj.user.screen_name + '" class="jtwitter-action-delete" rel="' + tweetObj.id + '"><span class="ui-icon ui-icon-trash">delete</span></a></li>';
  785. } else {
  786. html += '<li class="ui-state-default ui-corner-all"><a href="#" title="' + tweetObj.user.screen_name + '" class="jtwitter-action-reply" rel="' + tweetObj.id + '"><span class="ui-icon ui-icon-arrowreturnthick-1-w">reply</span></a></li>';
  787. }
  788.  
  789. //html += '<li class="ui-state-default ui-corner-all"><a href="#" title="' + tweetObj.user.screen_name + '" class="jtwitter-action-message" rel="' + tweetObj.user.id + '"><span class="ui-icon ui-icon-mail-closed">message</span></a></li>';
  790.  
  791. var favUnfav1 = (tweetObj.favorited == true) ? 'active' : 'default';
  792. var favUnfav2 = (tweetObj.favorited == true) ? 'unfavorite' : 'favorite';
  793.  
  794.  
  795.  
  796. html += '<li class="ui-state-' + favUnfav1 + ' ui-corner-all"><a href="#" title="' + favUnfav2 + ' tweet" class="jtwitter-action-' + favUnfav2 + '" rel="' + tweetObj.id + '"><span class="ui-icon ui-icon-star">favorite</span></a></li>';
  797. //html += '<li><a href="#" class="jtwitter-action-block" rel="' + tweetObj.user.id + '">block</a></li>';
  798. html += '</ul>';
  799. html += '</li>';
  800. //html += '<li class="jtwitter-clear"></li>';
  801.  
  802. return html;
  803. },
  804. buildUser: function(userObj){
  805. var html = '';
  806. html += '<li class="jtwitter-tweet" id="jtwitter-user-' + userObj.id + '">';
  807. html += '<ul>';
  808. html += '<li class="jtwitter-profile-image"><img src="' + userObj.profile_image_url + '" alt="Photo of ' + userObj.name + '"/></li>';
  809. html += '<li class="jtwitter-screen-name">';
  810. html += '<a class="jtwitter-url" href="http://twitter.com/' + userObj.screen_name + '" title="' + userObj.screen_name + '" rel="bookmark" target="_blank">';
  811. html += userObj.screen_name + '</a></li>';
  812. html += '<li class="jtwitter-meta">';
  813. html += '<span class="jtwitter-friends">' + userObj.friends_count + ' following</span> / ';
  814. html += '<span class="jtwitter-followers">' + userObj.followers_count + ' followers</span> / ';
  815. html += '<span class="jtwitter-updates">' + userObj.statuses_count + ' tweets</span>';
  816. //html += '<span class="jtwitter-favorites">' + userObj.favourites_count + ' favorites</span>';
  817. html += '</li>';
  818. html += '</ul>';
  819. html += '</li>';
  820.  
  821. return html;
  822. },
  823. buildRateStatus: function(statusObj){
  824. var html = '';
  825. html += '<li>API Hits: ' + statusObj.remaining_hits + '</li>';
  826. html += '<li>Hourly Limit: ' + statusObj.hourly_limit + '</li>';
  827. html += '<li>Reset Time: ' + statusObj.reset_time + '</li>';
  828. return html;
  829. }
  830. },
  831. _utils: {
  832. prettifyDate: function(time){
  833. var values = time.split(" ");
  834. time = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
  835.  
  836. var parsed_date = Date.parse(time);
  837.  
  838. var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
  839.  
  840. var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
  841. delta = delta + (relative_to.getTimezoneOffset() * 60);
  842.  
  843. var out = '';
  844.  
  845. if (delta < 60) {
  846. out = 'a minute ago';
  847. } else if (delta < 120) {
  848. out = 'couple of minutes ago';
  849. } else if (delta < (45 * 60)) {
  850. out = (parseInt(delta / 60)).toString() + ' minutes ago';
  851. } else if (delta < (90 * 60)) {
  852. out = 'an hour ago';
  853. } else if (delta < (24 * 60 * 60)) {
  854. out = '' + (parseInt(delta / 3600)).toString() + ' hours ago';
  855. } else if (delta < (48 * 60 * 60)) {
  856. out = '1 day ago';
  857. } else {
  858. out = (parseInt(delta / 86400)).toString() + ' days ago';
  859. }
  860.  
  861. return out;
  862. },
  863. checkUser: function(){
  864. if (defaultOptions.user.username === null && defaultOptions.user.password === null) {
  865. alert('You must login for this .');
  866. return false;
  867. } else {
  868. return true;
  869. }
  870. },
  871. handleActions: function(){
  872.  
  873. /***************************Favorite Actions**************************/
  874. //Add
  875. $('.jtwitter-action-favorite').live('click', function(){
  876. var link = this;
  877. //var confirm = confirm('You want to favorite tweet ' + $(this).attr('rel'));
  878. var confirm = true;
  879. if (confirm == true) {
  880. $.jtwitter.createFavorite($(this).attr('rel'), function(result){
  881. //Set it highlighted
  882. $(link).parent().removeClass('ui-state-default').addClass('ui-state-active');
  883. $(link).removeClass('jtwitter-action-favorite').addClass('jtwitter-action-unfavorite');
  884. });
  885. } else {
  886. return false;
  887. }
  888. return false;
  889. });
  890. //Remove
  891. $('.jtwitter-action-unfavorite').live('click', function(){
  892. var link = $(this);
  893. //var confirm = confirm('You want to un-favorite tweet ' + $(this).attr('rel'));
  894. var confirm = true;
  895. if (confirm == true) {
  896. $.jtwitter.deleteFavorite($(this).attr('rel'), function(result){
  897. //Set it un-highlighted
  898. $(link).parent().removeClass('ui-state-active').addClass('ui-state-default');
  899. $(link).removeClass('jtwitter-action-unfavorite').addClass('jtwitter-action-favorite');
  900. return false;
  901. });
  902. } else {
  903. return false;
  904. }
  905. });
  906.  
  907. /***************************Message Actions**************************/
  908. $('.jtwitter-action-message').bind('click', function(event){
  909. alert('Message to user id: ' + $(this).attr('rel'));
  910. });
  911.  
  912. /***************************Block Actions**************************/
  913. $('.jtwitter-action-block').live('click', function(){
  914. alert('Block user id: ' + $(this).attr('rel'));
  915. });
  916.  
  917. /***************************Reply Actions**************************/
  918. $('.jtwitter-action-reply').live('click', function(){
  919. $(defaultOptions.tweetarea).val('@' + $(this).attr('title'))
  920. //alert( 'Reply to tweet id: ' + $(this).attr('rel'));
  921. });
  922.  
  923. /***************************Follow Actions**************************/
  924. $('.jtwitter-action-follow').live('click', function(){
  925. alert('Follow user id: ' + $(this).attr('rel'));
  926. });
  927. /***************************Delete Actions**************************/
  928. $('.jtwitter-action-delete').live('click', function(){
  929. var confirmCheck = confirm('Are you sure?');
  930. var id = $(this).attr('rel');
  931. if (confirmCheck == true) {
  932. $.jtwitter.deleteTweet($(this).attr('rel'), function(result){
  933. $('#jtweet_' + id).remove('slow');
  934. });
  935. }
  936.  
  937. });
  938.  
  939. $(defaultOptions.search.next).bind('click', function(){
  940.  
  941. //alert('You clicked next. Current Page: ' + jtwitterSearchObject.page + ' Next Page: ' + (jtwitterSearchObject.page + 1) + ' Prev Page: ' + (jtwitterSearchObject.page - 1) + 'Max ID: ' + jtwitterSearchObject.max_id );
  942. $.jtwitter.searchKeywords(jtwitterSearchObject.query, (jtwitterSearchObject.page + 1), 0, 0, jtwitterSearchObject.max_id);
  943. });
  944.  
  945. $(defaultOptions.search.prev).bind('click', function(){
  946. $.jtwitter.searchKeywords(jtwitterSearchObject.query, (jtwitterSearchObject.page - 1), 0, 0, jtwitterSearchObject.max_id);
  947. //alert('You clicked prev');
  948. });
  949.  
  950. }
  951. }
  952.  
  953. };//ends jTwitterObject
  954. //If auto load data is true
  955. if (defaultOptions.autoFetch) {
  956. if (defaultOptions.defaultTimeline == 'public') {
  957. $.jtwitter.getPublicTimeline();
  958. } else if (defaultOptions.defaultTimeline == 'user') {
  959. $.jtwitter.getUserTimeline();
  960. } else if (defaultOptions.defaultTimeline == 'friends') {
  961. $.jtwitter.getFriendsTimeline();
  962. }
  963. }
  964.  
  965. if (defaultOptions.showActions) {
  966. $.jtwitter._utils.handleActions();
  967. }
  968.  
  969. return thisWrap;
  970.  
  971. };
  972. })(jQuery);

URL: http://code.google.com/p/jquery-jtwitter/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.