1 line
No EOL
1.7 KiB
JavaScript
1 line
No EOL
1.7 KiB
JavaScript
$(document).ready(function(){function t(){const t=["red_car","blue_car","green_car","orange_car","blue_bicycle","red_bicycle","green_bicycle","orange_bicycle"];return t[Math.floor(Math.random()*t.length)]}const e=$("#calculatorForm"),a=$("#formItems"),n=$("#item"),r=$("#itemcount"),i=$("#items"),o=$("#content"),l=$("#copyButton"),c=$("#downloadButton"),u=$("#minus"),s=$("#plus"),d=$("input[name='quant']");u.click(function(t){const e=parseInt(d.attr("value"));isNaN(e)?d.attr("value","0"):(console.log("#iteminput"+e),e>d.attr("min")&&d.attr("value",(e-1).toString()),e-1==d.attr("min")&&$(this).attr("disabled",!0),$("#iteminput"+e).remove())}),s.click(function(e){const a=parseInt(d.attr("value"));if(isNaN(a))d.attr("value","0");else{if(a<d.attr("max")&&d.attr("value",(a+1).toString()),a==d.attr("max"))return void $(this).attr("disabled",!0);parseInt(d.attr("value"))>1?u.prop("disabled",!1):u.prop("disabled",!0),i.append('<div id="iteminput'+(a+1)+'" class="mb-3 w-50"><label for="item'+(a+1)+'">Model for damage '+(a+1)+':</label><input type="text" class="form-control" id="item'+(a+1)+'" name="item'+(a+1)+'" placeholder="cars/'+t()+'" required></div>')}}),n.change(function(){if(""!==this.value){a.show();const t=itemDamages[this.value];r.prop("max",t),s.prop("disabled",!1)}else a.hide()}),e.on("submit",function(t){t.preventDefault();const e=n.val(),a=$(".btn-number").attr("data-field"),r=$("input[name='"+a+"']"),i=parseInt(r.attr("value")),l=[];for(let t=0;t<i;t++)l[t]=$("#item"+(t+1)).val();const u=buildJSON(e,l);o.html(syntaxHighlight(u));const s="data:text/json;charset=utf-8,"+encodeURIComponent(u);return c.attr("href",s),c.attr("download",e+".json"),!1}),l.click(function(t){t.preventDefault(),navigator.clipboard.writeText(o.text())})}); |