/public/js/application.js

https://github.com/etagwerker/chartly · JavaScript · 287 lines · 221 code · 55 blank · 11 comment · 7 complexity · cc2327a7df84a23ad7e320c8bc46da29 MD5 · raw file

  1. $(document).ready(
  2. function() {
  3. function tidy_id(_id) {
  4. _id = _id.replace('í','i');
  5. _id = _id.replace('ú','u');
  6. _id = _id.replace('ó','o');
  7. _id = _id.replace('é','e');
  8. _id = _id.replace('á','a');
  9. return _id;
  10. };
  11. window.Population = Backbone.Model.extend({
  12. url: function() {
  13. var area = this.get('area');
  14. return area.population_url();
  15. },
  16. parse: function(response) {
  17. this.set({total_mujeres: response.total_mujeres, total_varones: response.total_varones});
  18. }
  19. });
  20. window.Provincia = Backbone.Model.extend({
  21. population_url: function() {
  22. return 'http://censo.heroku.com/poblacion/' + tidy_id(this.get('id')) + "/totales?callback=?";
  23. },
  24. formatted_name: function() {
  25. return this.get('nombre');
  26. },
  27. type: function() {
  28. return 'provincia';
  29. },
  30. departamentos: function() {
  31. if (this.get('departamentos')) {
  32. this.get('departamentos').each(window.DepartamentosPartialView.addOne);
  33. return this.get('departamentos');
  34. } else {
  35. var provincia = this;
  36. var collection = new DepartamentosCollection();
  37. collection.fetch({success: function() {
  38. provincia.set({departamentos: collection});
  39. collection.each(function(departamento) {
  40. departamento.set({provincia: provincia});
  41. });
  42. collection.each(window.DepartamentosPartialView.addOne);
  43. }
  44. });
  45. }
  46. }
  47. });
  48. window.Departamento = Backbone.Model.extend({
  49. population_url: function() {
  50. var pcia = this.get('provincia');
  51. return 'http://censo.heroku.com/poblacion/' + tidy_id(pcia.get('id')) + '/' + tidy_id(this.get('id')) + "/totales?callback=?";
  52. },
  53. formatted_name: function() {
  54. var pcia = this.get('provincia')
  55. var name = this.get('nombre');
  56. var id = pcia.get('id');
  57. if (id == "ciudad_autónoma_de_buenos_aires") {
  58. return "Comuna " + name;
  59. } else {
  60. return name;
  61. }
  62. },
  63. type: function() {
  64. return 'departamento';
  65. }
  66. });
  67. // class DepartamentosCollection
  68. window.DepartamentosCollection = Backbone.Collection.extend({
  69. model: Departamento,
  70. url: function() {
  71. return 'http://censo.heroku.com/' + tidy_id(window.CurrentProvincia.get('id')) + '/departamentos?callback=?';
  72. }
  73. });
  74. // class ProvinciasCollection
  75. window.ProvinciasCollection = Backbone.Collection.extend({
  76. url: 'http://censo.heroku.com/provincias?callback=?',
  77. model: Provincia
  78. });
  79. // object Provincias
  80. window.Provincias = new ProvinciasCollection();
  81. // object Departamentos
  82. window.Departamentos = new DepartamentosCollection();
  83. window.ProvinciaPopulationView = Backbone.View.extend({
  84. population_graph: function(population) {
  85. var data = new google.visualization.DataTable();
  86. data.addColumn('string', 'Sexo');
  87. data.addColumn('number', 'Personas');
  88. data.addRows(2);
  89. data.setValue(0, 0, 'Varones');
  90. data.setValue(0, 1, population.get('total_varones'));
  91. data.setValue(1, 0, 'Mujeres');
  92. data.setValue(1, 1, population.get('total_mujeres'));
  93. // Create and draw the visualization.
  94. var chart = new google.visualization.PieChart(document.getElementById(this.model.type() + '-population-graph'));
  95. var area = population.get('area');
  96. chart.draw(data, {width: 450, height: 300, title: 'Personas en ' + area.get('nombre') });
  97. },
  98. render: function() {
  99. var population = this.model.get('population');
  100. $(this.el).html(this.template(population.toJSON()));
  101. return this;
  102. },
  103. initialize: function() {
  104. var v = this;
  105. var p = new Population({area: this.model, view: v});
  106. p.fetch({success: function(){
  107. v.model.set({population: p});
  108. $("div#" + v.model.type() + "-population-detail").html(v.render().el);
  109. v.population_graph(p);
  110. if (v.model.type() == "provincia") {
  111. window.DepartamentosPartialView = new DepartamentosView({model: v.model});
  112. }
  113. }
  114. });
  115. },
  116. template: _.template($('#provincia-population-template').html())
  117. });
  118. window.DepartamentoPopulationView = Backbone.View.extend({
  119. template: _.template($('#departamento-population-template').html()),
  120. initialize: function() {
  121. var v = this;
  122. var p = new Population({area: this.model, view: v});
  123. p.fetch({success: function(){
  124. v.model.set({population: p});
  125. $("div#" + v.model.type() + "-population-detail").html(v.render().el);
  126. v.population_graph(p);
  127. }
  128. });
  129. },
  130. population_graph: function(population) {
  131. var data = new google.visualization.DataTable();
  132. data.addColumn('string', 'Sexo');
  133. data.addColumn('number', 'Personas');
  134. data.addRows(2);
  135. data.setValue(0, 0, 'Varones');
  136. data.setValue(0, 1, population.get('total_varones'));
  137. data.setValue(1, 0, 'Mujeres');
  138. data.setValue(1, 1, population.get('total_mujeres'));
  139. // Create and draw the visualization.
  140. var chart = new google.visualization.PieChart(document.getElementById(this.model.type() + '-population-graph'));
  141. var area = population.get('area');
  142. chart.draw(data, {width: 450, height: 300, title: 'Personas en ' + area.get('nombre') });
  143. },
  144. render: function() {
  145. var population = this.model.get('population');
  146. $(this.el).html(this.template(population.toJSON()));
  147. return this;
  148. }
  149. });
  150. window.DepartamentoView = Backbone.View.extend({
  151. template: _.template($('#departamento-template').html()),
  152. tagName: "li",
  153. className: "departamento",
  154. events: {
  155. "click" : "select"
  156. },
  157. initialize: function() {
  158. },
  159. select: function() {
  160. window.CurrentDepartamento = this.model;
  161. this.model.set({selected: true});
  162. new DepartamentoPopulationView({model: this.model});
  163. },
  164. render: function() {
  165. $(this.el).html(this.template({formatted_name: this.model.formatted_name()}));
  166. return this;
  167. }
  168. });
  169. window.ProvinciaView = Backbone.View.extend({
  170. template: _.template($('#provincia-template').html()),
  171. className: "provincia",
  172. tagName: "li",
  173. events: {
  174. "click" : "select"
  175. },
  176. initialize: function() {
  177. },
  178. select: function() {
  179. window.CurrentProvincia = this.model;
  180. this.model.set({selected: true});
  181. new ProvinciaPopulationView({model: this.model});
  182. },
  183. render: function() {
  184. $(this.el).html(this.template(this.model.toJSON()));
  185. return this;
  186. }
  187. });
  188. window.DepartamentosView = Backbone.View.extend({
  189. initialize: function() {
  190. $("ul#departamentos-list").html("");
  191. this.model.departamentos();
  192. },
  193. // Add a single departamento to the list by creating a view for it, and
  194. // appending its element to the `<ul>`.
  195. addOne: function(departamento) {
  196. var view = new DepartamentoView({model: departamento});
  197. $("ul#departamentos-list").append(view.render().el);
  198. }
  199. });
  200. window.ProvinciasView = Backbone.View.extend({
  201. initialize: function() {
  202. this.addAll();
  203. },
  204. // Add a single provincia to the list by creating a view for it, and
  205. // appending its element to the `<ul>`.
  206. addOne: function(provincia) {
  207. console.log("Added: " + provincia.get('id'));
  208. var view = new ProvinciaView({model: provincia});
  209. $("ul#provincias-list").append(view.render().el);
  210. },
  211. // Add all items in the **Provincias** collection at once.
  212. addAll: function() {
  213. window.Provincias.each(this.addOne);
  214. }
  215. });
  216. window.AppView = Backbone.View.extend({
  217. el: $("#charts-app"),
  218. initialize: function() {
  219. window.Provincias.fetch({
  220. success: function() {
  221. console.log('Provincias cargadas: ' + window.Provincias.length + '.');
  222. new ProvinciasView;
  223. },
  224. error: function() {
  225. console.log('Error cargando provincias!');
  226. }
  227. });
  228. }
  229. });
  230. window.App = new AppView;
  231. }
  232. );