/*! * Quote */ jQuery(function($){var Quote={data:{},getStorage:function(key){var key=key?key:"quote";var data=window.localStorage.getItem(key);if(!data){data={}}else{data=JSON.parse(data)} return data},setStorage:function(key,data){var key=key?key:"quote";var data=JSON.stringify(data);window.localStorage.setItem(key,data);setTimeout(function(){var data=window.localStorage.getItem('quote');if(!data){data={}}else{data=JSON.parse(data)} var total=0;Object.keys(data).map(function(item){total+=data[item].qty;$("#totalOrcamento").html(total)})},500)},syncStorage:function(key,data){var key=key?key:"quote";var data=data?data:this.data;this.setStorage(key,data)},getTypeItem:function(type){if(!this.data[type]){this.data[type]={qty:1,choose:"auto",models:{},}} return this.data[type]},getModelItem:function(type,id){if(!type.models[id]){type.models[id]={qty:1,}} return type.models[id]},fixTypesData:function(data){$.each(data,function(index,item){var size=Object.keys(item.models).length;if(size){var count=0;$.each(item.models,function(i,model){count+=model.qty});item.qty=count;item.choose="manual"}else{item.choose="auto";item.models={}} data[index]=item});return data},triggerUpdate:function(){var self=this;self.data=self.fixTypesData(self.data);self.syncStorage();self.updateQuoteLink();$(window).trigger("reloadQuote")},updateQuoteLink:function(){var count=0;var self=this;$.each(self.data,function(index,item){count+=item.qty});$(".quote-link .count").text(count);if(count){$(".quote-link").addClass("visible")}else{$(".quote-link").removeClass("visible")}},getModelsByType:function(type){var models={};$.each(window.equipments,function(id,item){if(item.type!=type){return} models[id]=item});return models},initForm:function(){if(!$(".page-template-page-quote").length){return} var self=this;var template=$("template#quote-item").html();var templateModel=$("template#quote-item-model").html();var templateQty=$("template#quote-qty").html();var templateModal=$("template#model-template").html();var templateOption=$("template#model-template-option").html();function tag(string){return new RegExp("{"+string+"}","g")} function parseItem(index,type,data){var html=template;var id=window.types[type].id;var title=window.types[type].title;var choose=data.choose;var qty=data.qty;var qtyField=templateQty;var manualChecked="";var autoChecked='checked="checked"';var models="";var i=0;if(choose=="manual"){qtyField="";manualChecked='checked="checked"';autoChecked="";$.each(data.models,function(model,item){models+=parseModel(i,model,item);i++})} var all=self.getModelsByType(type);$.each(all,function(id,item){if(data.models[id]){delete all[id]}});var canAdd=Object.keys(all).length;var itemClass=type=="implemento"?"implement":"equipment";html=html.replace(tag("item-class"),itemClass).replace(tag("models"),models).replace(tag("id"),id).replace(tag("type"),type).replace(tag("title"),title).replace(tag("qty-field"),qtyField).replace(tag("qty"),qty).replace(tag("auto-checked"),autoChecked).replace(tag("manual-checked"),manualChecked).replace(tag("index"),index).replace(tag("position"),index+1);html=jQuery(html);if(choose=="auto"||!canAdd){html.find(".choose-another-model").hide();html.find(".choose-another-implement").hide()} return html[0].outerHTML} function parseModel(index,id,data){var html=templateModel;var equipment=window.equipments[id];var isImplement=equipment.type=="implemento";var manufacturer=equipment.manufacturer;var subtype=equipment.subtype;var id=equipment.id;var model=equipment.model;var image=equipment.image;var qtyField=templateQty;var qty=data.qty;var link="#";var related=[];var name=isImplement?subtype:manufacturer;if(isImplement){related=equipment.equipments;$.each(self.data,function(id,item){var index=related.indexOf(id);if(index!==-1){related.splice(index,1)}});if(related.length){link=window.types[related[0]].link}}else{related=equipment.implements;if(self.data.implemento&&self.data.implemento.models){$.each(self.data.implemento.models,function(id,item){var item=window.equipments[id];var index=related.indexOf(item.subtype);if(index!==-1){related.splice(index,1)}})} if(related.length&&window.types.implemento){link=window.types.implemento.link}} html=html.replace(tag("model-index"),index).replace(tag("id"),id).replace(tag("name"),name).replace(tag("manufacturer"),manufacturer).replace(tag("subtype"),subtype).replace(tag("model"),model).replace(tag("image"),image).replace(tag("qty-field"),qtyField).replace(tag("qty"),qty).replace(tag("link"),link);html=jQuery(html);if(isImplement){html.find(".image-box").hide()} if(!related.length){html.find(".need-implements").hide();html.find(".need-equipments").hide()} return html[0].outerHTML} $(window).on("reloadQuote",function(){var index=0;var html="";$.each(self.data,function(type,item){html+=parseItem(index,type,item);index++});$(".step-one .list").html(html);if(html){$(".choose-equipment").addClass("hide");$(".choose-another-equipment").removeClass("hide");$(".step-two").removeClass("hide");$(".step-three").removeClass("hide")}else{$(".choose-equipment").removeClass("hide");$(".choose-another-equipment").addClass("hide");$(".step-two").addClass("hide");$(".step-three").addClass("hide")} var currentSize=Object.keys(self.data).length;var typesSize=Object.keys(window.types).length;if(currentSize==typesSize){$(".choose-equipment").addClass("hide");$(".choose-another-equipment").addClass("hide")}});$("body").on("change",".group-choose input",function(e){var val=$(this).val();var parent=$(this).parents(".item");var type=parent.data("type");var _type=self.data[type];if(val=="auto"){_type.choose="auto";_type.models={};self.data[type]=_type;self.triggerUpdate();return} parent.find(".choose-another-model").trigger("click");window.setTimeout(function(){self.triggerUpdate()},200)});$("body").on("click",".qty .down",function(e){e.preventDefault();var type=$(this).parents(".item").data("type");var model=$(this).parents(".item-model").data("model");var _type=self.data[type];if(model){var qty=_type.models[model].qty;qty=qty-1;if(qty==0){qty=1} _type.models[model].qty=qty}else{var qty=_type.qty;qty=qty-1;if(qty==0){qty=1} _type.qty=qty} self[type]=_type;self.triggerUpdate()});$("body").on("click",".qty .up",function(e){e.preventDefault();var type=$(this).parents(".item").data("type");var model=$(this).parents(".item-model").data("model");var _type=self.data[type];if(model){var qty=_type.models[model].qty;_type.models[model].qty=qty+1}else{var qty=_type.qty;_type.qty=qty+1} self[type]=_type;self.triggerUpdate()});$("body").on("click",".qty .remove",function(e){e.preventDefault();var type=$(this).parents(".item").data("type");var model=$(this).parents(".item-model").data("model");if(model){var _type=self.data[type];delete _type.models[model];var size=Object.keys(_type.models).length;if(!size){_type.qty=1;_type.choose="auto";_type.models={}} self.data[type]=_type}else{delete self.data[type]} self.triggerUpdate()});$("body").on("click",".choose-another-equipment",function(e){e.preventDefault();$(".choose-equipment").trigger("click")});$("body").on("click",".choose-equipment",function(e){var theModal=$(".modal.equipment-types");theModal.find("label.hide").removeClass("hide");$.each(self.data,function(index,item){theModal.find("label.radio-"+index).addClass("hide")});e.preventDefault();Modal.show(theModal[0])});$("body").on("click",".choose-another-implement",function(e){e.preventDefault();$(this).parent().find(".choose-another-model").trigger("click")});$("body").on("click",".choose-another-model",function(e){var type=$(this).parents(".item").data("type");var theModal=$(".modal.equipment-models");var title=window.types[type].title;var link=window.types[type].link;var html=templateModal;var options="";var typeModels=self.getModelsByType(type);$.each(typeModels,function(index,item){if(self.data[type].models[index]){return} var manufacturer=item.manufacturer;if(type=="implemento"){manufacturer=item.subtype} var option=templateOption;option=option.replace(tag("id"),item.id).replace(tag("image"),item.image).replace(tag("manufacturer"),manufacturer).replace(tag("model"),item.model);options+=option});html=html.replace(tag("type"),title).replace(tag("radios"),options).replace(tag("link"),link);theModal.find(".models").html(html);e.preventDefault();Modal.show(theModal[0]);activeType=type});$("body").on("click",".modal.equipment-types .button",function(e){e.preventDefault();var theModal=$(".modal.equipment-types");$(theModal.find("input:checked")).each(function(i,obj){var type=$(this).val();if(type){var _type=self.getTypeItem(type);self.data[type]=_type;self.triggerUpdate()}});e.preventDefault();Modal.hide(theModal[0])});$("body").on("click",".modal.equipment-models .button",function(e){e.preventDefault();var theModal=$(".modal.equipment-models");$(theModal.find("input:checked")).each(function(i,obj){var type=activeType;var model=$(this).val();if(type&&model){var _type=self.getTypeItem(type);var _model=self.getModelItem(_type,model);_type.models[model]=_model;self.data[type]=_type;self.triggerUpdate()}});e.preventDefault();Modal.hide(theModal[0])});$(window).trigger("reloadQuote")},clearQuote:function(){var self=this;self.data={};self.triggerUpdate()},};Quote.init();Quote.initForm();window.Quote=Quote}) ;