Changeset 2141742
- Timestamp:
- 08/19/2019 08:53:15 AM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
ongkoskirim-id/trunk/public/js/ongkoskirim-id-public.js
r1966176 r2141742 1 (function( $ ) { 2 'use strict'; 3 4 var debug = 1; 5 6 var ref_city, ref_state; 7 var ref_district = []; 8 9 $( window ).load(function() { 10 OngkosKirimId_Init(); 11 }); 12 13 // actions goes here 14 function OngkosKirimId_Init(){ 15 16 OngkosKirimId_LoadCity(); 17 18 OngkosKirimId_CreateInputCityText("billing"); 19 OngkosKirimId_CountryOnchange("billing"); 20 OngkosKirimId_StateOnChange("billing"); 21 OngkosKirimId_CityOnChange("billing"); 22 23 24 OngkosKirimId_CreateInputCityText("shipping"); 25 OngkosKirimId_CountryOnchange("shipping"); 26 OngkosKirimId_StateOnChange("shipping"); 27 OngkosKirimId_CityOnChange("shipping"); 28 29 30 OngkosKirimId_LoadExistingCustomer(); 31 OngkosKirimId_ReorderFieldsDisplay(); 32 33 $( document.body ).bind( 'country_to_state_changing', function(){OngkosKirimId_ReorderFieldsDisplay();}); 34 } 35 36 function OngkosKirimId_CreateInputCityText(type){ 37 var type_tag = "#"+type+"_"; 38 var input = '<input type="text" class="input-text " name="'+type+'_city_text" id="'+type+'_city_text" placeholder="" value="" autocomplete="address-level2" style="display: none;" />'; 39 $(type_tag+"city").parent().append(input); 40 $(type_tag+"city_text").hide(); 41 } 42 43 function OngkosKirimId_CountryOnchange(type){ 44 var type_tag = "#"+type+"_"; 45 $(type_tag+"country").change(function(){ 46 if( $(type_tag+"country").val() != 'ID'){ 47 // required city hack 48 $(type_tag+"city").html("").select2().select2("destroy").append("<option value='-'>-</option>").val("-").change().hide(); 49 $(type_tag+"city_text").val("").show(); 50 51 $(type_tag+"district_field").hide(); 52 $(type_tag+"district").append("<option value='-'>-</option>").val("-"); 53 54 } 55 else{ 56 $(type_tag+"city_text").val("").hide(); 57 58 OngkosKirimId_DisableCity(type); 59 OngkosKirimId_DisableDistrict(type); 60 } 61 return true; 62 }); 63 } 64 function OngkosKirimId_StateOnChange(type){ 65 var type_tag = "#"+type+"_"; 66 $(type_tag+"state").change(function(){ 67 if( $(type_tag+"country").val() != 'ID') 68 return true; 69 70 var cities = new Array(); 71 var state = $(this).val(); 72 73 OngkosKirimId_DisableDistrict(type); 74 75 if( state == null || typeof state == 'undefined' || state == "" ) 76 return true; 77 if( state == 'KU' ) state = 'KA'; 78 79 if( debug ) console.log("ref_city[state]"); 80 if( debug ) console.log(ref_city); 81 if( debug ) console.log(state); 82 83 $.each( ref_city[state], function( key, value ) { 84 var tmp = new Object(); 85 tmp.id = key; 86 tmp.text = value; 87 cities.push(tmp); 88 }); 89 90 if( debug ) console.log("cities"); 91 if( debug ) console.log(cities); 92 93 OngkosKirimId_EnableCity(type); 94 $(type_tag+"city").select2({ 95 data: cities 96 }); 97 }); 98 } 99 100 function OngkosKirimId_CityOnChange(type){ 101 var type_tag = "#"+type+"_"; 102 $(type_tag+"city").change(function(){ 103 if( $(type_tag+"country").val() != 'ID') 104 return true; 105 106 var city_id = $(this).val(); 107 108 OngkosKirimId_DisableDistrict(); 109 110 if( city_id<1 ) 111 return true; 112 113 if( debug ) console.log("city_id"); 114 if( debug ) console.log(city_id); 115 116 OngkosKirimId_LoadDistrict(city_id, type); 117 }); 118 } 119 120 // existing customer 121 function OngkosKirimId_LoadExistingCustomer(){ 122 var cust = ongkoskirim_id.customer; 123 124 if( cust.logged_in != 1 ){ 125 if( $("#billing_state").val() != '' ){ 126 $("#billing_state").change(); 127 } 128 129 if( $("#shipping_state").val() != '' ) 130 $("#shipping_state").change(); 131 132 return true; 133 } 134 135 136 OngkosKirimId_LoadExistingType(cust, "billing"); 137 OngkosKirimId_LoadExistingType(cust, "shipping"); 138 } 139 140 function OngkosKirimId_LoadExistingType(cust, type){ 141 var country=type+"_country"; 142 var state=type+"_state"; 143 var city=type+"_city"; 144 var city_text=type+"_city_text"; 145 var district=type+"_district"; 146 147 $("#"+country).change(); 148 149 if(cust[country] == "") 150 return true; 151 152 if( $("#"+country).val()!="ID") 153 $("#"+city_text).val(cust[city]); 154 155 156 if(cust[state] == "") 157 return true; 158 159 $("#"+state).val(cust[state]).change(); 160 161 if(cust[city] == "") 162 return true; 163 164 165 if( $("#"+country).val()=="ID") 166 $("#"+city).val(cust[city]).change(); 167 168 169 if(cust[district] == "") 170 return true; 171 172 $("#"+district).on("after_district_populate", function(){ 173 $("#"+district).val(cust[district]).change(); 174 }); 175 176 } 177 178 function OngkosKirimId_LoadCity(){ 179 console.log(ongkoskirim_id); 180 ref_city = ongkoskirim_id.cities.city; 181 ref_state = ongkoskirim_id.cities.state; 182 } 183 184 function OngkosKirimId_LoadDistrict(city_id, type){ 185 186 var type_tag = "#"+type+"_"; 187 188 var data = { 189 'action': 'get_districts', 190 'city_id': city_id 191 }; 192 $("#billing_district_field label").html("Loading..."); 193 jQuery.post(ongkoskirim_id.ajax_url, data, function(data) { 194 $("#billing_district_field label").html('Kecamatan <abbr class="required" title="required">*</abbr>'); 195 var districts = new Array(); 196 197 ref_district[city_id] = data; 198 199 200 $.each( ref_district[city_id], function( key, value ) { 201 var tmp = new Object(); 202 tmp.id = key; 203 tmp.text = value; 204 districts.push(tmp); 205 }); 206 207 208 OngkosKirimId_EnableDistrict(type); 209 $(type_tag+"district").select2({ 210 data: districts 211 }).trigger('after_district_populate'); 212 213 214 215 }, "json"); 216 } 217 218 function OngkosKirimId_DisableDistrict(type){ 219 var type_tag = "#"+type+"_"; 220 $(type_tag+"district_field").show(); 221 $(type_tag+"district").html("").show().append("<option value='0'>Pilih Kecamatan</option>").select2().prop("disabled", true); 222 } 223 224 function OngkosKirimId_EnableDistrict(type){ 225 var type_tag = "#"+type+"_"; 226 $(type_tag+"district").html("").show().append("<option value='0'>Pilih Kecamatan</option>").select2().prop("disabled", false); 227 } 228 229 function OngkosKirimId_DisableCity(type){ 230 var type_tag = "#"+type+"_"; 231 $(type_tag+"city").html("").show().append("<option value='0'>Pilih Kota</option>").select2().prop("disabled", true); 232 } 233 234 function OngkosKirimId_EnableCity(type){ 235 var type_tag = "#"+type+"_"; 236 $(type_tag+"city").html("").show().append("<option value='0'>Pilih Kota</option>").select2().removeProp("disabled").removeAttr("disabled"); 237 } 238 239 function OngkosKirimId_ReorderFieldsDisplay(){ 240 // reorder dropdown provinsi 241 console.log("reorder dropdown"); 242 var w = $('.woocommerce-billing-fields__field-wrapper, .woocommerce-shipping-fields__field-wrapper'); 243 w.each( function( i, w ) { 244 var html = $(w).find('.form-row').sort(function (a, b) { 245 var ka = parseInt($(a).attr('data-priority')) || parseInt($(a).attr('data-sort')); 246 var ki = parseInt($(b).attr('data-priority')) || parseInt($(b).attr('data-sort')); 247 return ka-ki; 248 }); 249 html.appendTo(w); 250 }); 251 } 252 253 254 })( jQuery ); 1 (function ($) { 2 'use strict' 3 4 var debug = 1 5 6 var ref_city, ref_state 7 var ref_district = [] 8 9 $(window).load(function () { 10 OngkosKirimId_Init() 11 }) 12 13 // actions goes here 14 function OngkosKirimId_Init () { 15 OngkosKirimId_LoadCity() 16 17 OngkosKirimId_CreateInputCityText('billing') 18 OngkosKirimId_CountryOnchange('billing') 19 OngkosKirimId_StateOnChange('billing') 20 OngkosKirimId_CityOnChange('billing') 21 22 OngkosKirimId_CreateInputCityText('shipping') 23 OngkosKirimId_CountryOnchange('shipping') 24 OngkosKirimId_StateOnChange('shipping') 25 OngkosKirimId_CityOnChange('shipping') 26 27 OngkosKirimId_LoadExistingCustomer() 28 OngkosKirimId_ReorderFieldsDisplay() 29 30 $(document.body).bind('country_to_state_changing', function () { OngkosKirimId_ReorderFieldsDisplay() }) 31 } 32 33 function OngkosKirimId_CreateInputCityText (type) { 34 var type_tag = '#' + type + '_' 35 var input = '<input type="text" class="input-text " name="' + type + '_city_text" id="' + type + '_city_text" placeholder="" value="" autocomplete="address-level2" style="display: none;" />' 36 $(type_tag + 'city').parent().append(input) 37 $(type_tag + 'city_text').hide() 38 } 39 40 function OngkosKirimId_CountryOnchange (type) { 41 var type_tag = '#' + type + '_' 42 $(type_tag + 'country').change(function () { 43 if ($(type_tag + 'country').val() != 'ID') { 44 // required city hack 45 $(type_tag + 'city').html('').select2().select2('destroy').append("<option value='-'>-</option>").val('-').change().hide() 46 $(type_tag + 'city_text').val('').show() 47 48 $(type_tag + 'district_field').hide() 49 $(type_tag + 'district').append("<option value='-'>-</option>").val('-') 50 } else { 51 $(type_tag + 'city_text').val('').hide() 52 53 OngkosKirimId_DisableCity(type) 54 OngkosKirimId_DisableDistrict(type) 55 } 56 return true 57 }) 58 } 59 function OngkosKirimId_StateOnChange (type) { 60 var type_tag = '#' + type + '_' 61 $(type_tag + 'state').change(function () { 62 if ($(type_tag + 'country').val() != 'ID') { return true } 63 64 var cities = new Array() 65 var state = $(this).val() 66 67 OngkosKirimId_DisableDistrict(type) 68 69 if (state == null || typeof state === 'undefined' || state == '') { return true } 70 // if( state == 'KU' ) state = 'KA'; 71 72 if (debug) console.log('ref_city[state]') 73 if (debug) console.log(ref_city) 74 if (debug) console.log(state) 75 76 $.each(ref_city[state], function (key, value) { 77 var tmp = new Object() 78 tmp.id = key 79 tmp.text = value 80 cities.push(tmp) 81 }) 82 83 if (debug) console.log('cities') 84 if (debug) console.log(cities) 85 86 OngkosKirimId_EnableCity(type) 87 $(type_tag + 'city').select2({ 88 data: cities 89 }) 90 }) 91 } 92 93 function OngkosKirimId_CityOnChange (type) { 94 var type_tag = '#' + type + '_' 95 $(type_tag + 'city').change(function () { 96 if ($(type_tag + 'country').val() != 'ID') { return true } 97 98 var city_id = $(this).val() 99 100 OngkosKirimId_DisableDistrict() 101 102 if (city_id < 1) { return true } 103 104 if (debug) console.log('city_id') 105 if (debug) console.log(city_id) 106 107 OngkosKirimId_LoadDistrict(city_id, type) 108 }) 109 } 110 111 // existing customer 112 function OngkosKirimId_LoadExistingCustomer () { 113 var cust = ongkoskirim_id.customer 114 115 if (cust.logged_in != 1) { 116 if ($('#billing_state').val() != '') { 117 $('#billing_state').change() 118 } 119 120 if ($('#shipping_state').val() != '') { $('#shipping_state').change() } 121 122 return true 123 } 124 125 OngkosKirimId_LoadExistingType(cust, 'billing') 126 OngkosKirimId_LoadExistingType(cust, 'shipping') 127 } 128 129 function OngkosKirimId_LoadExistingType (cust, type) { 130 var country = type + '_country' 131 var state = type + '_state' 132 var city = type + '_city' 133 var city_text = type + '_city_text' 134 var district = type + '_district' 135 136 $('#' + country).change() 137 138 if (cust[country] == '') { return true } 139 140 if ($('#' + country).val() != 'ID') { $('#' + city_text).val(cust[city]) } 141 142 if (cust[state] == '') { return true } 143 144 $('#' + state).val(cust[state]).change() 145 146 if (cust[city] == '') { return true } 147 148 if ($('#' + country).val() == 'ID') { $('#' + city).val(cust[city]).change() } 149 150 if (cust[district] == '') { return true } 151 152 $('#' + district).on('after_district_populate', function () { 153 $('#' + district).val(cust[district]).change() 154 }) 155 } 156 157 function OngkosKirimId_LoadCity () { 158 console.log(ongkoskirim_id) 159 ref_city = ongkoskirim_id.cities.city 160 ref_state = ongkoskirim_id.cities.state 161 } 162 163 function OngkosKirimId_LoadDistrict (city_id, type) { 164 var type_tag = '#' + type + '_' 165 166 var data = { 167 'action': 'get_districts', 168 'city_id': city_id 169 } 170 $('#billing_district_field label').html('Loading...') 171 jQuery.post(ongkoskirim_id.ajax_url, data, function (data) { 172 $('#billing_district_field label').html('Kecamatan <abbr class="required" title="required">*</abbr>') 173 var districts = new Array() 174 175 ref_district[city_id] = data 176 177 $.each(ref_district[city_id], function (key, value) { 178 var tmp = new Object() 179 tmp.id = key 180 tmp.text = value 181 districts.push(tmp) 182 }) 183 184 OngkosKirimId_EnableDistrict(type) 185 $(type_tag + 'district').select2({ 186 data: districts 187 }).trigger('after_district_populate') 188 }, 'json') 189 } 190 191 function OngkosKirimId_DisableDistrict (type) { 192 var type_tag = '#' + type + '_' 193 $(type_tag + 'district_field').show() 194 $(type_tag + 'district').html('').show().append("<option value='0'>Pilih Kecamatan</option>").select2().prop('disabled', true) 195 } 196 197 function OngkosKirimId_EnableDistrict (type) { 198 var type_tag = '#' + type + '_' 199 $(type_tag + 'district').html('').show().append("<option value='0'>Pilih Kecamatan</option>").select2().prop('disabled', false) 200 } 201 202 function OngkosKirimId_DisableCity (type) { 203 var type_tag = '#' + type + '_' 204 $(type_tag + 'city').html('').show().append("<option value='0'>Pilih Kota</option>").select2().prop('disabled', true) 205 } 206 207 function OngkosKirimId_EnableCity (type) { 208 var type_tag = '#' + type + '_' 209 $(type_tag + 'city').html('').show().append("<option value='0'>Pilih Kota</option>").select2().removeProp('disabled').removeAttr('disabled') 210 } 211 212 function OngkosKirimId_ReorderFieldsDisplay () { 213 // reorder dropdown provinsi 214 console.log('reorder dropdown') 215 var w = $('.woocommerce-billing-fields__field-wrapper, .woocommerce-shipping-fields__field-wrapper') 216 w.each(function (i, w) { 217 var html = $(w).find('.form-row').sort(function (a, b) { 218 var ka = parseInt($(a).attr('data-priority')) || parseInt($(a).attr('data-sort')) 219 var ki = parseInt($(b).attr('data-priority')) || parseInt($(b).attr('data-sort')) 220 return ka - ki 221 }) 222 html.appendTo(w) 223 }) 224 } 225 })(jQuery)
Note: See TracChangeset
for help on using the changeset viewer.