/cctv/static/assets/scripts/app.js

https://bitbucket.org/halbhalb/cctv-webapp · JavaScript · 148 lines · 128 code · 18 blank · 2 comment · 9 complexity · c09585a5f2240fe927da30aeb13e5cea MD5 · raw file

  1. var App = {
  2. Views: {},
  3. Routers: {},
  4. Models: {},
  5. Collections: {},
  6. init: function() {
  7. App.router = new App.Routers.AppRouter();
  8. Backbone.history.start();
  9. }
  10. };
  11. App.Models.Spot = Backbone.Model.extend({
  12. idAttribute: "hash"
  13. });
  14. App.Collections.Spots = Backbone.Collection.extend({
  15. model: App.Models.Spot,
  16. page: 0,
  17. items: null,
  18. fetching: false,
  19. url: function() {
  20. return "/spot/index/page/"+this.page;
  21. },
  22. fetch: function(options) {
  23. var self = this;
  24. if (self.fetching) {
  25. return;
  26. }
  27. self.fetching = true;
  28. self.page = 1;
  29. var success = options.success;
  30. options = options ? _.clone(options) : {};
  31. options.url = self.url();
  32. options.dataType = 'json';
  33. var nextPage = function(data, status, xhr) {
  34. options.url = self.url();
  35. self.page++;
  36. if (self.page==2) {
  37. self.reset();
  38. self.add(data);
  39. Backbone.ajax(options);
  40. } else {
  41. if (data.length) {
  42. self.add(data);
  43. Backbone.ajax(options);
  44. } else {
  45. var method = options.update ? 'update' : 'reset';
  46. self.fetching = false;
  47. if (success) success(self.collection, self.items, options);
  48. }
  49. }
  50. };
  51. options.success = nextPage;
  52. return Backbone.ajax(options);
  53. },
  54. });
  55. App.Views.BaseView = Backbone.View.extend({
  56. template: null,
  57. initialize: function() {
  58. this.render();
  59. },
  60. render: function() {
  61. return this;
  62. }
  63. });
  64. App.Views.Index = App.Views.BaseView.extend({
  65. initialize: function() {
  66. this.initializeMap();
  67. this.markers = {};
  68. this.popups = {};
  69. },
  70. render: function() {
  71. },
  72. addSpot: function (spot) {
  73. var lat = spot.get('latitude');
  74. var lng = spot.get('longitude');
  75. var hash = spot.get('hash');
  76. var self = this;
  77. if (!this.markers[hash]) {
  78. var marker = L.marker([lat, lng]).addTo(self.map);
  79. marker.on("click", function() {
  80. var popup = null;
  81. if (!self.popups[hash]) {
  82. var popupHtml = '<a href="#"><img class="popup-image" src="/spot/read/'+hash+'/image" ></a>';
  83. var popupContent = $(popupHtml);
  84. popupContent.click(function() {
  85. self.map.closePopup();
  86. });
  87. var popup = L.popup({className: 'photo-popup', minWidth: 300})
  88. .setLatLng([lat+0.003, lng])
  89. .setContent(popupContent[0]);
  90. self.popups[hash] = popup;
  91. } else {
  92. popup = self.popups[hash];
  93. }
  94. popup.openOn(self.map);
  95. })
  96. this.markers[hash] = marker;
  97. }
  98. },
  99. initializeMap: function() {
  100. this.map = new L.Map(this.$('#map')[0], {zoomControl:false});
  101. this.map.addControl(new L.Control.Zoom({position:'bottomleft'}));
  102. var osmUrl='http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png';
  103. var osmAttrib='Map data &copy; OpenStreetMap contributors';
  104. var osm = new L.TileLayer(osmUrl, {minZoom:8, attribution:osmAttrib});
  105. this.map.setView(new L.LatLng(51.507, -0.128),13);
  106. this.map.addLayer(osm);
  107. }
  108. });
  109. App.Routers.AppRouter = Backbone.Router.extend({
  110. routes: {
  111. "": "indexAction"
  112. },
  113. indexAction: function() {
  114. var self=this;
  115. self.spots = new App.Collections.Spots();
  116. self.indexView = new App.Views.Index({el:$('#app'), model:self.spots});
  117. self.spots.bind("add",function(model,collection,opts){
  118. self.indexView.addSpot(model);
  119. });
  120. self.spots.fetch({
  121. success:function(collection, response) {
  122. self.indexView.render();
  123. },
  124. error:function(collection, response){
  125. //this happens if you reload the page
  126. //alert( 'Error reading data from the server.');
  127. }
  128. });
  129. }
  130. });