PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/city_aware/templates/home.html

https://github.com/hack4reno2011/Rock-Star-Scientists
HTML | 315 lines | 281 code | 34 blank | 0 comment | 0 complexity | 136c902e411972a8d1e8e11ed95e2c85 MD5 | raw file
  1. {% extends "foundation.html" %}
  2. {% load url from future %}
  3. {% block headers %}
  4. <script src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
  5. <style type="text/css">
  6. table#calendar {
  7. width:500px;
  8. margin:auto;
  9. }
  10. table#calendar td {
  11. width:150px;
  12. height:75px;
  13. border:1px solid black;
  14. margin:3px;
  15. padding:3px;
  16. }
  17. table#calendar td:hover {
  18. background-color:#55a;
  19. color:white;
  20. }
  21. table#calendar td.active {
  22. background-color:#55a;
  23. color:white;
  24. }
  25. #mapCanvas {
  26. width: 100%;
  27. height: 500px;
  28. }
  29. #main {
  30. padding:0;
  31. margin:0;
  32. }
  33. .counter {
  34. font-size:200%;
  35. }
  36. </style>
  37. <script>
  38. var buttonMenu = "<button id='refresh'>Refresh</button><button id='sortByName'>Sort by Name</button><button id='sortById'>Sort by ID</button><input type='text'></input><button id='new'>New Item</button><ul></ul>";
  39. var noIdItem = '<div class="label"><%= name %><div class="options"><button class="delete">x</button></div></div>';
  40. var editTemp = '<div class="editHolder"><input class="labelInput" value="<%= name %>"></input><div class="options"><button class="delete">x</button></div></div>';
  41. var data = []
  42. var blueIcon = new google.maps.MarkerImage(google.maps.G_DEFAULT_ICON);
  43. blueIcon.url = "http://www.google.com/intl/en_us/mapfiles/ms/micons/blue-dot.png";
  44. function addMarker(map, item) {
  45. //use the object's properties
  46. var venue = item.get('venue');
  47. var position = new google.maps.LatLng(venue['latitude'], venue['longitude']);
  48. console.log("drawing marker");
  49. marker = new google.maps.Marker({
  50. title: item.get('name'),
  51. position: position,
  52. map: map,
  53. draggable: true,
  54. raiseOnDrag: true
  55. });
  56. var closeInfos;
  57. google.maps.event.addListener(marker, 'click', function() {
  58. var info;
  59. console.log('marker clicked');
  60. closeInfos();
  61. info = new google.maps.InfoWindow({
  62. content: "<a href='/events/" + item.get('id') + "'>" + this.title + "</a>"
  63. });
  64. info.open(map, this);
  65. return infos[0] = info;
  66. });
  67. closeInfos = function() {
  68. if (infos.length > 0) {
  69. infos[0].set('marker', null);
  70. infos[0].close;
  71. return infos.length = 0;
  72. }
  73. };
  74. }
  75. infos = [];
  76. //google.maps.event.addListener(marker, 'click', function() {
  77. // map.setZoom(8);
  78. // });
  79. $(function() {
  80. var defaultLat = 39.52546, defaultLong = -119.81423;
  81. //initial focus
  82. var myOptions = {
  83. zoom: 13,
  84. center: new google.maps.LatLng(defaultLat, defaultLong),
  85. mapTypeId: google.maps.MapTypeId.ROADMAP
  86. };
  87. var map = new google.maps.Map(document.getElementById("mapCanvas"), myOptions);
  88. var BusStop = Backbone.Model.extend({
  89. defaults: {'name': "New Bus Stop", 'latitude': defaultLat, 'longitude': defaultLong},
  90. urlRoot: '/api/events/',
  91. });
  92. //var BusStopView = Backbone.View.extend({
  93. //})
  94. var Item = Backbone.Model.extend({
  95. defaults: {'name': "New Bus Stop", 'latitude': defaultLat, 'longitude': defaultLong},
  96. urlRoot: '/api/events/',
  97. });
  98. var List = Backbone.Collection.extend({
  99. model: Item,
  100. url: '/api/events/',
  101. parse: function(data) {
  102. return data.objects;
  103. },
  104. comparator: function(item) {
  105. return item.get('id');
  106. }
  107. });
  108. var ItemView = Backbone.View.extend({
  109. tagName: 'li',
  110. className: 'green',
  111. events: {
  112. 'click button.delete': 'remove',
  113. 'click div.label': "moveMap",
  114. 'dblclick div.label': "editLabel",
  115. },
  116. initialize: function() {
  117. _.bindAll(this, 'render', 'unrender', 'remove', 'editLabel', 'saveLabel', "moveMap");
  118. this.model.bind('change', this.render);
  119. this.model.bind('remove', this.unrender);
  120. },
  121. render: function() {
  122. $(this.el).html(_.template(noIdItem, this.model.toJSON()));
  123. //TODO create the google maps object
  124. if (!this.model.get('is_drawn')) {
  125. //TODO tie the models together
  126. this.model.set({'is_drawn': true, 'marker': addMarker(map, this.model)});
  127. }
  128. return this;
  129. },
  130. editLabel: function() {
  131. //change the template to editing mode
  132. $(this.el).html(_.template(editTemp, this.model.toJSON()))
  133. $('input.labelInput', this.el).change(this.saveLabel);
  134. return this;
  135. },
  136. saveLabel: function() {
  137. //TODO confirm new text, delete if empty
  138. var newText = $('input.labelInput', this.el).val();
  139. this.model.set({'name': newText});
  140. this.model.save();
  141. },
  142. unrender: function() {
  143. $(this.el).remove();
  144. console.log("unrender");
  145. if (this.model.get('is_drawn')) {
  146. console.log("removing market");
  147. this.model.get('marker').setMap(null);
  148. }
  149. },
  150. remove: function() {
  151. //this.model.destroy();
  152. console.log("preparing to destroy:", this.model);
  153. this.model.destroy({error: function(model, response) {
  154. console.log("DELETE error:", response);
  155. },
  156. success: function(model, response) {
  157. //apparently, reports success on 301 ?!?!
  158. console.log("DELETE success:", model, response);
  159. }
  160. });
  161. },
  162. moveMap: function() {
  163. //change the color of the icon
  164. if (this.model.get('is_drawn')) {
  165. console.log("moving map");
  166. var marker = this.model.get('marker');
  167. marker.setMap(null);
  168. marker.icon = blueIcon;
  169. marker.setMap(map);
  170. map.setCenter(marker.getPosition())
  171. }
  172. }
  173. });
  174. var iList = (new List()).add(data);
  175. var AppView = Backbone.View.extend({
  176. el: $('#container'),
  177. events: {
  178. 'click button#refresh': "refreshList",
  179. 'click button#new': "newItem",
  180. 'click button#sortByName': "sortByName",
  181. 'click button#sortById': "sortById",
  182. },
  183. initialize: function() {
  184. _.bindAll(this, 'render', 'addItem', 'appendItem', 'sortByName', 'renderList', 'sortById', 'newItem', 'refreshList');
  185. this.collection = iList.bind('add', this.appendItem);
  186. this.sortOrder = -1;
  187. this.render();
  188. //figure out the current Date
  189. var newDate = new Date();
  190. var tomorrowDate = (new Date()).setDate(newDate.getDate() + 1);
  191. //TODO wahhhhhhhhhh
  192. var todayStr = newDate.getFullYear() + "-" + (newDate.getMonth() + 1) + "-" + newDate.getDate();
  193. var tommorowStr = newDate.getFullYear() + "-" + (newDate.getMonth() + 1) + "-" + (newDate.getDate() + 1);
  194. var yesterdayStr = newDate.getFullYear() + "-" + (newDate.getMonth() + 1) + "-" + (newDate.getDate() - 1);
  195. //$('#yesterdayBlock').html(yesterdayStr);
  196. //$('#todayBlock').html("This Week" + todayStr);
  197. //$('#tomorrowBlock').html(tommorowStr);
  198. //console.log(todayStr, tommorowStr, yesterdayStr);
  199. this.collection = iList.bind('reset', this.renderList);
  200. this.collection.fetch({data: {end_time__gte:'2011-10-17', start_time__lt:'2011-10-24'}}); //fires 'reset'
  201. //this.collection.fetch({add: true}); //otherwise list will 'reset'
  202. //this.collection.fetch({add: true, data: {offset: 50}}); //otherwise list will 'reset'
  203. //this.collection.sort(); //should fire 'reset'
  204. },
  205. render: function() {
  206. $(this.el).append(_.template(buttonMenu, {}));
  207. this.renderList();
  208. },
  209. renderList: function() {
  210. $('ul', this.el).empty();
  211. // _.each?
  212. console.log('fetched ' + this.collection.models.length + ' items');
  213. $('#todayCount').html(this.collection.models.length + ' items');
  214. for (var i = 0; i < this.collection.models.length; i++) {
  215. this.appendItem(this.collection.models[i]);
  216. }
  217. },
  218. addItem: function() {
  219. this.collection.add(new Item({'name': "new bus stop"}));
  220. },
  221. appendItem: function(item) {
  222. var itemView = new ItemView({
  223. model: item
  224. });
  225. $('ul', this.el).append(itemView.render().el);
  226. },
  227. sortByName: function() {
  228. this.sortOrder = -this.sortOrder;
  229. this.collection.comparator = buildNameComparator(this.sortOrder);
  230. this.collection.sort();
  231. },
  232. sortById: function() {
  233. this.sortOrder = -this.sortOrder;
  234. this.collection.comparator = buildIdComparator(this.sortOrder);
  235. this.collection.sort();
  236. },
  237. newItem: function() {
  238. var newText = $('input', this.el).val();
  239. if (newText) {
  240. //TODO not empty?
  241. var randItem = new Item({'name': newText});
  242. randItem.save({}, {success: buildSuccessReponse(this.collection)});
  243. var newText = $('input', this.el).val("");
  244. }
  245. },
  246. refreshList: function() {
  247. this.collection.fetch(); //fires 'reset'
  248. }
  249. });
  250. function buildIdComparator(sortOrder) {
  251. return function(country) {
  252. return sortOrder * country.get('id');
  253. }
  254. }
  255. function buildNameComparator(sortOrder) {
  256. return function(item) {
  257. return _.map(item.get("name").toLowerCase().split(""), function(letter) {
  258. return String.fromCharCode(sortOrder * (letter.charCodeAt(0)));
  259. });;
  260. }
  261. }
  262. function buildSuccessReponse(collection) {
  263. return function(model, response) {
  264. collection.add(model);
  265. collection.sort();
  266. }
  267. }
  268. var app = new AppView();
  269. });
  270. </script>
  271. {% endblock %}
  272. {% block content %}
  273. <h3>{{venue.name}}</h3>
  274. <div id="mapCanvas"></div>
  275. <div><table id="calendar">
  276. <tr>
  277. <td align='center' valign='middle'>Last Week<div id='yesterdayBlock'>11-10 to 11-17</div><div class='counter' id='yesterdayCount'></div></td>
  278. <td align='center' valign='middle' class="active">This Week<div id='todayBlock'>11-17 to 11-24</div><div class='counter' id='todayCount'></div></td>
  279. <td align='center' valign='middle'>Next Week<div id='tomorrowBlock'>11-24 to 11-31</div><div class='counter' id='tomorrowCount'></div></td>
  280. </tr>
  281. {% endblock %}