PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/src/middleware/live_logger/public/app.js

http://github.com/mdp/middlefiddle
JavaScript | 203 lines | 158 code | 34 blank | 11 comment | 19 complexity | c5aa20c6e4b2e34af9a8836512bbcc76 MD5 | raw file
  1. // Load the application once the DOM is ready, using `jQuery.ready`:
  2. $(function(){
  3. // Router
  4. window.Router = Backbone.Router.extend({
  5. routes: {
  6. "request/:id": "showRequest",
  7. "": "index",
  8. "/": "index"
  9. },
  10. showRequest: function(id){
  11. App.rememberScrollPosition();
  12. var reqDetails = new RequestDetail({id: id});
  13. reqDetails.fetch({
  14. success: function(model){
  15. var view = new RequestDetailView({model: model});
  16. view.render();
  17. }
  18. });
  19. $("#list-view").hide();
  20. },
  21. index: function(){
  22. $("#list-view").show();
  23. App.setScrollPosition();
  24. $("#detail-view").hide();
  25. }
  26. });
  27. // Request Model
  28. // ----------
  29. window.Request = Backbone.Model.extend({
  30. statusClass: function(){
  31. var status = this.attributes.status;
  32. if (status >= 500) {
  33. return 'five_hundred';
  34. } else if (status >= 400) {
  35. return 'four_hundred';
  36. } else if (status >= 300) {
  37. return 'three_hundred';
  38. } else if (status >= 200) {
  39. return 'two_hundred';
  40. } else {
  41. return 'two_hundred';
  42. }
  43. }
  44. });
  45. window.Requests = Backbone.Collection.extend({
  46. model: Request,
  47. url: function() {
  48. return '/all'
  49. }
  50. });
  51. window.RequestDetail = Backbone.Model.extend({
  52. url: function(){
  53. return "/" + this.id;
  54. }
  55. });
  56. // The DOM element for an individual request...
  57. window.RequestView = Backbone.View.extend({
  58. tagName: 'li',
  59. // Cache the template function for a single item.
  60. template: $("#request-item").html(),
  61. container: $('#list-view'),
  62. events: {
  63. "click": "openRequestDetail"
  64. },
  65. openRequestDetail: function(){ Controller.navigate("request/" + this.model.id, true) },
  66. // Render the Request
  67. render: function() {
  68. var req = $.mustache(this.template, this.model.attributes);
  69. $(this.el).attr('class', this.model.get('method') + ' ' + this.model.statusClass());
  70. $(this.el).html(req);
  71. return this;
  72. }
  73. });
  74. window.RequestDetailView = Backbone.View.extend({
  75. // Cache the template function for a single item.
  76. template: $("#requestDetailTemplate").html(),
  77. container: $('#detail-view'),
  78. events: {
  79. "click": "closeRequestDetail"
  80. },
  81. closeRequestDetail: function(){ Controller.navigate("/", true) },
  82. // Render the Request
  83. render: function() {
  84. var reqDetail = $.mustache(this.template, this.model.toJSON());
  85. this.container.show();
  86. this.container.html(reqDetail);
  87. App.disableScrolling();
  88. return this;
  89. }
  90. });
  91. window.ScrollButtonView = Backbone.View.extend({
  92. className: 'scroll-btn',
  93. render: function(){
  94. $(this.el).html(req);
  95. }
  96. });
  97. // The Application
  98. // ---------------
  99. window.AppView = Backbone.View.extend({
  100. initialize: function() {
  101. var self = this;
  102. this.Requests = new Requests;
  103. window.socket.on('request', function (data) {
  104. self.Requests.add(data['request'])
  105. });
  106. this.Requests.bind('add', function(model){
  107. var view = new RequestView({model: model});
  108. $("ul#requests").append(view.render().el);
  109. if (window.scrollWithIt == true) {
  110. window.scrollTo(0, document.body.scrollHeight);
  111. }
  112. });
  113. this.Requests.bind('reset', function(models){
  114. _.each(models.models, function(model){
  115. var view = new RequestView({model: model});
  116. $("ul#requests").append(view.render().el);
  117. });
  118. window.scrollTo(0, document.body.scrollHeight);
  119. });
  120. this.Requests.fetch();
  121. },
  122. disableScrolling: function(){
  123. window.scrollWithIt = false;
  124. $('#scroll-btn').attr('class', '');
  125. },
  126. enableScrolling: function(){
  127. window.scrollWithIt = true;
  128. $('#scroll-btn').attr('class', 'active');
  129. window.scrollTo(0, document.body.scrollHeight);
  130. },
  131. rememberScrollPosition: function(){
  132. if (window.scrollWithIt === true) {
  133. this.disableScrolling();
  134. this.lastScrollPosition = -1;
  135. } else {
  136. this.lastScrollPosition = window.pageYOffset;
  137. }
  138. },
  139. setScrollPosition: function(){
  140. if (this.lastScrollPosition >= 0) {
  141. window.scrollTo(0, this.lastScrollPosition);
  142. } else {
  143. this.enableScrolling();
  144. }
  145. },
  146. scrollEvent: function(){
  147. if (window.pageYOffset < this.lastScrollPosition){
  148. this.disableScrolling();
  149. }
  150. if (window.scrollWithIt === true) {
  151. this.lastScrollPosition = window.pageYOffset;
  152. }
  153. },
  154. toggleScrolling: function() {
  155. if (window.scrollWithIt == true) {
  156. this.disableScrolling();
  157. } else {
  158. this.enableScrolling();
  159. }
  160. }
  161. });
  162. window.socket = io.connect('/');
  163. window.App = new AppView;
  164. window.Controller = new Router;
  165. Backbone.history.start();
  166. $('#scroll-btn').click(function() {
  167. App.toggleScrolling();
  168. });
  169. });