PageRenderTime 27ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/public/js/app.js

https://bitbucket.org/rmanalan/tenyears-instagram
JavaScript | 147 lines | 139 code | 7 blank | 1 comment | 13 complexity | ff46e65cbc6f7242c8d0aa1300ffa09a MD5 | raw file
  1. var StreamCollection = Backbone.Collection.extend({
  2. stream: function(options) {
  3. // Cancel any potential previous stream
  4. this.unstream();
  5. var _update = _.bind(function() {
  6. this.fetch(options);
  7. this._intervalFetch = window.setTimeout(_update, options.interval || 1000);
  8. }, this);
  9. _update();
  10. },
  11. unstream: function() {
  12. window.clearTimeout(this._intervalFetch);
  13. delete this._intervalFetch;
  14. },
  15. isStreaming : function() {
  16. return _.isUndefined(this._intervalFetch);
  17. }
  18. });
  19. var PhotoModel = Backbone.Model.extend({});
  20. var PhotoCollection = StreamCollection.extend({
  21. model: PhotoModel,
  22. initialize: function(options){
  23. this.options = options;
  24. },
  25. url: function(){
  26. return "/v1/tags/" + this.options.tag + "/media/recent?client_id=" + this.options.clientId;
  27. },
  28. parse: function(d) {
  29. this.options.pagination = d.pagination;
  30. return d.data;
  31. },
  32. comparator: function(d) {
  33. return - d.get('created_time');
  34. },
  35. recent: function(){
  36. return this.toJSON().splice(0,this.options.recent);
  37. },
  38. add : function(models, options) {
  39. var newModels = [];
  40. _.each(models, function(model) {
  41. if (_.isUndefined(this.get(model.id))) {
  42. newModels.push(model);
  43. }
  44. }, this);
  45. return Backbone.Collection.prototype.add.call(this, newModels, options);
  46. }
  47. });
  48. var ThumbnailsView = Backbone.View.extend({
  49. el: $('#thumbs'),
  50. template: _.template($('#thumb-template').html()),
  51. initialize: function(){
  52. this.render();
  53. this.collection.stream({interval: 5000, add: true});
  54. this.collection.bind('add',function(model){
  55. this.$el.prepend(this.template(model.toJSON()));
  56. $('.thumb.hidden').removeClass('hidden');
  57. if ($('.thumb').length > 100) {
  58. $('.thumb').slice(100 - $('.thumb').length).each(function(i,e){
  59. $(e).remove();
  60. });
  61. }
  62. },this);
  63. },
  64. render: function(){
  65. this.collection.each(function(d){
  66. this.$el.append(this.template(d.toJSON()));
  67. $('.thumb.hidden').removeClass('hidden');
  68. },this);
  69. }
  70. });
  71. var CurrentPhotoView = Backbone.View.extend({
  72. el: $('#current-photo'),
  73. template: _.template($('#photo-template').html()),
  74. initialize: function(){
  75. this.queue = this.collection.recent();
  76. this.render();
  77. this.swapInterval = 7000;
  78. this.updateCurrentPhoto();
  79. this.reposition();
  80. var _self = this;
  81. $(window).on('resize',function(){
  82. _self.reposition();
  83. });
  84. },
  85. render: function(){
  86. this.$el.html(this.template(this.queue.pop()));
  87. $('.photo').removeClass('hidden');
  88. },
  89. reposition: function(){
  90. this.$el.css('top',($(window).height() - $('footer').height() - this.$el.height())/2);
  91. },
  92. updateCurrentPhoto: function(){
  93. var nextPhoto = this.queue.pop();
  94. if (nextPhoto) {
  95. this.$el.html(this.template(nextPhoto));
  96. $('.photo').removeClass('hidden');
  97. } else {
  98. this.queue = this.collection.recent();
  99. }
  100. var _self = this;
  101. setTimeout(function(){ _self.updateCurrentPhoto() }, this.swapInterval);
  102. }
  103. });
  104. var TagView = Backbone.View.extend({
  105. el:$('#tag'),
  106. initialize:function(){
  107. this.$el.html(this.options.tag);
  108. }
  109. });
  110. function getQueryParam(param) {
  111. var query = window.location.search.substring(1);
  112. var params = query.split("&");
  113. for (var i = 0; i < params.length; i++) {
  114. var pair = params[i].split("=");
  115. if (pair[0] === param) {
  116. return unescape(pair[1]);
  117. }
  118. }
  119. }
  120. var options = {
  121. clientId: getQueryParam('clientId') || "9571ea7bf9d84fa98e4bcdb10719a73e",
  122. tag: getQueryParam('tag') || 'atlassian',
  123. recent: getQueryParam('recent') || 5
  124. };
  125. var tagView = new TagView({tag: options.tag});
  126. var photoCollection = new PhotoCollection(options);
  127. photoCollection.fetch().done(function(d){
  128. var currentPhotoView = new CurrentPhotoView({collection: photoCollection});
  129. function getMore(d,cnt) {
  130. photoCollection.fetch({add: true, data: {max_tag_id: d.pagination.next_max_tag_id}}).done(function(d){
  131. if(photoCollection.length < 100 && cnt < 5) {
  132. cnt += 1;
  133. getMore(d,cnt);
  134. } else {
  135. var thumbnailsView = new ThumbnailsView({collection: photoCollection});
  136. }
  137. });
  138. };
  139. getMore(d,0);
  140. });