Advertisement
srikat

js.js

May 14th, 2015
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.73 KB | None | 0 0
  1. jQuery(function( $ ){
  2. /**
  3. * Module construntor
  4. * @param node
  5. * @param params
  6. * @returns {PostPerformanceModule}
  7. */
  8. var PostPerformanceModule = function(node, p) {
  9. var params = p || {};
  10. this.threshold = params.threshold || 2000;
  11. this.apiUrl = params.url || 'http://api.moviepilot.com/v3/things/2459188/tower_stats';
  12. this.$node = $(node);
  13. this.$reads = this.$node.find('[data-tower="reads"]');
  14. this.$shares = this.$node.find('[data-tower="shares"]');
  15. this.$comments = this.$node.find('[data-tower="comments"]');
  16.  
  17. this.init();
  18.  
  19. return this;
  20. };
  21.  
  22. /**
  23. * Module initialization
  24. */
  25. PostPerformanceModule.prototype.init = function() {
  26. var that = this;
  27. this.fetch().done(function(data) {
  28. that.currentValue = {
  29. reads: Math.round(data.reads * 0.9999),
  30. comments: Math.round(data.comments * 0.9),
  31. shares: Math.round(data.shares * 0.99)
  32. };
  33.  
  34. that.update(that.currentValue);
  35. that.setNextValue(data);
  36. that.start();
  37. });
  38.  
  39. }
  40.  
  41. /**
  42. * Fetch data from API
  43. * @returns {jqXHR}
  44. */
  45. PostPerformanceModule.prototype.fetch = function() {
  46. var that = this;
  47. return $.get(this.apiUrl).fail(function() {
  48. console.log("error fetching data, try again");
  49. })
  50. }
  51.  
  52. /**
  53. * Sets next value
  54. * @param data {Object} — { reads: .., comments: ..., shares: ..., refresh_interval: ...}
  55. */
  56. PostPerformanceModule.prototype.setNextValue = function(data) {
  57. this.nextValue = {
  58. reads: data.reads,
  59. comments: data.comments,
  60. shares: data.shares
  61. };
  62.  
  63. this.refreshInterval = data.refresh_interval
  64. }
  65.  
  66. /**
  67. * Sets current value
  68. * @param data {Object} — { reads: .., comments: ..., shares: ..., refresh_interval: ...}
  69. */
  70. PostPerformanceModule.prototype.setCurrentValue = function() {
  71. var re = new RegExp(/,/g);
  72.  
  73. this.currentValue = {
  74. reads: parseInt(this.$reads.text().replace(re, '')),
  75. comments: parseInt(this.$comments.text().replace(re, '')),
  76. shares: parseInt(this.$shares.text().replace(re, ''))
  77. };
  78. }
  79.  
  80. /**
  81. * Update counters in DOM
  82. * @param data {Object} — { reads: .., comments: ..., shares: ...}
  83. */
  84. PostPerformanceModule.prototype.update = function(data) {
  85. var re = new RegExp(/(\d)(?=(\d\d\d)+([^\d]|$))/g);
  86. this.$reads.text(('' + Math.round(data.reads)).replace(re, '$1,'));
  87. this.$shares.text(('' + Math.round(data.shares)).replace(re, '$1,'));
  88. this.$comments.text(('' + Math.round(data.comments)).replace(re, '$1,'));
  89. }
  90.  
  91. /**
  92. * Starts animation
  93. */
  94. PostPerformanceModule.prototype.start = function() {
  95. var that = this;
  96. // do not do animation if new value the same
  97. if (!objectEquals(this.currentValue, this.nextValue)) {
  98. $(this.currentValue).animate(this.nextValue, {
  99. duration: this.refreshInterval * 1000,
  100. easing: 'easeOutSine',
  101. progress: function(promise, progress, remainingMs) {
  102. that.update(this);
  103. if (remainingMs < that.threshold && !that.fetchDone) {
  104. that.fetchDone = true;
  105. that.fetch().done(function(data) {
  106. that.setNextValue(data);
  107. });
  108. }
  109. },
  110. complete: function() {
  111. that.fetchDone = false;
  112. that.setCurrentValue();
  113. that.start();
  114. }
  115. }
  116. );
  117. } else {
  118. //wait for refreshInterval - threshold
  119. setTimeout(function() {
  120. // fetch data
  121. that.fetch().done(function(data) {
  122. that.setNextValue(data);
  123. // after threshold start()
  124. setTimeout(function() {
  125. that.start();
  126. }, this.threshold);
  127. });
  128. }, (this.refreshInterval * 1000) - this.threshold);
  129. }
  130. };
  131.  
  132. var postPerformance = new PostPerformanceModule('.b-ss-tower');
  133. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement