PageRenderTime 41ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 1ms

/static/app2.js

https://github.com/weblancer23/Final-Project
JavaScript | 405 lines | 330 code | 55 blank | 20 comment | 34 complexity | 0c71b207201fc32030096c1ad331fcc4 MD5 | raw file
  1. $(function(){
  2. String.prototype.linkify = function() {
  3. return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/, function(m) {
  4. return m.link(m);
  5. });
  6. };
  7. function relative_time(time_value) {
  8. var values = time_value.split(" ");
  9. time_value = values[2] + " " + values[1] + ", " + values[3] + " " + values[5];
  10. var parsed_date = Date.parse(time_value);
  11. var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
  12. var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
  13. delta = delta + (relative_to.getTimezoneOffset() * 60);
  14. var r = '';
  15. if (delta < 60) {
  16. r = 'a minute ago';
  17. } else if(delta < 120) {
  18. r = 'couple of minutes ago';
  19. } else if(delta < (45*60)) {
  20. r = (parseInt(delta / 60)).toString() + ' minutes ago';
  21. } else if(delta < (90*60)) {
  22. r = 'an hour ago';
  23. } else if(delta < (24*60*60)) {
  24. r = '' + (parseInt(delta / 3600)).toString() + ' hours ago';
  25. } else if(delta < (48*60*60)) {
  26. r = '1 day ago';
  27. } else {
  28. r = (parseInt(delta / 86400)).toString() + ' days ago';
  29. }
  30. return r;
  31. }
  32. var app = {
  33. mainController: null,
  34. models: {},
  35. collections: {},
  36. views: {}
  37. };
  38. // Search Module
  39. //------------------
  40. app.models.SearchModel = Backbone.Model.extend({
  41. initialize: function() {
  42. var type = '';
  43. if (this.get('isFB') === true) {
  44. type = type + ' F ';
  45. }
  46. if (this.get('isTwitter') === true) {
  47. type = type + ' T ';
  48. }
  49. this.set('type', type);
  50. }
  51. });
  52. app.collections.SearchCollection = Backbone.Collection.extend({
  53. model: app.models.SearchModel,
  54. initialize: function() {
  55. }
  56. });
  57. app.views.SearchView = Backbone.View.extend({
  58. tagName: 'li',
  59. className: 'search-item',
  60. events: {
  61. 'click .item': 'research'
  62. },
  63. initialize: function() {
  64. this.render();
  65. },
  66. render: function() {
  67. this.template = _.template($('#search-item-view').html());
  68. var dict = this.model.toJSON();
  69. var markup = this.template(dict);
  70. $(this.el).html(markup);
  71. return this;
  72. },
  73. research: function() {
  74. application.search(this.model.get('query'), this.model.get('isFB'), this.model.get('isTwitter'));
  75. }
  76. });
  77. app.views.SearchHistoryView = Backbone.View.extend({
  78. events: {
  79. },
  80. initialize: function () {
  81. this._searchViews = [];
  82. this.searchList = this.options.searchList;
  83. //set event handlers
  84. _.bindAll(this, 'onSearchAdd');
  85. this.searchList.bind('add', this.onSearchAdd);
  86. },
  87. load: function () {
  88. },
  89. onSearchAdd: function(model) {
  90. console.log('search item added', model.get('query'));
  91. var searchController = new app.views.SearchView({
  92. model: model
  93. });
  94. //display tweet item
  95. this._searchViews.push(searchController);
  96. this.$('.search-list').append(searchController.render().el);
  97. }
  98. });
  99. // Facebook MVC
  100. //-----------------
  101. app.models.FBFeedModel = Backbone.Model.extend({
  102. initialize: function() {
  103. if (this.get('message') !== undefined) {
  104. this.set('text', this.get('message'));
  105. } else if (this.get('description') !== undefined) {
  106. this.set('text', this.get('description'));
  107. } else if (this.get('caption') !== undefined) {
  108. this.set('text', this.get('caption'));
  109. } else if (this.get('story') !== undefined) {
  110. this.set('text', this.get('story'));
  111. } else if (this.get('name') !== undefined) {
  112. this.set('text', this.get('name'));
  113. }
  114. }
  115. });
  116. app.collections.FBFeedCollection = Backbone.Collection.extend({
  117. model: app.models.FBFeedModel,
  118. initialize: function() {
  119. },
  120. url: function() {
  121. return 'http://search.twitter.com/search.json?q=' + this.query + '&rpp=1000' + '&callback=?';
  122. },
  123. query: '', //default query
  124. page: '1',
  125. parse: function(resp, xhr) {
  126. return resp.results;
  127. }
  128. });
  129. app.views.FBFeedController = Backbone.View.extend({
  130. tagName: 'li',
  131. events: {
  132. },
  133. initialize: function() {
  134. this.render();
  135. },
  136. render: function() {
  137. this.template = _.template($('#fb-feed-view').html());
  138. var dict = this.model.toJSON();
  139. var markup = this.template(dict);
  140. $(this.el).html(markup);
  141. return this;
  142. }
  143. });
  144. // Twitter MVC
  145. //-----------------
  146. app.models.TweetModel = Backbone.Model.extend({
  147. initialize: function() {
  148. var txt = this.get('text');
  149. this.set('text', txt.linkify());
  150. this.set('relative_time', relative_time(this.get('created_at')));
  151. }
  152. });
  153. app.collections.TweetCollection = Backbone.Collection.extend({
  154. model: app.models.TweetModel,
  155. initialize: function() {
  156. },
  157. url: function() {
  158. return 'http://search.twitter.com/search.json?q=' + this.query + '&rpp=1000' + '&callback=?';
  159. },
  160. query: '', //default query
  161. parse: function(resp, xhr) {
  162. return resp.results;
  163. }
  164. });
  165. app.views.TweetController = Backbone.View.extend({
  166. tagName: 'li',
  167. events: {
  168. "click .tweet-reply": "onReply",
  169. "click .tweet-retweet": "onRetweet",
  170. "click .tweet-favorite": "onFavorite"
  171. },
  172. initialize: function() {
  173. this.render();
  174. },
  175. render: function() {
  176. this.template = _.template($('#tweet-view-new').html());
  177. var dict = this.model.toJSON();
  178. var markup = this.template(dict);
  179. $(this.el).html(markup);
  180. return this;
  181. },
  182. onReply: function() {
  183. var url = "https://twitter.com/intent/tweet?in_reply_to=" + this.model.get('id');
  184. window.open(url, "_newtab");
  185. },
  186. onRetweet: function() {
  187. var url = "https://twitter.com/intent/retweet?tweet_id=" + this.model.get('id');
  188. window.open(url, "_newtab");
  189. },
  190. onFavorite: function() {
  191. var url = "https://twitter.com/intent/favorite?tweet_id=" + this.Model.get('id');
  192. window.open(url, "_newtab");
  193. }
  194. });
  195. // Main App
  196. //------------
  197. app.mainController = Backbone.View.extend({
  198. events: {
  199. 'submit .tweet-search': 'onSearch',
  200. 'click #fb-login': 'onFBLogin',
  201. 'click #fb-logout': 'onFBLogout',
  202. 'click #clear':'clearLocalStorage'
  203. },
  204. initialize: function () {
  205. this._tweetsView = [];
  206. this.tweets = new app.collections.TweetCollection();
  207. this.fbFeeds = new app.collections.FBFeedCollection();
  208. this.searchList = new app.collections.SearchCollection();
  209. new app.views.SearchHistoryView({
  210. el: $('.sidebar-history'),
  211. searchList: this.searchList,
  212. });
  213. //get local storage
  214. var count=1;
  215. while(localStorage.getItem("counter")>=count)
  216. {
  217. var query= localStorage.getItem("query"+count);
  218. var FB=false;
  219. if(localStorage.getItem("FB"+count)==="true")
  220. FB=true;
  221. var Tweet=false;
  222. if(localStorage.getItem("Twit"+count)==="true")
  223. Tweet=true;
  224. this.searchList.add({
  225. query:query,
  226. isFB: FB,
  227. isTwitter: Tweet
  228. });
  229. count++;
  230. }
  231. //set event handlers
  232. _.bindAll(this, 'onTweetAdd');
  233. this.tweets.bind('add', this.onTweetAdd);
  234. _.bindAll(this, 'onFBFeedAdd');
  235. this.fbFeeds.bind('add', this.onFBFeedAdd);
  236. },
  237. loadTweets: function () {
  238. var that = this;
  239. this.tweets.reset();
  240. this.tweets.fetch({
  241. add: that.onTweetAdd,
  242. success: function() {
  243. }
  244. });
  245. },
  246. incResultCount: function() {
  247. var searchCount = parseInt($('.search-count').text(), 10);
  248. $('.search-count').text(searchCount + 1);
  249. },
  250. loadFbFeeds: function() {
  251. var that = this;
  252. var query = this.fbFeeds.query;
  253. this.fbFeeds.reset();
  254. FB.api('/me/home?limit=100&q=' + query,
  255. // FB.api('http://graph.facebook.com/search/?q=' + query + '&limit=100&callback=processFeed',
  256. function (response){
  257. console.log(response);
  258. that.fbFeeds.add(response.data);
  259. });
  260. },
  261. clearLocalStorage: function()
  262. {
  263. localStorage.clear();
  264. },
  265. onFBLogin: function() {
  266. fbUser.login();
  267. },
  268. onFBLogout: function() {
  269. fbUser.logout();
  270. },
  271. search: function(query, isFB, isTwitter) {
  272. $('.title').html('<span class="blue search-count">0</span> results for: "' + query +'"');
  273. this.tweets.query = query;
  274. this.fbFeeds.query = query;
  275. this.$('.tweets-result li').remove();
  276. //check if new search
  277. var newSearch = true;//saves to the database, key/value
  278. this.searchList.each(function(search) {
  279. if (search.get('query') === query) {
  280. if (search.get('isFB') === isFB && search.get('isTwitter') === isTwitter) {
  281. newSearch = false;
  282. }
  283. }
  284. });
  285. //if not add to search history
  286. if (newSearch) {
  287. if(localStorage.getItem("counter")==null)
  288. {
  289. localStorage.setItem("counter",1);
  290. }
  291. else
  292. {
  293. localStorage.setItem("counter",parseInt(localStorage.getItem("counter"))+1)
  294. }
  295. var count=localStorage.getItem("counter");
  296. localStorage.setItem("query"+count, query);
  297. localStorage.setItem("FB"+count, isFB);
  298. localStorage.setItem("Twit"+count, isTwitter);
  299. this.searchList.add({
  300. query: query,
  301. isFB: isFB,
  302. isTwitter: isTwitter
  303. });
  304. }
  305. if (isFB) {
  306. this.loadFbFeeds();
  307. }
  308. if (isTwitter) {
  309. this.loadTweets();
  310. }
  311. },
  312. onSearch: function() {
  313. var isFB = false;
  314. var isTwitter = false;
  315. //check if Facebook checkbox is checked
  316. if ($('input[name="cb-facebook"]:checked').length > 0 ) {
  317. isFB = true;
  318. }
  319. //check if Twitter checkbox is checked
  320. if ($('input[name="cb-twitter"]:checked').length > 0 ) {
  321. isTwitter = true;
  322. }
  323. //check if new search
  324. var query = this.$('.search-query').val();
  325. this.search(query, isFB, isTwitter);
  326. return false;
  327. },
  328. onTweetAdd: function(model) {
  329. this.incResultCount();
  330. console.log('tweet added', model.get('text'));
  331. var tweetController = new app.views.TweetController({
  332. model: model
  333. });
  334. //display tweet item
  335. this._tweetsView.push(tweetController);
  336. this.$('.tweets-result').append(tweetController.render().el);
  337. },
  338. onFBFeedAdd: function(model) {
  339. this.incResultCount();
  340. var fbfeedController = new app.views.FBFeedController({
  341. model: model
  342. });
  343. //display tweet item
  344. this.$('.tweets-result').append(fbfeedController.render().el);
  345. }
  346. });
  347. window.application = new app.mainController({
  348. el: $('body')
  349. });
  350. });