var PublicPluginNeedsMap=(function(){var _this=this;_this.FRONT_FILE_SIZE_ERROR='Przesłane zdjęcie przekracza dopuszczalny rozmiar, maksymalny rozmiar to '+Math.floor(($('input[name="MAX_FILE_SIZE"]').val()/(1024*1024)))+'MB';_this.params={config:null};_this.vars={map:null,list:null};_this.photo=null;_this.setConfig=function(config){_this.params.config=config;};_this.getConfig=function(name){return _this.params.config[name];};_this.getConfigAll=function(){return _this.params.config;};_this.setMap=function(map){_this.vars.map=map;};_this.getMap=function(){return _this.vars.map;};_this.setRequestUrl=function(url){_this.vars.requestUrl=url;};_this.getRequestUrl=function(){return _this.vars.requestUrl;};_this.unbindEvents=function(){$('body').off('plugin-needs-map-open-form');$('body').off('#needs-map-new-need');$('body').off('click','#need-map-geolocate');$('body').off('submit','#needs_map_add_form');$('.input-disabled').off('keydown paste');$('#needs_map_photo').off('change');$('#photo_show_preview').off('click');$('#photo_remove').off('click');};_this.init=function(config){_this.setRequestUrl(window.location.protocol+'//'+window.location.host+'/public/request.php');_this.setConfig(config);_this.bindEvents();_this.initMap();_this.initList();_this.getNeeds();if(_this.getConfig('recaptcha_sitekey')&&_this.getConfig('recaptcha_secretkey')) {_this.loadRecaptchaLib();} _this.checkGeolocation();};_this.bindEvents=function(){_this.unbindEvents();_this.bindOpenForm();_this.bindGeolocate();_this.bindSubmitForm();};_this.bindOpenForm=function(){$('body').on('plugin-needs-map-open-form',function(event,lat,lng){_this.clearForm();_this.openForm(lat,lng);});$('body').on('click','#needs-map-new-need',function(event){_this.clearForm();_this.openForm(null,null);});$('body').on('plugin-needs-map-form-on-map-click',function(event,lat,lng){if($('div#need-map-form').is(':visible')) {_this.openForm(lat,lng);}});};_this.bindGeolocate=function(){$('body').on('click','#need-map-geolocate',function(){var isIE=!!document.documentMode;var geolocationOptions={enableHighAccuracy:!isIE,timeout:5000,maximumAge:0};_this.getMap().geolocate(_this.setFormLatLng,_this.formSetAddress,_this.errorGeolocate,geolocationOptions);});};_this.bindSubmitForm=function(){$('body').on('submit','#needs_map_add_form',function(e){e.preventDefault();e.stopPropagation();_this.submitForm();});$('.input-disabled').on('keydown paste',function(e){e.preventDefault();e.stopPropagation();});$('#needs_map_photo').on('change',function(e){var previewContainer=$('#needs_map_photo_preview');if(this.files&&this.files[0]) {if(this.files[0].size>$('input[name="MAX_FILE_SIZE"]').val()) {e.preventDefault();e.stopPropagation();_this.removePhoto();$('.need_map_error_container').show().find('.error_content').html('

'+_this.FRONT_FILE_SIZE_ERROR+'

');$('div#need-map-form').scrollTop('0px');} else {var reader=new FileReader();reader.onload=function(e){previewContainer.find('#photo_preview_container').html(''+e.target.result+'');};reader.readAsDataURL(this.files[0]);_this.photo=this.files[0];previewContainer.find('#filename_preview_container input').val(this.files[0].name);previewContainer.show();}}});$('#photo_show_preview').on('click',function(e){e.preventDefault();e.stopPropagation();var _this=$(this);var previewContainer=$('#needs_map_photo_preview');previewContainer.find('#photo_preview_container').slideToggle(300);_this.find('i').toggleClass('fa-chevron-down').toggleClass('fa-chevron-right');});$('#photo_remove').on('click',function(e){e.preventDefault();e.stopPropagation();_this.removePhoto();});};_this.removePhoto=function(){var previewContainer=$('#needs_map_photo_preview');previewContainer.find('#photo_preview_container').html('').hide();$('#needs_map_photo').val('');var photoPreviewIcon=$('#photo_show_preview i');if(!photoPreviewIcon.hasClass('fa-chevron-down')) {photoPreviewIcon.removeClass('fa-chevron-right');photoPreviewIcon.addClass('fa-chevron-down');} _this.photo=null;previewContainer.hide();};_this.initMap=function(){var map=new PublicPluginNeedsMapMap();map.init(_this.getConfigAll());_this.setMap(map);};_this.openForm=function(lat,lng){if(lat&&lng){_this.getMap().reverseGeocode(lat,lng,_this.formSetAddress);} _this.hideList();_this.showForm();if(lat&&lng){_this.setFormLatLng(lat,lng);_this.getMap().addNewNeedMarker(lat,lng);}};_this.loadRecaptchaLib=function() {window.setTimeout(function(){if(typeof grecaptcha==='undefined') {var script=document.createElement('script');script.src='../../www.google.com/recaptcha/api.js';script.type='text/javascript';document.getElementsByTagName('head')[0].appendChild(script);}},1000);};_this.checkGeolocation=function(){var isHttp=location.protocol==='informacja-publiczna.html';if(!navigator.geolocation||isHttp) {$('#need-map-geolocate').parent('div.form-group').hide();}};_this.submitForm=function(){var formData=new FormData();if(_this.photo!=null) {formData.append('needs_map_photo',_this.photo);} formData.append('Action','addNeed');formData.append('PluginName','needs_map');formData.append('form',JSON.stringify($('#needs_map_add_form').serializeArray()));_this.ajaxRequestWithFile(formData,_this.successSubmitForm);};_this.successSubmitForm=function(response){$('.need_map_error_container').hide().find('.error_content').html('');if(response.status=='success'){$('.need_map_error_container').show().find('.error_content').html('

'+response.info+'

');_this.clearForm();if(typeof grecaptcha!='undefined') {grecaptcha.reset();} $('.need_map_error_container').fadeOut(3000,function(){_this.addNeed(response.need);_this.vars.list.sort('id',{order:'desc'});_this.hideForm();_this.showList();})}else{$('.need_map_error_container').show().find('.error_content').html('

'+response.info+'

');if(typeof grecaptcha!='undefined') {grecaptcha.reset();}} $('div#need-map-form').scrollTop('0px');};_this.formSetAddress=function(address){$('#needs_map_street').val('');if(address.Address) {$('#needs_map_street').val(address.Address);} if(address.City&&!address.Neighborhood) {$('#needs_map_city').val(address.City);} if(address.Neighborhood) {$('#needs_map_city').val(address.Neighborhood);} if(address.Postal) {$('#needs_map_post').val(address.Postal);}};_this.clearForm=function(){$('#need-map-form input.clearable').val('');$('#need-map-form textarea.clearable').val('');$('#need-map-form .form-check-input').attr('checked',false);$("#need-map-form select").val($("#need-map-form select option:first").val());$('#needs_map_city').val(_this.getConfig('city'));_this.removePhoto();if(_this.getMap()) {_this.getMap().removeNewNeedMarker();}};_this.hideList=function(){$('#need-map-needs').hide();};_this.showList=function(){$('#need-map-needs').show();};_this.hideForm=function(){$('#need-map-form').hide();};_this.showForm=function(){$('#need-map-form').show();};_this.getFormAdress=function(){var address=$('#needs_map_street').val();if($('#needs_map_post').val()){address+=', '+$('#needs_map_post').val();} return address;};_this.setFormLatLng=function(lat,lng){$('#needs_map_lat').val(lat);$('#needs_map_lng').val(lng);$('#needs_map_lat_text').html(magellan(lat).latitude().toDMS());$('#needs_map_lng_text').html(magellan(lng).latitude().toDMS());};_this.errorGeolocate=function(message){alert(message);};_this.initList=function(){var options={valueNames:['id','date','category','address'],pagination:true,page:6,item:'
  • Data:
    Status: W trakcie
    Rodzaj:
    Adres:
  • '};_this.vars.list=new List('need-map-needs',options);};_this.getNeeds=function(){var dataSet={"Action":"getMarkers","PluginName":"needs_map"};_this.ajaxRequest(dataSet,_this.processNeeds);};_this.processNeeds=function(response){$.each(response,function(index,value){_this.addNeed(value);});};_this.addNeed=function(need){_this.getMap().addMarker(need.lat,need.lng,need.icon,need.description,need.cat,moment(need.created_at.date).format('YYYY-MM-DD hh:mm:ss'));_this.vars.list.add({id:need.id,date:moment(need.created_at.date).format('YYYY-MM-DD hh:mm:ss'),category:need.cat,address:need.street});};_this.ajaxRequest=function(dataSet,successCallback,type,dataType){type=typeof a!=='undefined'?type:'POST';dataType=typeof dataType!=='undefined'?dataType:'JSON';$.ajax({type:type,url:_this.vars.requestUrl,data:dataSet,success:successCallback,dataType:dataType});};_this.ajaxRequestWithFile=function(dataSet,successCallback,type,dataType){type=typeof a!=='undefined'?type:'POST';dataType=typeof dataType!=='undefined'?dataType:'JSON';$.ajax({type:type,url:_this.vars.requestUrl,data:dataSet,success:successCallback,dataType:dataType,processData:false,contentType:false});};var publicFunctions={init:function(config){_this.init(config);}};return publicFunctions;});