PageRenderTime 113ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/js/autoturisme.js

https://gitlab.com/adiclepcea/fisaautoturism
JavaScript | 168 lines | 154 code | 13 blank | 1 comment | 13 complexity | dc7cf2620ad362e95057d95c25adf08a MD5 | raw file
  1. var Autoturism = Backbone.Model.extend({
  2. url: "/autoturisme",
  3. defaults:{
  4. nr:"",
  5. type:"",
  6. city_consumption:0,
  7. out_of_city_consumption:0,
  8. mixt_consumption:0,
  9. fuel_type:""
  10. },
  11. className: "row",
  12. validate: function(attrs){
  13. var errors = [];
  14. if(!attrs.nr){
  15. errors.push({name:"addNr", message:"Trebuie sa introduceti numarul masinii!"});
  16. }
  17. if(!attrs.type){
  18. errors.push({name:"addType",message:"Trebuie sa introduceti marca masinii!"});
  19. }
  20. if(isNaN(attrs.city_consumption) || attrs.city_consumption==""){
  21. errors.push({name:"addCityConsumption",message:"Trebuie sa introduceti un consum in oras!"});
  22. }
  23. if(isNaN(attrs.out_of_city_consumption) || attrs.out_of_city_consumption==""){
  24. errors.push({name:"addOutOfCityConsumption",message:"Trebuie sa introduceti un consum extraurban!"});
  25. }
  26. if(isNaN(attrs.mixt_consumption) || attrs.mixt_consumption==""){
  27. errors.push({name:"addMixtConsumption",message:"Trebuie sa introduceti un consum mixt!"});
  28. }
  29. if(attrs.fuel_type!='Benzina' && attrs.fuel_type!='Diesel'){
  30. errors.push({name:"addFuelType",message:"Trebuie sa introduceti un combustibil!"});
  31. }
  32. return errors.length > 0 ? errors: false;
  33. }
  34. });
  35. var Autoturisme = Backbone.Collection.extend({
  36. model: Autoturism
  37. });
  38. var autoturisme = new Autoturisme([{nr:"AR-48-SLC",type:"skoda",city_consumption:"2",
  39. out_of_city_consumption:"2",mixt_consumption:"2",fuel_type:"Benzina"},
  40. {nr:"AR-11-NNG",type:"VW",city_consumption:"2",
  41. out_of_city_consumption:"2",mixt_consumption:"2",fuel_type:"Benzina"}]);
  42. var AutoturismView = Backbone.View.extend({
  43. tagName:"tr",
  44. template: _.template($("#tmplAutoturism").html()),
  45. model: new Autoturism({nr:"AR-48-SLC",type:"",city_consumption:"",
  46. out_of_city_consumption:"",mixt_consumption:"",fuel_type:"Benzina"}),
  47. events: {
  48. "click .editAutoturism": 'editAuto',
  49. "click .cancelAutoturism": 'cancelAuto',
  50. "click .delAutoturism": 'delAuto',
  51. "click .saveAutoturism": 'saveAuto'
  52. },
  53. editAuto: function(e){
  54. $(".editAutoturism").hide();
  55. $(".delAutoturism").hide();
  56. this.$(".saveAutoturism").show();
  57. this.$(".cancelAutoturism").show();
  58. nr=this.$(".nr").html();
  59. type=this.$(".type").html();
  60. city_consumption=this.$(".city_consumption").html();
  61. out_of_city_consumption=this.$(".out_of_city_consumption").html();
  62. mixt_consumption=this.$(".mixt_consumption").html();
  63. fuel_type=this.$(".fuel_type").html();
  64. this.$(".nr").html("<input type='TEXT' class='form-control nr-update' value='"+nr+"'/>");
  65. this.$(".type").html("<input type='TEXT' class='form-control type-update' value='"+type+"'/>");
  66. this.$(".city_consumption").html("<input type='TEXT' class='form-control city_consumption-update' value='"+city_consumption+"'/>");
  67. this.$(".out_of_city_consumption").html("<input type='TEXT' class='form-control out_of_city_consumption-update' value='"+out_of_city_consumption+"'/>");
  68. this.$(".mixt_consumption").html("<input type='TEXT' class='form-control mixt_consumption-update' value='"+mixt_consumption+"'/>");
  69. this.$(".fuel_type").html("<select class='form-control fuel_type-update'><option value='Benzina' "+(fuel_type=="Benzina"?"selected":"")+">Benzina</option><option value='Diesel' "+(fuel_type=="Diesel"?"selected":"")+">Diesel</option></select>");
  70. },
  71. cancelAuto: function(e){
  72. $(".editAutoturism").show();
  73. $(".delAutoturism").show();
  74. $(".saveAutoturism").hide();
  75. $(".cancelAutoturism").hide();
  76. this.render();
  77. },
  78. delAuto : function(e){
  79. if(!confirm("Sigur doriti stergerea autoturismului selectat?")){
  80. this.cancelAuto();
  81. return;
  82. }
  83. this.model.destroy();
  84. },
  85. saveAuto: function(){
  86. this.model.set("nr",this.$(".nr-update").val());
  87. this.model.set("type",this.$(".type-update").val());
  88. this.model.set("city_consumption",this.$(".city_consumption-update").val());
  89. this.model.set("out_of_city_consumption",this.$(".out_of_city_consumption-update").val());
  90. this.model.set("mixt_consumption",this.$(".mixt_consumption-update").val());
  91. this.model.set("fuel_type",this.$(".fuel_type-update").val());
  92. this.render();
  93. this.cancelAuto();
  94. },
  95. initialize: function(){
  96. this.listenTo(this.model,"remove",this.remove);
  97. this.render();
  98. },
  99. render: function(){
  100. this.$el.html(this.template(this.model.toJSON()));
  101. return this;
  102. }
  103. });
  104. var AutoturismeView = Backbone.View.extend({
  105. el: $("#container"),
  106. template: _.template($("#tmplAutoturisme").html()),
  107. model: autoturisme,
  108. initialize: function(){
  109. this.render();
  110. self = this;
  111. this.model.on("invalid",function(error){
  112. var err = "";
  113. self.clearErrorSigns();
  114. _.each(error.validationError, function(error){
  115. err+=error.message+"\n";
  116. $("#div"+error.name).addClass("has-error");
  117. });
  118. alert(err);
  119. });
  120. this.model.on("remove",this.render,this);
  121. },
  122. clearErrorSigns: function(){
  123. $("div .form-group").removeClass("has-error");
  124. },
  125. events:{
  126. "click #addAutoturism":'add'
  127. },
  128. add: function(e){
  129. e.preventDefault();
  130. var a = new Autoturism({
  131. nr:$("#addNr").val(),
  132. type:$("#addType").val(),
  133. city_consumption:$("#addCityConsumption").val(),
  134. out_of_city_consumption:$("#addOutOfCityConsumption").val(),
  135. mixt_consumption:$("#addMixtConsumption").val(),
  136. fuel_type:$("#addFuelType").val()
  137. });
  138. if(this.model.create(a).validationError==null){
  139. this.render();
  140. }
  141. //console.log(this.model.add([a],{validate:true})[0]);
  142. },
  143. render: function(){
  144. $("#MainMenu").find("li").removeClass("active");
  145. $('a[href="#Autoturisme"]').parent().addClass("active");
  146. $("#header").html("");
  147. this.$el.html(this.template(this.model));
  148. _.each(this.model.toArray(),function(a){
  149. if(a.validationError==null){
  150. $("#tblAutoturisme tbody").append(new AutoturismView({model:a}).render().el);
  151. }
  152. });
  153. return this;
  154. }
  155. });